以编程方式拖动窗口

3
我正在开发一个程序,可以在我的多屏幕设置上打开两个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轴,窗口就会垂直移动...

请问有什么想法可以解决这个问题吗? 提前感谢您的帮助...


2
你可以尝试使用SetWindowPosition来代替编程重现鼠标拖动,这样可能会更加顺利。 - Manfred Radlwimmer
这似乎不是C#。你错过了一个标签吗?或者你错过了大部分的代码? - nvoigt
@nvoigt听起来像WPF,但代码并不完全代表C#。 - James Gould
抱歉,我编辑了第一篇帖子... - Lee Davy
当你说你尝试抓取窗口句柄时,你是如何移动窗口的? - Thejaka Maldeniya
公共枚举 SetWindowPosFlags : uint { SWP_NOZORDER = 0x0004, SWP_NOREDRAW = 0x0008 } 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
1个回答

2

试试这个:

public class MouseManager
{
    public void MoveCursor(int x, int y)
    {
        Win32.POINT p = new Win32.POINT
        {
            x = x,
            y = y
        };

        Win32.SetCursorPos(p.x, p.y);
    }

    public int GetX()
    {
        var p = Win32.GetCursorPosition();
        return p.x;
    }

    public int GetY()
    {
        var p = Win32.GetCursorPosition();
        return p.y;
    }

    public void Click()
    {
        Win32.MouseEvent(Win32.MouseEventFlags.LeftDown);
        Win32.MouseEvent(Win32.MouseEventFlags.LeftUp);
    }

    public void RightClick()
    {
        Win32.MouseEvent(Win32.MouseEventFlags.RightDown);
        Win32.MouseEvent(Win32.MouseEventFlags.RightUp);
    }

    public void DoubleClick()
    {
        Win32.MouseEvent(Win32.MouseEventFlags.LeftDown);
        Win32.MouseEvent(Win32.MouseEventFlags.LeftUp);
        Win32.MouseEvent(Win32.MouseEventFlags.LeftDown);
        Win32.MouseEvent(Win32.MouseEventFlags.LeftUp);
    }

    public void Scroll(int y)
    {
        Win32.Scroll(y);
    }

    public void ClickDown()
    {
        Win32.MouseEvent(Win32.MouseEventFlags.LeftDown);
    }

    public void ClickUp()
    {
        Win32.MouseEvent(Win32.MouseEventFlags.LeftUp);
    }
}
  1. 将光标移动到窗口位置

  2. 按下鼠标左键

  3. 再次移动光标以移动窗口位置

  4. 松开鼠标左键

var manager= new MouseManager();
manager.MoveCursor(-960,15);
manager.ClickDown();
manager.MoveCursor(960,15);
manager.ClickUp();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接