#include <Windows.h>
#include <iostream>
#include <tchar.h>
HINSTANCE g_hInstance;
HANDLE g_hStdOut;
PAINTSTRUCT ps;
HDC hdc;
LRESULT CALLBACK WndProc(HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
static int nX = 0,nY = 0;
RECT rect;
GetClientRect(hWnd,&rect);
switch (uMsg)
{
case WM_KEYDOWN:
{
switch(wParam)
{
case VK_LEFT:
{
if(nX >= (rect.left + 10))
{
nX -= 10;
}
InvalidateRect(hWnd,NULL,TRUE);
}
break;
case VK_UP:
{
if( nY >= (rect.top + 10))
{
nY -= 10;
}
InvalidateRect(hWnd,NULL,TRUE);
}
break;
case VK_RIGHT:
{
if(nX <= (rect.right - 40)) //请帮我找找逻辑上的错误
{
nX += 10;
}
InvalidateRect(hWnd,NULL,TRUE);
}
break;
case VK_DOWN:
{
if(nY <= (rect.bottom - 21)) //请帮我找找逻辑上的错误
{
nY += 10;
}
InvalidateRect(hWnd,NULL,TRUE);
}
break;
}
}
case WM_PAINT:
{
hdc = BeginPaint(hWnd,&ps);
TCHAR szText[] = TEXT("hello");;
TextOut(hdc,nX,nY,szText,lstrlen(szText));
EndPaint(hWnd,&ps);
break;
}
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
void RegisterWnd(LPCTSTR lpClassName)
{
WNDCLASSEX wce;
wce.style = CS_HREDRAW | CS_VREDRAW;
wce.lpfnWndProc = WndProc;
wce.cbSize = sizeof(WNDCLASSEX);
wce.cbClsExtra = 0;
wce.cbWndExtra = 0;
wce.hInstance = g_hInstance;
wce.hIcon = 0;
wce.hCursor = 0;
wce.hIconSm = 0;
wce.hbrBackground = HBRUSH(COLOR_BTNFACE + 1);
wce.lpszClassName = lpClassName;
wce.lpszMenuName = 0;
if (!RegisterClassEx(&wce))
{
MessageBox(NULL, TEXT("注册失败"), TEXT("提示"), MB_OK);
}
}
HWND CreateWnd(LPCTSTR lpClassName)
{
HWND hWnd = CreateWindowEx(0, lpClassName, L"HELLO",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, g_hInstance, NULL);
return hWnd;
}
void DisplayWnd(HWND hWnd)
{
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
}
void Message()
{
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
g_hInstance = hInstance;
RegisterWnd(L"hello");
HWND hWnd = CreateWnd(L"hello");
DisplayWnd(hWnd);
Message();
return 0;
}