首页 > 代码库 > VC++中GDI和GDI+ 的坐标系统介绍
VC++中GDI和GDI+ 的坐标系统介绍
在Windows应用程序中,只要进行绘图,就要使用GDI坐标系统。Windows提供了几种映射方式,每一种映射都对应着一种坐标系。例如,绘制图形时,必须给出图形各个点在客户区的位置,其位置用x 和y两个坐标表示,x 表示横坐标,y表示纵坐标。在所有的GDI绘制函数中,这些坐标使用的是一种“逻辑单位”。当GDI函数将结果输出送到某个物理设备上时,Windows将逻辑坐标转换成设备坐标(如屏幕或打印机的像素点)。本文讨论了图形环境中的各个映射模式,包括它们是什么,怎么工作的,以及它们真正的含义。
一、基础知识
三、更改坐标系统
正如上面所看到的,默认的坐标系统坐标原点位于窗口的左上角,水平轴的正方向向右,垂直轴的正方向向下。为了进一步说明这一点,让我们来绘制一个半径为50个单位,圆心位于(0,0)点,同时绘制一个连接(0,0)(100,100)两点的直线。
void CExoDraw1View::OnPaint() { CPaintDC dc(this); // device context for painting // A circle whose center is at the origin (0, 0) dc.Ellipse(-50, -50, 50, 50); // A line that starts at (0, 0) and ends at (100, 100) dc.MoveTo(0, 0); dc.LineTo(100, 100); } |
图四、代码效果图 |
MFC提供了各种函数来处理坐标定位及扩展绘制区域的问题,包括在屏幕上任意位置设置坐标原点的函数。因为你是在一个设备上下文上进行绘图操作,因此,你所需要做的就是调用CDC::SetViewportOrg()函数。这个函数重载了两个版本,这允许你使用X、Y坐标或是一个定义的Point点。这个函数的语法如下:
SetViewportOrg(int X, int Y); SetViewportOrg(CPoint Pt); |
void CExoDraw1View::OnPaint() { CPaintDC dc(this); //绘图的设备上下文; dc.SetViewportOrg(200, 150); // 圆心位于坐标原点(0, 0) dc.Ellipse(-50, -50, 50, 50); // 连接(0, 0) 和 (100, 100)点的直线; dc.MoveTo(0, 0); dc.LineTo(100, 100); } |
图五、代码效果图 |
需要注意的是,你也可以相对于客户区域来指定坐标原点
void CExoDraw1View::OnPaint() { CPaintDC dc(this); //绘图的设备上下文; CRect Recto; //获取客户区尺寸; GetClientRect(&Recto); dc.SetViewportOrg(Recto.Width() / 2, Recto.Height() / 2); // A circle whose center is at the origin (0, 0) dc.Ellipse(-50, -50, 50, 50); // A line that starts at (0, 0) and ends at (100, 100) dc.MoveTo(0, 0); dc.LineTo(100, 100); } |
图六、代码效果图 |
现在你已了解了如何设置坐标原点,让我们来将(380,220)点作为坐标原点,并绘制出笛卡尔的坐标轴:
void CExoDraw1View::OnPaint() { CPaintDC dc(this); // device context for painting CRect Recto; dc.SetViewportOrg(380, 220); // Use a red pen CPen PenRed(PS_SOLID, 1, RGB(255, 0, 0)); dc.SelectObject(PenRed); // A circle whose center is at the origin (0, 0) dc.Ellipse(-100, -100, 100, 100); // Use a blue pen CPen PenBlue(PS_SOLID, 1, RGB(0, 0, 255)); dc.SelectObject(PenBlue); // Horizontal axis dc.MoveTo(-380, 0); dc.LineTo(380, 0); // Vertical axis dc.MoveTo(0, -220); dc.LineTo(0, 220); } |
图七、代码效果图 |
正如已经看到的,SetViewportOrg()函数可以更改设备上下文的坐标原点,同时,它也用来规定坐标轴的正方向,即水平轴向右,垂直轴向下:
图八、坐标轴示意图 |
为了说明这一点,下面来绘制一条黄色的45度角的直线:
void CExoDraw1View::OnPaint() { CPaintDC dc(this); // device context for painting dc.SetViewportOrg(380, 220); // Use a red pen CPen PenRed(PS_SOLID, 1, RGB(255, 0, 0)); dc.SelectObject(PenRed); // A circle whose center is at the origin (0, 0) dc.Ellipse(-100, -100, 100, 100); // Use a blue pen CPen PenBlue(PS_SOLID, 1, RGB(0, 0, 255)); dc.SelectObject(PenBlue); // Horizontal axis dc.MoveTo(-380, 0); dc.LineTo(380, 0); // Vertical axis dc.MoveTo(0, -220); dc.LineTo(0, 220); // An orange pen CPen PenOrange(PS_SOLID, 1, RGB(255, 128, 0)); dc.SelectObject(PenOrange); // A diagonal line at 45 degrees dc.MoveTo(0, 0); dc.LineTo(120, 120); } |
图九、代码效果图 |
正如你所看到的,我们的直线没有在45度位置,而是位于坐标系统的第四象限,造成这种情况的原因是默认的坐标系统。
int SetMapMode(int nMapMode); |
void CExoDraw1View::OnPaint() { CPaintDC dc(this); // device context for painting dc.SetMapMode(MM_TEXT); dc.SetViewportOrg(380, 220); // Use a red pen CPen PenRed(PS_SOLID, 1, RGB(255, 0, 0)); dc.SelectObject(PenRed); // A circle whose center is at the origin (0, 0) dc.Ellipse(-100, -100, 100, 100); // Use a blue pen CPen PenBlue(PS_SOLID, 1, RGB(0, 0, 255)); dc.SelectObject(PenBlue); // Horizontal axis dc.MoveTo(-380, 0); dc.LineTo(380, 0); // Vertical axis dc.MoveTo(0, -220); dc.LineTo(0, 220); // An orange pen CPen PenOrange(PS_SOLID, 1, RGB(255, 128, 0)); dc.SelectObject(PenOrange); // A diagonal line at 45 degrees dc.MoveTo(0, 0); dc.LineTo(120, 120); } |
图十、代码效果图 |
MM_LOENGLISH模式,与其他一些映射模式(不包括MM_TEXT模式)一样,执行两个动作,它改变坐标轴的方向,垂直坐标轴的正方向向上;
图十一、MM_LOENGLISH映射模式下的坐标系 |
void CExoDraw1View::OnPaint() { CPaintDC dc(this); // device context for painting dc.SetMapMode(MM_LOENGLISH); dc.SetViewportOrg(380, 220); . . . } |
图十二、代码效果图 |
void CExoDraw1View::OnPaint() { CPaintDC dc(this); // device context for painting dc.SetMapMode(MM_HIENGLISH); dc.SetViewportOrg(380, 220); . . . Same as previous } |
图十三、代码效果图 |
void CExoDraw1View::OnPaint() { CPaintDC dc(this); // device context for painting dc.SetMapMode(MM_LOMETRIC); dc.SetViewportOrg(380, 220); . . . } |
图十四、代码效果图 |
MM_HIMETRIC使用与上述三种映射模式相同的坐标系,但它的坐标单位是0.01毫米,下面例子代码如下:
void CExoDraw1View::OnPaint() { CPaintDC dc(this); // device context for painting dc.SetMapMode(MM_HIMETRIC); dc.SetViewportOrg(380, 220); . . . Same as previous } |
图十五、代码效果图 |
void CExoDraw1View::OnPaint() { CPaintDC dc(this); // device context for painting CRect Recto; dc.SetMapMode(MM_TWIPS); dc.SetViewportOrg(380, 220); . . . } |
图十六、代码效果图 |
void CExoDraw1View::OnPaint() { CPaintDC dc(this); // device context for painting CPen PenRed(PS_SOLID, 1, RGB(255, 0, 0)); CBrush BrushAqua(RGB(0, 255, 255)); dc.SelectObject(PenRed); dc.SelectObject(BrushAqua); // Draw a square with a red border and an aqua background dc.Rectangle(-100, -100, 100, 100); CPen BluePen(PS_SOLID, 1, RGB(0, 0, 255)); dc.SelectObject(BluePen); // Diagonal line at 45 degrees starting at the origin (0, 0) dc.MoveTo(0, 0); dc.LineTo(200, 200); } |
图十七、代码效果图 |
void CExoDraw1View::OnPaint() { CPaintDC dc(this); // device context for painting dc.SetViewportOrg(340, 220); CPen PenRed(PS_SOLID, 1, RGB(255, 0, 0)); CBrush BrushAqua(RGB(0, 255, 255)); dc.SelectObject(PenRed); dc.SelectObject(BrushAqua); // Draw a square with a red border and an aqua background dc.Rectangle(-100, -100, 100, 100); CPen BluePen(PS_SOLID, 1, RGB(0, 0, 255)); dc.SelectObject(BluePen); // Diagonal line at 45 degrees starting at the origin (0, 0) dc.MoveTo(0, 0); dc.LineTo(200, 200); } |
图十八、代码效果图 |
void CExoDraw1View::OnPaint() { CPaintDC dc(this); // device context for painting dc.SetMapMode(MM_ISOTROPIC); dc.SetViewportOrg(340, 220); CPen PenRed(PS_SOLID, 1, RGB(255, 0, 0)); CBrush BrushAqua(RGB(0, 255, 255)); dc.SelectObject(PenRed); dc.SelectObject(BrushAqua); // Draw a square with a red border and an aqua background dc.Rectangle(-100, -100, 100, 100); CPen BluePen(PS_SOLID, 1, RGB(0, 0, 255)); dc.SelectObject(BluePen); // Diagonal line at 45 degrees starting at the origin (0, 0) dc.MoveTo(0, 0); dc.LineTo(200, 200); } |
图十九、代码效果图 |
CSize SetWindowExt(int cx, int cy); CSize SetWindowExt(SIZE size); |
void CExoDraw1View::OnPaint() { CPaintDC dc(this); // device context for painting dc.SetMapMode(MM_ISOTROPIC); dc.SetViewportOrg(340, 220); dc.SetWindowExt(480, 480); CPen PenRed(PS_SOLID, 1, RGB(255, 0, 0)); CBrush BrushAqua(RGB(0, 255, 255)); dc.SelectObject(PenRed); dc.SelectObject(BrushAqua); // Draw a square with a red border and an aqua background dc.Rectangle(-100, -100, 100, 100); CPen BluePen(PS_SOLID, 1, RGB(0, 0, 255)); dc.SelectObject(BluePen); // Diagonal line at 45 degrees starting at the origin (0, 0) dc.MoveTo(0, 0); dc.LineTo(200, 200); } |
图二十、代码效果图 |
CSize SetViewportExt(int cx, int cy); CSize SetViewportExt(SIZE size); |
SetWindowExt(int Lwidth, int Lheight) //参数的单位为逻辑单位(Logical); SetViewportExt(int Pwidth, int Pheight) //参数的单位为像素(Pixel); |
void CExoDraw1View::OnPaint() { CPaintDC dc(this); // device context for painting dc.SetMapMode(MM_ISOTROPIC); dc.SetViewportOrg(340, 220); dc.SetWindowExt(480, 480); dc.SetViewportExt(440, -680); CPen PenRed(PS_SOLID, 1, RGB(255, 0, 0)); CBrush BrushAqua(RGB(0, 255, 255)); dc.SelectObject(PenRed); dc.SelectObject(BrushAqua); // Draw a square with a red border and an aqua background dc.Rectangle(-100, -100, 100, 100); CPen BluePen(PS_SOLID, 1, RGB(0, 0, 255)); dc.SelectObject(BluePen); // Diagonal line at 45 degrees starting at the origin (0, 0) dc.MoveTo(0, 0); dc.LineTo(200, 200); } |
图二十一、代码效果图 |
void CExoDraw1View::OnPaint() { CPaintDC dc(this); // device context for painting CBrush bgBrush(BLACK_BRUSH); dc.SelectObject(bgBrush); dc.Rectangle(Recto); dc.SetMapMode(MM_ISOTROPIC); dc.SetViewportOrg(0, 440); dc.SetWindowExt(480, 480); dc.SetViewportExt(440, -680); CPen PenWhite(PS_SOLID, 1, RGB(255, 255, 255)); dc.SelectObject(PenWhite); dc.MoveTo(21, 20); dc.LineTo(21, 75); // Up arrow dc.MoveTo(16, 75); dc.LineTo(21, 90); dc.LineTo(26, 75); dc.LineTo(16, 75); dc.MoveTo(21, 22); dc.LineTo(75, 22); // Right arrow dc.MoveTo(75, 17); dc.LineTo(90, 22); dc.LineTo(75, 27); dc.LineTo(75, 17); dc.SetBkMode(TRANSPARENT); dc.SetTextColor(RGB(255, 255, 255)); dc.TextOut(16, 114, ’Y’); dc.TextOut(100, 32, ’X’); dc.Rectangle(15, 15, 30, 30); } |
图二十二、代码效果图 |
void CExoDraw1View::OnPaint() { CPaintDC dc(this); // device context for painting CRect Recto; GetClientRect(&Recto); CBrush bgBrush(BLACK_BRUSH); dc.SelectObject(bgBrush); dc.Rectangle(Recto); CPen PenBlue(PS_SOLID, 1, RGB(0, 0, 255)); dc.SelectObject(PenBlue); for(int x = 0; x < Recto.Width(); x += 20) { dc.MoveTo(x, 0); dc.LineTo(x, Recto.Height()); } for(int y = 0; y < Recto.Height(); y += 20) { dc.MoveTo(0, y); dc.LineTo(Recto.Width(), y); } } |
图二十三、代码效果图 |
void CExoDraw1View::OnPaint() { CPaintDC dc(this); // device context for painting CRect Recto; GetClientRect(&Recto); CBrush bgBrush(BLACK_BRUSH); dc.SelectObject(bgBrush); dc.Rectangle(Recto); for(int x = 0; x < Recto.Width(); x += 20) { for(int y = 0; y < Recto.Height(); y += 20) { dc.SetPixel(x, y, RGB(255, 255, 255)); } } } |
图二十四、代码效果 |
void CExoView::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here dc.SetMapMode(MM_ANISOTROPIC); dc.SetViewportOrg(340, 220); dc.SetWindowExt(1440, 1440); dc.SetViewportExt(-1440, -220); CPen PenBlue(PS_SOLID, 1, RGB(0, 0, 255)); dc.SelectObject(PenBlue); // Axes dc.MoveTo(-300, 0); dc.LineTo( 300, 0); dc.MoveTo( 0, -1400); dc.LineTo( 0, 1400); // I am exaggerating with the PI value here but why not? const double PI = 3.141592653589793238462643383279; // The following two values were chosen randomly by me. // You can chose other values you like const int MultiplyEachUnitOnX = 50; const int MultiplyEachUnitOnY = 250; for(double i = -280; i < 280; i += 0.01) { double j = sin(PI / MultiplyEachUnitOnX * i) * MultiplyEachUnitOnY; dc.SetPixel(i, j, RGB(255, 0, 0)); } // Do not call CView::OnPaint() for painting messages } |
图二十五、代码效果图 |
3、全窗口坐标,包括一个程序的整个窗口,包括标题条、菜单、滚动条和窗口框,窗口的左上角为(0,0)。使用GetWindowDC得到的窗口设备环境,可以将逻辑单位转换成窗口”坐标。
(三)映射。映射方式定义了Windows如何将GDI函数中指定的逻辑坐标映射为设备坐标。在下文中我们将介绍常用的映射方式。
当在微软的窗口中进行绘图时,绘图的坐标原点在屏幕的左上角,任何物体在屏幕上定位都要参考这个坐标原点。在笛卡尔坐标系统中这个点被定义为坐标原点(0,0),水平坐标轴的正方向是从该点出发向右延伸,垂直坐标轴的正方向是从该点出发向下延伸。
图一、笛卡尔坐标系 |
这个坐标原点只是操作系统默认的坐标原点,所以如果你调用Ellipse(-100, -100, 100, 100)函数来绘制图形的话,你将得到一个圆,它的圆心位于屏幕的左上角,仅仅只有圆的四分之一部分(270度到360度的部分)显示在屏幕上。代码及效果图如下
void CExoDraw1View::OnPaint() { CPaintDC dc(this); // 绘图的设备厂上下文 CPen PenBlue; // 兰色画笔 PenBlue.CreatePen(PS_SOLID, 1, RGB(0, 12, 255)); dc.SelectObject(&pPen); dc.Ellipse(-100, -100, 100, 100); } |
图二、代码效果图 |
按照同样的原理,你可以使用CpaintDC的方法或按照你的要求创建函数来绘制任何几何或非几何图形。例如,下面的代码绘制了两条相互垂直的直线,垂点位与窗口的中心:
void CExoDraw1View::OnPaint() { CPaintDC dc(this); // 绘图的设备上下文 CRect Recto; CPen PenBlue; PenBlue.CreatePen(PS_SOLID, 1, RGB(0, 12, 255)); dc.SelectObject(&PenBlue); dc.Ellipse(-100, -100, 100, 100); CPen PenBlack; PenBlack.CreatePen(PS_SOLID, 1, BLACK_PEN); dc.SelectObject(&PenBlack); // 得到客户区域的尺寸; GetClientRect(&Recto); dc.MoveTo(Recto.Width() / 2, 0); dc.LineTo(Recto.Width() / 2, Recto.Height()); dc.MoveTo(0, Recto.Height() / 2); dc.LineTo(Recto.Width(), Recto.Height() / 2); } |
VC++中GDI和GDI+ 的坐标系统介绍