Jump to content

Sending mouse clicks without moving the mouse


wickss
 Share

Recommended Posts

I am using AutoItX in VB.net. I need to have my program click at certain coordinates in a window. If I use MouseClick, it moves the mouse cursor. I don't want to have to save the mouse position, click, and then move the cursor back because that will have less than professional feel. I want to use ControlClick, but the click isn't going to a windows control. If I leave out the control name it doesn't seem to do anything. Is there any thing I am missing?

AutoIT.ControlClick(hwnd, "", "", , , X, Y)

My last resort will be to use SendMessage. But I don't know which message to send.

Link to comment
Share on other sites

With the help of http://www.codeguru.com/forum/showthread.php?t=377394 great resource.

It's still necessary to store the mouse position to be able to restore it because of the nature of SendInput or mouse_event functions.

#define _WIN32_WINNT 0x0501
#include <windows.h>

VOID MouseMove(INT, INT);
VOID MouseLeft();
VOID MouseRight();

INT WINAPI WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    INT nShowCmd)
{
    POINT pt;
    GetCursorPos(&pt);
    ShowCursor(FALSE);
    MouseMove(30, 30);
    MouseRight();
    MouseMove(pt.x, pt.y);
    ShowCursor(TRUE);

    return 0;
}


VOID MouseMove(INT nX, INT nY)
{
    INPUT input;
    DOUBLE fScreenWidth = GetSystemMetrics( SM_CXSCREEN )-1; 
    DOUBLE fScreenHeight  = GetSystemMetrics( SM_CYSCREEN )-1; 
    DOUBLE fX = nX*(65535.0f/fScreenWidth);
    DOUBLE fY = nY*(65535.0f/fScreenHeight);

    RtlZeroMemory(&input, sizeof(input));
    input.type = INPUT_MOUSE;
    input.mi.dwFlags = MOUSEEVENTF_MOVE|MOUSEEVENTF_ABSOLUTE;
    input.mi.dx = (LONG)fX;
    input.mi.dy = (LONG)fY;

    SendInput(1, &input, sizeof(input));
}

VOID MouseLeft()
{
    INPUT input;
    RtlZeroMemory(&input, sizeof(input));

    input.type = INPUT_MOUSE;
    input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;

    SendInput(1, &input, sizeof(input));

    RtlZeroMemory(&input, sizeof(input));
    input.type = INPUT_MOUSE;
    input.mi.dwFlags = MOUSEEVENTF_LEFTUP;

    SendInput(1, &input, sizeof(input));
}

VOID MouseRight()
{
    INPUT input;
    RtlZeroMemory(&input, sizeof(input));

    input.type = INPUT_MOUSE;
    input.mi.dwFlags = MOUSEEVENTF_RIGHTDOWN;

    SendInput(1, &input, sizeof(input));

    RtlZeroMemory(&input, sizeof(input));
    input.type = INPUT_MOUSE;
    input.mi.dwFlags = MOUSEEVENTF_RIGHTUP;

    SendInput(1, &input, sizeof(input));
}
Link to comment
Share on other sites

  • 2 weeks later...

With the help of http://www.codeguru.com/forum/showthread.php?t=377394 great resource.

It's still necessary to store the mouse position to be able to restore it because of the nature of SendInput or mouse_event functions.

I don't know C++ so I don't know how to use this. If I figure out how to make a dll out of this, will it run fast enough so that the user can't see the flicker of moving the mouse?

Autohotkeys can click without moving the mouse, but I can't use that in VB.net.

Link to comment
Share on other sites

Well, you can use right or left WM_LBUTTONDOWN and PostMessage. I don't know VB.NET but I guess that it's possible to use the previous code with lib importing.

#include <windows.h>

INT WINAPI WinMain(
       HINSTANCE    hInstance,
       HINSTANCE    hPrevInstance,
       LPSTR        lpCmdLine,
       INT          nShowCmd)
{
    POINT pt;
        pt.x = 30;
        pt.y = 30;

    HWND hWnd = WindowFromPoint(pt);
    LPARAM lParam = MAKELPARAM(30, 30);
    PostMessage(hWnd, WM_RBUTTONDOWN, MK_RBUTTON, lParam);
    PostMessage(hWnd, WM_RBUTTONUP, MK_RBUTTON, lParam);

    return 0;
}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...