📄 vnchooks.cpp
字号:
break; case WM_TIMER: if (prf_use_Timer) SendDeferredWindowRect(hWnd); break; case WM_HSCROLL: case WM_VSCROLL: if (((int) LOWORD(wParam) == SB_THUMBTRACK) || ((int) LOWORD(wParam) == SB_ENDSCROLL)) SendDeferredWindowRect(hWnd); break; case 485: // HACK to handle popup menus { // Get the old popup menu selection value HANDLE prop = GetProp(hWnd, (LPCTSTR) MAKELONG(VNC_POPUPSELN_ATOM, 0)); if (prop != (HANDLE) wParam) { // It did, so update the menu & the selection value SendDeferredWindowRect(hWnd); SetProp(hWnd, (LPCTSTR) MAKELONG(VNC_POPUPSELN_ATOM, 0), (HANDLE) wParam); } } break; //////////////////////////////////////////////////////////////// // Messages indicating a full window update case WM_SYSCOLORCHANGE: case WM_PALETTECHANGED: case WM_SETTEXT: case WM_ENABLE: case BM_SETCHECK: case BM_SETSTATE: case EM_SETSEL: //case WM_MENUSELECT: SendDeferredWindowRect(hWnd); break; //////////////////////////////////////////////////////////////// // Messages indicating that an area of the window needs updating // Uses GetUpdateRect to find out which case WM_PAINT: if (prf_use_GetUpdateRect) { HRGN region; region = CreateRectRgn(0, 0, 0, 0); // Get the affected region if (GetUpdateRgn(hWnd, region, FALSE) != ERROR) { int buffsize; UINT x; RGNDATA *buff; POINT TopLeft; // Get the top-left point of the client area TopLeft.x = 0; TopLeft.y = 0; if (!ClientToScreen(hWnd, &TopLeft)) break; // Get the size of buffer required buffsize = GetRegionData(region, 0, 0); if (buffsize != 0) { buff = (RGNDATA *) new BYTE [buffsize]; if (buff == NULL) break; // Now get the region data if(GetRegionData(region, buffsize, buff)) { for (x=0; x<(buff->rdh.nCount); x++) { // Obtain the rectangles from the list RECT *urect = (RECT *) (((BYTE *) buff) + sizeof(RGNDATAHEADER) + (x * sizeof(RECT))); SendDeferredUpdateRect( hWnd, (SHORT) (TopLeft.x + urect->left), (SHORT) (TopLeft.y + urect->top), (SHORT) (TopLeft.x + urect->right), (SHORT) (TopLeft.y + urect->bottom) ); } } delete [] buff; } } // Now free the region if (region != NULL) DeleteObject(region); } else SendDeferredWindowRect(hWnd); break; //////////////////////////////////////////////////////////////// // Messages indicating full repaint of this and a different window // Send the new position of the window case WM_WINDOWPOSCHANGING: if (IsWindowVisible(hWnd)) SendWindowRect(hWnd); break; case WM_WINDOWPOSCHANGED: if (IsWindowVisible(hWnd)) SendDeferredWindowRect(hWnd); break; //////////////////////////////////////////////////////////////// // WinVNC also wants to know about mouse movement case WM_NCMOUSEMOVE: case WM_MOUSEMOVE: // Inform WinVNC that the mouse has moved and pass it the current cursor handle { ULONG new_cursor = (ULONG)GetCursor(); if (new_cursor != old_cursor) { if (!PostThreadMessage( vnc_thread_id, MouseMoveMessage, (ULONG) new_cursor, 0)) vnc_thread_id = 0; old_cursor=new_cursor; } } break; //////////////////////////////////////////////////////////////// // VNCHOOKS PROPERTIES HANDLING WINDOWS case WM_DESTROY: RemoveProp(hWnd, (LPCTSTR) MAKELONG(VNC_POPUPSELN_ATOM, 0)); break; } return TRUE;}// Hook procedure for CallWindow hookLRESULT CALLBACK CallWndProc(int nCode, WPARAM wParam, LPARAM lParam){ // Do we have to handle this message? if (nCode == HC_ACTION) { // Process the hook if the WinVNC thread ID is valid if (vnc_thread_id) { CWPSTRUCT *cwpStruct = (CWPSTRUCT *) lParam; HookHandle(cwpStruct->message, cwpStruct->hwnd, cwpStruct->wParam, cwpStruct->lParam); } } // Call the next handler in the chain return CallNextHookEx (hCallWndHook, nCode, wParam, lParam);}// Hook procedure for GetMessageProc hookLRESULT CALLBACK GetMessageProc(int nCode, WPARAM wParam, LPARAM lParam){ // Do we have to handle this message? if (nCode == HC_ACTION) { // Process the hook only if the WinVNC thread id is valid if (vnc_thread_id) { MSG *msg = (MSG *) lParam; // Only handle application messages if they're being removed: if (wParam & PM_REMOVE) { // Handle the message HookHandle(msg->message, msg->hwnd, msg->wParam, msg->lParam); } } } // Call the next handler in the chain return CallNextHookEx (hGetMsgHook, nCode, wParam, lParam);}// Hook procedure for DialogMessageProc hookLRESULT CALLBACK DialogMessageProc(int nCode, WPARAM wParam, LPARAM lParam){ // Do we have to handle this message? if (nCode >= 0) { // Process the hook only if the WinVNC thread ID is valid if (vnc_thread_id) { MSG *msg = (MSG *) lParam; // Handle the message HookHandle(msg->message, msg->hwnd, msg->wParam, msg->lParam); } } // Call the next handler in the chain return CallNextHookEx (hGetMsgHook, nCode, wParam, lParam);}// Hook procedure for LowLevel Keyboard filtering#ifdef WH_KEYBOARD_LLLRESULT CALLBACK LowLevelKeyboardFilterProc(int nCode, WPARAM wParam, LPARAM lParam){ // Are we expected to handle this callback? if (nCode == HC_ACTION) { // Is this keyboard event "real" or "injected" // i.e. hardware or software-produced? KBDLLHOOKSTRUCT *hookStruct = (KBDLLHOOKSTRUCT*)lParam; if (!(hookStruct->flags & LLKHF_INJECTED)) { // Message was not injected - reject it! return TRUE; } } // Otherwise, pass on the message return CallNextHookEx(hLLKeyboardHook, nCode, wParam, lParam);}#endif// Hook procedure for LowLevel Mouse filtering#ifdef WH_MOUSE_LLLRESULT CALLBACK LowLevelMouseFilterProc(int nCode, WPARAM wParam, LPARAM lParam){ // Are we expected to handle this callback? if (nCode == HC_ACTION) { // Is this mouse event "real" or "injected" // i.e. hardware or software-produced? MSLLHOOKSTRUCT *hookStruct = (MSLLHOOKSTRUCT*)lParam; if (!(hookStruct->flags & LLMHF_INJECTED)) { // Message was not injected - reject it! return TRUE; } } // Otherwise, pass on the message return CallNextHookEx(hLLMouseHook, nCode, wParam, lParam);}#endifchar * NameFromPath(const char *path){ int x; int l = strlen(path); char *temp = NULL; // Find the file part of a filename for (x=l-1; x>0; x--) { if (path[x] == '\\') { temp = strdup(&(path[x+1])); break; } } // If we didn't fine a \ then just return a copy of the original if (temp == NULL) temp = strdup(path); return temp;}/////////////////////////////////////////////////////////////////////////////// Initialise / Exit routines.// These functions handle the update settings for any apps used with WinVNC.static const TCHAR szSoftware[] = "Software";static const TCHAR szCompany[] = "ORL";static const TCHAR szProfile[] = "VNCHooks";HKEY hModuleKey = NULL;HKEY OpenKey(HKEY basekey, const char* path[], bool writable, bool create) { HKEY key = basekey; DWORD flags = KEY_READ; if (writable) flags |= KEY_WRITE; if (create) flags |= KEY_CREATE_SUB_KEY; unsigned int i = 0; while (path[i]) { HKEY newkey; DWORD result, dw; if (create) { _RPT2(_CRT_WARN, "vncHooks : creating %s from %x\n", path[i], key); result = RegCreateKeyEx(key, path[i], 0, REG_NONE, REG_OPTION_NON_VOLATILE, flags, NULL, &newkey, &dw); } else { _RPT2(_CRT_WARN, "vncHooks : opening %s from %x\n", path[i], key); result = RegOpenKeyEx(key, path[i], 0, flags, &newkey); } if (key && (key != basekey)) RegCloseKey(key); key = newkey; if (result != ERROR_SUCCESS) { _RPT2(_CRT_WARN, "vncHooks : failed to open %s(%lu)\n", path[i], result); return NULL; } else { _RPT2(_CRT_WARN, "vncHooks : opened %s (%x)\n", path[i], key); } i++; } return key;}HKEY GetModuleKey(HKEY basekey, const char* proc_name, bool writable, bool create) { // Work out the registry key to save this under if (!sModulePrefs) { sModulePrefs = (char *) malloc(strlen(proc_name) + 1); if (sModulePrefs == NULL) return FALSE; strcpy(sModulePrefs, proc_name); } // Check whether the library's entry exists! const char* appPath[] = {szSoftware, szCompany, szProfile, 0}; HKEY appKey = OpenKey(basekey, appPath, writable, create); if (!appKey) return NULL; // Attempt to open the registry section for the application const char* modPath[] = {sPrefSegment, sModulePrefs, 0}; HKEY modKey = OpenKey(appKey, modPath, writable, false); if (!modKey) { // Cut off the app directory and just use the name char *file_name = NameFromPath(proc_name); if (!file_name) { RegCloseKey(appKey); return NULL; } // Adjust the moduleprefs name strcpy(sModulePrefs, file_name); free(file_name); // Now get the module key again const char* modPath2[] = {sPrefSegment, sModulePrefs, 0}; modKey = OpenKey(appKey, modPath2, writable, create); } RegCloseKey(appKey); return modKey;}int GetProfileInt(LPTSTR key, int def){ DWORD type; DWORD value; ULONG size = sizeof(value); if (RegQueryValueEx( hModuleKey, key, NULL, &type, (unsigned char *)&value, &size) == ERROR_SUCCESS) { // Is the value of the right type? if (type != REG_DWORD) { return def; } else { return value; } } else { return def; }}void WriteProfileInt(LPTSTR key, int value){ RegSetValueEx( hModuleKey, key, 0, REG_DWORD, (unsigned char *)&value, sizeof(value));}void ReadSettings() { // Read in the prefs prf_use_GetUpdateRect = GetProfileInt( "use_GetUpdateRect", TRUE ); prf_use_Timer = GetProfileInt( "use_Timer", FALSE ); prf_use_KeyPress = GetProfileInt( "use_KeyPress", TRUE ); prf_use_LButtonUp = GetProfileInt( "use_LButtonUp", TRUE ); prf_use_MButtonUp = GetProfileInt( "use_MButtonUp", TRUE ); prf_use_RButtonUp = GetProfileInt( "use_RButtonUp", TRUE ); prf_use_Deferral = GetProfileInt( "use_Deferral", TRUE );}BOOL InitInstance() { // Create the global atoms VNC_POPUPSELN_ATOM = GlobalAddAtom(VNC_POPUPSELN_ATOMNAME); if (VNC_POPUPSELN_ATOM == NULL) return FALSE; // Get the module name char proc_name[_MAX_PATH]; DWORD size; // Attempt to get the program/module name if ((size = GetModuleFileName( GetModuleHandle(NULL), (char *) &proc_name, _MAX_PATH )) == 0) return FALSE; // Get the default system key for the module hModuleKey = GetModuleKey(HKEY_LOCAL_MACHINE, proc_name, false, false); if (hModuleKey != NULL) { _RPT0(_CRT_WARN, "vncHooks : loading machine prefs\n"); ReadSettings(); RegCloseKey(hModuleKey); hModuleKey = NULL; } // Get the key for the module hModuleKey = GetModuleKey(HKEY_CURRENT_USER, proc_name, false, false); if (hModuleKey != NULL) { _RPT0(_CRT_WARN, "vncHooks : loading user prefs\n"); ReadSettings(); RegCloseKey(hModuleKey); hModuleKey = NULL; } return TRUE;}BOOL ExitInstance() { // Free the created atoms if (VNC_POPUPSELN_ATOM != NULL) { // GlobalDeleteAtom(VNC_POPUPSELN_ATOM); VNC_POPUPSELN_ATOM = NULL; } // Write the module settings to disk if (sModulePrefs != NULL) { // Get the module name char proc_name[_MAX_PATH]; DWORD size; // Attempt to get the program/module name if ((size = GetModuleFileName( GetModuleHandle(NULL), (char *) &proc_name, _MAX_PATH )) == 0) return FALSE; // Get the key for the module _RPT0(_CRT_WARN, "vncHooks : locating user prefs\n"); hModuleKey = GetModuleKey(HKEY_CURRENT_USER, proc_name, true, true); if (hModuleKey == NULL) return FALSE; _RPT0(_CRT_WARN, "vncHooks : writing user prefs\n"); WriteProfileInt( "use_GetUpdateRect", prf_use_GetUpdateRect ); WriteProfileInt( "use_Timer", prf_use_Timer ); WriteProfileInt( "use_KeyPress", prf_use_KeyPress ); WriteProfileInt( "use_LButtonUp", prf_use_LButtonUp ); WriteProfileInt( "use_MButtonUp", prf_use_MButtonUp ); WriteProfileInt( "use_RButtonUp", prf_use_RButtonUp ); WriteProfileInt( "use_Deferral", prf_use_Deferral ); free(sModulePrefs); sModulePrefs = NULL; } // Close the registry key for this module if (hModuleKey != NULL) { RegCloseKey(hModuleKey); hModuleKey = NULL; } return TRUE;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -