CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 18 of 18
  1. #16
    Join Date
    Aug 2004
    Location
    Bucharest, Romania... sometimes
    Posts
    1,039

    Re: Simulate Mouse Click

    Quote Originally Posted by JBudOner
    =/


    [DllImport("user32.dll")]
    public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, UIntPtr wParam,
    IntPtr lParam);

    public const int WM_NCLBUTTONDOWN =0x00A1;
    public const int WM_NCLBUTTONUP = 0x00A2;



    private void button1_Click(object sender, EventArgs e)
    {
    Win32APICall.SendMessage(Web.Handle, Win32APICall.WM_NCLBUTTONDOWN, 544, 472);
    Win32APICall.SendMessage(Web.Handle, Win32APICall.WM_NCLBUTTONUP, 544, 472);
    }




    So this is what I came out to now. Web being the name of the WebBrowser. Not sure if, 'WM_LBUTTONDOWN' is different from, 'WM_NCLBUTTONDOWN'. I checked around for a number that it would be, but couldn't find any. I've got an error when trying to run it, "Cannot convert from 'int' to 'System.UIntPtr'," and, "Cannot convert form 'int' to 'System.IntPtr'." I've got one of both of these for both the SendMessages. Any ideas?
    'WM_LBUTTONDOWN' is different from 'WM_NCLBUTTONDOWN'! First means "window message left button down" and second means "window message non-client area left button down"! In my oppinion, it might be useless to send any of these messages to a window in order to hit a hyperlink within it. You could try detecting which message is processed when the hyperlink is hit by using something like Spy++, 'cause might not be WM_LBUTTONDOWN & WM_LBUTTONUP but also to idetify what parameters are expected when these messages are sent. I would recommend SendInput() for sending keyboard or mouse events. Also, I have used in the past mouse_event to simulate mouse events and I find it strange that doesn't work for you.
    Regards,
    Bogdan Apostol
    ESRI Developer Network

    Compilers demystified - Function pointers in Visual Basic 6.0
    Enables the use of function pointers in VB6 and shows how to embed native code in a VB application.

    Customize your R2H
    The unofficial board dedicated to ASUS R2H UMPC owners.

  2. #17
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    40

    Re: Simulate Mouse Click

    Great =) I tried SendInput() and it worked now. Heres what I put:


    public class Win32APICall
    {
    public const int INPUT_MOUSE = 0;
    public const int MOUSEEVENTF_LEFTDOWN = 0x0002;
    public const int MOUSEEVENTF_LEFTUP = 0x0004;



    [DllImport("User32.dll", SetLastError = true)]
    public static extern int SendInput(int nInputs, ref INPUT pInputs, int cbSize);

    public struct INPUT
    {
    public int type;
    public MOUSEINPUT mi;
    }

    public struct MOUSEINPUT
    {
    public int dx;
    public int dy;
    public int mouseData;
    public int dwFlags;
    public int time;
    public int dwExtraInfo;
    }
    }



    private void Updater_Tick(object sender, EventArgs e)
    {
    Win32APICall.MOUSEINPUT aInput;
    aInput.dx = 544;
    aInput.dy = 472;
    aInput.time = 0;
    aInput.mouseData = 0;
    aInput.dwExtraInfo = 0;
    aInput.dwFlags = Win32APICall.MOUSEEVENTF_LEFTDOWN;
    Win32APICall.INPUT bInput;
    bInput.type = 0;
    bInput.mi = aInput;
    Win32APICall.SendInput(1, ref bInput, 28);
    aInput.dwFlags = Win32APICall.MOUSEEVENTF_LEFTUP;
    bInput.mi = aInput;
    Win32APICall.SendInput(1, ref bInput, 28);
    }



    Thanks very much everyone, I really appreciate the help.

    To those who were curious why im trying to autoclick a link in a website: Im not. There is a game in which you have to click on the screen to move your character at that position, and do different things in the game. I want to make an, 'auto' and therefor need it to autoclick for me. This is just the start, im not going to keep it on a timer, im going to make it a void and call it everytime there are two colors that match (in an xml and on the game).



    One thing I didn't get though is that it didn't change the x and y position of the mouse. It just simply auto clicks everytime the interval is hit on the timer.

  3. #18
    Join Date
    Aug 2004
    Location
    Bucharest, Romania... sometimes
    Posts
    1,039

    Re: Simulate Mouse Click

    Quote Originally Posted by JBudOner
    One thing I didn't get though is that it didn't change the x and y position of the mouse. It just simply auto clicks everytime the interval is hit on the timer.
    SendInput does not change mouse position. It inserts events in the queue that are processed by OS and messages are dispatched to corresponding windows from all processes. The mouse coordinates that you store as event parameters might not be used by the application receiving the message. Many application are retrieving cursor position with GetCursorPos instead of using the parameters of the message received (because they want the last position, in case there's lag between send & receive). In order to change mouse pointer position, call SetCursorPos() API. Then you can use SendInput to insert buttons' events.
    PS: There're some applications (some games in particular) that do not process mouse events through windows messages, which makes SendInput or mouse_event APIs useless. Those applications poll themself the state of the device directly from the driver. In such case you'll need to patch its Import Address Table (IAT) to call a stub function of yours that changes device's behaviour as you wish.
    PPS: Using a timer might be unhealty, ... instead, I'd rather use the status of a "special key" like SCROLL_LOCK to know if clicks should be performed by my code.
    Best regards,
    Bogdan Apostol
    ESRI Developer Network

    Compilers demystified - Function pointers in Visual Basic 6.0
    Enables the use of function pointers in VB6 and shows how to embed native code in a VB application.

    Customize your R2H
    The unofficial board dedicated to ASUS R2H UMPC owners.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured