Non Client Area Mouse Messages Example

//non client area mouse clicks
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
MSG msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = TEXT("myWindowClass");
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&wc);
CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("myWindowClass"), TEXT("Non client area mouse clicks"), WS_VISIBLE | WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 700, 200, NULL, NULL, hInstance, NULL);
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static TCHAR msg[40]= TEXT("\0");
HDC hdc;
hdc=GetDC(hwnd);
switch (message)
{
//detect position of non-client area mouse clicks
case WM_NCHITTEST:
{
LRESULT hittest =DefWindowProc (hwnd, message, wParam, lParam);
switch ( hittest)
{
case HTCAPTION:
wsprintf(msg,TEXT("You clicked in the Title Bar "));
break;
case HTCLOSE:
wsprintf(msg,TEXT("You clicked the Close Button "));
break;
case HTREDUCE:
wsprintf(msg,TEXT("You clicked the Minimize button "));
break;
case HTGROWBOX:
wsprintf(msg,TEXT("You clicked the Restore button "));
break;
case HTSYSMENU:
wsprintf(msg,TEXT("You clicked in the System menu "));
break;
case HTZOOM:
wsprintf(msg,TEXT("You clicked the Restore button "));
break;
case HTRIGHT:
wsprintf(msg,TEXT("You clicked the Right border "));
break;
case HTLEFT:
wsprintf(msg,TEXT("You clicked the left border "));
break;
case HTBOTTOM:
wsprintf(msg,TEXT("You clicked the bottom border "));
break;
case HTTOP:
wsprintf(msg,TEXT("You clicked the top border "));
break;
}
break;
}
//respond to non-client area mouse clicks by displaying location of mouse click
case WM_NCLBUTTONDOWN:
{
int strlen=lstrlen(msg);
TextOut(hdc,0,0,msg,strlen);
return 0;
}
break;
//respond to non-client area double click by closing windows
case WM_NCLBUTTONDBLCLK:
PostQuitMessage (0) ;
break;
case WM_DESTROY:
DeleteDC(hdc);
PostQuitMessage (0) ;
return 0;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}