Non-Client Area Mouse Click Example
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
const wchar_t CLASS_NAME[] = L"MyWindowClass";
WNDCLASSEXW wc = {0};
MSG msg;
// Register the window class
wc.cbSize = sizeof(WNDCLASSEXW);
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
wc.lpszClassName = CLASS_NAME;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassExW(&wc))
return 0;
HWND hwnd = CreateWindowExW(
WS_EX_CLIENTEDGE,
CLASS_NAME,
L"Non-Client Area Mouse Clicks",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT,
CW_USEDEFAULT,
700,
200,
NULL,
NULL,
hInstance,
NULL);
if (!hwnd)
return 0;
// Message loop
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return static_cast<int>(msg.wParam);
}
LRESULT CALLBACK WndProc(HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
static wchar_t text[80] = L"Click in the non-client area.";
switch (message)
{
case WM_NCLBUTTONDOWN:
{
switch (wParam)
{
case HTCAPTION:
wsprintfW(text, L"You clicked the title bar.");
break;
case HTCLOSE:
wsprintfW(text, L"You clicked the Close button.");
break;
case HTMINBUTTON:
wsprintfW(text, L"You clicked the Minimize button.");
break;
case HTMAXBUTTON:
wsprintfW(text, L"You clicked the Maximize button.");
break;
case HTSYSMENU:
wsprintfW(text, L"You clicked the System menu.");
break;
case HTLEFT:
wsprintfW(text, L"You clicked the left border.");
break;
case HTRIGHT:
wsprintfW(text, L"You clicked the right border.");
break;
case HTTOP:
wsprintfW(text, L"You clicked the top border.");
break;
case HTBOTTOM:
wsprintfW(text, L"You clicked the bottom border.");
break;
case HTTOPLEFT:
wsprintfW(text, L"You clicked the top-left corner.");
break;
case HTTOPRIGHT:
wsprintfW(text, L"You clicked the top-right corner.");
break;
case HTBOTTOMLEFT:
wsprintfW(text, L"You clicked the bottom-left corner.");
break;
case HTBOTTOMRIGHT:
wsprintfW(text, L"You clicked the bottom-right corner.");
break;
default:
wsprintfW(text, L"You clicked another non-client area.");
break;
}
InvalidateRect(hwnd, NULL, TRUE);
return 0;
}
case WM_NCLBUTTONDBLCLK:
DestroyWindow(hwnd);
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
TextOutW(hdc,
10,
10,
text,
lstrlenW(text));
EndPaint(hwnd, &ps);
return 0;
}
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProcW(hwnd,
message,
wParam,
lParam);
}