Windows绘图中的GDI映射模式

  

  对Windows编程新手来说,GDI编程中最困难的部分就是映射模式(Mapping Mode)。

      什么是映射模式?

      我们知道,GDI画图是在设备描述表这个逻辑意义上的显示平面上进行,其使用的是逻辑坐标,简单地说,映射模式就是设备描述表的属性,用于确定从逻辑坐标到设备坐标值的转换方式。

       Windows支持8中映射模式:MM_TEXT为默认。

   

  

  /* Mapping Modes

       #define MM_TEXT                   1

       #define MM_LOMETRIC           2

       #define MM_HIMETRIC            3

       #define MM_LOENGLISH          4

       #define MM_HIENGLISH           5

       #define MM_TWIPS                6

       #define MM_ISOTROPIC           7

       #define MM_ANISOTROPIC       8

       */

      

       CDC* pDC = GetDC();

       // 默认映射模式 MM_TEXT

      pDC->Ellipse(0, 0, 100, 100);

       // 设置映射模式 MM_LOMETRIC   y向下为负 0.1mm

       pDC->SetMapMode(MM_LOMETRIC);

       pDC->Ellipse(0, 0, 100, -100);

       // 设置映射模式 MM_HIMETRIC   y向下为负 0.01mm

       pDC->SetMapMode(MM_HIMETRIC);

       pDC->Ellipse(0, 0, 100, -100);

       // 设置映射模式 MM_LOENGLISH   y向下为负 0.01in  0.254mm    1英寸(in)=25.4毫米(mm)

       pDC->SetMapMode(MM_LOENGLISH);

       pDC->Ellipse(0, 0, 100, -100);

       // 设置映射模式 MM_HIENGLISH   y向下为负 0.001in 0.0254mm   1英寸(in)=25.4毫米(mm)

       pDC->SetMapMode(MM_HIENGLISH);

       pDC->Ellipse(0, 0, 100, -100);

       // 设置映射模式 MM_TWIPS   y向下为负 0.0007in   1英寸(in)=25.4毫米(mm)

   pDC->SetMapMode(MM_TWIPS);

   pDC->Ellipse(0, 0, 100, -100);

       /* 可编程映射模式 */

       // 根据窗口尺寸按比例自动调节画图的输出大小

       CRect rect;

       GetClientRect(&rect);

       // 各向异性 MM_ANISOTROPIC

       pDC->SetMapMode(MM_ANISOTROPIC);

       pDC->SetWindowExt(100, 100);

       pDC->SetViewportExt(rect.Width(), rect.Height());

       pDC->Ellipse(0, 0, 100, 100);

       pDC->SetMapMode(MM_ANISOTROPIC);

       pDC->SetWindowExt(100, -100);

       pDC->SetViewportExt(rect.Width(), rect.Height());

       pDC->Ellipse(0, 0, 100, -100);

       //各向同性  MM_ISOTROPIC

       pDC->SetMapMode(MM_ISOTROPIC);

       pDC->SetWindowExt(100, -100);

       pDC->SetViewportExt(rect.Width(), rect.Height());

       pDC->Ellipse(0, 0, 100, -100);

       pDC->SetMapMode(MM_ISOTROPIC);

       pDC->SetWindowExt(100, -100);

       pDC->SetViewportExt(rect.Width(), rect.Height());

       pDC->Ellipse(0, 0, 100, -100);

  

   CDC::SetWindowExt             设定“窗口范围”

       CDC::SetViewportExt           设定“视口范围”

       可以这样认为,窗口的尺寸以逻辑单位计算,视口的尺寸以设备尺寸或像素点计算。

       需要注意的是,在MM_ISOTROPIC模式下,应该首先调用SetWindowExt,否则部分窗口客户区可能会因落在窗口的逻辑范围之外而无法使用。

原文地址:https://www.cnblogs.com/MakeView660/p/6537384.html