Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mouse drag window programmatically

Tags:

c#

multiscreen

I'm trying to develop a program which will open 2 Guitar Pro files up and display them on different screens on my multiscreen setup.

I have everything working except the moving of 1 window from screen 1 to screen 2.

Guitar pro is a bit dodgy and for some reason will only open files in screen 1... I have tried to move the window by grabbing the window handle, but this only moves the main container and leaves all the child windows in place. I've decided to cheat it a bit and programmatically move the mouse cursor to click and drag the window from screen to screen, but am still running into issues...

[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);
    }

Using the code above moves the cursor horizontally but not the window... If I change the cursor Y axis, the window moves vertically...

Any ideas why how I can fix this? Thanks in advance...

like image 523
Lee Davy Avatar asked Jan 25 '26 11:01

Lee Davy


1 Answers

try this:

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

steps:

  1. move cursor to window position

  2. click down

  3. move cursor again in order to move window

  4. click up

steps:

var manager= new MouseManager();
manager.MoveCursor(-960,15);
manager.ClickDown();
manager.MoveCursor(960,15);
manager.ClickUp();
like image 97
xszaboj Avatar answered Jan 27 '26 01:01

xszaboj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!