Code:
#include <windows.h>
#include <string.h>
//**************************************************************************
LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,
WPARAM wParam,LPARAM lParam)
{
HDC dc;
PAINTSTRUCT ps;
RECT rect;
static enum STATE{MOUSEDOWN = 0,MOUSEUP} eState;
char* szString;
static HBRUSH hBrush = CreateSolidBrush(RGB(255,255,255));
switch(uMsg){
case WM_CREATE:
eState = MOUSEUP;
break;
case WM_CLOSE:
PostQuitMessage(0);
break;
case WM_LBUTTONUP:
ReleaseCapture();
eState = MOUSEUP;
InvalidateRect(hWnd,0,TRUE);
UpdateWindow(hWnd);
break;
case WM_LBUTTONDOWN:
SetCapture(hWnd);
eState = MOUSEDOWN;
InvalidateRect(hWnd,0,TRUE);
UpdateWindow(hWnd);
break;
case WM_PAINT:
dc = BeginPaint(hWnd,&ps);
GetClientRect(hWnd,&rect);
szString = (eState == MOUSEDOWN ? "Mouse Button Down" :
"Mouse Button Up");
FillRect(dc,&rect,hBrush);
DrawText(dc,szString,strlen(szString),&rect,
DT_VCENTER | DT_CENTER | DT_SINGLELINE);
EndPaint(hWnd,&ps);
break;
default:
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hInstPrev,
LPSTR szCommand,int nCmdShow)
{
const char* szClass = "Magos";
HBRUSH hBrush = CreateSolidBrush(RGB(255,255,255));
WNDCLASSEX wcx = {0};
wcx.cbSize = sizeof(wcx);
wcx.style = CS_HREDRAW|CS_VREDRAW;
wcx.lpfnWndProc = WndProc;
wcx.hInstance = hInst;
wcx.hIcon = LoadIcon(0,IDI_APPLICATION);
wcx.hCursor = LoadCursor(0,IDC_ARROW);
wcx.hbrBackground = hBrush;
wcx.lpszClassName = szClass;
if (!RegisterClassEx(&wcx))
{
MessageBox(0,"Failed to register wnd class",0,MB_OK);
return 1;
}
HWND hWnd = CreateWindowEx(0,szClass,"Magos",
WS_OVERLAPPEDWINDOW,GetSystemMetrics(SM_CXSCREEN)/4,
GetSystemMetrics(SM_CYSCREEN)/4,GetSystemMetrics(SM_CXSCREEN)/2,
GetSystemMetrics(SM_CYSCREEN)/2,0,0,hInst,0);
if (!hWnd)
{
MessageBox(0,"Window creation failed",0,MB_OK);
return 1;
}
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
MSG msg;
while(GetMessage(&msg,0,0,0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}