#include <windows.h> #include "resource.h"
//Globals
//Proc LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
//******************************************************************* // WinMain //*******************************************************************
HINSTANCE pInstance;
int WINAPI WinMain (HINSTANCE hInstance,HINSTANCE hPrevInstance, PSTR szCmdLine,int iCmdShow) { static char szAppName[]="AppName"; HWND hwnd; MSG msg;
WNDCLASSEX wndclass; wndclass.cbSize =sizeof(wndclass); wndclass.style =CS_HREDRAW|CS_VREDRAW; wndclass.lpfnWndProc =WndProc; wndclass.cbClsExtra =0; wndclass.cbWndExtra =0; wndclass.hInstance=hInstance; wndclass.hIcon =LoadIcon(NULL,IDI_APPLICATION); wndclass.hCursor =LoadCursor(NULL,IDC_ARROW); wndclass.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName=MAKEINTRESOURCE(IDR_MENU1); wndclass.lpszClassName =szAppName; wndclass.hIconSm=LoadIcon(NULL,IDI_APPLICATION);
RegisterClassEx(&wndclass);
hwnd=CreateWindow(szAppName, "窗口标题", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd,iCmdShow); UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } //******************************************************************* // 窗口过程 //******************************************************************* LRESULT CALLBACK WndProc (HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam) { HDC hdc; PAINTSTRUCT ps;
static HDC memDC; static HBITMAP hBitmap; HBRUSH hBrush; static int maxX,maxY; int response;
switch(iMsg) {
case WM_CREATE:{
[COLOR=green] maxX=GetSystemMetrics(SM_CXSCREEN); maxY=GetSystemMetrics(SM_CYSCREEN); hdc = GetDC (hwnd); //得到当前的设备描述表 memDC=CreateCompatibleDC(hdc);//得到兼容的设备描述表 hBitmap=CreateCompatibleBitmap(hdc,maxX,maxY);//创建兼容位图 SelectObject(memDC,hBitmap);//将位图选入内存设备描述表 hBrush=(HBRUSH)GetStockObject(WHITE_BRUSH);//得到白色画刷 SelectObject(memDC,hBrush);//将画刷选入内存设备描述表 PatBlt(memDC,0,0,maxX,maxY,PATCOPY);//用当前画刷填充 ReleaseDC(hwnd,hdc);//释放当前设备描述表[/COLOR] break; }
case WM_PAINT: hdc=BeginPaint(hwnd,&ps); [COLOR=green]BitBlt(hdc,ps.rcPaint.left,ps.rcPaint.top,ps.rcPaint.right-ps.rcPaint.left,ps.rcPaint.bottom-ps.rcPaint.top, memDC,ps.rcPaint.left,ps.rcPaint.top,SRCCOPY);//对需要重画的区域进行重画 //将memDC总得内容复制到hdc中去 */[/COLOR] EndPaint(hwnd,&ps); return 0; case WM_COMMAND: switch(LOWORD (wParam)) { case IDM_DRAW: hdc=GetDC(hwnd); int x,y,width,height; int red,green,blue; width=GetSystemMetrics(SM_CXFULLSCREEN); height=GetSystemMetrics(SM_CYFULLSCREEN);//得到客户区的高和宽 for(x=0;x<width;x++) //画出晚霞~ for(y=0;y<height;y++) { red=x*255/width; green=y*255/height; blue=(x*255/width+(height-y)*255/height)/2; SetPixel(hdc,x,y,RGB(red,green,blue));//输出到物理窗口 [COLOR=green]SetPixel(memDC,x,y,RGB(red,green,blue));//输出到虚拟窗口[/COLOR] } ReleaseDC(hwnd,hdc); break;
case IDM_CLEAR: hdc=GetDC(hwnd); PatBlt(hdc,0,0,maxX,maxY,PATCOPY);//清除物理屏幕 PatBlt(memDC,0,0,maxX,maxY,PATCOPY);//清除虚拟屏幕 ReleaseDC(hwnd,hdc); break;
case ID_EXIT:
response=MessageBox(hwnd,"真的要退出吗?","退出",MB_YESNO); if(response==IDYES) PostQuitMessage(0); break;
} return 0; break;
case WM_DESTROY: DeleteDC(memDC);//释放内存设备描述表 DeleteObject(hBitmap); PostQuitMessage(0); return 0; } return DefWindowProc(hwnd,iMsg,wParam,lParam); }
|