2015-08-25 27 views
0

我嘗試編寫一個簡單的測試工具,它測試網站(贏取窗體,使用WebBrowser控件)。 我需要發送鼠標點擊和擊鍵到網站。 它在窗體頂部工作,但我想在後臺運行測試儀。 如何將鼠標點擊,擊鍵發送到最小化/背景形式? 當前鼠標事件代碼:如何將鼠標事件發送到c#中的最小化形式?

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] 
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, UIntPtr dwExtraInfo); 

[Flags] 
public enum MouseEventFlags 
{ 
    LEFTDOWN = 0x00000002, 
    LEFTUP = 0x00000004, 
    MIDDLEDOWN = 0x00000020, 
    MIDDLEUP = 0x00000040, 
    MOVE = 0x00000001, 
    ABSOLUTE = 0x00008000, 
    RIGHTDOWN = 0x00000008, 
    RIGHTUP = 0x00000010 
} 

void mouseEvent(uint flag, Point p) 
{ 
    p = caller.PointToScreen(p); 

    Cursor.Position = p; 
    mouse_event(flag, (uint) 0, (uint) 0, (uint) 0, (UIntPtr) 0); 
} 
public void sendMouseClick(Point p) 
{ 
    uint flag = (uint) MouseEventFlags.LEFTDOWN + (uint) MouseEventFlags.LEFTUP; 

    mouseEvent(flag, p); 
} 

- 編輯:

我試過SendMessage函數,但沒有工作:( 目前,我嘗試用一​​個簡單的從2個按鈕,沒有網絡瀏覽器,只是正常windows.Form和按鈕。我嘗試當我按下按鈕2從代碼中單擊Button1。:)

 // On the form, when i press the button 2 then minimize, wait, and try to press the button1 
    private void button2_Click(object sender, EventArgs e) 
    { 
     // this.RaiseMouseEvent(); 
     MouseHelper mh = new MouseHelper(this.Text); 
     this.WindowState = FormWindowState.Minimized; 
     Thread.Sleep(2000); 
     this.Refresh(); 
     Thread.Sleep(2000); 
     mh.SendMouseClick(25,25); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     throw new Exception("BUTTON 1 CLICKED"); 
    } 

    // In the MouseHelper I call the left click 
    public void SendMouseClick(int p_x, int p_y) 
    { 
     Int32 l_parm1 = (p_y << 16) | (p_x & 0xffff); 
     SendMessage(windowPtr, WM_LBUTTONDOWN, 0, l_parm1); 
     SendMessage(windowPtr, WM_LBUTTONUP, 0, l_parm1); 
    } 

    public MouseHelper(String windowTitle) 
    { 
     windowPtr = FindWindowByCaption(IntPtr.Zero, windowTitle); 
    } 

    // defintions 
    public const uint WM_LBUTTONDOWN = 0x0201; 
    public const uint WM_LBUTTONUP = 0x0202; 

    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] 
    public static extern int SendMessage(
     IntPtr hWnd, 
     uint Msg, 
     Int32 wParam, 
     Int32 lParam 
    ); 

我做了什麼錯?

即使窗口沒有被迷你化也不起作用:(第一種解決方案在窗口處於活動狀態時工作,但第二種方式不起作用:( 我嘗試了25,25和147,47(PoinToScreen的結果爲25, 25)

回答

0

大概SendMessage函數將工作 見SendMessageSystem Defined Messages(更具體here

喜歡的東西:

SendMessage(hwnd, WM_LBUTTONDOWN, 0, (123<<16)|(456)); 
SendMessage(hwnd, WM_LBUTTONUP, 0, (123<<16)|(456)); 
+0

THX,我試過了,但在問題1不好:(見上在「 - 編輯」後標記 – Mancika

+0

好的,所以一個原因可能是Webbrowser控件的實現。你也可以考慮訪問WebBrowser控件的方法(如[這裏](http://stackoverflow.com/questions/5150610/how-to-simulate-mouse-click-with-the-webbrowser-control))並使用用於測試的Javascript。 – user2358582

+0

對不起,button1是一個標準的winforms按鈕,不在WebBrowser中。我嘗試從逐步構建:) button1是窗體上面板上的普通System.Windows.Forms.Button,而不是在瀏覽器控件中。 – Mancika

相關問題