- 1 year ago
How to prevent window from becoming disabled when losing focus
By default, a window can become disabled (grayed out and unresponsive) when it loses focus. This behavior can be prevented by handling the WM_ACTIVATEAPP
message in the window's message loop.
Here's an example in C++:
c++LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_ACTIVATEAPP:
// wParam contains the activation state
if (wParam == TRUE)
{
// The window is being activated, so enable it
EnableWindow(hwnd, TRUE);
}
else
{
// The window is being deactivated, so disable it
EnableWindow(hwnd, FALSE);
}
break;
// Other cases...
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
In this example, we're handling the WM_ACTIVATEAPP
message, which is sent to a window when it becomes activated or deactivated. We're checking the value of the wParam
parameter to determine whether the window is being activated or deactivated. If the window is being activated (wParam
is TRUE
), we're enabling the window using the EnableWindow()
function. If the window is being deactivated (wParam
is FALSE
), we're disabling the window.
By handling the WM_ACTIVATEAPP
message in this way, you can prevent the window from becoming disabled when it loses focus.