Responding to user input | WM_KEYDOWN | Virtual Key
Following Window Procedure draws a specific pattern using WM_PAINT message (executes correctly). Now when user hit the UP arrow key, I want this pattern to move from position x=500, y=250 (declared as global variables) to position x=500, y=y+5.
I tried to implement this using WM_KEYDOWN message, but with no luck. Any help would be highly appreciated...
Code:
LRESULT CALLBACK MainWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_CLOSE:
if(MessageBox(hwnd, "Really quit?", "My application", MB_OKCANCEL)==IDOK)
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_PAINT:
DrawPixels(hwnd);
break;
case WM_KEYDOWN:
{
switch(wParam)
{
case VK_UP:
y = y + 5;
break;
default:
break;
}
}
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
Code for DrawPixels function is also enclosed for reference purpose:
Code:
void DrawPixels(HWND hwnd)
{
PAINTSTRUCT ps;
RECT r;
GetClientRect(hwnd, &r);
HDC hdc = BeginPaint(hwnd, &ps);
SetPixel(hdc, -5 + x, y, RGB(0,0,0));
SetPixel(hdc, -4 + x, y, RGB(0,0,0));
SetPixel(hdc, -3 + x, y, RGB(0,0,0));
SetPixel(hdc, 3 + x, y, RGB(0,0,0));
SetPixel(hdc, 4 + x, y, RGB(0,0,0));
SetPixel(hdc, 5 + x, y, RGB(0,0,0));
SetPixel(hdc, x, -5 + y, RGB(0,0,0));
SetPixel(hdc, x, -4 + y, RGB(0,0,0));
SetPixel(hdc, x, -3 + y, RGB(0,0,0));
SetPixel(hdc, x, 3 + y, RGB(0,0,0));
SetPixel(hdc, x, 4 + y, RGB(0,0,0));
SetPixel(hdc, x, 5 + y, RGB(0,0,0));
EndPaint(hwnd, &ps);
}