how to move a ball in window using arrow keys ? I wrote this code. I got the output but, ball was no

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
#include <windows.h>
#include <iostream>


int x=160;
int y=155;



LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam );
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow );


int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR szCmdLine,
int iCmdShow )
{


#pragma region part 1 - STARTUP STUFF

WNDCLASS wc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject( OEM_FIXED_FONT );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = TEXT("ballmoving");

wc.lpszMenuName = 0;
wc.style = CS_HREDRAW | CS_VREDRAW;

RegisterClass( &wc );



HWND hwnd = CreateWindow(TEXT("ballmoving"), TEXT("windows title!"), WS_OVERLAPPEDWINDOW, 10, 10, 600, 600, NULL, NULL, hInstance, NULL );


ShowWindow(hwnd, iCmdShow );
<span style="white-space:pre UpdateWindow(hwnd);
#pragma endregion

#pragma region part 2 - ENTER A LOOP TO CONTINUALLY KEEP CHECKING WITH WIN O/S FOR USER INTERACTION

MSG msg;

while( GetMessage( &msg, NULL, 0, 0 ) )
{

TranslateMessage( &msg );
DispatchMessage( &msg );


}
#pragma endregion

return msg.wParam;
}

LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam )
{


switch( message )
{
case WM_CREATE:

Beep( 50, 10 );
return 0;
break;


case WM_PAINT:
{

HDC hdc;
PAINTSTRUCT ps;
<span style="white-space:pre HBRUSH NewBrush;
hdc = BeginPaint( hwnd, &ps );
<span style="white-space:pre

<span style="white-space:pre NewBrush = CreateSolidBrush(RGB(250, 25, 5));
<span style="white-space:pre case WM_KEYDOWN:
<span style="white-space:pre
<span style="white-space:pre switch(wparam)
<span style="white-space:pre {
<span style="white-space:pre case VK_UP:
<span style="white-space:pre y--;
<span style="white-space:pre break;
<span style="white-space:pre case VK_DOWN:
<span style="white-space:pre y++;
<span style="white-space:pre break;
<span style="white-space:pre case VK_LEFT:
<span style="white-space:pre x--;
<span style="white-space:pre break;
<span style="white-space:pre case VK_RIGHT:
<span style="white-space:pre x++;
<span style="white-space:pre break;
<span style="white-space:pre }


SelectObject(hdc, NewBrush);
Ellipse( hdc, x, y, x+30, y+30 );

}
return 0;
break;

case WM_DESTROY:
PostQuitMessage( 0 ) ;
return 0;
break;
<span style="white-space:pre


}



return DefWindowProc( hwnd, message, wparam, lparam );
}
<hr class="sig Navi

View the full article
 
Back
Top