简体   繁体   中英

How could I handle a Window's Message?

I'm working on some CreateWindow things. My work need I to insert a Button into one of an Application's Window, and when I click the button, my application can do something. That Application isn't my Application, so I think I need to do some "hook thing". And I tried this:

HWND hwnd = FindWindowEx("className",NULL)
CreateWindowEx(...hwnd...)

Its worked. The Button successfully inserted into target application.

but when I try to handle that window's message I failed. This hwnd is belongs to my application hInstance , but my Application has its own message loop by CEF. I tried SetWindowsHookEx ,but its not working. Whatever I do, it will not handled. and if i use while(GetMessage (&messages, NULL, 0, 0)) ,it will block my application's thread

WNDCLASS xxx and RegisterClass(xxx) or CreateWindow(className) confused me.

I try to use LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) but the while() will block my application's thread and in my application's wndProc , there is no WM_COMMAND message in.

    HWND hwnd = FindWindowEx(0, 0, L"TCustomBaseForm" , NULL);
    HWND hwndButton = CreateWindowEx(0L,_T("Button"), L"Btn",  WS_CHILD|WS_VISIBLE| BS_PUSHBUTTON, 435, 45, 35, 45, hwnd, NULL, GetModuleHandle(0), 0);
    DWORD dwProcId = 0;
    DWORD dwThreadId = 0;
    dwThreadId = GetWindowThreadProcessId(hwndButton, &dwProcId);
    SetWindowsHookEx(WH_MOUSE, Hookproc, g_hInstance, dwProcId);

Until now, my "inserted button" never work. So, is there any way to make this "inserted window" or button same as a MFC button that, when I click it, I can handle a message like WM_LBUTTONCLICK in my WndProc ?

A button sends a WM_COMMAND message to its parent window when you click it.

There are at least two ways to deal with this without hooking:

  • Create the button as a child of a STATIC control (or a custom window) that you also create. You need to subclass the STATIC control to receive the message.
  • Subclass the button and catch the mouse up message and space/enter key-down messages.

Thank you all. I did it.
I create a Window that have a button. And I use SetParent(myWindowHwnd,myTargetWindowHwnd) and handle the button's click event in my window, it works.
No hook, no problems. Thank you all very much. And thanks for edit my question, for my poooooooooooor English.
I've learned a lot about Windows Hook.

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