//设置映像与使用映像模式实例,本例中的程序运行时,初始阶段按模式MM_TEXT绘图,图形为一个坐标系, //以逻辑坐标系的原点为原点,X,Y轴分别是逻辑坐标系的X,Y轴.当用户按下'A'键,'B'键或'C'键时,产生 //WM_CHAR消息,将映像模式分别设置为ISOTROPIC,ANISOTROPIC或LOMETRIC,同时调用InvalidateRect函数 //刷新用户区。
#include <windows.h> #include <string.h> #include <stdlib.h> #include <stdio.h> LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); int nMode=MM_TEXT;
int WINAPI WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpszCmdLine, INT nCmdShow ) { HWND hwnd; MSG Msg; WNDCLASS wndclass; char lpszClassName[]="Drawing"; char lpszTitle[]="My_Drawing";
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.lpszClassName=lpszClassName; wndclass.lpszMenuName=NULL; wndclass.style=0;
if(!RegisterClass(&wndclass)) { MessageBeep(0); return false; } hwnd=CreateWindow ( lpszClassName, lpszTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL ); ShowWindow(hwnd,nCmdShow); UpdateWindow(hwnd);
while(GetMessage(&Msg,NULL,0,0)) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; HPEN hPen; switch(message) { case WM_CHAR: if(wParam=='a'||wParam=='A') nMode=MM_ISOTROPIC; if(wParam=='b'||wParam=='B') nMode=MM_ANISOTROPIC; if(wParam=='c'||wParam=='C') nMode=MM_LOMETRIC; if(wParam=='s'||wParam=='S') nMode=MM_LOENGLISH; if(wParam=='d'||wParam=='D') nMode=MM_TWIPS; InvalidateRect(hwnd,NULL,1); break; case WM_PAINT: hdc=BeginPaint(hwnd,&ps); SetMapMode(hdc,nMode); SetWindowExtEx(hdc,15,15,NULL); SetViewportExtEx(hdc,15,10,NULL); SetViewportOrgEx(hdc,120,120,NULL); hPen=CreatePen(PS_SOLID,2,RGB(255,0,0)); SelectObject(hdc,hPen); LineTo(hdc,200,0); LineTo(hdc,195,-5); MoveToEx(hdc,200,0,NULL); LineTo(hdc,195,5); MoveToEx(hdc,0,0,NULL); LineTo(hdc,0,200); LineTo(hdc,-5,195); MoveToEx(hdc,0,200,NULL); LineTo(hdc,5,195); DeleteObject(hPen); EndPaint(hwnd,&ps); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd,message,wParam,lParam); break; } return 0; } |