帮忙做一个C++小程序,用GCC内核的编译器:

2025-01-24 05:31:46
推荐回答(1个)
回答1:

#include
#include
int X = 320;
int Y = 240;
HBRUSH hBG = 0, hFG = 0;

LRESULT CALLBACK MsgProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam) // second message parameter
{
HDC hDC = 0; int i = 0; RECT r = {0, 0, 0, 0}, p ={0, 0, 0, 0}; int width = 0;
PAINTSTRUCT ps;
switch(uMsg){
case WM_KEYDOWN:
switch ((unsigned long)(wParam)) {
case VK_UP: Y -= 5; if(Y < 0) Y = 0; break;
case VK_DOWN: Y += 5; if(Y > 480) Y = 480; break;
case VK_LEFT: X -= 5; if(X < 0) X = 0; break;
case VK_RIGHT: X += 5; if(X > 640) X = 0; break;
}//end case
InvalidateRect(hwnd, NULL, TRUE);
return TRUE;
case WM_PAINT:
hDC=BeginPaint(hwnd,&ps);
GetClientRect(hwnd, &r);
FillRect(hDC, &r, hBG);
p.left = X - 5; p.right = X + 5; p.top = Y - 5; p.bottom = Y + 5;
FillRect(hDC, &p, hFG);
EndPaint(hwnd,&ps);
return TRUE;
case WM_CLOSE:
exit(0);
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}//end case
return 0;
}//end MsgProc

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
WNDCLASS wc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wc.hCursor=LoadCursor(NULL,IDC_CROSS);
wc.hIcon=LoadIcon(NULL,IDI_ERROR);
wc.hInstance=0;
wc.lpfnWndProc=MsgProc;
wc.lpszClassName="POINT Demo";
wc.lpszMenuName=NULL;
wc.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wc);

hBG = CreateSolidBrush(0xFFFFFF);
hFG = CreateSolidBrush(0xFF00);

HWND hwnd;
hwnd=CreateWindow("POINT Demo","Enoch WILLS 2010",WS_OVERLAPPEDWINDOW,
0,0,640,480,0,0,0,0);

ShowWindow(hwnd,SW_SHOW);
UpdateWindow(hwnd);

MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}//end while
return 0;
}//end main