二、Windows编程GDI—格式化文字

Windows启动后,系统字体的大小就不会发生改变,所以在程序执行过程中,程序写作者只需要呼叫一次GetTexMetrics。最好是在窗口消息处理程序中处理WM_CREATE消息时进行此呼叫,WM_CREATE消息是窗口消息处理程序接收的第一个消息。在WinMain中呼叫CreateWindow时,Windows会以一个WM_CREATE消息呼叫窗口消息处理程序。

假设要编写一个Windows程序,在显示区域显示几行文字,这需要先取得字符宽度和高度。您可以在窗口消息处理程序内定义两个变量来保存平均字符宽度(cxChar)和总的字符高度(cyChar):

static int cxChar, cyChar ;
        

变量名的前缀c代表「count」,在这里指图素数,与x和y结合,分别指宽和高。这些变量定义为static静态变量,因为它们在窗口消息处理程序中处理其它消息(如WM_PAINT)时也应该是有效的。如果变量在函数外面定义,则不需要定义为static。

下面是取得系统字体的字符宽度和高度的WM_CREATE程序代码:

case WM_CREATE:
        
    hdc = GetDC (hwnd) ;
        
    GetTextMetrics (hdc, &tm) ;
        
    cxChar = tm.tmAveCharWidth ;
        
    cyChar = tm.tmHeight + tm.tmExternalLeading ;
        

    ReleaseDC (hwnd, hdc) ;
        
    return 0 ;
        

注意我在计算cyChar时包括了tmExternalLeading字段,虽然该字段在系统字体中为0,但是因为它使得文字的可读性更好,所以还是应该把它包括进去。沿着窗口向下每隔cyChar图素就会显示一行文字。

您会发现常常需要显示格式化的数字跟简单的字符串。我在第二章讲到过,您不能使惯用的工具(可爱的printf函数)来完成这项工作,但是可以使用sprintf和Windows版的sprintf-wsprintf。这些函数与printf相似,只是把格式化字符串放到字符串中。然后,可以用TextOut将字符串输出到显示器上。非常方便的是,从sprintf和wsprintf传回的值就是字符串的长度。您可以将这个值传递给TextOut作为iLength参数。下面的程序代码显示了wsprintf与TextOut的典型组合:

int  iLength ;
        
TCHAR szBuffer [40] ;
        
其它行程序
        
iLength = wsprintf (szBuffer, TEXT ("The sum of %i and %i is %i"),
        
                        iA, iB, iA + iB) ;
        
TextOut (hdc, x, y, szBuffer, iLength) ;
        

对于这样简单的情况,可以将nLength的定义值与TextOut放在同一条叙述中,从而无需定义iLength:

TextOut (hdc, x, y, szBuffer,
        
  wsprintf (szBuffer, TEXT ("The sum of %i and %i is %i"),
        
                    iA, iB, iA + iB)) ;
        

虽然这样子写起来不好看,但是功能与前者是一样的。


综合使用

现在,我们似乎已经具备了在屏幕上显示多行文字所需要的所有知识。我们知道如何在WM_PAINT消息处理期间取得一个设备内容句柄,如何使用TextOut函数以及如何根据字符大小来安排字距,剩下的就是显示一点有意义的东西了。

前一章里,我们大概知道从Windows的GetSystemMetrics函数中取得的信息是很有意义的,该函数传回Windows中不同视觉组件的大小信息,如图标、光标、标题列和滚动条等。它们的大小因显示卡和驱动程序的不同而有所不同。GetSystemMetrics是在程序中完成与设备无关图形输出的重要函数。

该函数需要一个参数,叫做「索引」,在Windows表头文件定义了75个整数索引标识符(标识符的数量随着每个版本的Windows的发布而不断地增加,在Windows 1.0的程序写作者文件中仅列出了26个)。GetSystemMetrics传回一个整数,这个整数通常就是参数中指定的图形组件大小。

让我们来编写一个程序,显示一些可以从GetSystemMetrics呼叫中取得的信息,显示格式为每种视觉组件一行。如果我们建立一个表头文件,在表头文件中定义一个结构数组,此结构包含GetSystemMetrics索引对应的Windows表头文件标识符和呼叫所传回的每个值对应的字符串,这样处理起来要容易一些。表头文件名为SYSMETS.H,如程序4-1所示。

程序4-1 SYSMETS.H
        
/*---------------------------------------------------------
        
SYSMETS.H -- System metrics display structure
        
-----------------------------------------------------------*/
        
#define NUMLINES ((int) (sizeof sysmetrics / sizeof sysmetrics [0]))
        
struct
        
{
        
    int    Index ;
        
    TCHAR *       szLabel ;
        
   TCHAR *       szDesc ;
        
}
        
sysmetrics [] =
        
{
        
  SM_CXSCREEN,    TEXT ("SM_CXSCREEN"),            
        
                  TEXT ("Screen width in pixels"),
        
    SM_CYSCREEN, TEXT ("SM_CYSCREEN"),            
        
                         TEXT ("Screen height in pixels"),
        
    SM_CXVSCROLL, TEXT ("SM_CXVSCROLL"),           
        
                          TEXT ("Vertical scroll width"),
        
    SM_CYHSCROLL,  TEXT ("SM_CYHSCROLL"),           
        
                          TEXT ("Horizontal scroll height"),
        
    SM_CYCAPTION, TEXT ("SM_CYCAPTION"),           
        
                             TEXT ("Caption bar height"),
        
    SM_CXBORDER, TEXT ("SM_CXBORDER"),            
        
                          TEXT ("Window border width"),
        
    SM_CYBORDER,  TEXT ("SM_CYBORDER"),            
        
                          TEXT ("Window border height"),
        
    SM_CXFIXEDFRAME,TEXT ("SM_CXFIXEDFRAME"),        
        
                          TEXT ("Dialog window frame width"),
        
    SM_CYFIXEDFRAME,TEXT ("SM_CYFIXEDFRAME"),        
        
                          TEXT ("Dialog window frame height"),
        
    SM_CYVTHUMB,  TEXT ("SM_CYVTHUMB"),            
        
                         TEXT ("Vertical scroll thumb height"),
        
    SM_CXHTHUMB,    TEXT ("SM_CXHTHUMB"),            
        
                             TEXT ("Horizontal scroll thumb width"),
        
    SM_CXICON,       TEXT ("SM_CXICON"),             
        
                          TEXT ("Icon width"),
        
    SM_CYICON,       TEXT ("SM_CYICON"),              
        
                        TEXT ("Icon height"),
        
    SM_CXCURSOR,  TEXT ("SM_CXCURSOR"),            
        
                          TEXT ("Cursor width"),
        
    SM_CYCURSOR,    TEXT ("SM_CYCURSOR"),           
        
                          TEXT ("Cursor height"),
        
    SM_CYMENU,       TEXT ("SM_CYMENU"),              
        
                          TEXT ("Menu bar height"),
        
    SM_CXFULLSCREEN,TEXT ("SM_CXFULLSCREEN"),        
        
                          TEXT ("Full screen client area width"),
        
    SM_CYFULLSCREEN,TEXT ("SM_CYFULLSCREEN"),        
        
                          TEXT ("Full screen client area height"),
        
    SM_CYKANJIWINDOW,TEXT ("SM_CYKANJIWINDOW"),       
        
                           TEXT ("Kanji window height"),
        
    SM_MOUSEPRESENT, TEXT ("SM_MOUSEPRESENT"),        
        
                           TEXT ("Mouse present flag"),
        
    SM_CYVSCROLL,TEXT ("SM_CYVSCROLL"),           
        
                           TEXT ("Vertical scroll arrow height"),
        
    SM_CXHSCROLL,TEXT ("SM_CXHSCROLL"),           
        
                          TEXT ("Horizontal scroll arrow width"),
        
    SM_DEBUG,         TEXT ("SM_DEBUG"),               
        
                           TEXT ("Debug version flag"),
        
    SM_SWAPBUTTON,TEXT ("SM_SWAPBUTTON"),          
        
                           TEXT ("Mouse buttons swapped flag"),
        
    SM_CXMIN,         TEXT ("SM_CXMIN"),               
        
                           TEXT ("Minimum window width"),
        
    SM_CYMIN,         TEXT ("SM_CYMIN"),               
        
                           TEXT ("Minimum window height"),
        
    SM_CXSIZE,        TEXT ("SM_CXSIZE"),              
        
                           TEXT ("Min/Max/Close button width"),
        
    SM_CYSIZE,       TEXT ("SM_CYSIZE"),             
        
                           TEXT ("Min/Max/Close button height"),
        
    SM_CXSIZEFRAME,TEXT ("SM_CXSIZEFRAME"),         
        
                           TEXT ("Window sizing frame width"),
        
    SM_CYSIZEFRAME,TEXT ("SM_CYSIZEFRAME"),         
        
                           TEXT ("Window sizing frame height"),
        
    SM_CXMINTRACK,TEXT ("SM_CXMINTRACK"),          
        
                           TEXT ("Minimum window tracking width"),
        
    SM_CYMINTRACK,TEXT ("SM_CYMINTRACK"),          
        
                           TEXT ("Minimum window tracking height"),
        
    SM_CXDOUBLECLK,TEXT ("SM_CXDOUBLECLK"),         
        
                           TEXT ("Double click x tolerance"),
        
    SM_CYDOUBLECLK,TEXT ("SM_CYDOUBLECLK"),         
        
                           TEXT ("Double click y tolerance"),
        
    SM_CXICONSPACING,TEXT ("SM_CXICONSPACING"),       
        
                           TEXT ("Horizontal icon spacing"),
        
    SM_CYICONSPACING,TEXT ("SM_CYICONSPACING"),       
        
                           TEXT ("Vertical icon spacing"),
        
    SM_MENUDROPALIGNMENT,TEXT ("SM_MENUDROPALIGNMENT"),   
        
                                  TEXT ("Left or right menu drop"),
        
    SM_PENWINDOWS,       TEXT ("SM_PENWINDOWS"),          
        
                                  TEXT ("Pen extensions installed"),
        
    SM_DBCSENABLED,       TEXT ("SM_DBCSENABLED"),         
        
                                  TEXT ("Double-Byte Char Set enabled"),
        
    SM_CMOUSEBUTTONS,        TEXT ("SM_CMOUSEBUTTONS"),       
        
                                 TEXT ("Number of mouse buttons"),
        
    SM_SECURE,               TEXT ("SM_SECURE"),              
        
                                  TEXT ("Security present flag"),
        
    SM_CXEDGE,               TEXT ("SM_CXEDGE"),              
        
                                  TEXT ("3-D border width"),
        
    SM_CYEDGE,               TEXT ("SM_CYEDGE"),              
        
                                  TEXT ("3-D border height"),
        
    SM_CXMINSPACING,         TEXT ("SM_CXMINSPACING"),        
        
                                  TEXT ("Minimized window spacing width"),
        
    SM_CYMINSPACING,         TEXT ("SM_CYMINSPACING"),        
        
                                  TEXT ("Minimized window spacing height"),
        
    SM_CXSMICON,         TEXT ("SM_CXSMICON"),            
        
                                  TEXT ("Small icon width"),
        
    SM_CYSMICON,        TEXT ("SM_CYSMICON"),            
        
                                  TEXT ("Small icon height"),
        
    SM_CYSMCAPTION,       TEXT ("SM_CYSMCAPTION"),         
        
                                  TEXT ("Small caption height"),
        
    SM_CXSMSIZE,         TEXT ("SM_CXSMSIZE"),            
        
                                 TEXT ("Small caption button width"),
        
    SM_CYSMSIZE,         TEXT ("SM_CYSMSIZE"),            
        
                                  TEXT ("Small caption button height"),
        
    SM_CXMENUSIZE,       TEXT ("SM_CXMENUSIZE"),          
        
                                  TEXT ("Menu bar button width"),
        
    SM_CYMENUSIZE,       TEXT ("SM_CYMENUSIZE"),          
        
                                TEXT ("Menu bar button height"),
        
    SM_ARRANGE,              TEXT ("SM_ARRANGE"),             
        
                                  TEXT ("How minimized windows arranged"),
        
    SM_CXMINIMIZED,      TEXT ("SM_CXMINIMIZED"),         
        
                                  TEXT ("Minimized window width"),
        
    SM_CYMINIMIZED,       TEXT ("SM_CYMINIMIZED"),         
        
                                  TEXT ("Minimized window height"),
        
    SM_CXMAXTRACK,       TEXT ("SM_CXMAXTRACK"),         
        
                                  TEXT ("Maximum draggable width"),
        
    SM_CYMAXTRACK,       TEXT ("SM_CYMAXTRACK"),          
        
                                 TEXT ("Maximum draggable height"),
        
    SM_CXMAXIMIZED,       TEXT ("SM_CXMAXIMIZED"),         
        
                                  TEXT ("Width of maximized window"),
        
    SM_CYMAXIMIZED,       TEXT ("SM_CYMAXIMIZED"),         
        
                                  TEXT ("Height of maximized window"),
        
    SM_NETWORK,          TEXT ("SM_NETWORK"),             
        
                                  TEXT ("Network present flag"),
        
    SM_CLEANBOOT,         TEXT ("SM_CLEANBOOT"),           
        
                                  TEXT ("How system was booted"),
        
    SM_CXDRAG,               TEXT ("SM_CXDRAG"),              
        
                                  TEXT ("Avoid drag x tolerance"),
        
    SM_CYDRAG,               TEXT ("SM_CYDRAG"),              
        
                                  TEXT ("Avoid drag y tolerance"),
        
    SM_SHOWSOUNDS,        TEXT ("SM_SHOWSOUNDS"),          
        
                                  TEXT ("Present sounds visually"),
        
    SM_CXMENUCHECK,       TEXT ("SM_CXMENUCHECK"),         
        
                                 TEXT ("Menu check-mark width"),
        
    SM_CYMENUCHECK,       TEXT ("SM_CYMENUCHECK"),         
        
                                 TEXT ("Menu check-mark height"),
        
    SM_SLOWMACHINE,       TEXT ("SM_SLOWMACHINE"),         
        
                                  TEXT ("Slow processor flag"),
        
    SM_MIDEASTENABLED,       TEXT ("SM_MIDEASTENABLED"),      
        
                                  TEXT ("Hebrew and Arabic enabled flag"),
        
    SM_MOUSEWHEELPRESENT,TEXT ("SM_MOUSEWHEELPRESENT"),   
        
                                  TEXT ("Mouse wheel present flag"),
        
    SM_XVIRTUALSCREEN,       TEXT ("SM_XVIRTUALSCREEN"),      
        
                                  TEXT ("Virtual screen x origin"),
        
    SM_YVIRTUALSCREEN,       TEXT ("SM_YVIRTUALSCREEN"),      
        
                                  TEXT ("Virtual screen y origin"),
        
    SM_CXVIRTUALSCREEN,      TEXT ("SM_CXVIRTUALSCREEN"),     
        
                                  TEXT ("Virtual screen width"),
        
    SM_CYVIRTUALSCREEN,      TEXT ("SM_CYVIRTUALSCREEN"),     
        
                                  TEXT ("Virtual screen height"),
        
    SM_CMONITORS,       TEXT ("SM_CMONITORS"),           
        
                                TEXT ("Number of monitors"),
        
    SM_SAMEDISPLAYFORMAT,TEXT ("SM_SAMEDISPLAYFORMAT"),   
        
                                  TEXT ("Same color format flag")
        
} ;
        

显示信息的程序命名为SYSMETS1。SYSMETS1.C的原始码如程序4-2所示。现在大多数程序代码看起来都很熟悉。WinMain中的程序代码实际上与HELLOWIN中的程序代码相同,并且WndProc中的大部分程序代码都已经讨论过了。

程序4-2 SYSMETS1.C
        
/*------------------------------------------------------------------
        
SYSMETS1.C -- System Metrics Display Program No. 1
        
                 (c) Charles Petzold, 1998
        
----------------------------------------------------------------*/
        
#include <windows.h>
        
#include "sysmets.h"
        
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
        
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
        
                 PSTR szCmdLine, int iCmdShow)
        
{
        
    static TCHAR szAppName[] = TEXT ("SysMets1") ;
        
    HWND   hwnd ;
        
    MSG   msg ;
        
    WNDCLASS      wndclass ;
        
    wndclass.style       = CS_HREDRAW | CS_VREDRAW ;
        
   wndclass.lpfnWndProc  = WndProc ;
        
   wndclass.cbClsExtra   = 0 ;
        
   wndclass.cbWndExtra   = 0 ;
        
   wndclass.hInstance    = hInstance ;
        
   wndclass.hIcon        = LoadIcon (NULL, IDI_APPLICATION) ;
        
    wndclass.hCursor      = LoadCursor (NULL, IDC_ARROW) ;
        
    wndclass.hbrBackground        = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
        
    wndclass.lpszMenuName         = NULL ;
        
    wndclass.lpszClassName        = szAppName ;
        
   
        
    if (!RegisterClass (&wndclass))
        
    {
        
                 MessageBox (  NULL, TEXT ("This program requires Windows NT!"),
        
                          szAppName, MB_ICONERROR) ;
        
  
        
    return 0 ;
        
    }
        
    hwnd = CreateWindow (szAppName, TEXT ("Get System Metrics No. 1"),
        
                   WS_OVERLAPPEDWINDOW,
        
                   CW_USEDEFAULT, CW_USEDEFAULT,
        
                   CW_USEDEFAULT, CW_USEDEFAULT,
        
                   NULL, NULL, hInstance, NULL) ;
        
    ShowWindow (hwnd, iCmdShow) ;
        
    UpdateWindow (hwnd) ;
        
    while (GetMessage (&msg, NULL, 0, 0))
        
                  {
        
      TranslateMessage (&msg) ;
        
      DispatchMessage (&msg) ;
        
             }
        
    return msg.wParam ;
        
     }
        

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
        
{
        
    static int    cxChar, cxCaps, cyChar ;
        
    HDC                   hdc ;
        
    int                   i ;
        
    PAINTSTRUCT ps ;     
        
    TCHAR                szBuffer [10] ;
        
    TEXTMETRIC  tm ;
        

    switch (message)
        
    {
        
   case   WM_CREATE:
        
         hdc = GetDC (hwnd) ;
        

          GetTextMetrics (hdc, &tm) ;
        
          cxChar = tm.tmAveCharWidth ;
        
          cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2 ;
        
          cyChar = tm.tmHeight + tm.tmExternalLeading ;
        

          ReleaseDC (hwnd, hdc) ;
        
          return 0 ;
        

    case   WM_PAINT :
        
          hdc = BeginPaint (hwnd, &ps) ;
        

          for (i = 0 ; i < NUMLINES ; i++)
        
                  {
        
            TextOut       (hdc, 0, cyChar * i,                    
        
                         sysmetrics[i].szLabel,
        
                         lstrlen (sysmetrics[i].szLabel)) ;
        

            TextOut       (hdc, 22 * cxCaps, cyChar * i,    
        
                         sysmetrics[i].szDesc,
        
                         lstrlen (sysmetrics[i].szDesc)) ;
        

                   SetTextAlign (hdc, TA_RIGHT | TA_TOP) ;
        
        TextOut (hdc, 22 * cxCaps + 40 * cxChar, cyChar * i,   szBuffer,
        
    wsprintf (szBuffer, TEXT ("%5d"),
        
    GetSystemMetrics (sysmetrics[i].iIndex))) ;
        
              SetTextAlign (hdc, TA_LEFT | TA_TOP) ;
        
         }
        
            EndPaint (hwnd, &ps) ;
        
            return 0 ;
        
    case   WM_DESTROY :
        
            PostQuitMessage (0) ;
        
    return 0 ;
        
    }
        
    return DefWindowProc (hwnd, message, wParam, lParam) ;
        
}
        

图4-4显示了在标准VGA上执行的SYSMETS1。在程序显示区域的前两行可以看到,屏幕宽度是640个图素,屏幕高度是480个图素,这两个值以及程序所显示的其它值可能会因视讯显示器型态的不同而不同。