3

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...

Lee Davy
  • 31
  • 3
  • 2
    You might have more luck with [SetWindowPosition](http://www.pinvoke.net/default.aspx/user32.setwindowpos) instead of trying to programmatically recreate a mouse drag. – Manfred Radlwimmer Nov 24 '16 at 14:42
  • This does not seem to be C#. Did you miss a tag? Or did you miss the bulk of your code? – nvoigt Nov 24 '16 at 14:43
  • @nvoigt sounds like WPF but the code isn't exactly representative of C#. – James Gould Nov 24 '16 at 14:44
  • Sorry, edited first post... – Lee Davy Nov 24 '16 at 14:51
  • When you say you tried grabbing the window handle, how exactly did you move the window? – Thejaka Maldeniya Nov 24 '16 at 15:16
  • `public enum 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); }` But all this does is move the container window :( – Lee Davy Nov 24 '16 at 15:55

1 Answers1

2

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();
xszaboj
  • 1,035
  • 7
  • 21