Try this. It uses GetMessagePos() instead of GetCursorPos() and also an invisible window. When there is a left button click in the top 50 pixels you should hear a Beep as the app exits.
EDIT: Added DestroyWindow(hwnd);
John
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <commctrl.h>
#include <tchar.h>
static LRESULT WINAPI MainWndProc(HWND, UINT, WPARAM, LPARAM);
static HANDLE ghInstance;
void GetCursorPosWinCE(POINT *pt)
{
DWORD pos = GetMessagePos();
pt->x = LOWORD(pos);
pt->y = HIWORD(pos);
}
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
WNDCLASS wc;
HWND hwnd;
MSG msg;
ghInstance = hInstance;
/* Register the main window class */
wc.lpszClassName = _T("testerClass");
wc.lpfnWndProc = MainWndProc;
wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
wc.hInstance = ghInstance;
wc.hIcon = LoadIcon(ghInstance, NULL);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
if (!RegisterClass(&wc))
return 1;
/* Create the main window but don't show it */
hwnd = CreateWindow(_T("testerClass"), _T("tester Program"), WS_OVERLAPPEDWINDOW, 0, 0, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, ghInstance, NULL);
if (!hwnd)
return 1;
// DON'T SHOW THE WINDOW
// ShowWindow(hwnd, nCmdShow);
// UpdateWindow(hwnd);
POINT p;
SetTimer(hwnd, 1, 100, NULL);
while (GetMessage(&msg, NULL, 0, 0))
{
// GetCursorPos(&p);
GetCursorPosWinCE(&p);
if (p.x > 0 && p.x < 50 && p.y > 0 && p.y < 50)
{
GetAsyncKeyState(VK_LBUTTON); // for first time, clear buffer
if (GetAsyncKeyState(VK_LBUTTON) & 0x8000)
{
break;
}
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
MessageBeep(16);
KillTimer(hwnd, 1);
DestroyWindow(hwnd);
return msg.wParam;
}
static LRESULT CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
}