2016-11-24 60 views
1

我想開發一個程序,它會打開2個吉他Pro文件並在我的多屏設置不同的屏幕上顯示出來。鼠標拖動窗口編程

我除了1個窗口從屏幕1移動到屏幕的一切工作2

吉他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(http://www.pinvoke.net/default.aspx/user32.setwindowpos)而不是嘗試以編程方式重新創建鼠標拖動。 –

+0

這似乎不是C#。你錯過了一個標籤嗎?或者你錯過了大部分代碼? – nvoigt

+0

@nvoigt聽起來像WPF但代碼是不完全代表的C#。 –

回答

1

試試這個:再次

  1. 把光標移到窗口的位置

  2. 點擊向下

  3. 移動光標:

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

    步驟爲了移動窗口

  4. 點擊了

步驟:

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