#include<windows.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain
(
HINSTANCE hInstance,
HINSTANCE hPrevInst,
LPSTR lpszCmdLine,
int nCmdShow
)
{
HWND hwnd;
MSG Msg;
WNDCLASS wndclass;
char lpszClassName[]="映象模式";
char lpszTitle[]="My_Map_Mode";
wndclass.style=0;
wndclass.lpfnWndProc=WndProc;
wndclass.cbClsExtra=0;
wndclass.cbClsExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground=(HBRUSH)(GetStockObject(WHITE_BRUSH));
wndclass.lpszClassName=NULL;
wndclass.lpszClassName=lpszClassName;
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;
HBRUSH hB1,hB2;
int nMode; //定义映象模式变量nMode
switch(message)
{
case WM_CREATE: //窗口创建时设置映象模式为MM_TEXT
nMode=MM_TEXT;
case WM_LBUTTONDOWN: //单击鼠标左键更改映象模式为MM_ISOTROPIC
nMode=MM_ISOTROPIC;
InvalidateRect(hwnd,NULL,1);//刷新用户区
break;
case WM_RBUTTONDOWN://单击鼠标右键更改显示模式为MM_ANISOTROPIC
nMode=MM_ANISOTROPIC;
InvalidateRect(hwnd,NULL,1);//刷新用户区
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);//取得设备环境句柄
SetMapMode(hdc,nMode);//设置映象模式
SetWindowExtEx(hdc,150,150,NULL);//设置窗口区域
SetViewportExtEx(hdc,150,100,NULL);//设置视口原点
hB1=(HBRUSH)(GetStockObject(WHITE_BRUSH));//定义白色画刷
hB2=(HBRUSH)(GetStockObject(BLACK_BRUSH));//定义黑色画刷
SelectObject(hdc,hB1);//将黑白画刷选入设备环境
RoundRect(hdc,0,0,150,150,30,30);//使用当前画笔绘制一个带圆角的矩形,并用当前画刷填充
SelectObject(hdc,hB2);//更改当前画刷为黑色画刷
Ellipse(hdc,0,10,150,140);//使用当前画笔绘制椭圆
//并用当前画刷填充
EndPaint(hwnd,&ps);//释放设备环境句柄
break;
case WM_DESTROY:
DeleteObject(hB1); //退出窗口时删除画刷
DeleteObject(hB2);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
return 0;
}