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

 

  现在我们对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下输出文字并不如我们所想象的那么容易,在前面我们已经知道输出文字和坐标有关,我们为了精确的输出文字,就必须对系统的字体和字符大小有进一步的认识。

系统字体

  对于输出字符串的函数TextOut来说,在默认情况下设备描述表属性中使用的是系统字体(SYSTEM_FONT),系统字体是Windows用来在标题栏,功能表和对话框中显示字符串所使用的默认字体。

  但值得注意的是,字体如果按宽度来区分的话,大致可以分为两类:

  1. 等宽字体
  2. 变宽字体

  等宽字体意味着所有的字符宽度都是一致的,但随着计算技术的不断发展和推广,这种等宽字体就不再能够满足需要,于是变宽字体出现了,变宽字体不同的字符宽度都不一定相同。

  系统字体是一种点阵字体,字体被定义成了一个个的像素点,字体的确切大小取决于显示器的大小(分辨率的大小)。

字符大小

  如果要使用TextOut函数显示多行文字,那么就必须确定字体字符的大小,字体的高度确定了下一行字符的显示位置,字体的宽度确定了下一列的显示位置。

  屏幕的分辨率和字符大小是确定如何显示字符的主要依据,为了获得当前系统上各种与视觉属性相关的信息,我们可以调用GetSystemMetrics函数获取,调用GetTextMetrics函数可以获取字体大小。

  以下是这两个函数的原形以及参数的详细定义:

int GetSystemMetrics(

  int nIndex  // 索引

);

  GetSystemMetrics函数是完成Windows图形输出的重要函数,它返回Windows中各种与视觉属性相关的信息,该函数需要只需要一个参数,它是一个索引,这些索引是在Windows头文件中定义的一些常量,这些常量分别指定了不同的与视觉相关的设备属性,这些索引的多少取决与Windows的版本。

  以下是这些索引的含义:

ValueMeaning
SM_ARRANGEFlags specifying how the system arranged minimized windows. For more information about minimized windows, see the following Remarks section.
SM_CLEANBOOTValue 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_CMONITORSNumber of display monitors on the desktop. See Remarks for more information. Windows NT, Windows 95:  This value is not supported.
SM_CMOUSEBUTTONSNumber of buttons on mouse, or zero if no mouse is installed.
SM_CXBORDER, SM_CYBORDERWidth 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_CYCURSORWidth and height of a cursor, in pixels. The system cannot create cursors of other sizes.
SM_CXDLGFRAME, SM_CYDLGFRAMESame as SM_CXFIXEDFRAME and SM_CYFIXEDFRAME.
SM_CXDOUBLECLK, SM_CYDOUBLECLKWidth 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_CYDRAGWidth 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_CYEDGEDimensions of a 3-D border, in pixels. These are the 3-D counterparts of SM_CXBORDER and SM_CYBORDER.
SM_CXFIXEDFRAME, SM_CYFIXEDFRAMEThickness 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_CYFOCUSBORDERWidth 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_CYFRAMESame as SM_CXSIZEFRAME and SM_CYSIZEFRAME.
SM_CXFULLSCREEN, SM_CYFULLSCREENWidth 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_CYHSCROLLWidth of the arrow bitmap on a horizontal scroll bar, in pixels; and height of a horizontal scroll bar, in pixels.
SM_CXHTHUMBWidth of the thumb box in a horizontal scroll bar, in pixels.
SM_CXICON, SM_CYICONDefault width and height of an icon, in pixels. The LoadIcon function can load only icons of these dimensions.
SM_CXICONSPACING, SM_CYICONSPACINGDimensions 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_CYMAXIMIZEDDefault dimensions, in pixels, of a maximized top-level window on the primary display monitor.
SM_CXMAXTRACK, SM_CYMAXTRACKDefault 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_CYMENUCHECKDimensions of the default menu check-mark bitmap, in pixels.
SM_CXMENUSIZE, SM_CYMENUSIZEDimensions of menu bar buttons, such as the child window close button used in the multiple document interface, in pixels.
SM_CXMIN, SM_CYMINMinimum width and height of a window, in pixels.
SM_CXMINIMIZED, SM_CYMINIMIZEDDimensions of a minimized window, in pixels.
SM_CXMINSPACING SM_CYMINSPACINGDimensions 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_CYMINTRACKMinimum 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_CYSCREENWidth 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_CYSIZEWidth and height of a button in a window's caption or title bar, in pixels.
SM_CXSIZEFRAME, SM_CYSIZEFRAMEThickness 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_CYSMICONRecommended dimensions of a small icon, in pixels. Small icons typically appear in window captions and in small icon view.
SM_CXSMSIZE SM_CYSMSIZEDimensions of small caption buttons, in pixels.
SM_CXVIRTUALSCREEN, SM_CYVIRTUALSCREENWidth 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_CYVSCROLLWidth of a vertical scroll bar, in pixels; and height of the arrow bitmap on a vertical scroll bar, in pixels.
SM_CYCAPTIONHeight of a caption area, in pixels.
SM_CYKANJIWINDOWFor 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_CYMENUHeight of a single-line menu bar, in pixels.
SM_CYSMCAPTIONHeight of a small caption, in pixels.
SM_CYVTHUMBHeight of the thumb box in a vertical scroll bar, in pixels.
SM_DBCSENABLEDTRUE 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_DEBUGTRUE or nonzero if the debug version of User.exe is installed; FALSE or zero otherwise.
SM_IMMENABLEDTRUE 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_MENUDROPALIGNMENTTRUE 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_MIDEASTENABLEDTRUE if the system is enabled for Hebrew and Arabic languages.
SM_MOUSEPRESENTTRUE or nonzero if a mouse is installed; FALSE or zero otherwise.
SM_MOUSEWHEELPRESENTTRUE 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_NETWORKLeast significant bit is set if a network is present; otherwise, it is cleared. The other bits are reserved for future use.
SM_PENWINDOWSTRUE or nonzero if the Microsoft Windows for Pen computing extensions are installed; FALSE or zero otherwise.
SM_REMOTECONTROLThis 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_REMOTESESSIONThis 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_SECURETRUE if security is present; FALSE otherwise.
SM_SAMEDISPLAYFORMATTRUE 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_SHOWSOUNDSTRUE 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_SHUTTINGDOWNTRUE if the current session is shutting down; FALSE otherwise. Windows 2000/NT, Windows Me/98/95:  This value is not supported.
SM_SLOWMACHINETRUE if the computer has a low-end (slow) processor; FALSE otherwise.
SM_SWAPBUTTONTRUE or nonzero if the meanings of the left and right mouse buttons are swapped; FALSE or zero otherwise.
SM_XVIRTUALSCREEN, SM_YVIRTUALSCREENCoordinates 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;           // 字符基准线上下最大纵向高度,是tmAscenttmDescent之和。

LONG        tmAscent;       // 字符基准线以上所占的高度。

LONG        tmDescent;          // 字符基准线以下所占的高度。

LONG        tmInternalLeading;   // 内部间距,也是重音符号出现的地方。

LONG        tmExternalLeading;  // 行距

LONG        tmAveCharWidth;    // 小写字母的加权平均宽度,对于大写字母来说可以用小写字母的加权平均宽度乘以150%计算出来。

LONG        tmMaxCharWidth;    // 字符中字宽字符的宽度。

  字符的纵向大小是由TEXTMETRIC结构的前五个属性决定的。

  具体情况见下图:

  字体的大小是取决于当前屏幕的分辨率或是所选字体本身的默认大小的,在编写应用程序的时候不要把字体的大小以猜想的方式固定了,因为字体的大小是可变化的,利用GetTextMetrics函数动态的获取它们才是正确的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值