LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
static int cxClient, cyClient;
static int iMapMode;
switch (message)
{
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect);
//视口和窗口
//逻辑坐标系统 ->窗口(程序员编码的)
//设备坐标系统 -> 视口(眼睛看到的) //映射模式决定逻辑坐标的单位
//设备坐标的单位是像素 //在窗口下编程,在视口下显示 MoveToEx(hdc, cxClient / , , NULL);
LineTo(hdc, cxClient / , cyClient);
MoveToEx(hdc, , cyClient / , NULL);
LineTo(hdc, cxClient, cyClient / );
//获取旧的映射模式
iMapMode = GetMapMode(hdc);
//设置新的 x向右 y向上 单位 0.1mm
SetMapMode(hdc, MM_LOMETRIC);
//设置视口
SetViewportOrgEx(hdc, cxClient / , cyClient / , NULL); //新的视口原点x,新的视口原点y,以及一个point指针用于存放原来视口位置(0,0) TextOut(hdc, , , L"ViewPort", ); EndPaint(hwnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage();
return ;
} return DefWindowProc(hwnd, message, wParam, lParam);
}
正弦函数练习
#define NUM 1000
#define PI 3.1415926 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
static int cxClient, cyClient;
static int iMapMode;
POINT apt[];
switch (message)
{
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect);
//视口和窗口
//逻辑坐标系统 ->窗口(程序员编码的)
//设备坐标系统 -> 视口(眼睛看到的) //映射模式决定逻辑坐标的单位
//设备坐标的单位是像素 //在窗口下编程,在视口下显示 MoveToEx(hdc, , cyClient/, NULL);
LineTo(hdc, cxClient, cyClient/);
MoveToEx(hdc, , , NULL);
LineTo(hdc, , cyClient);
//获取旧的映射模式
iMapMode = GetMapMode(hdc);
//设置视口
SetViewportOrgEx(hdc, , cyClient / , NULL); //新的视口原点x,新的视口原点y,以及一个point指针用于存放原来视口位置(0,0) for (int i = ; i < NUM;i++)
{
apt[i].x = cxClient / NUM*i; //单位不一致,需要测试
apt[i].y = -(int)( * sin( * PI*i / NUM));
} Polyline(hdc, apt, ); EndPaint(hwnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage();
return ;
} return DefWindowProc(hwnd, message, wParam, lParam);
}
坐标转换
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
static int cxClient, cyClient;
static int iMapMode;
POINT apt[];
switch (message)
{
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect);
//视口和窗口
//逻辑坐标系统 ->窗口(程序员编码的)
//设备坐标系统 -> 视口(眼睛看到的) //映射模式决定逻辑坐标的单位
//设备坐标的单位是像素 //在窗口下编程,在视口下显示 //设备坐标转换为逻辑坐标 DPtoLP
//逻辑坐标转换为设备坐标 LPtoDP MoveToEx(hdc, , cyClient/, NULL);
LineTo(hdc, cxClient, cyClient/);
MoveToEx(hdc, , , NULL);
LineTo(hdc, , cyClient); //获取旧的映射模式
iMapMode = GetMapMode(hdc);
SetMapMode(hdc,MM_LOMETRIC); DPtoLP(hdc, &rect, ); //句柄,POINT指针,点个数 rect是两个点,矩形左上,右下 //设置视口
SetViewportOrgEx(hdc, , cyClient / , NULL); //新的视口原点x,新的视口原点y,以及一个point指针用于存放原来视口位置(0,0) for (int i = ; i < NUM;i++)
{
apt[i].x = rect.right*i / NUM; //单位不一致,需要测试
apt[i].y = (int)(rect.bottom/ * sin( * PI*i / NUM));
} Polyline(hdc, apt, ); EndPaint(hwnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage();
return ;
} return DefWindowProc(hwnd, message, wParam, lParam);
}