我正在开发一个程序,可以在我的多屏幕设置上打开两个Guitar Pro文件并在不同的屏幕上显示它们。除了将一个窗口从屏幕1移动到屏幕2之外,我已经完成了所有工作。由于某种原因,Guitar Pro有点不稳定,只能在屏幕1中打开文件。我尝试通过抓取窗口句柄来移动窗口,但这只会移动主容器,而所有子窗口仍然保持原位。我决定通过编程方式移动鼠标光标来单击和拖动窗口从一个屏幕移到另一个屏幕,但仍然遇到问题...
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool SetCursorPos(int x, int y);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
public void OpenFileFn()
{
Process file1 = new Process();
file1.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
file1.StartInfo.FileName = file;
file1.Start();
Thread.Sleep(500);
Process file2 = new Process();
file2.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
file2.StartInfo.FileName = file;
file2.Start();
file2.WaitForInputIdle();
Thread.Sleep(3000);
int posX = Cursor.Position.X;
int posY = Cursor.Position.Y;
SetCursorPos(-960, 15);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
SetCursorPos(960, 15);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
SetCursorPos(posX, posY);
}
使用上述代码可以使光标在水平方向移动,但不会移动窗口...如果我更改光标的Y轴,窗口就会垂直移动...
请问有什么想法可以解决这个问题吗? 提前感谢您的帮助...
public static bool MoveToMonitor(IntPtr windowHandle, int monitor) { monitor = monitor - 1; return SetWindowPos(windowHandle, IntPtr.Zero, Screen.AllScreens[monitor].WorkingArea.Left, Screen.AllScreens[monitor].WorkingArea.Top, 1000, 800, SetWindowPosFlags.SWP_NOZORDER | SetWindowPosFlags.SWP_NOREDRAW); }
但是这只是移动了容器窗口 :( - Lee Davy