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 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函數動態的獲取它們才是正確的。

Windows API-GDI入門基礎知識詳解(1)
Windows API-GDI入門基礎知識詳解(1)
什麽是GDI?  GDI是Graphics Device Interface的縮寫,含義是圖形設備接口,它的主要任務是負責系統與繪圖程序之間的信息交換,處理所有Windows程序的圖形輸出。 ...查看完整版>>Windows API-GDI入門基礎知識詳解(1)
 
Windows API-GDI入門基礎知識詳解(3)
格式化輸出 假如要使用TextOut函數在Windows應用程序中輸出多行文字,我們就必須先取得字符的寬度和高度,通常我們在窗口消息處理函數中處理WM_CREATE消息的時候調用GetTextMetrics函數來獲取他們,之所以放到WM_CR...查看完整版>>Windows API-GDI入門基礎知識詳解(3)
 
原創]Windows Gdi入門初級應用(VC SDK)
原創]Windows Gdi入門初級應用(VC SDK) 好久沒發貼了,今天手癢癢,發一個。GDI的繪圖函數基本上都是有狀態的,所有的函數都要求一個HDC類型的句柄。這個HDC的獲得有幾個途徑BeginPaint,GetWindowDC, GetDC.他們...查看完整版>>原創]Windows Gdi入門初級應用(VC SDK)
 
網絡管理員入門VPN基礎知識詳解-應用技巧
  虛擬專用網絡(VPN)是一門新型的網絡技術,它爲我們提供了一種通過公用網絡(如最大的公用因特網)安全地對企業內部專用網絡進行遠程訪問的連接方式。我們知道一個網絡連接通常由三個部分組成:客戶機、傳輸介質和服...查看完整版>>網絡管理員入門VPN基礎知識詳解-應用技巧
 
網絡管理員入門VPN基礎知識詳解一
  虛擬專用網絡(VPN)是一門新型的網絡技術,它爲我們提供了一種通過公用網絡(如最大的公用因特網)安全地對企業內部專用網絡進行遠程訪問的連接方式。我們知道一個網絡連接通常由三個部分組成:客戶機、傳輸介質和服...查看完整版>>網絡管理員入門VPN基礎知識詳解一
 
網絡管理員入門 VPN基礎知識詳解一
  虛擬專用網絡(VPN)是一門新型的網絡技術,它爲我們提供了一種通過公用網絡(如最大的公用因特網)安全地對企業內部專用網絡進行遠程訪問的連接方式。我們知道一個網絡連接通常由三個部分組成:客戶機、傳輸介質和服...查看完整版>>網絡管理員入門 VPN基礎知識詳解一
 
網絡管理員入門 VPN基礎知識詳解二
  2. 主要VPN協議    通過前面的介紹知道VPN隧道協議主要有三種:PPTP、L2TP和IPSec,PPTP和L2TP協議是工作在OSI/RM開放模型中的第二層,所以又稱之爲第二層隧道協議。其實在第二層隧道協議中還有一種不是很主流...查看完整版>>網絡管理員入門 VPN基礎知識詳解二
 
網絡管理員入門VPN基礎知識詳解一
  虛擬專用網絡(VPN)是一門新型的網絡技術,它爲我們提供了一種通過公用網絡(如最大的公用因特網)安全地對企業內部專用網絡進行遠程訪問的連接方式。我們知道一個網絡連接通常由三個部分組成:客戶機、傳輸介質和服...查看完整版>>網絡管理員入門VPN基礎知識詳解一
 
網絡管理員入門VPN基礎知識詳解二
  2. 主要VPN協議  通過前面的介紹知道VPN隧道協議主要有三種:PPTP、L2TP和IPSec,PPTP和L2TP協議是工作在OSI/RM開放模型中的第二層,所以又稱之爲第二層隧道協議。其實在第二層隧道協議中還有一種不是很主流的協...查看完整版>>網絡管理員入門VPN基礎知識詳解二
 
 
回到王朝網路移動版首頁