PAINT和WM_ERASEBKGND消息

来源:互联网 发布:淘宝商城源码 编辑:程序博客网 时间:2024/05/19 18:39
#include<windows.h>#include<stdio.h>LRESULT CALLBACK WinProc(// WinProc这个名字可以随便改的WindowProc HWND hwnd,      // handle to window UINT uMsg,      // message identifier WPARAM wParam,  // first message parameter LPARAM lParam   // second message parameter );int WINAPI WinMain(   HINSTANCE hInstance,  // handle to current instance   HINSTANCE hPrevInstance,  // handle to previous instance   LPSTR lpCmdLine,      // pointer to command line   int nCmdShow          // show state of window   ){//1.注册窗口类WNDCLASS wc;wc.cbClsExtra=0;wc.cbWndExtra=0;//wc.hbrBackground=::CreateSolidBrush(0X000000ff);//也可以这样写wc.hbrBackground=::CreateSolidBrush(RGB(255,0,0));//可以用CreateSolidBrush也可以用GetStockObjectwc.hbrBackground=(HBRUSH)::GetStockObject(WHITE_BRUSH);//里面的颜色是系统默认的一些值,BLACK_BRUSH也是其中一个wc.hCursor=::LoadCursor(NULL,IDC_ARROW);// 这里不可以用HINSTANCE hInstance, 主要是我们现在还没有做,所以就没有wc.hIcon=::LoadIcon(NULL,IDI_HAND) ;wc.hInstance= hInstance;wc.lpfnWndProc=WinProc;//指向命令行的指针wc.lpszClassName = "wuciqiu2015";wc.lpszMenuName = NULL;//菜单名为空wc.style = CS_HREDRAW | CS_VREDRAW |CS_DBLCLKS;//窗口在水平方向上发生变化时会发生同会ATOM wu= ::RegisterClass(&wc);if(0 == wu)//失败了{return 0;}//2.创建窗口类HWND hWnd = ::CreateWindow(wc.lpszClassName,"wuciqiu first win",WS_OVERLAPPEDWINDOW,10,10,800,600,NULL,NULL,hInstance,NULL);//会发送一个WM_CREATE消息到window procedureif(0 == hWnd) return 0;//3.显示窗口::ShowWindow(hWnd,SW_SHOW);//4.更新窗口::UpdateWindow(hWnd);//5.消息循环MSG msg;//这里的msg不要付初值,因为就是通过getmessage来获取信息的while(BOOL bok = ::GetMessage(&msg,NULL,0,0)){::TranslateMessage(&msg);//WM_CHAR::DispatchMessage(&msg);}//MSG msg;//return msg.wParam;return 0;}LRESULT CALLBACK WinProc(// WinProc这个名字可以随便改的WindowProc HWND hWnd,      // handle to window UINT uMsg,      // message identifier WPARAM wParam,  // first message parameter LPARAM lParam   // second message parameter ){ switch(uMsg){case WM_CLOSE:{::DestroyWindow(hWnd);// 它运行完将发送WM_DESTROY信息//::SendMessage(hWnd,WM_QUIT,0,0);//思路是对的,要向信息队列里面发送信息,但是不能这样发对于WM_QUIT// ::PostQuitMessage(0);//而是用它来退出,也可以用case WM_DESTROY:来完成退出}break;case WM_DESTROY:{::PostQuitMessage(0);}break;case WM_CHAR:{char szMsg[10]={0};sprintf(szMsg,"%c",wParam);//字符串格式化HDC hdc = GetDC(hWnd);//写字绘图的三步曲::TextOut(hdc,10,10,szMsg,strlen(szMsg));::ReleaseDC(hWnd,hdc);} break;case WM_RBUTTONDOWN:{  HDC hdc;hdc = ::GetDC(hWnd);::TextOut(hdc,LOWORD(lParam),HIWORD(lParam),"hello",5);::ReleaseDC(hWnd,hdc);}break;case WM_PAINT://永恒绘图{  HDC hdc;PAINTSTRUCT ps;hdc = ::BeginPaint(hWnd,&ps);//WM_PAINT的hdc要用Bedinpaint::TextOut(hdc,LOWORD(lParam),HIWORD(lParam),"hello world",11);::EndPaint(hWnd,&ps);}break;case WM_KEYDOWN:{char szMsg[10]={0};sprintf(szMsg,"%d",wParam);//字符串格式化HDC hdc = GetDC(hWnd);//写字绘图的三步曲::TextOut(hdc,10,50,szMsg,strlen(szMsg));::ReleaseDC(hWnd,hdc);}break;case WM_ERASEBKGND:{HDC hdc=::GetDC(hWnd);//RECT rt={5,5,100,100};这样刷只能刷一部分::GetClientRect(hWnd,&rt);//全部的HBRUSH hbr= ::CreateSolidBrush(0X0000ff);::FillRect((HDC)wParam,&rt,hbr);::ReleaseDC(hWnd,hdc);}break;default:return ::DefWindowProc(hWnd,uMsg,wParam,lParam);}return 0;}

0 0
原创粉丝点击