Home | API | MFC | C++ | C | Up

Programming Windows API

Non Client Area Mouse Messages Example

//double click anywhere in non-client area to close window
#include <windows.h>
#include <tchar.h>
TCHAR mousestr[255]=TEXT("\0");
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow)
{
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style=CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc=WndProc ;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance ;
wndclass.hIcon=LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor=LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground=(HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName = TEXT("myWindowClass");
RegisterClass (&wndclass);
hwnd = CreateWindow (TEXT("myWindowClass"), TEXT ("Non client area mouse click demo"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT,400, 100,NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 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");
static int linecount=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) ;
}

Home | API | MFC | C++ | C | Up
Creating a Simple Window | Common Elements | Data Types and Character Sets | The Device Context | Graphics Device Interface | Displaying Text | Displaying Graphics | Mapping Modes | Keyboard Input | Working with the Mouse | Menus | Child Windows | ScrollBar Control | The Dialog Box | Windows Message Box | Common Dialog Box | Bitmaps | Common Controls | Creating a Toolbar | Multiple Document Interface | Timers | DLL’s | Creating Custom Controls | Owner Drawn Controls | API Hooking and DLL Injection | File Management Functions | String Manipulation | System Information Functions

Last Updated:26 October 2022