witam to jest mój pierwszy program od kilku lat.
Program działa tylko wtedy gdy się myszką najedzie na niego samego.
W jaki sposób zrobićten program żeby cały czs klikało?
I żeby przy okazj dalej działałten hook na klawaiturę?
Program napiasny w console aplikation bez urzycia hooka działa prawidłowo.
#include <windows.h>
#include <stdlib.h>
#include <time.h>
#include <cstdio>
//#include <iostream>
#include <cstdlib>
#define _WIN32_WINNT 0x050
#define IDC_MAIN_BUTTON 101 // Button identifier
#define IDC_MAIN_EDIT 102 // Edit box identifier
#define IDC_MAIN_EDIT2 103 // Edit box identifier
#define IDC_MAIN_EDIT3 104 // Edit box identifier
#define IDC_MAIN_EDIT4 105 // Edit box identifier
#define IDC_MAIN_EDIT5 106 // Edit box identifier
#define IDC_MAIN_EDIT6 107 // Edit box identifier
#define IDC_MAIN_EDIT7 108 // Edit box identifier
HWND hEdit;
HWND hEdit2;
HWND hEdit3;
HWND hEdit4;
HWND hEdit5;
HWND hEdit6;
HWND hEdit7;
char buffer1[256];//x1
char buffer2[256];//y1
char buffer3[256];//x2
char buffer4[256];//y2
char buffer5[256];//sleep ms
char buffer6[256];//d³ugoœc wykonywania
char buffer7[256];//ile rund
int x1;//x1
int y1;//y1
int x2;//x2
int y2;//y2
int sleepTime;//sleep ms
int duration;//d³ugoœc wykonywania
int amount;//ile rund
int j=0;
LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
BOOL fEatKeystroke = FALSE;
if (nCode == HC_ACTION)
{
switch (wParam)
{
case WM_KEYDOWN:
{
KBDLLHOOKSTRUCT hooked_key = *((KBDLLHOOKSTRUCT*)lParam);
// DWORD dwMsg = 1;
//dwMsg += hooked_key.scanCode << 16;
// dwMsg += hooked_key.flags << 24;
// char lpszKeyName[1024] = {0};
//int i = GetKeyNameText(dwMsg, (lpszKeyName+1),0xFF) + 1;
int key = hooked_key.vkCode;
if (key =='x'||key=='X')
{
MessageBox(0, "pauza3", "Informacja3", 0);
}
break;
}
case WM_SYSKEYDOWN:
case WM_KEYUP:
case WM_SYSKEYUP:
PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)lParam;
if (fEatKeystroke = (p->vkCode == 0x41)) { //redirect a to b
MessageBox(0, "pauza2", "Informacja2", 0);
keybd_event('B', 0, 0, 0);
keybd_event('B', 0, KEYEVENTF_KEYUP, 0);
break;
}
break;
}
}
return(fEatKeystroke ? 1 : CallNextHookEx(NULL, nCode, wParam, lParam));
}
static double diffclock(clock_t clock2,clock_t clock1)
{
double diffticks=clock1-clock2;
double diffms=(diffticks)/(CLOCKS_PER_SEC/1000);
return diffms;
}
void click( int x, int y)
{
SetCursorPos(x,y);
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
Sleep( sleepTime );
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
}
void click2( int x, int y)
{
SetCursorPos(x,y);
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
Sleep(150);
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
}
void click3( int x, int y)
{
SetCursorPos(x,y);
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
Sleep(15);
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
}
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nShowCmd)
{
WNDCLASSEX wClass;
ZeroMemory(&wClass,sizeof(WNDCLASSEX));
wClass.cbClsExtra=NULL;
wClass.cbSize=sizeof(WNDCLASSEX);
wClass.cbWndExtra=NULL;
wClass.hbrBackground=(HBRUSH)COLOR_WINDOW;
wClass.hCursor=LoadCursor(NULL,IDC_ARROW);
wClass.hIcon=NULL;
wClass.hIconSm=NULL;
wClass.hInstance=hInst;
wClass.lpfnWndProc=(WNDPROC)WinProc;
wClass.lpszClassName="Window Class1";
wClass.lpszMenuName=NULL;
wClass.style=CS_HREDRAW|CS_VREDRAW;
if(!RegisterClassEx(&wClass))
{
int nResult=GetLastError();
MessageBox(NULL,
"Window class creation failed\r\n",
"Window Class Failed",
MB_ICONERROR);
}
HWND hWnd=CreateWindowEx(NULL,
"Window Class1",
"Windows application1",
WS_OVERLAPPEDWINDOW,
200,
200,
640,
480,
NULL,
NULL,
hInst,
NULL);
// Install the low-level keyboard & mouse hooks
HHOOK hhkLowLevelKybd = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, 0, 0);
if(!hWnd)
{
int nResult=GetLastError();
MessageBox(NULL,
"Window creation failed\r\n",
"Window Creation Failed",
MB_ICONERROR);
}
ShowWindow(hWnd,nShowCmd);
MSG msg;
ZeroMemory(&msg,sizeof(MSG));
GetMessage(&msg,NULL,0,0);
TranslateMessage(&msg);
DispatchMessage(&msg);
MessageBox(0, "poczatek", "Informacja6", 0);
GetMessage(&msg,NULL,0,0);
TranslateMessage(&msg);
DispatchMessage(&msg);
GetWindowText(hEdit, buffer1, 256);
GetWindowText(hEdit2, buffer2, 256);
GetWindowText(hEdit3, buffer3, 256);
GetWindowText(hEdit4, buffer4, 256);
GetWindowText(hEdit5, buffer5, 256);
GetWindowText(hEdit6, buffer6, 256);
GetWindowText(hEdit7, buffer7, 256);
//GetDlgItemText(hwnd, 123, string);
//konwencja
x1=std::atoi(buffer1);//x1
y1=std::atoi(buffer2);//y1
x2=std::atoi(buffer3);//x2
y2=std::atoi(buffer4);//y2
sleepTime=std::atoi(buffer5);//sleep ms
duration=std::atoi(buffer6);//dlugoœc wykonywania
amount=std::atoi(buffer7);//ile rund
/*
char buffer11 [16] = "";
sprintf(buffer11, "%ld", x1);
char buffer12 [16] = "";
sprintf(buffer12, "%ld", y1);
char buffer13 [16] = "";
sprintf(buffer13, "%ld", x2);
char buffer14 [16] = "";
sprintf(buffer14, "%ld", y2);
char buffer15 [16] = "";
sprintf(buffer15, "%ld", sleepTime);
char buffer16 [16] = "";
sprintf(buffer16, "%ld", duration);
char buffer17 [16] = "";
sprintf(buffer17, "%ld", amount);
//LPARAM nr1=reinterpret_cast<LPARAM>(buffer11);
MessageBox(NULL,buffer11,"Information1",MB_ICONINFORMATION);
MessageBox(NULL,buffer12,"Information2",MB_ICONINFORMATION);
MessageBox(NULL,buffer13,"Information3",MB_ICONINFORMATION);
MessageBox(NULL,buffer14,"Information4",MB_ICONINFORMATION);
MessageBox(NULL,buffer15,"Information5",MB_ICONINFORMATION);
MessageBox(NULL,buffer16,"Information6",MB_ICONINFORMATION);
MessageBox(NULL,buffer17,"Information7",MB_ICONINFORMATION);
*/
//////////////// główna pętla programu: /////////////////////////////////
for ( int i = 0; i < amount; ++i )
{
clock_t start = clock();
while ( diffclock( start, clock() ) <= duration)
{
GetMessage(&msg,NULL,0,0);
TranslateMessage(&msg);
DispatchMessage(&msg);
Sleep(1);
click(x1,y1);
j++;
if(j>=50)
{
j=0;
Sleep(1);
click2(x2,y2);
}
}
MessageBox(0, "skonczyła się runda", "Informacja5", 0);
}
UnhookWindowsHookEx(hhkLowLevelKybd);
return 0;
}
//////////////////////////////////////////////////////////////////
LRESULT CALLBACK WinProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
HINSTANCE hInst;
switch(msg)
{
case WM_CREATE:
{
// Create an edit box
hEdit=CreateWindowEx(WS_EX_CLIENTEDGE,
"EDIT",
"100",
WS_CHILD|WS_VISIBLE|
ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL,
50,
10,
50,
30,
hWnd,
(HMENU)IDC_MAIN_EDIT,
GetModuleHandle(NULL),
NULL);
HGDIOBJ hfDefault=GetStockObject(DEFAULT_GUI_FONT);
CreateWindow(TEXT("STATIC"), "x1=", WS_CHILD | WS_VISIBLE | SS_LEFT, 10, 10, 20, 30, hWnd, (HMENU) 1, hInst, NULL);
hEdit2=CreateWindowEx(WS_EX_CLIENTEDGE,
"EDIT",
"200",
WS_CHILD|WS_VISIBLE|
ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL,
50,
50,
50,
30,
hWnd,
(HMENU)IDC_MAIN_EDIT2,
GetModuleHandle(NULL),
NULL);
HGDIOBJ hfDefault2=GetStockObject(DEFAULT_GUI_FONT);
CreateWindow(TEXT("STATIC"), "y1=", WS_CHILD | WS_VISIBLE | SS_LEFT, 10, 50, 20, 30, hWnd, (HMENU) 1, hInst, NULL);
hEdit3=CreateWindowEx(WS_EX_CLIENTEDGE,
"EDIT",
"300",
WS_CHILD|WS_VISIBLE|
ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL,
50,
110,
50,
30,
hWnd,
(HMENU)IDC_MAIN_EDIT3,
GetModuleHandle(NULL),
NULL);
HGDIOBJ hfDefault3=GetStockObject(DEFAULT_GUI_FONT);
CreateWindow(TEXT("STATIC"), "x2=", WS_CHILD | WS_VISIBLE | SS_LEFT, 10, 110, 20, 30, hWnd, (HMENU) 1, hInst, NULL);
hEdit4=CreateWindowEx(WS_EX_CLIENTEDGE,
"EDIT",
"400",
WS_CHILD|WS_VISIBLE|
ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL,
50,
150,
50,
30,
hWnd,
(HMENU)IDC_MAIN_EDIT4,
GetModuleHandle(NULL),
NULL);
HGDIOBJ hfDefault4=GetStockObject(DEFAULT_GUI_FONT);
CreateWindow(TEXT("STATIC"), "y3=", WS_CHILD | WS_VISIBLE | SS_LEFT, 10, 150, 20, 30, hWnd, (HMENU) 1, hInst, NULL);
hEdit5=CreateWindowEx(WS_EX_CLIENTEDGE,
"EDIT",
"25",
WS_CHILD|WS_VISIBLE|
ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL,
75,
220,
50,
30,
hWnd,
(HMENU)IDC_MAIN_EDIT5,
GetModuleHandle(NULL),
NULL);
HGDIOBJ hfDefault5=GetStockObject(DEFAULT_GUI_FONT);
CreateWindow(TEXT("STATIC"), "sleep(ms)=", WS_CHILD | WS_VISIBLE | SS_LEFT, 10, 220, 65, 30, hWnd, (HMENU) 1, hInst, NULL);
hEdit6=CreateWindowEx(WS_EX_CLIENTEDGE,
"EDIT",
"120000",
WS_CHILD|WS_VISIBLE|
ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL,
120,
260,
90,
30,
hWnd,
(HMENU)IDC_MAIN_EDIT6,
GetModuleHandle(NULL),
NULL);
HGDIOBJ hfDefault6=GetStockObject(DEFAULT_GUI_FONT);
CreateWindow(TEXT("STATIC"), "czas trwania(ms)=", WS_CHILD | WS_VISIBLE | SS_LEFT, 10, 260, 100, 30, hWnd, (HMENU) 1, hInst, NULL);
hEdit7=CreateWindowEx(WS_EX_CLIENTEDGE,
"EDIT",
"7",
WS_CHILD|WS_VISIBLE|
ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL,
85,
300,
50,
30,
hWnd,
(HMENU)IDC_MAIN_EDIT7,
GetModuleHandle(NULL),
NULL);
HGDIOBJ hfDefault7=GetStockObject(DEFAULT_GUI_FONT);
CreateWindow(TEXT("STATIC"), "ile rund=", WS_CHILD | WS_VISIBLE | SS_LEFT, 10, 300, 70, 30, hWnd, (HMENU) 1, hInst, NULL);
// Create a push button
HWND hWndButton=CreateWindowEx(NULL,
"BUTTON",
"OK",
WS_TABSTOP|WS_VISIBLE|
WS_CHILD|BS_DEFPUSHBUTTON,
250,
220,
100,
24,
hWnd,
(HMENU)IDC_MAIN_BUTTON,
GetModuleHandle(NULL),
NULL);
SendMessage(hWndButton,
WM_SETFONT,
(WPARAM)hfDefault,
MAKELPARAM(FALSE,0));
SendMessage(hWndButton,
WM_SETFONT,
(WPARAM)hfDefault2,
MAKELPARAM(FALSE,0));
}
break;
case WM_COMMAND:
{
}
switch(LOWORD(wParam))
{
case IDC_MAIN_BUTTON:
{
/*
SendMessage(hEdit,
WM_GETTEXT,
sizeof(buffer1)/sizeof(buffer1[0]),
reinterpret_cast<LPARAM>(buffer1));
SendMessage(hEdit2,
WM_GETTEXT,
sizeof(buffer2)/sizeof(buffer2[0]),
reinterpret_cast<LPARAM>(buffer2));
SendMessage(hEdit3,
WM_GETTEXT,
sizeof(buffer3)/sizeof(buffer3[0]),
reinterpret_cast<LPARAM>(buffer3));
SendMessage(hEdit4,
WM_GETTEXT,
sizeof(buffer4)/sizeof(buffer4[0]),
reinterpret_cast<LPARAM>(buffer4));
SendMessage(hEdit5,
WM_GETTEXT,
sizeof(buffer5)/sizeof(buffer5[0]),
reinterpret_cast<LPARAM>(buffer5));
SendMessage(hEdit6,
WM_GETTEXT,
sizeof(buffer6)/sizeof(buffer6[0]),
reinterpret_cast<LPARAM>(buffer6));
SendMessage(hEdit7,
WM_GETTEXT,
sizeof(buffer7)/sizeof(buffer7[0]),
reinterpret_cast<LPARAM>(buffer7));
//konwencja
x1=std::atoi(buffer1);//x1
y1=std::atoi(buffer2);//y1
x2=std::atoi(buffer3);//x2
y2=std::atoi(buffer4);//y2
sleepTime=std::atoi(buffer5);//sleep ms
duration=std::atoi(buffer6);//d³ugoœc wykonywania
amount=std::atoi(buffer7);//ile rund
*/
}
break;
}
break;
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
break;
}
return DefWindowProc(hWnd,msg,wParam,lParam);
}