how simulate an mouse click?

Good night,I have been learning about windows API, i'm trying simulate a mouse click, but it don't executes how I expect (don't simulates the click...).
This is the piece of the code:
1
2
3
4
5
6
7
8
        case WM_HOTKEY:
             if(wParam == (long) VK_F4){
                 GetCursorPos(&pos_cursor);
                 hwnd_outra_win = WindowFromPoint(pos_cursor);
                 SendMessage(hwnd_outra_win,WM_LBUTTONDOWN,MK_LBUTTON,MAKELPARAM(pos_cursor.x,pos_cursor.y));
                 SendMessage(hwnd_outra_win,WM_LBUTTONUP,MK_LBUTTON,MAKELPARAM(pos_cursor.x,pos_cursor.y));
                 }
             break;

How can I do this?
sorry the broken english... i'm from Brazil... I have been learning english too...
thanks...
Do you know if the WM_HOTKEY message is being received?

Try putting a call to MessageBox in you WM_HOTKEY case and see if it displays.
Last edited on
yes the WM_HOTKEY message is being received.... Here is the code... it isn't working...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <windows.h>
HWND hwnd,hwnd_outra_win;
MSG messages;
POINT pos_cursor;
LRESULT CALLBACK WinProc_Principal (HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){
    switch (message){
        case WM_HOTKEY:
             if(wParam == (long) VK_F4){
                 //MessageBox(0,"test","MB_test",MB_ICONQUESTION);
                 GetCursorPos(&pos_cursor);
                 hwnd_outra_win = WindowFromPoint(pos_cursor);
                 SendMessage(hwnd_outra_win,WM_LBUTTONDOWN,MK_LBUTTON,MAKELPARAM(pos_cursor.x,pos_cursor.y));
                 SendMessage(hwnd_outra_win,WM_LBUTTONUP,0,MAKELPARAM(pos_cursor.x,pos_cursor.y));
                 }
             break;
        case WM_DESTROY:
            PostQuitMessage (0);       
            break;
        default:                   
            return DefWindowProc (hwnd, message, wParam, lParam);
    }
    return 0;
}
int WINAPI WinMain(HINSTANCE hThisInstance,HINSTANCE hPrevInstance,LPSTR lpszArgument,int nFunsterStil){                    
    WNDCLASSEX wincl ={sizeof(WNDCLASSEX),0,WinProc_Principal,0,0,hThisInstance,LoadIcon(NULL,IDI_APPLICATION),
         LoadCursor(NULL,IDC_ARROW),CreateSolidBrush(RGB(0,0,0xFF)),0,"C_Win",LoadIcon(NULL,IDI_APPLICATION)};
    if(!RegisterClassEx (&wincl))return -1;
    hwnd = CreateWindowEx(0,"C_Win","S_Clicks",WS_OVERLAPPED|WS_SYSMENU|WS_VISIBLE,CW_USEDEFAULT,CW_USEDEFAULT,300,200,HWND_DESKTOP,NULL,hThisInstance,NULL);
    RegisterHotKey(hwnd,VK_F4,0,VK_F4);
    while(GetMessage (&messages, NULL, 0, 0)){
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    };
    return messages.wParam;
}

I'm using DevCpp v.4.8...
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <windows.h>

void LeftClick ( )
{  
  INPUT    Input={0};
  // left down 
  Input.type      = INPUT_MOUSE;
  Input.mi.dwFlags  = MOUSEEVENTF_LEFTDOWN;
  ::SendInput(1,&Input,sizeof(INPUT));

  // left up
  ::ZeroMemory(&Input,sizeof(INPUT));
  Input.type      = INPUT_MOUSE;
  Input.mi.dwFlags  = MOUSEEVENTF_LEFTUP;
  ::SendInput(1,&Input,sizeof(INPUT));
}

int main()
{
    LeftClick();
}


This can be done with "WinMain" too but, this is just easier.
Serpent!!!It's exactly what I have been searching, I'm trying code a program that moves the mouse's cursor with the arrows and simulates a mouse's click with F4 button.
for now, here is my code:
tested in VC++...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "stdafx.h"
#include <windows.h>
HDC hdc;
PAINTSTRUCT ps; 
/***Serpent's function****************/
void LeftClick(void)
{  
  INPUT Input={0};
  Input.type      = INPUT_MOUSE;
  Input.mi.dwFlags  = MOUSEEVENTF_LEFTDOWN;
  ::SendInput(1,&Input,sizeof(INPUT));
  ::ZeroMemory(&Input,sizeof(INPUT));
  Input.type      = INPUT_MOUSE;
  Input.mi.dwFlags  = MOUSEEVENTF_LEFTUP;
  ::SendInput(1,&Input,sizeof(INPUT));
}/************************************/
HWND hwnd,hwnd_outra_win;
MSG messages;
POINT pos_cursor;
POINT Move_mouse(int x,int y){
      GetCursorPos(&pos_cursor);
      pos_cursor.x+=(long)x;pos_cursor.y+=(long)y;
      SetCursorPos(pos_cursor.x,pos_cursor.y);
      return pos_cursor;
}
const int i_velox = 10; 
const int hk_f4 = 0x64;
const int hk_cima = 0x26;
const int hk_baixo = 0x28;
const int hk_dir = 0x27;
const int hk_esq = 0x25;
LRESULT CALLBACK WinProc_Principal (HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){
    switch (message){
		case WM_PAINT:
            hdc=BeginPaint(hwnd,&ps);
            SetBkMode(hdc,TRANSPARENT);
			ExtTextOut(hdc,10,5,ETO_OPAQUE,0,TEXT("by: wn_br"),9,0);
			ExtTextOut(hdc,10,30,ETO_OPAQUE,0,TEXT("thanks to Serpent !"),19,0);
            EndPaint(hwnd,&ps);
            break;
        case WM_HOTKEY:
             if(wParam == (long)hk_f4)
                 LeftClick();
             else if(wParam == (long)hk_cima) Move_mouse(0,-i_velox);
             else if(wParam == (long)hk_baixo) Move_mouse(0,+i_velox);
             else if(wParam == (long)hk_dir) Move_mouse(+i_velox,0);
             else if(wParam == (long)hk_esq) Move_mouse(-i_velox,0);
             break;
        case WM_DESTROY:
            PostQuitMessage (0);       
            break;
        default:                   
            return DefWindowProc (hwnd, message, wParam, lParam);
    }
    return 0;
}
int WINAPI WinMain (HINSTANCE hThisInstance,HINSTANCE hPrevInstance,LPSTR lpszArgument,int nFunsterStil){                    
    WNDCLASSEX wincl ={sizeof(WNDCLASSEX),0,WinProc_Principal,0,0,
          hThisInstance,LoadIcon(NULL,IDI_APPLICATION),LoadCursor(NULL,IDC_ARROW),
          CreateSolidBrush(RGB(0,0,0xFF)),0,TEXT("C_Win"),LoadIcon(NULL,IDI_APPLICATION)};
    if(!RegisterClassEx (&wincl))return 0;
    hwnd = CreateWindowEx (0,TEXT("C_Win"),TEXT("S_Clicks"),WS_OVERLAPPED|WS_SYSMENU|WS_VISIBLE,CW_USEDEFAULT,CW_USEDEFAULT,150,100,HWND_DESKTOP,NULL,hThisInstance,NULL);
    RegisterHotKey(hwnd,hk_f4,0,VK_F4);
    RegisterHotKey(hwnd,hk_cima,0,hk_cima);
    RegisterHotKey(hwnd,hk_baixo,0,hk_baixo);
    RegisterHotKey(hwnd,hk_dir,0,hk_dir);
    RegisterHotKey(hwnd,hk_esq,0,hk_esq);
    while (GetMessage (&messages, NULL, 0, 0)){
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    };
    return messages.wParam;
}

thanks all.

wn_br
Last edited on
Does that work? Here is some code that moves the mouse to x, y 100, 100. Then it simulates a left click. You can put together all the hotkeys because quite frankly, I am horrible with the RegisterHotKey function. Maybe you can help me. ^^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <windows.h>

// Forward declaration of the LeftClick function
void LeftClick ( );

// Forward declaration of the MouseMove function
void MouseMove ( int x, int y );

int main()
{
    MouseMove(100, 100);
    LeftClick();
    return 0;
}

// LeftClick function
void LeftClick ( )
{  
  INPUT    Input={0};
  // left down 
  Input.type      = INPUT_MOUSE;
  Input.mi.dwFlags  = MOUSEEVENTF_LEFTDOWN;
  ::SendInput(1,&Input,sizeof(INPUT));

  // left up
  ::ZeroMemory(&Input,sizeof(INPUT));
  Input.type      = INPUT_MOUSE;
  Input.mi.dwFlags  = MOUSEEVENTF_LEFTUP;
  ::SendInput(1,&Input,sizeof(INPUT));
}

// MouseMove function
void MouseMove (int x, int y )
{  
	double fScreenWidth    = ::GetSystemMetrics( SM_CXSCREEN )-1; 
	double fScreenHeight  = ::GetSystemMetrics( SM_CYSCREEN )-1; 
	double fx = x*(65535.0f/fScreenWidth);
	double fy = y*(65535.0f/fScreenHeight);
	INPUT  Input={0};
	Input.type      = INPUT_MOUSE;
	Input.mi.dwFlags  = MOUSEEVENTF_MOVE|MOUSEEVENTF_ABSOLUTE;
	Input.mi.dx = fx;
	Input.mi.dy = fy;
	::SendInput(1,&Input,sizeof(INPUT));
}
Topic archived. No new replies allowed.