Count number of mouse clicks C
我有一个C代码,用来检查鼠标左键是否被按下。它工作得很好,但我想用它来计算按钮被单击的次数,并在按钮被随机单击多次时调用函数。
这是代码:
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 | LRESULT CALLBACK mouseProc(int nCode, WPARAM wParam, LPARAM lParam) { int count = 0; MOUSEHOOKSTRUCT * pMouseStruct = (MOUSEHOOKSTRUCT *)lParam; if (pMouseStruct != NULL){ if (wParam == WM_LBUTTONDOWN) { count++; printf("%d",count); if (count==finalNum){ // user clicked random times the mouse so we launch the final function printf(" done! "); final(); } printf("clicked"); } printf("Mouse position X = %d Mouse Position Y = %d ", pMouseStruct->pt.x, pMouseStruct->pt.y); } return CallNextHookEx(hMouseHook, nCode, wParam, lParam); } DWORD WINAPI MyMouseLogger(LPVOID lpParm) { HINSTANCE hInstance = GetModuleHandle(NULL); // here I put WH_MOUSE instead of WH_MOUSE_LL hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, mouseProc, hInstance, NULL); MSG message; while (GetMessage(&message, NULL, 0, 0)) { TranslateMessage(&message); DispatchMessage(&message); } UnhookWindowsHookEx(hMouseHook); return 0; } void custom_delay(){ } int main(int argc, char *argv[]) { int count = 0; HANDLE hThread; DWORD dwThread; //////Generate random number to call a function after rand() number of clicks srand(time(NULL)); // Seed the time int finalNum = rand() % (150 - 50) + 50; // Generate the number, assign to variable. //////////////////////////////////////////////////////////////////////////// printf("%d", finalNum); hThread = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)MyMouseLogger, (LPVOID)argv[0], NULL, &dwThread); if (hThread) return WaitForSingleObject(hThread, INFINITE); else return 1; } } |
问题是,每次鼠标事件发生时,COUNT变量都重置为0,因此我无法跟踪用户用鼠标单击的时间。
另一个问题是,我想生成一个介于50到150之间的随机次数来调用final()函数。我怎样才能把那个随机数作为参数发送呢?
谢谢你的帮助!
因为您在函数中声明
或者在
现在,对于问题的下一部分,您可以使用
另外,如果你想存储一些随机数并能从