#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); } |