简体   繁体   English

如何在C#中将鼠标事件发送到最小化形式?

[英]How to send mouse events to a minimized form in c#?

I try to write a simple tester tool, it testing web site (win form, using WebBrowser control). 我尝试编写一个简单的测试器工具,它测试网站(赢得表格,使用WebBrowser控件)。 I need to send mouse click and keystrokes to the site. 我需要将鼠标单击和击键发送到该站点。 It works when the form is on top, but i would like to run the tester in the background. 当表单位于顶部时,它可以工作,但是我想在后台运行测试仪。 How can i send mouse click, keystrokes to a minimized/background form? 如何将鼠标单击和击键发送到最小化/背景形式? Current mouse event code: 当前的鼠标事件代码:

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

-- Edited: -编辑:

I tried the SendMessage but didn't works :( Currently i try to use a simple from with 2 buttons, no web browser, just normal windows.Form and buttons. i try to click button1 from code when i push the button2. :) 我尝试了SendMessage但没有用:(目前,我尝试使用带有2个按钮的简单from控件,没有Web浏览器,只有普通的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
    );

What I did wrong? 我做错了什么?

It doesn't work even the window is not minimezed:( The 1st solution works when window is active, but the 2nd not :( I tried with 25,25 and 147,47 (result of PoinToScreen of 25,25) 即使没有最小化窗口,它也不起作用:(当窗口处于活动状态时,第一种解决方案有效,但是第二种则无效:(我尝试使用25,25和147,47(PoinToScreen为25,25的结果)

Probably SendMessage would work. 可能SendMessage可以工作。 See SendMessage and System Defined Messages (more specifically here ) 请参阅SendMessage系统定义的消息在此处更具体)

Something like: 就像是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM