pDC->SetMapMode(MM_ANISOTROPIC);
CRect rcClient;
GetClientRect(rcClient);
int cx = rcClient.Width();
int cy = rcClient.Height();
pDC->SetViewportOrg(cx/2, cy/2);
pDC->SetViewportExt(cx, -cy);
pDC->SetWindowExt(1000, 1000);
在OnDraw(CDC* pDC)实现如下
CRect rcClient;
GetClientRect(rcClient);
int cx = rcClient.Width();
int cy = rcClient.Height();
CDC memDc;
// 1.创建内存DC
memDc.CreateCompatibleDC(NULL);
CBitmap memBtm;
// 2.创建内存位图(没有该位图不能再内存中作图,相当于画板)
memBtm.CreateCompatibleBitmap(pDC, cx, cy);
memDc.SelectObject(&memBtm);
//
设置映射模式
memDc.SetMapMode(MM_ANISOTROPIC);
memDc.SetViewportOrg(cx/2, cy/2);
memDc.SetViewportExt(cx, -cy);
memDc.SetWindowExt(1000, 1000);
//
设备坐标转为逻辑坐标(特别重要)
memDc.DPtoLP(rcClient);
memDc.BitBlt(rcClient.left, rcClient.top, rcClient.Width(), rcClient.Height(),
pDC, rcClient.left, rcClient.top, SRCCOPY);
//
填充内存画板颜色,不然为黑色
memDc.FillSolidRect(rcClient.left, rcClient.top, rcClient.Width(), rcClient.Height(),RGB(255,255,255));
CPen pen;
pen.CreatePen(PS_SOLID, 1, RGB(234,23,53));
CPen *pOldPen = (CPen*)memDc.SelectObject(&pen);
memDc.MoveTo(0, 480);
memDc.LineTo(0, -480);
memDc.MoveTo(-480, 0);
memDc.LineTo(480, 0);
memDc.SelectObject(pOldPen);
pen.DeleteObject();
const double PI = 3.141592653589793238462643383279;
const int MultiplyEachUnitOnX = 50;
const int MultiplyEachUnitOnY = 500;
//
绘制正弦波
for(double i = -400; i < 400; i += 0.01)
{
double j = sin(PI / MultiplyEachUnitOnX * i) * MultiplyEachUnitOnY;
memDc.SetPixel(i, j, RGB(0, 0, 255));
}
pDC->BitBlt(rcClient.left, rcClient.top, rcClient.Width(), rcClient.Height(),
&memDc, rcClient.left, rcClient.top, SRCCOPY);
ReleaseDC(&memDc);