I have a program with several custom controls. One of these custom controls is a text input control. Since a window does not automatically receive keyboard focus when you click on it, i've created a mouse hook in my program that calls SetFocus() on a window when the user clicks in that window. However, there is a problem.
If another program has focus when you click on my program's window (or any of the controls in that window) SetFocus() fails. I then have to click again for it to succeed. Here's the code:
LRESULT CALLBACK kbfProc(int nCode, WPARAM wParam, LPARAM lParam) // Keyboard focus switching procedure
{
switch(nCode)
{
case HC_ACTION:
{
if(wParam == WM_LBUTTONDOWN || wParam == WM_NCLBUTTONDOWN)
{
MOUSEHOOKSTRUCT * mhs = (MOUSEHOOKSTRUCT*) lParam;
if(SetFocus(mhs->hwnd) == NULL)
{
printf("SetFocus(Hwnd = %.8x) failed. Error code: %lu\n", mhs->hwnd, GetLastError());
} else {
printf("SetFocus(Hwnd = %.8x) returned success.\n", mhs->hwnd);
}
}
}
break;
}
return CallNextHookEx(0, nCode, wParam, lParam);
}
And the output of those printf calls is this:
SetFocus(Hwnd = 00410c06) failed. Error code: 87
SetFocus(Hwnd = 00410c06) returned success.
SetFocus(Hwnd = 01740fc8) failed. Error code: 87
SetFocus(Hwnd = 01740fc8) returned success.
Error code 87 is ERROR_INVALID_PARAMETER, but i'm obviously passing a valid window handle to the function, so why is it failing?
Whenever you're calling SetFocus, the window must be attached to the calling thread's message queue or SetFocus will return invalid if it's not. To workaround this, use SetForegroundWindow first when the mouse moves over your window before calling SetFocus.
I know I'm couple of days late, but since I just spent a whole day trying to fix this, I'll add my fix here as well, just in case it helps someone.
Basically it was the AttachThreadInput thing mentioned above in a comment. GetActiveWindow() was always returning NULL as well. Which window you need to attach to might vary case by case, but I needed the root window, so I used GetAncestor. If you know the window handle you want, then you can just use it instead. So this is the code that fixed it for me:
AttachThreadInput(GetCurrentThreadId(), GetWindowThreadProcessId(GetAncestor(hWnd, GA_ROOT), NULL), TRUE);
I've found a solution. After a lot of googling and trial & error I eventually came across this webpage (backup link). It goes over the behavior of window focus and activation in detail.
I ended up adding some code to the WM_ACTIVATE handler of my main window that searches for the child window that was clicked when the window is activated. Here's all the code:
Here's the hook procedure:
LRESULT CALLBACK kbfProc(int nCode, WPARAM wParam, LPARAM lParam)
{
switch(nCode)
{
case HC_ACTION:
{
if(wParam == WM_LBUTTONDOWN || wParam == WM_NCLBUTTONDOWN)
{
MOUSEHOOKSTRUCT * mhs = (MOUSEHOOKSTRUCT*) lParam;
BringWindowToTop(MainWindow->t_hwnd);
SetFocus(mhs->hwnd);
}
}
break;
}
return CallNextHookEx(0, nCode, wParam, lParam);
}
Here's the code i put in the WM_ACTIVATE handler:
case WM_ACTIVATE:
{
unsigned long state = (unsigned long) wParam & 0x0000FFFF;
unsigned long mState = (unsigned long) wParam & 0xFFFF0000;
if(state != 0)
{
...[some code here]...
FocusChildWindow(hwnd);
}
...[some code here]...
}
break;
And here's the FocusChildWindow() function:
void FocusChildWindow(HWND hwnd)
{
POINT mpos;
GetCursorPos(&mpos);
RECT wr;
GetWindowRect(hwnd, &wr);
mpos.x -= wr.left;
mpos.y -= wr.top;
HWND cw = ChildWindowFromPoint(hwnd, mpos);
if(cw == NULL || cw == hwnd)
{
SetFocus(hwnd);
} else {
GetCursorPos(&mpos);
HWND cw2;
while(1)
{
POINT sc = mpos;
MapWindowPoints(HWND_DESKTOP, cw, &sc, 1);
cw2 = ChildWindowFromPoint(cw, sc);
if(cw2 == NULL || cw2 == cw)
{
SetFocus(cw);
break;
} else {
cw = cw2;
}
}
}
}
The following worked for me when SetFocus() had no effect setting the keyboard focus to a child control window on a property sheet page window:
::SendMessage(m_hPropPageWnd, WM_UPDATEUISTATE, MAKEWPARAM(UIS_CLEAR, UISF_HIDEFOCUS), 0);
Related
I've got C++ application (used for share application's window via network). I need to update application's area on client side when it's size was changed on server side. For this purpose once in a period of time I call GetWindowPos to check if the window was resized. But I don't want to send the info when the window is in resizing state and send the info only when resizing is completed. I noticed that on Windows 8.1 and Windows 10 GetWindowPos returns same values when the window is in resizing state, however on Windows 7 it returns different values when the window is in resizing state. So the question is how to understand if window is in resizing state?
UPD: Implementation of WM_ENTERSIZEMOVE - WM_EXITSIZEMOVE variant
void WindowsDisplayHelperMasterWindow::SetMsgHook()
{
m_pThis = this;
m_msgHook = SetWindowsHookEx(WH_GETMESSAGE, MsgPoc, NULL, 0);
}
Static function that call non-static method of the class:
LRESULT CALLBACK WindowsDisplayHelperMasterWindow::MsgPoc(int code, WPARAM wParam, LPARAM lParam)
{
if (m_pThis != nullptr)
{
return m_pThis->GetMsgProcHook(code, wParam, lParam);
}
return CallNextHookEx(0, code, wParam, lParam);
}
Hook function:
LRESULT CALLBACK WindowsDisplayHelperMasterWindow::GetMsgProcHook(int code, WPARAM wParam, LPARAM lParam)
{
if (code < 0)
{
return CallNextHookEx(0, code, wParam, lParam);
}
MSG* lpmsg = (MSG*)lParam;
if (lpmsg->hwnd != m_windowHandle)
{
return CallNextHookEx(0, code, wParam, lParam);
}
if (lpmsg->message == WM_ENTERSIZEMOVE && !m_isWindowResizing)
{
m_isWindowResizing = true;
}
else if (lpmsg->message == WM_EXITSIZEMOVE && m_isWindowResizing)
{
m_isWindowResizing = false;
}
return CallNextHookEx(0, code, wParam, lParam);
}
m_pThis and m_msgHook are static class members:
WindowsDisplayHelperMasterWindow* WindowsDisplayHelperMasterWindow::m_pThis = nullptr;
HHOOK WindowsDisplayHelperMasterWindow::m_msgHook = NULL;
And here is the check itself:
if (!m_displayMode.IsEqualGeometry(displayMode) && !m_isWindowResizing)
{
DUMPER_DEBUG("DS_ERROR_MODE_CHANGED");
return DS_ERROR_MODE_CHANGED; // depending on this value server asks client to update application's window area
}
Thanks.
A window receives a WM_ENTERSIZEMOVE message, after it has entered the moving and sizing modal loop. A window receives a WM_EXITSIZEMOVE message, after it has exited the moving or sizing modal loop.
If you monitor those two messages, you know when a window is in moving and sizing state.
I'm using C++ and DirectD3D9 to draw a menu.
I wish to navigate the menu with the mouse.
I can get the mouse position, however, checking if the left button is clicked is proving tricky.
I am able to check if it is being held down, but not clicked.
bool LBUTTONDOWN = false;
LRESULT CALLBACK LowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode == HC_ACTION && (wParam == WM_LBUTTONUP || wParam == WM_LBUTTONDOWN)) {
LBUTTONDOWN = wParam == WM_LBUTTONDOWN;
}
return CallNextHookEx(0, nCode, wParam, lParam);
}
How can I add a check to see if I clicked the left button?
You need to use a timing trick. Create a variable named something like 'nTime',
Set the zero for it when the LButton is up. Increase the variable value using a '+=' operator when the LButton is down and check the variable against a value something like that -
bool LBUTTONDOWN = false;
int nTime = 0;
LRESULT CALLBACK LowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HC_ACTION && (wParam == WM_LBUTTONUP || wParam == WM_LBUTTONDOWN))
{
LBUTTONDOWN = wParam == WM_LBUTTONDOWN;
if ( LBUTTONDOWN )
{
nTime += 1;
if ( nTime > 1000 /*( this value depends on you )*/ )
{
nTime = 0;
// Here is your hold event code.
}
}
else
nTime = 0;
}
return CallNextHookEx(0, nCode, wParam, lParam);
}
There is no DoubleClick message for LowLevelMouseProc. However, I suppose you can have a work around:
Record the time interval between LBUTTONDOWN and LBUTTONUP, then to check whether it is quick enough to be a click event. And because the mouse acts very fast, it is better to set a timer for the mouse capturing.
For the mouse capturing, you can still call LowLevelMouseProc. However, the DirectInput is more convenient for processing mouse movements.
In DirectX SDK samples, there is a DirectInput sample named "CustomFormat". It shows how to set up a timer to capture mouse input.
I hope this helps.
I am having issues with creating a modeless dialog from a DLL file. My dialog has nothing special on it, just an OK button and an edit box. I have looked at this Microsoft KB Article (http://support.microsoft.com/kb/233263) and have implemented its solution to create a window hook to grab and process messages.
The method provided by Microsoft solves the tab key problem, however, it creates another problem. When I type into the edit box on the dialog, whatever I press is duplicated 4 times. For example, if I press 'a' on the keyboard, 'aaaa' will show up in the edit box.
If I disable the Window Hook, then the edit box works correctly and only displays one 'a'.
What do I need to do to the Window Hook procedure to solve this problem?
Any help is greatly appreciated.
- - EDIT - -
As per request, my Window Hook Procedure Code: (It's the same as the KB article)
LRESULT FAR PASCAL GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam) {
LPMSG lpMsg = (LPMSG) lParam;
if (nCode >= 0 && PM_REMOVE == wParam) {
// Don't translate non-input events.
if ((lpMsg->message >= WM_KEYFIRST && lpMsg->message <= WM_KEYLAST)) {
if (IsDialogMessage(hwndDllDlg, lpMsg)) {
// The value returned from this hookproc is ignored,
// and it cannot be used to tell Windows the message has been handled.
// To avoid further processing, convert the message to WM_NULL
// before returning.
lpMsg->message = WM_NULL;
lpMsg->lParam = 0;
lpMsg->wParam = 0;
}
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
My Dialog Callback Procedure:
BOOL CALLBACK DllDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_INITDIALOG:
hHook = SetWindowsHookEx(WH_GETMESSAGE, GetMsgProc, NULL, GetCurrentThreadId());
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
DestroyWindow(hwndDlg);
hwndDllDlg = NULL;
}
return TRUE;
case WM_DESTROY:
UnhookWindowsHookEx(hHook);
return FALSE;
}
return FALSE;
}
}
Both hHook and hwndDllDlg are defined as HHOOK and HWND respectively.
HHOOK hHook;
HWND hwndDllDlg = CreateDialog(0, MAKEINTRESOURCE(DLG_MAIN), 0, DllDlgProc);
I looked at the KB article. It sounds reasonable. There is some point where you was not enough accurate while following the instructions from KB. Post your code. This may help.
If you have control over the message pump of the executable and can add IsDialogMessage there, then you do not need any hook. Code from the dll is part of the code of the process. Window handles are in the common space either.
Other approach is starting your own UI thread. If you create your dialog on this thread, then you will have your own message pump. The hook will not be needed in this case either.
Well, this is more of a question to author of the post..
I have the tab key issue and am trying to understand the microsoft article better.
So my dialog is shipped out as Dll and the application which I don't have access to is launching dialog from my dll.
HWND hwndDllDlg = CreateDialog(0, MAKEINTRESOURCE(DLG_MAIN), 0, DllDlgProc);
I don't understand what dialog the code refers to when they said hwndDllDlg in the article. Should I point my dialog creation to this variable ?
I have a problem: I use SendMessage from a procedure in a DLL to communicate with the main window; procedure is a hook procedure that allows main window to know when mouse right button is clicked in a editbox; it sends also handle of the editbox. It works well, except for this bug: when program is running without breakpoints main window receives twice the same message (in this case WM_APP), while if I put a breakpoint in the hook procedure or in the block that handles WM_APP messages the message is considered once. For further descriptions ask me. Following the code of the hook procedure and of the block that handles WM_APP messages. Thanks
Hook procedure
MYDLL_API LRESULT CALLBACK mouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
// processes the message
if(nCode >= 0)
{
// if user clicked with mouse right button
if(wParam != NULL && (wParam == WM_RBUTTONDOWN || wParam == WM_RBUTTONUP))
{
wchar_t *s = (wchar_t*) malloc(CLASSNAMELEN*sizeof(wchar_t));
//MessageBox(mainHwnd, (LPCWSTR)L"Captured mouse right button", (LPCWSTR)L"Test", MB_OK);
MOUSEHOOKSTRUCT *m = (MOUSEHOOKSTRUCT*) lParam;
GetClassName(m->hwnd, (LPWSTR) s, CLASSNAMELEN);
//MessageBox(mainHwnd, (LPCWSTR) s, (LPCWSTR)L"Test", MB_OK);
// only if user clicked on a edit box
if(wcsncmp(s, L"Edit", 4) == 0)
SendMessage(mainHwnd, WM_APP, 0, (LPARAM) lParam);
free(s);
s = NULL;
}
}
// calls next hook in chain
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
block in main program that handles WM_APP messages
case WM_APP:
{
//MessageBox(hWnd, (LPCWSTR)L"Received WM_APP", (LPCWSTR)L"Test", MB_OK);
// copies text from the edit box
MOUSEHOOKSTRUCT *m = (MOUSEHOOKSTRUCT*) lParam;
int n = GetWindowTextLength(m->hwnd);
// if text has been inserted
if(n > 0 && n < 1024)
{
wchar_t *s = (wchar_t*) malloc((n+1)*sizeof(wchar_t));
// gets text
GetWindowText(m->hwnd, (LPWSTR) s, n+1);
s[n] = (wchar_t) 0;
//MessageBox(hWnd, (LPCWSTR)s, (LPCWSTR)L"Test", MB_OK);
// saves text in database
stateClassPointer->insertInList(s);
}
}
break;
It is probably because you are sending the message for WM_RBUTTONDOWN and WM_RBUTTONUP, that is when the right button is pressed and when it is released.
When you are debugging the WM_RBUTTONUP is eaten by the debugger so you don't get it.
PS: Shouldn't you use PostMessage() instead of SendMessage(), just to be safe?
I am trying to subclass the currently focused window on a Windows system using a global CBT hook. This is related to what happens in this question, but the bug is different.
What happens when this subclassing is in effect, is that Opera's (version 10.50) main window is prevented from displaying. Opera has a "splash screen" where you are required to click "Start" for the main window to show that appears after Opera has not shut down properly. Whenever this window pops up, Opera's main window won't show. If Opera was shut down properly, and this splash screen does not show, the main window displays as it should.
HHOOK hHook;
HWND hWndSubclass = 0;
void SubclassWindow(HWND hWnd)
{
Unsubclass();
FARPROC lpfnOldWndProc = (FARPROC)SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LPARAM)SubClassFunc);
SetProp(hWnd, L"PROP_OLDWNDPROC", lpfnOldWndProc);
hWndSubclass = hWnd;
}
void Unsubclass()
{
if (hWndSubclass != 0 && IsWindow(hWndSubclass))
{
FARPROC lpfnOldWndProc = (FARPROC)GetProp(hWndSubclass, L"PROP_OLDWNDPROC");
RemoveProp(hWndSubclass, L"PROP_OLDWNDPROC");
SetWindowLongPtr(hWndSubclass, GWLP_WNDPROC, (LPARAM)lpfnOldWndProc);
hWndSubclass = 0;
}
}
static LRESULT CALLBACK SubClassFunc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if (message == WM_MOVING)
{
// do something irrelevant
}
else if (message == WM_DESTROY)
{
Unsubclass();
}
FARPROC lpfnOldWndProc = (FARPROC)GetProp(hWndSubclass, L"PROP_OLDWNDPROC");
return CallWindowProc((WNDPROC)lpfnOldWndProc, hWndSubclass, message, wParam, lParam);
}
static LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HCBT_SETFOCUS && hWndServer != NULL)
{
SubclassWindow((HWND)wParam);
}
if (nCode < 0)
{
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
return 0;
}
BOOL APIENTRY DllMain( HINSTANCE hInstance,
DWORD Reason,
LPVOID Reserved
)
{
switch(Reason)
{
case DLL_PROCESS_ATTACH:
hInst = hInstance;
return TRUE;
case DLL_PROCESS_DETACH:
Unsubclass();
return TRUE;
}
return TRUE;
}
My suspicion is that Opera's main window is somehow already subclassed. I imagine the following is happening:
The window is created with it's own basic WndProc, and is given focus
My application subclasses the window, storing the original WndProc
Opera subclasses its own window
When the window loses focus, I restore the original WndProc, thus ignoring the second WndProc
Can this really be the case? Are there any other explanations?
This can happen, as Raymond Chen writes:
Consider what would happen if somebody else had subclassed the window during the "... do stuff ..." section. When we unsubclassed the window, we would have removed two subclasses, the one we installed, and the one that was installed after us. If the other subclass allocated memory (which is very common), then that memory got leaked, in addition to the subclass failing to do whatever it was trying to do.
He continues with a solution:
This is quite a cumbersome process, so the shell team wrote some helper functions to do all this for you. The SetWindowSubclass function does all the grunt work of installing a subclass procedure, remembering the previous one, and passing reference data to the subclass procedure you provide. You use the DefSubclassProc function to forward the message to the previous subclass procedure, and when you're done, you use the RemoveWindowSubclass function to remove yourself from the chain. RemoveWindowSubclass does all the work to do the right thing if you are not the window procerure at the top of the chain.