【问题标题】:Mouse drag window programmatically以编程方式鼠标拖动窗口
【发布时间】:2016-11-24 14:39:38
【问题描述】:

我正在尝试开发一个程序,该程序将打开 2 个 Guitar Pro 文件并将它们显示在我的多屏设置的不同屏幕上。

除了将 1 个窗口从屏幕 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 轴,则窗口垂直移动...

任何想法为什么我可以解决这个问题? 提前谢谢...

【问题讨论】:

  • 你可能会更幸运SetWindowPosition,而不是尝试以编程方式重新创建鼠标拖动。
  • 这似乎不是 C#。你错过了一个标签吗?还是您错过了大部分代码?
  • @nvoigt 听起来像 WPF,但代码并不完全代表 C#。
  • 抱歉,已编辑第一篇文章...
  • 当您说您尝试抓住窗口把手时,您究竟是如何移动窗口的?

标签: c# multiscreen


【解决方案1】:

试试这个:

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();

【讨论】:

    猜你喜欢
    • 2012-10-25
    • 2017-09-22
    • 1970-01-01
    • 1970-01-01
    • 2012-05-08
    • 1970-01-01
    • 2010-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多