Хочу сделать кнопке собственную процедуру обработки сообщений (WM_LBUTTONDOWN). Подскажите пожалуйста, почему не работает код:
#include <windows.h>
char szTitle[] = "Buttonz";
char szWindowClass[] = "Buttonz window class";
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE hInst;
HWND hMainWnd;
HWND hBtn;
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
MyRegisterClass(hInstance);
if (!InitInstance (hInstance, nCmdShow)) return FALSE;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_BUTTON);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground= (HBRUSH)(COLOR_BTNFACE+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName= szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance;
hMainWnd = CreateWindowEx(WS_EX_CLIENTEDGE | WS_EX_TOPMOST,
szWindowClass, szTitle,
WS_OVERLAPPEDWINDOW | WS_BORDER | WS_CLIPCHILDREN,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hMainWnd) return FALSE;
ShowWindow(hMainWnd, nCmdShow);
UpdateWindow(hMainWnd);
return TRUE;
}
static LRESULT CALLBACK ButtProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC dc;
HBRUSH brush;
RECT rect;
PAINTSTRUCT ps;
switch (message)
{
case WM_ERASEBKGND:
dc = GetDC(hWnd);
brush = (HBRUSH)GetStockObject(BLACK_BRUSH);
brush = (HBRUSH)SelectObject(dc, brush);
GetClientRect(hWnd, &rect);
FillRect(dc, &rect, 0);
SelectObject(dc, brush);
ReleaseDC(hWnd, dc);
return 1;
break;
case WM_PAINT:
dc = BeginPaint(hWnd, &ps);
brush = (HBRUSH)COLOR_BTNFACE+1;
brush = (HBRUSH)SelectObject(dc, brush);
GetClientRect(hWnd, &rect);
FillRect(dc, &rect, 0);
DrawEdge(dc, &rect, EDGE_RAISED, BF_TOPLEFT);
DrawEdge(dc, &rect, EDGE_RAISED, BF_BOTTOMRIGHT);
DrawText(dc, "the", -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
EndPaint(hWnd, &ps);
break;
case WM_LBUTTONDOWN:
MessageBox(NULL, "Pressed", "Button", MB_OK); // !!!!!!!!!! - не работает
break;
}
return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
hBtn = CreateWindowEx(0, "BUTTON", "Ok", WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN,
100, 10, 80, 80,
hWnd, NULL, hInst, NULL);
SetWindowLong(hBtn, GWL_WNDPROC, (LONG)&ButtProc);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}