Code:
#include <windows.h>
#include <stdio.h>
#define CONTROL_ID1 41
#define CONTROL_ID2 42
LRESULT CALLBACK WinProc(HWND, UINT, WPARAM, LPARAM);
int main()
{
const char *className = "WindowsApp";
WNDCLASSA wincl = {0};
wincl.hInstance = GetModuleHandle(0);
wincl.lpszClassName = className;
wincl.lpfnWndProc = WinProc;
wincl.style = CS_DBLCLKS;
wincl.hIcon = LoadIcon(0, IDI_APPLICATION);
wincl.hCursor = LoadCursor(0, IDC_ARROW);
wincl.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
if (!RegisterClassA(&wincl))
return 1;
HWND hwndParent;
hwndParent = CreateWindowA(className, "Windows App",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT,
200, 110, HWND_DESKTOP, 0,
GetModuleHandle(0), 0);
if (!hwndParent)
return 1;
HWND hwndChild1;
hwndChild1 = CreateWindowExA(WS_EX_CLIENTEDGE, "Edit", "Text1",
WS_CHILD | WS_VISIBLE | WS_TABSTOP,
10, 10, 170, 20,
hwndParent, (HMENU)CONTROL_ID1,
GetModuleHandle(0), 0);
if (!hwndChild1)
return 1;
HWND hwndChild2;
hwndChild2 = CreateWindowExA(WS_EX_CLIENTEDGE, "Edit", "Text2",
WS_CHILD | WS_VISIBLE | WS_TABSTOP,
10, 45, 170, 20,
hwndParent, (HMENU)CONTROL_ID2,
GetModuleHandle(0), 0);
if (!hwndChild2)
return 1;
MSG msg;
BOOL bRet;
for (;;)
{
bRet = GetMessageA(&msg, hwndParent, 0, 0);
if ((bRet == 0) || (bRet == -1))
break;
if (!IsWindow(hwndParent) || !IsDialogMessage(hwndParent, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}//if
}//while
return (int)msg.wParam;
}//main
LRESULT CALLBACK WinProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam)
{
static HWND s_lastFocus = 0;
switch(msg)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}//case
case WM_SYSCOMMAND:
{
if (wParam == SC_MINIMIZE)
{
// capture the focus before minimizing - must do this here
// instead of in WA_INACTIVE when minimizing
HWND prev = s_lastFocus;
s_lastFocus = GetFocus();
printf("Minimize: %p -> %p\n", prev, s_lastFocus);
}//if
break;
}//case
case WM_ACTIVATE:
{
if (LOWORD(wParam) == WA_INACTIVE)
{
// do not capture who has focus on minimize, the SC_MINIMIZE
// handler will do that
const bool bIsMinimizing = HIWORD(wParam) != 0;
if (!bIsMinimizing)
{
HWND prev = s_lastFocus;
s_lastFocus = GetFocus();
printf("Inactive: %p -> %p\n", prev, s_lastFocus);
}//if
else
{
printf("Inactive: Ignoring minimize\n");
}//else
}//if
else if (s_lastFocus)
{
printf("Restore: %p\n", s_lastFocus);
SetFocus(s_lastFocus);
// don't let DefWindowProc() handle it, or it will change focus
return 0;
}//else if
else
{
printf("Activate: wParam = %u | %u\n",
LOWORD(wParam), HIWORD(wParam));
}//else
}//case
}//switch
return DefWindowProcA(hwnd, msg, wParam, lParam);
}//WinProc