win32中的度量方式

 众所周知,在win32编程中,关于度量的问题是一个及其让人讨厌的问题,在这篇文章里,我将一些关于度量的问题进行一下总结。

 

一.窗口和视口

在win32程序中,映射方式决定了如何将逻辑坐标映射为设备坐标,二设备坐标系完全取决于所使用的获取设备描述表的API函数。

而窗口到视口的映射对于映射方式同样有着巨大的影响。

 

“视口”:它是基于设备坐标(像素)而言的。通常情况下,视口与客户区相同,当然你可以通过使用GetWindowDC或者CreateDC获取设备描述表,以此获取整个屏幕或者屏幕的坐标。

 

“窗口”:窗口是基于逻辑坐标而言的,它的坐标系可以依据你选择的映射方式的改变而改变。

 

在windows下,通过SetViewportOrgEx()和SetWindowportOrgEx()来改变视口和窗口的原点的坐标。

例如:

      SetViewportOrgEx(hdc,cxClient/2,cyClient/2,NULL);                 //将视口的原点设置为整个视口的中心

 

这时候

 

      TextOut(hdc,0,0,Text("Hello"),5);

 

字符将会在视口的中心输出;

 

而你如果使用

      SetWindowportEx(hdc,-cxClient/2,-cyClient/2,NULL);

 

可以达到同样的效果。

 

 

 

二.设备坐标和逻辑坐标

 

在windows中,对于任何消息(即使是WM_SIZE),任何非GDI函数以及部分的GDI函数,均采用设备坐标。从某种程度上来说,映射方式是设备描述表的属性,对于不使用设备描述表句柄作为参数的函数,映射方式自然不会起到作用。

不过,需要注意,GetTextMetrics是使用逻辑单位的。

 

 

三.映射方式

在windows下,存在多种的逻辑坐标映射为设备坐标的映射方式:

  • MM_ANISOTROPIC   Logical units are converted to arbitrary units with arbitrarily scaled axes. Setting the mapping mode to MM_ANISOTROPIC does not change the current window or viewport settings. To change the units, orientation, and scaling, call the SetWindowExt and SetViewportExt member functions.

  • MM_HIENGLISH   Each logical unit is converted to 0.001 inch. Positive x is to the right; positive y is up.

  • MM_HIMETRIC   Each logical unit is converted to 0.01 millimeter. Positive x is to the right; positive y is up.

  • MM_ISOTROPIC   Logical units are converted to arbitrary units with equally scaled axes; that is, 1 unit along the x-axis is equal to 1 unit along the y-axis. Use the SetWindowExt and SetViewportExt member functions to specify the desired units and the orientation of the axes. GDI makes adjustments as necessary to ensure that the x and y units remain the same size.

  • MM_LOENGLISH   Each logical unit is converted to 0.01 inch. Positive x is to the right; positive y is up.

  • MM_LOMETRIC   Each logical unit is converted to 0.1 millimeter. Positive x is to the right; positive y is up.

  • MM_TEXT   Each logical unit is converted to 1 device pixel. Positive x is to the right; positive y is down.

  • MM_TWIPS   Each logical unit is converted to 1/20 of a point. (Because a point is 1/72 inch, a twip is 1/1440 inch.) Positive x is to the right; positive y is up.

这是我从MSDN中摘录的。

 

你可能感兴趣的:(编程,windows,api,null)