简体   繁体   中英

Subclass existing window with Win32

I am currently trying to grab all of the user input to the windows calculator app. It seems the way to do this is to use Win32 to intercept all of the keyboard and mouse inputs that are intended for the calculator window. I have read the MSDN page on subclassing a window at the link below and have done some research on subclassing.

I have the syntax for subclassing a window, but I am not sure how to tell the program which window I am looking to subclass.

the code that I have so far is listed below. My problem right now is that I am not sure how the variable "hWndEdit" is assigned. I am pretty new to Win32 programming so any help is appreciated.

(link) http://msdn.microsoft.com/en-us/library/windows/desktop/ms633570(v=vs.85).aspx

WNDPROC wpOrigEditProc; 

wpOrigEditProc = (WNDPROC) SetWindowLong(hWndEdit,GWL_WNDPROC,(long) WndEditProc);

LRESULT CALLBACK WndEditProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message) 
        {
        case WM_CHAR:
        case WM_KEYUP:
        case WM_KEYDOWN:
            if (hWnd == hWndEdit)
                return 0;
        break;
        case WM_DESTROY: 
            // Remove the subclass from the edit control. 
            SetWindowLong(hWndEdit, GWL_WNDPROC, (LONG) wpOrigEditProc); 
        break;
        default:
            return CallWindowProc((WNDPROC ) wpOrigEditProc, hWnd, message, wParam, lParam);
        }
    return CallWindowProc((WNDPROC ) wpOrigEditProc, hWnd, message, wParam, lParam);
}

To find a window, first use Spy++ (A tool that gets installed with Visual Studio) to find the class name and the window name of the calculator main window. Then, in your application, use the FindWindow API:

hWndEdit = FindWindow(className, windowName);

Although, I'm not sure that subclassing is the right method here since the Calculator window is not owned by your application. You should do this with hooks .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM