Cały program:
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
// Register the window class.
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
// Create the window.
HWND hwnd = CreateWindowEx(
0, // Optional window styles.
CLASS_NAME, // Window class
L"Learn to Program Windows", // Window text
WS_OVERLAPPEDWINDOW, // Window style
// Size and position
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
if (hwnd == NULL)
{
return 0;
}
ShowWindow(hwnd, nCmdShow);
// Run the message loop.
MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_LBUTTONDOWN:
int xPos,yPos;
xPos = GET_X_LPARAM(lParam);
yPos = GET_Y_LPARAM(lParam);
MessageBox(0,L"You've pressed the left mouse button",L"Conrgatulations!",0);
break;
case WM_DESTROY:
PostQuitMessage(0);
MessageBox(0,L"GoodBye!",L"Bye",0);
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));
EndPaint(hwnd, &ps);
}
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
Chodzi mi głównie o tego MessageBox'a
MessageBox(0,L"You've pressed the left mouse button",L"Conrgatulations!",0);
Co zrobić, żeby w treści komunikatu pokazywane były współrzędne punktu okienka w które kliknął użytkownik? Na razie debugger mówi:
1>c:\users\maciek\documents\visual studio 2010\projects\win32c++roughdraft\win32c++roughdraft\winmain.cpp(64): error C3861: 'GET_X_LPARAM': identifier not found
1>c:\users\maciek\documents\visual studio 2010\projects\win32c++roughdraft\win32c++roughdraft\winmain.cpp(65): error C3861: 'GET_Y_LPARAM': identifier not found
@Edit
W dokumentacji dopatrzyłem się, że makra GET_X_LPARAM oraz GET_Y_LPARAM wchodzą w skład biblioteki windowsX.h . Teraz procedura kliknięcia wygląda tak:
case WM_LBUTTONDOWN:
int xPos,yPos;
xPos =LOWORD(lParam);
yPos = HIWORD(lParam);
char buffer[150];
sprintf(buffer, "You've pressed the left mouse button at the : x=%d , y=%d" , xPos , yPos);
MessageBoxW(0,(LPCWSTR)buffer,L"Congratulations!",0);
break;
Po skompilowaniu i kliknięciu w interaktywne pole utworzonego okienka pojawia się wiadomość , która w nagłówku posiada "Congratulations" a w treści jakieś chińskie znaki :(
Tylko nie mówcie żeby zamienić na funkcję MessageBoxA. Chyba nie muszę tłumaczyć dlaczego ta metoda jest mega-lamerska ;)