WinAPI. Check if window resizing has been finished - c++

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.

Related

Blocking windows mouse click using SetWindowsHookEx()

I have written an application to hook some procedure onto a new process to monitor mouse down events and to disable mouse down events on the new process. As of now, I am able to capture to mouse down events coming to this process and I am trying to disable all mouse down events as a POC. This is what I am doing currently in the hook procedure.
extern "C" __declspec(dllexport) LRESULT __stdcall meconnect(int code, WPARAM wParam, LPARAM lParam) {
if (code >= 0) {
LPMSG msg = (LPMSG)lParam;
if (msg->message == WM_LBUTTONDOWN) {
OutputDebugString(L"Mouse down event happened \n");
return false;
}
}
return(CallNextHookEx(NULL, code, wParam, lParam));
}
When I perform mouse down event, I am getting the log message that I have written. But I also expect that click event to be blocked since I am returning false. but it does not happen so and click event proceed as a normal click. How could I disable mouse down event. Thanks in advance for any help on this
This is how I call setWindowsHookEx
HHOOK handle = SetWindowsHookEx(WH_GETMESSAGE, addr, dll, threadID);
The reason you are supposed to call CallNextHookEx during your hook routine is so that the message can be passed on to any other hooks that might be installed. Failing to do so does not prevent the message from being seen by the application that received it.
The documentation for WM_NULL explains how to block the message:
For example, if an application has installed a WH_GETMESSAGE hook and wants to prevent a message from being processed, the GetMsgProc callback function can change the message number to WM_NULL so the recipient will ignore it.
The corrected code should therefore look something like this:
extern "C" __declspec(dllexport) LRESULT __stdcall meconnect(int code, WPARAM wParam, LPARAM lParam) {
if (code >= 0) {
LPMSG msg = (LPMSG)lParam;
if (msg->message == WM_LBUTTONDOWN) {
OutputDebugString(L"Mouse down event happened \n");
msg->message = WM_NULL;
return false;
}
}
return(CallNextHookEx(NULL, code, wParam, lParam));
}
However, this may cause inconsistent behaviour if other hooks are present, because whether another hook sees WM_LBUTTONDOWN or WM_NULL will depend on the order of the hook chain, which is unpredictable. It might be preferable to try something like this:
extern "C" __declspec(dllexport) LRESULT __stdcall meconnect(int code, WPARAM wParam, LPARAM lParam) {
if (code >= 0) {
LPMSG msg = (LPMSG)lParam;
int result = CallNextHookEx(NULL, code, wParam, lParam);
if (msg->message == WM_LBUTTONDOWN) {
OutputDebugString(L"Mouse down event happened \n");
msg->message = WM_NULL;
}
return result;
}
return(CallNextHookEx(NULL, code, wParam, lParam));
}

Low level mouse hook - mouse button click vs hold

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.

XInput 360 controller not working with cocos2d-x

Ello All,
I'm pretty new to c++ and I've been trying to get this to work for longer than I care to admit. So I've gone off of the following refs and gotten the controller to work in a console app.
xbox360 controller input with c using x input
Code proj article
MSDN
Here is the result
xbox360Controller.h
xbox360Controller.cpp
From there I'm trying to get it to work with cocos2d-x using steve tranby's post at the bottom of this thread (he adds to the )and adapting that to the 360 gamepad.
While I've gotten the keypad events to work
(it was standard windows input so not too bad)
LRESULT CCEGLView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
BOOL bProcessed = FALSE;
CCLog("Message sent = %d",message);
//note* only showing relavant sections of code for brevity
switch (message)
{
case WM_KEYDOWN:
if (wParam == VK_F1 || wParam == VK_F2)
{
CCDirector* pDirector = CCDirector::sharedDirector();
if (GetKeyState(VK_LSHIFT) getKeypadDispatcher()->dispatchKeypadMSG(wParam == VK_F1 ? kTypeBackClicked : kTypeMenuClicked);
}
}
else if (wParam == VK_ESCAPE)
{
CCDirector::sharedDirector()->getKeypadDispatcher()->dispatchKeypadMSG(kTypeBackClicked);
}
else
{
CCDirector::sharedDirector()->getKeypadDispatcher()->dispatchKeypadDown(wParam);
}
if ( m_lpfnAccelerometerKeyHook!=NULL )
{
(*m_lpfnAccelerometerKeyHook)( message,wParam,lParam );
}
break;
default:
if (m_wndproc)
{
m_wndproc(message, wParam, lParam, &bProcessed);
if (bProcessed) break;
}
return DefWindowProc(m_hWnd, message, wParam, lParam);
}
if (m_wndproc && !bProcessed)
{
m_wndproc(message, wParam, lParam, &bProcessed);
}
return 0;
}
I can't figure out where to put the controller logic. I've tried
in the WindowProc method and realized that didnt work as it only
fires as a callback to WindowProc events(I probably don't have the correct lingo for it, sorry)
The closest I've gotten it to something that fires often enough to check is at the point
static LRESULT CALLBACK _WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
CCDirector* pDirector = CCDirector::sharedDirector();
XboxController* player1 = new XboxController(GamePadIndex_One);
if(player1->IsConnected())
{
player1->Update();
for(int i =0;iState._buttons[i]==true)
{
//CCApplication::sharedApplication()->getKe
pDirector->getKeypadDispatcher()->dispatchKeypadDown(i);
}
}
}
delete player1;
if (s_pMainWindow && s_pMainWindow->getHWnd() == hWnd)
{
return s_pMainWindow->WindowProc(uMsg, wParam, lParam);
}
else
{
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
}
The full code is here
and the source is at github Here.
Anyone know the right place to put the XboxController instance in order for it to respond to KeypadDown properly?
First of, WindowProc is a callback function that is called after there's an message sent to the window from Windows.
So, if there's no message, this function would not get called.
XInput api is not message-based api, it does not generate messages. It requires the app to read its state as frequent as possible. Usually it's read in every game loop, just before the game logic is processed. Alternatively, you could have a separated thread to poll the state every 33ms or so.
I'd to recommend you to take a look at how XInput work, the concept of Windows programming, and the basic concept of game engine architecture.

SetFocus() fails with a valid window handle

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);

Safely remove window subclassing?

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.