用按键来控制页面的:
case WM_KEYDOWN:
注意,数字和字母的虚拟键码是ASCII码。Windows程序几乎从不使用这些虚拟键码
wParam消息参数含有上面所讨论的虚拟键码
WM_PAINT: 重绘消息 当窗口状态发生变化的时候都会被触发
1.获得HDC BeginPaint(窗口句柄,定义结构体)
2绘画
3.交还HDC endpaint(窗口句柄,结构体地址)
其他要重绘消息时
1.获得HDC GetDC(窗口句柄)
2.绘画
3.ReleaseDC(窗口句柄 ,hdc)
绘画时,可以购买画刷和使用画刷
购买画刷:CreateSolidBrush(RGB(0,255,0));
使用画刷: SelectObject(hdc,brush);
#include <Windows.h>
#include <stdio.h>
LRESULT CALLBACK My_WNDPROC(HWND hwnd, UINT message, WPARAM wPAREAM, LPARAM lPARAM);
int CALLBACK WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
WNDCLASSEX ex;
HWND hwnd;
MSG msg;
ex.style = CS_HREDRAW | CS_VREDRAW;
ex.cbSize = sizeof(ex);
ex.cbClsExtra = 0;
ex.cbWndExtra = 0;
ex.hInstance = hInstance;
ex.hIcon = NULL;
ex.hCursor = NULL;
ex.hbrBackground = CreateSolidBrush(RGB(0,128,128));
ex.hIconSm = NULL;
ex.lpfnWndProc = &My_WNDPROC;
ex.lpszMenuName = NULL;
ex.lpszClassName = "111";
RegisterClassEx(&ex);
hwnd = CreateWindow(ex.lpszClassName,"原谅他",WS_OVERLAPPEDWINDOW,50,50,500,500,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOW);
/*SetTimer(hwnd,1,1000,NULL);*/
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK My_WNDPROC(HWND hwnd, UINT message, WPARAM wPAREAM, LPARAM lPARAM)
{
int x;
int y;
char str[10] = {0};
HDC hdc;
PAINTSTRUCT ps;
HBRUSH brush;
switch (message)
{
case WM_CLOSE:
PostQuitMessage(0);
break;
case WM_KEYDOWN:
/*if(wPAREAM == 65)
{
MessageBox(hwnd,"想要生活过得去,不怕身上背的绿","忍者神龟",MB_ABORTRETRYIGNORE );
}*/
hdc = GetDC(hwnd);
brush = CreateSolidBrush(RGB(255,0,0));
SelectObject(hdc,brush);
Ellipse( hdc,130,130,270,270);
ReleaseDC(hwnd,hdc);
break;
case WM_LBUTTONDOWN:
x = LOWORD (lPARAM);
y = HIWORD (lPARAM) ;
/* MessageBox(hwnd,"想要生活过得去,不怕身上背的绿","忍者神龟",MB_ABORTRETRYIGNORE );*/
/*sprintf(str,"%d,%d",x,y);
MessageBox(hwnd,str,"忍者神龟",MB_ABORTRETRYIGNORE);*/
hdc = GetDC(hwnd);
Ellipse( hdc,x-25,y-25,x+25,y+25);
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
hdc = BeginPaint(hwnd,&ps);
Rectangle(hdc,100,100,300,300);
EndPaint(hwnd,&ps);
break;
case WM_TIMER:
MessageBox(hwnd,"你是*","你真聪明",MB_OK );
break;
}
return DefWindowProc(hwnd,message,wPAREAM,lPARAM);
}