首页 > 学院 > 开发设计 > 正文

Windows API-GDI入门基础知识详解(2)

2019-11-17 05:02:03
字体:
来源:转载
供稿:网友
现在我们对GDI的基础概念有了一定的了解,下面我们将对GDI一些常用的函数进行学习。TextOut函数 TextOut函数的作用是把指定的字符串输出在我们指定的屏幕位置上。函数原型:BOOL TextOut(  HDC hdc,          // 设备描述表句柄  int nXStart,         // 输出的x轴水平位置  int nYStart,         // 输出的y轴垂直位置  LPCTSTR lpString,  // 指向要输出字符串的长指针  int cbString         // 字符串长度);  第一参数是设备描述表句柄,它既可以是通过BeginPaint函数获得的,也可以是通过GetDC函数获得的,需要提一下的是,设备描述表中的属性控制了显示的字符串的一些细节特征,比如字体、字体颜色、文字背景等,但要注重的是保存在设备描述表属性中的文字背景颜色和WNDCLASS结构中的屏幕背景是有区别的,文字背景指的是紧靠字符四周的矩形空间,又叫做字符框。而窗口背景则是一个画刷,Windows用它来擦除显示区域,它不是设备描述表结构中的一部分。  第二和第三个参数定义了显示区域内字符串的开始位置,x是水平位置,y是垂直位置,字符串第一个字符位于坐标点(x,y),在设备描述表属性中,原点(x,y)均为为0,是显示区域的左上角,对于坐标来说,坐标的映射方式的不同决定了单位的不同,在通常情况下传递给函数的坐标被称为逻辑坐标,Windows有许多坐标映射方式,它们是用来控制GDI函数指定的逻辑坐标转换为显示器的实际像素坐标的方式。映射方式在设备描述表的属性中定义,默认的映射方式是MM_TEXT,我们可以在WinGdi.h头文件中找到。在MM_TEXT映射模式下,逻辑单位于实际单位都相同,都是像素,对于坐标来说,x的值从左向右递增,y的值则从上向下递增(见下图),MM_TEXT坐标系与Windows在PAINTSTURCT结构中定义的无效矩形所使用的坐标系相同。Windows API-GDI入门基础知识详解(2)  第四个参数是指向要输出字符串的长指针。  第五个参数是要输出的字符串的实际长度。  在Windows下输出文字并不如我们所想象的那么轻易,在前面我们已经知道输出文字和坐标有关,我们为了精确的输出文字,就必须对系统的字体和字符大小有进一步的熟悉。系统字体  对于输出字符串的函数TextOut来说,在默认情况下设备描述表属性中使用的是系统字体(SYSTEM_FONT),系统字体是Windows用来在标题栏,功能表和对话框中显示字符串所使用的默认字体。  但值得注重的是,字体假如按宽度来区分的话,大致可以分为两类:  1. 等宽字体

  2. 变宽字体  等宽字体意味着所有的字符宽度都是一致的,但随着计算技术的不断发展和推广,这种等宽字体就不再能够满足需要,于是变宽字体出现了,变宽字体不同的字符宽度都不一定相同。  系统字体是一种点阵字体,字体被定义成了一个个的像素点,字体的确切大小取决于显示器的大小(分辨率的大小)。字符大小  假如要使用TextOut函数显示多行文字,那么就必须确定字体字符的大小,字体的高度确定了下一行字符的显示位置,字体的宽度确定了下一列的显示位置。  屏幕的分辨率和字符大小是确定如何显示字符的主要依据,为了获得当前系统上各种与视觉属性相关的信息,我们可以调用GetSystemMetrics函数获取,调用GetTextMetrics函数可以获取字体大小。  以下是这两个函数的原形以及参数的具体定义:int GetSystemMetrics(  int nIndex  // 索引);  GetSystemMetrics函数是完成Windows图形输出的重要函数,它返回Windows中各种与视觉属性相关的信息,该函数需要只需要一个参数,它是一个索引,这些索引是在Windows头文件中定义的一些常量,这些常量分别指定了不同的与视觉相关的设备属性,这些索引的多少取决与Windows的版本。  以下是这些索引的含义:Value Meaning SM_ARRANGE Flags specifying how the system arranged minimized windows. For more information about minimized windows, see the following Remarks section. SM_CLEANBOOT Value that specifies how the system was started: 0 Normal boot
1 Fail-safe boot
2 Fail-safe with network boot Fail-safe boot (also called SafeBoot, Safe Mode, or Clean Boot) bypasses the user's startup files. SM_CMONITORS Number of display monitors on the desktop. See Remarks for more information. Windows NT, Windows 95:  This value is not supported. SM_CMOUSEBUTTONS Number of buttons on mouse, or zero if no mouse is installed. SM_CXBORDER, SM_CYBORDER Width and height of a window border, in pixels. This is equivalent to the SM_CXEDGE value for windows with the 3-D look. SM_CXCURSOR, SM_CYCURSOR Width and height of a cursor, in pixels. The system cannot create cursors of other sizes. SM_CXDLGFRAME, SM_CYDLGFRAME Same as SM_CXFIXEDFRAME and SM_CYFIXEDFRAME. SM_CXDOUBLECLK, SM_CYDOUBLECLK Width and height of the rectangle around the location of a first click in a double-click sequence, in pixels. The second click must occur within this rectangle for the system to consider the two clicks a double-click. (The two clicks must also occur within a specified time.) To set the width and height of the double-click rectangle, call SystemParametersInfo with the SPI_SETDOUBLECLKHEIGHT and SPI_SETDOUBLECLKWIDTH flags. SM_CXDRAG, SM_CYDRAG Width and height of a rectangle centered on a drag point to allow for limited movement of the mouse pointer before a drag Operation begins. These values are in pixels. It allows the user to click and release the mouse button easily without unintentionally starting a drag operation. SM_CXEDGE, SM_CYEDGE Dimensions of a 3-D border, in pixels. These are the 3-D counterparts of SM_CXBORDER and SM_CYBORDER. SM_CXFIXEDFRAME, SM_CYFIXEDFRAME Thickness of the frame around the perimeter of a window that has a caption but is not sizable, in pixels. SM_CXFIXEDFRAME is the height of the horizontal border and SM_CYFIXEDFRAME is the width of the vertical border. Same as SM_CXDLGFRAME and SM_CYDLGFRAME. SM_CXFOCUSBORDER, SM_CYFOCUSBORDER Width of the left and right edges and the height of the top and bottom edges of the focus rectangle drawn by DrawFocusRect. These values are in pixels. Windows 2000/NT, Windows Me/98/95:  This value is not supported. SM_CXFRAME, SM_CYFRAME Same as SM_CXSIZEFRAME and SM_CYSIZEFRAME. SM_CXFULLSCREEN, SM_CYFULLSCREEN Width and height of the client area for a full-screen window on the PRimary display monitor, in pixels. To get the coordinates of the portion of the screen not obscured by the system taskbar or by application desktop toolbars, call the SystemParametersInfo function with the SPI_GETWORKAREA value. SM_CXHSCROLL, SM_CYHSCROLL Width of the arrow bitmap on a horizontal scroll bar, in pixels; and height of a horizontal scroll bar, in pixels. SM_CXHTHUMB Width of the thumb box in a horizontal scroll bar, in pixels. SM_CXICON, SM_CYICON Default width and height of an icon, in pixels. The LoadIcon function can load only icons of these dimensions. SM_CXICONSPACING, SM_CYICONSPACING Dimensions of a grid cell for items in large icon view, in pixels. Each item fits into a rectangle of this size when arranged. These values are always greater than or equal to SM_CXICON and SM_CYICON. SM_CXMAXIMIZED, SM_CYMAXIMIZED Default dimensions, in pixels, of a maximized top-level window on the primary display monitor. SM_CXMAXTRACK, SM_CYMAXTRACK Default maximum dimensions of a window that has a caption and sizing borders, in pixels. This metric refers to the entire desktop. The user cannot drag the window frame to a size larger than these dimensions. A window can override these values by processing the WM_GETMINMAXINFO message. SM_CXMENUCHECK, SM_CYMENUCHECK Dimensions of the default menu check-mark bitmap, in pixels. SM_CXMENUSIZE, SM_CYMENUSIZE Dimensions of menu bar buttons, such as the child window close button used in the multiple document interface, in pixels. SM_CXMIN, SM_CYMIN Minimum width and height of a window, in pixels. SM_CXMINIMIZED, SM_CYMINIMIZED Dimensions of a minimized window, in pixels. SM_CXMINSPACING SM_CYMINSPACING Dimensions of a grid cell for a minimized window, in pixels. Each minimized window fits into a rectangle this size when arranged. These values are always greater than or equal to SM_CXMINIMIZED and SM_CYMINIMIZED. SM_CXMINTRACK, SM_CYMINTRACK Minimum tracking width and height of a window, in pixels. The user cannot drag the window frame to a size smaller than these dimensions. A window can override these values by processing the WM_GETMINMAXINFO message. SM_CXSCREEN, SM_CYSCREEN Width and height of the screen of the primary display monitor, in pixels. These are the same values oBTained by calling GetDeviceCaps(hdcPrimaryMonitor, HORZRES/VERTRES). SM_CXSIZE, SM_CYSIZE Width and height of a button in a window's caption or title bar, in pixels. SM_CXSIZEFRAME, SM_CYSIZEFRAME Thickness of the sizing border around the perimeter of a window that can be resized, in pixels. SM_CXSIZEFRAME is the width of the horizontal border, and SM_CYSIZEFRAME is the height of the vertical border. Same as SM_CXFRAME and SM_CYFRAME. SM_CXSMICON, SM_CYSMICON Recommended dimensions of a small icon, in pixels. Small icons typically appear in window captions and in small icon view. SM_CXSMSIZE SM_CYSMSIZE Dimensions of small caption buttons, in pixels. SM_CXVIRTUALSCREEN, SM_CYVIRTUALSCREEN Width and height of the virtual screen, in pixels. The virtual screen is the bounding rectangle of all display monitors. The SM_XVIRTUALSCREEN, SM_YVIRTUALSCREEN metrics are the coordinates of the top-left corner of the virtual screen. Windows NT, Windows 95:  This value is not supported. SM_CXVSCROLL, SM_CYVSCROLL Width of a vertical scroll bar, in pixels; and height of the arrow bitmap on a vertical scroll bar, in pixels. SM_CYCAPTION Height of a caption area, in pixels. SM_CYKANJIWINDOW For double byte character set versions of the system, this is the height of the Kanji window at the bottom of the screen, in pixels. SM_CYMENU Height of a single-line menu bar, in pixels. SM_CYSMCAPTION Height of a small caption, in pixels. SM_CYVTHUMB Height of the thumb box in a vertical scroll bar, in pixels. SM_DBCSENABLED TRUE or nonzero if User32.dll supports DBCS; FALSE or zero otherwise. Windows Me/98/95:   TRUE or nonzero if the double-byte character-set (DBCS) version of User.exe is installed; FALSE or zero otherwise. SM_DEBUG TRUE or nonzero if the debug version of User.exe is installed; FALSE or zero otherwise. SM_IMMENABLED TRUE or nonzero if Input Method Manager/Input Method Editor features are enabled; FALSE or zero otherwise. Windows NT, Windows Me/98/95:  This value is not supported. SM_IMMENABLED indicates whether the system is ready to use a Unicode-based IME on a Unicode application. To ensure that a language-dependent IME works, check SM_DBCSENABLED and the system ANSI code page. Otherwise the ANSI-to-Unicode conversion may not be performed correctly, or some components like fonts or registry setting may not be present. SM_MENUDROPALIGNMENT TRUE or nonzero if drop-down menus are right-aligned with the corresponding menu-bar item; FALSE or zero if the menus are left-aligned. SM_MIDEASTENABLED TRUE if the system is enabled for Hebrew and Arabic languages. SM_MOUSEPRESENT TRUE or nonzero if a mouse is installed; FALSE or zero otherwise. SM_MOUSEWHEELPRESENT TRUE or nonzero if a mouse with a wheel is installed; FALSE or zero otherwise. Windows 3.51 and earlier, Windows 95:  This value is not supported. SM_NETWORK Least significant bit is set if a network is present; otherwise, it is cleared. The other bits are reserved for future use. SM_PENWINDOWS TRUE or nonzero if the Microsoft Windows for Pen computing extensions are installed; FALSE or zero otherwise. SM_REMOTECONTROL This system metric is used in a Terminal Services environment. Its value is TRUE if the current session is remotely controlled; FALSE otherwise. Windows 2000/NT, Windows Me/98/95:  This value is not supported. SM_REMOTESESSION This system metric is used in a Terminal Services environment. If the calling process is associated with a Terminal Services client session, the return value is TRUE or nonzero. If the calling process is associated with the Terminal Server console session, the return value is zero. Windows NT 4.0 SP3 and earlier, Windows Me/98/95:  This value is not supported. SM_SECURE TRUE if security is present; FALSE otherwise. SM_SAMEDISPLAYFORMAT TRUE if all the display monitors have the same color format, FALSE otherwise. Note that two displays can have the same bit depth, but different color formats. For example, the red, green, and blue pixels can be encoded with different numbers of bits, or those bits can be located in different places in a pixel's color value. Windows NT, Windows 95:  This value is not supported. SM_SHOWSOUNDS TRUE or nonzero if the user requires an application to present information visually in situations where it would otherwise present the information only in audible form; FALSE, or zero, otherwise. SM_SHUTTINGDOWN TRUE if the current session is shutting down; FALSE otherwise. Windows 2000/NT, Windows Me/98/95:  This value is not supported. SM_SLOWMACHINE TRUE if the computer has a low-end (slow) processor; FALSE otherwise. SM_SWAPBUTTON TRUE or nonzero if the meanings of the left and right mouse buttons are swapped; FALSE or zero otherwise. SM_XVIRTUALSCREEN, SM_YVIRTUALSCREEN Coordinates for the left side and the top of the virtual screen. The virtual screen is the bounding rectangle of all display monitors. The SM_CXVIRTUALSCREEN, SM_CYVIRTUALSCREEN metrics are the width and height of the virtual screen. Windows NT, Windows 95:  This value is not supported.
BOOL GetTextMetrics(  HDC hdc,              // 当前的设备描述表句柄  LPTEXTMETRIC lptm   // 指向TEXTMETRICS结构对象的指针);
  GetTextMetrics函数利用当前选择字体的各种度量值来填充由lptm参数所指向的缓冲区,函数假如运行成功则返回TURE,失败则返回FALSE。  TEXTMETRIC结构定义在WinGdi.h头文件中。  结构如下:typedef struct tagTEXTMETRICA{    LONG        tmHeight;    LONG        tmAscent;    LONG        tmDescent;    LONG        tmInternalLeading;    LONG        tmExternalLeading;    LONG        tmAveCharWidth;    LONG        tmMaxCharWidth;    LONG        tmWeight;    LONG        tmOverhang;    LONG        tmDigitizedaspectX;    LONG        tmDigitizedAspectY;    BYTE         tmFirstChar;    BYTE        tmLastChar;    BYTE        tmDefaultChar;    BYTE        tmBreakChar;    BYTE        tmItalic;    BYTE        tmUnderlined;    BYTE        tmStruckOut;    BYTE        tmPitchAndFamily;    BYTE        tmCharSet;} TEXTMETRICA, *PTEXTMETRICA, NEAR *NPTEXTMETRICA, FAR *LPTEXTMETRICA;typedef struct tagTEXTMETRICW{    LONG        tmHeight;    LONG        tmAscent;    LONG        tmDescent;    LONG        tmInternalLeading;    LONG        tmExternalLeading;    LONG        tmAveCharWidth;    LONG        tmMaxCharWidth;    LONG        tmWeight;    LONG        tmOverhang;    LONG        tmDigitizedAspectX;    LONG        tmDigitizedAspectY;    WCHAR      tmFirstChar;    WCHAR      tmLastChar;    WCHAR      tmDefaultChar;    WCHAR      tmBreakChar;    BYTE        tmItalic;    BYTE        tmUnderlined;    BYTE        tmStruckOut;    BYTE        tmPitchAndFamily;    BYTE        tmCharSet;} TEXTMETRICW, *PTEXTMETRICW, NEAR *NPTEXTMETRICW, FAR *LPTEXTMETRICW;
  该结构拥有大约20个属性,这些属性的值的单位取决于设备描述表的映射方式,默认情况下是MM_TEXT,对于TextOut输出来说我们只需要用到前7个,他们的单位是像素。LONG        tmHeight;           // 字符基准线上下最大纵向高度,是tmAscent与tmDescent之和。LONG        tmAscent;       // 字符基准线以上所占的高度。LONG        tmDescent;          // 字符基准线以下所占的高度。LONG        tmInternalLeading;   // 内部间距,也是重音符号出现的地方。LONG        tmExternalLeading;  // 行距。LONG        tmAveCharWidth;    // 小写字母的加权平均宽度,对于大写字母来说可以用小写字母的加权平均宽度乘以150%计算出来。LONG        tmMaxCharWidth;    // 字符中字宽字符的宽度。  字符的纵向大小是由TEXTMETRIC结构的前五个属性决定的。  具体情况见下图:Windows API-GDI入门基础知识详解(2)  字体的大小是取决于当前屏幕的分辨率或是所选字体本身的默认大小的,在编写应用程序的时候不要把字体的大小以猜想的方式固定了,因为字体的大小是可变化的,利用GetTextMetrics函数动态的获取它们才是正确的。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表