windows程序设计——输出文字

通常windows程序以响应WM_PAINT消息的方式而不是主动绘制,在客户区中输出文字和图形来向用户展示信息。在发生下面几种事件之一时,视窗讯息处理程式会接收到一个WM_PAINT 消息:
1.在使用者移动视窗或显示视窗时,视窗中先前被隐藏的区域重新可见。

2.使用者改变视窗的大小(如果视窗类别样式有著CS_HREDRAW 和CS_VREDRAW位元旗标的设定)。

3.程式使用ScrollWindow 或ScrollDC 函式滚动显示区域的一部分。

4.程式使用InvalidateRect或InvalidateRgn函式刻意产生WM_PAINT讯息。 

在某些情况下,显示区域的一部分被临时覆盖,Windows 试图保存一个显示区域,并在以後恢复它,但这不一定能成功。在以下情况下,Windows 可能发送WM_PAINT 讯息:

1.Windows 擦除覆盖了部分视窗的对话方块或讯息方块。

2.功能表下拉出来,然後被释放。

3.显示工具提示讯息。

而滑鼠游标穿越显示区域或者图示拖过显示区域时windows总是能够保存成功,故而不会产生WM_PAINT 讯息。

Windows之所以会产生WM_PAINT 消息是由于系统或者程序设计者在一些条件下需要重新绘制一部分或者全部,需要重新绘制的这部分就称为无效区域,保存在一个矩形结构中。为提高绘制效率窗口值保存一个无效矩形,不需要每次更新一小块,通常在消息队列中存在WM_PAINT时,再次发送WM_PAINT消息只更新无效矩形而不会再次插入一条WM_PAINT消息。并需要在处理WM_PAINT消息时把无效区域设为有效。

与其他windows系统的API一样绘制接口并不会直接操作设备对象,而且操作设备句柄。更新界面时就使用绘图句柄,在指定的区域上绘制图形或文字。windows为每一个窗体保存了一个绘图结构:

typedef struct tagPAINTSTRUCT
{
    HDC  hdc ; //绘图句柄
    BOOL fErase ; //是否擦除背景
    RECT rcPaint ; //无效矩形
    BOOL fRestore ; //系统内部使用
    BOOL fIncUpdate ; //系统内部使用
    BYTE rgbReserved[32] ; //系统内部使用
}PAINTSTRUCT ;
其中fErase指定是否擦除背景,若擦除则用wndclass.hbrBackground指定的画刷刷新背景或者在处理WM_ERASEBKGND消息时自定义背景擦除动作。是否擦除一般由函数InvalidateRect的最后一个参数决定。无效矩形只在绘图是有意义,获取和使用hdc的方法有两个:

//1.在处理WM_PAINT消息时
case WM_PAINT:
    hdc = BeginPaint (hwnd, &ps) ;
    //使用GDI 函式
    EndPaint (hwnd, &ps) ;
    return 0 ;

//2.非WM_PAINT消息中
hdc = GetDC (hwnd) ;
//使用GDI 函式
ReleaseDC (hwnd, hdc) ;
方法1中的ps,即为PAINTSTRUCT结构的对象,故在处理WM_PAINT消息时可以获得无效矩形的大小,而方法2的获得的矩形为整个客户区的大小。并BeginPaint,EndPaint和GetDC,ReleaseDC必须成对出现,既使用完一个DC之后必须释放它。

输出文字为最简单的绘图方式:

/*-----------------------------------------------
   SYSMETS.H -- System metrics display structure
  -----------------------------------------------*/

#define NUMLINES ((int) (sizeof sysmetrics / sizeof sysmetrics [0]))

struct
{
     int     iIndex ;
     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.C -- System Metrics Display Program No. 1
                 (c) Charles Petzold, 1998
  ----------------------------------------------------*/

#define WINVER 0x0500
#include 
#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:
          //获取DC
          hdc = GetDC (hwnd) ;
          //获取系统字体大小
          GetTextMetrics (hdc, &tm) ;
          cxChar = tm.tmAveCharWidth ; //均宽
          cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2 ; //大写字母均宽 可变大小时为均宽*1.5,固定时cxChar
          cyChar = tm.tmHeight + tm.tmExternalLeading ; //总高 字高+外间距
          //释放DC
          ReleaseDC (hwnd, hdc) ;
          return 0 ;

     case WM_PAINT :
          //获取DC
          hdc = BeginPaint (hwnd, &ps) ;

          for (i = 0 ; i < NUMLINES ; i++)
          {
			   //绘制 label
               TextOut (hdc, 0, cyChar * i,                      
                        sysmetrics[i].szLabel,
                        lstrlen (sysmetrics[i].szLabel)) ;
               //绘制 szDesc
               TextOut (hdc, 22 * cxCaps, cyChar * i,      
                        sysmetrics[i].szDesc,
                        lstrlen (sysmetrics[i].szDesc)) ;
               //设置显示为对齐靠右,靠上对齐
               SetTextAlign (hdc, TA_RIGHT | TA_TOP) ;
               //输出index
               TextOut (hdc, 22 * cxCaps + 40 * cxChar, cyChar * i, szBuffer,
                        wsprintf (szBuffer, TEXT ("%5d"),
                             GetSystemMetrics (sysmetrics[i].iIndex))) ;
               //设置显示为对齐靠左,靠上对齐
               SetTextAlign (hdc, TA_LEFT | TA_TOP) ;
          }
          //释放DC
          EndPaint (hwnd, &ps) ;
          return 0 ;

     case WM_DESTROY :
          PostQuitMessage (0) ;
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}
GetTextMetrics获得的系统字体大小结构如下:

typedef struct tagTEXTMETRIC
{
	LONG tmHeight ;
	LONG tmAscent ;
	LONG tmDescent ;
	LONG tmInternalLeading ;
	LONG tmExternalLeading ;
	LONG tmAveCharWidth ;
	LONG tmMaxCharWidth ;
	.....
}
TEXTMETRIC, * PTEXTMETRIC ;
对应如下图结构:

windows程序设计——输出文字_第1张图片

使用TextOut函数输出文字:

TextOut (
    hdc, //绘图句柄,保存了字体,颜色等信息
    x, //逻辑坐标 x,与系统采用的坐标系有关
    y, //逻辑坐标 y,与系统采用的坐标系有关
    psText, //输出文字
    iLength) ; //文字长度
绘图过程中超出的客户显示区的部分不会被显示。为解决这个问题可以在窗口中加入滚动条来实现。
/*----------------------------------------------------
   SYSMETS2.C -- System Metrics Display Program No. 2
                 (c) Charles Petzold, 1998
  ----------------------------------------------------*/

#define WINVER 0x0500
#include 
#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 ("SysMets2") ;
     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. 2"),
                          WS_OVERLAPPEDWINDOW | WS_VSCROLL,
                          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, cyClient, iVscrollPos ;
     HDC         hdc ;
     int         i, y ;
     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) ;
          //设置滚动范围
          SetScrollRange (hwnd, SB_VERT, 0, NUMLINES - 1, FALSE) ;
          //设置滚动条的位置
          SetScrollPos   (hwnd, SB_VERT, iVscrollPos, TRUE) ;
          return 0 ;

     case WM_SIZE: //处理窗口大小改变的信息
          //获取客户区高度
          cyClient = HIWORD (lParam) ;
          return 0 ;

     case WM_VSCROLL://处理纵向滚动条滚动消息
          switch (LOWORD (wParam))
          {
          case SB_LINEUP: //往上翻一行(单击滚动条的按钮)
               iVscrollPos -= 1 ;
               break ;
     
          case SB_LINEDOWN://往下翻一行(单击滚动条的按钮)
               iVscrollPos += 1 ;
               break ;
     
          case SB_PAGEUP://往上翻一页(单击滚动条的空白处)
               iVscrollPos -= cyClient / cyChar ;
               break ;
     
          case SB_PAGEDOWN://往下翻一页(单击滚动条的空白处)
               iVscrollPos += cyClient / cyChar ;
               break ;
     
          case SB_THUMBPOSITION://直接定位滚动条位置(拖动)
               iVscrollPos = HIWORD (wParam) ;
               break ;
     
          default :
               break ;
          }
          //确保位置在滚动范围之内
          iVscrollPos = max (0, min (iVscrollPos, NUMLINES - 1)) ;
          //如果滚动条的位置发生了变化
          if (iVscrollPos != GetScrollPos (hwnd, SB_VERT))
          {
               //设置滚动条位置
               SetScrollPos (hwnd, SB_VERT, iVscrollPos, TRUE) ;
               //刷新界面
               InvalidateRect (hwnd, NULL, TRUE) ;
          }
          return 0 ;

     case WM_PAINT:
          hdc = BeginPaint (hwnd, &ps) ;
     
          for (i = 0 ; i < NUMLINES ; i++)
          {
               //计算每个item的位置,只有0 < y < cyClient,才会被显示出来,其他的显示在了客户区之外
               y = cyChar * (i - iVscrollPos) ;
     
               TextOut (hdc, 0, y,
                        sysmetrics[i].szLabel,
                        lstrlen (sysmetrics[i].szLabel)) ;
     
               TextOut (hdc, 22 * cxCaps, y,
                        sysmetrics[i].szDesc,
                        lstrlen (sysmetrics[i].szDesc)) ;
     
               SetTextAlign (hdc, TA_RIGHT | TA_TOP) ;
     
               TextOut (hdc, 22 * cxCaps + 40 * cxChar, y, 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) ;
}
对比SYSMETS1的代码发现,想要在窗口中增加纵向滚动条的支持只需要在CreateWindow的时候设置WS_VSCROLL风格,则系统自动为窗口添加了滚动条支持。

win32API新增了SetScrollInfo和GetScrollInfo来更方便的处理滚动条信息。

/*----------------------------------------------------
   SYSMETS3.C -- System Metrics Display Program No. 3
                 (c) Charles Petzold, 1998
  ----------------------------------------------------*/

#define WINVER 0x0500
#include 
#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 ("SysMets3") ;
     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 ("Program requires Windows NT!"), 
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }
     
     hwnd = CreateWindow (szAppName, TEXT ("Get System Metrics No. 3"),
                          WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,
                          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, cxClient, cyClient, iMaxWidth ;
     HDC         hdc ;
     int         i, x, y, iVertPos, iHorzPos, iPaintBeg, iPaintEnd ;
     PAINTSTRUCT ps ;
     SCROLLINFO  si ;
     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) ;

          //三列需要的宽度
          iMaxWidth = 40 * cxChar + 22 * cxCaps ;
          return 0 ;
          
     case WM_SIZE:
          cxClient = LOWORD (lParam) ;
          cyClient = HIWORD (lParam) ;

          //通过si设置纵向滚动条范围和页大小
          si.cbSize = sizeof (si) ;
          si.fMask  = SIF_RANGE | SIF_PAGE ;
          si.nMin   = 0 ;
          si.nMax   = NUMLINES - 1 ;
          si.nPage  = cyClient / cyChar ;
          SetScrollInfo (hwnd, SB_VERT, &si, TRUE) ;

          //通过si设置横向滚动条范围和页大小
          si.cbSize = sizeof (si) ;
          si.fMask  = SIF_RANGE | SIF_PAGE ;
          si.nMin   = 0 ;
          si.nMax   = 2 + iMaxWidth / cxChar ;
          si.nPage  = cxClient / cxChar ;
          SetScrollInfo (hwnd, SB_HORZ, &si, TRUE) ;
          return 0 ;
          
     case WM_VSCROLL:
          //获取纵向滚动条信息
          si.cbSize = sizeof (si) ;
          si.fMask  = SIF_ALL ;
          GetScrollInfo (hwnd, SB_VERT, &si) ;

          //从si中取出当前位置
          iVertPos = si.nPos ;
          
		  //根据消息类型计算位置
          switch (LOWORD (wParam))
          {
          case SB_TOP: //滚动到顶端
               si.nPos = si.nMin ;
               break ;
               
          case SB_BOTTOM: //滚动到底部
               si.nPos = si.nMax ;
               break ;
               
          case SB_LINEUP: //往上翻一行(单击滚动条的按钮)
               si.nPos -= 1 ;
               break ;
               
          case SB_LINEDOWN: //往下翻一行(单击滚动条的按钮)
               si.nPos += 1 ;
               break ;
               
          case SB_PAGEUP: //往上翻一页(单击滚动条的空白处)
               si.nPos -= si.nPage ;
               break ;
               
          case SB_PAGEDOWN: //往下翻一页(单击滚动条的空白处)
               si.nPos += si.nPage ;
               break ;
               
          case SB_THUMBTRACK: //直接拖动滚动方块(并未释放)
               si.nPos = si.nTrackPos ;
               break ;
               
          default:
               break ;         
          }
          //设置pos信息,并取出设置之后的si信息
          si.fMask = SIF_POS ;
          SetScrollInfo (hwnd, SB_VERT, &si, TRUE) ;
          GetScrollInfo (hwnd, SB_VERT, &si) ;

          //如果滚动条的位置发生了变化
          if (si.nPos != iVertPos)
          {
               //滚动窗口绘图区域
               ScrollWindow (hwnd, 0, cyChar * (iVertPos - si.nPos), NULL, NULL) ;
               UpdateWindow (hwnd) ;
          }
          return 0 ;
          
     case WM_HSCROLL: //与纵向滚动条处理类似
          // Get all the vertial scroll bar information
          si.cbSize = sizeof (si) ;
          si.fMask  = SIF_ALL ;

          // Save the position for comparison later on
          GetScrollInfo (hwnd, SB_HORZ, &si) ;
          iHorzPos = si.nPos ;

          switch (LOWORD (wParam))
          {
          case SB_LINELEFT:
               si.nPos -= 1 ;
               break ;
               
          case SB_LINERIGHT:
               si.nPos += 1 ;
               break ;
               
          case SB_PAGELEFT:
               si.nPos -= si.nPage ;
               break ;
               
          case SB_PAGERIGHT:
               si.nPos += si.nPage ;
               break ;
               
          case SB_THUMBPOSITION:
               si.nPos = si.nTrackPos ;
               break ;
               
          default :
               break ;
          }
          // Set the position and then retrieve it.  Due to adjustments
          // by Windows it may not be the same as the value set.
          si.fMask = SIF_POS ;
          SetScrollInfo (hwnd, SB_HORZ, &si, TRUE) ;
          GetScrollInfo (hwnd, SB_HORZ, &si) ;
          
          // If the position has changed, scroll the window 
          if (si.nPos != iHorzPos)
          {
               ScrollWindow (hwnd, cxChar * (iHorzPos - si.nPos), 0, NULL, NULL) ;
          }
          return 0 ;

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

          //获取纵向滚动条的位置
          si.cbSize = sizeof (si) ;
          si.fMask  = SIF_POS ;
          GetScrollInfo (hwnd, SB_VERT, &si) ;
          iVertPos = si.nPos ;

          //获取横向滚动条的位置
          GetScrollInfo (hwnd, SB_HORZ, &si) ;
          iHorzPos = si.nPos ;

          //找到需要能在客户区显示的item
          iPaintBeg = max (0, iVertPos + ps.rcPaint.top / cyChar) ;
          iPaintEnd = min (NUMLINES - 1, iVertPos + ps.rcPaint.bottom / cyChar) ;
          //只画需要显示的item
          for (i = iPaintBeg ; i <= iPaintEnd ; i++)
          {
               x = cxChar * (1 - iHorzPos) ;
               y = cyChar * (i - iVertPos) ;
               
               TextOut (hdc, x, y, sysmetrics[i].szLabel, lstrlen (sysmetrics[i].szLabel)) ;
               TextOut (hdc, x + 22 * cxCaps, y, sysmetrics[i].szDesc, lstrlen (sysmetrics[i].szDesc)) ;
               SetTextAlign (hdc, TA_RIGHT | TA_TOP) ;
               TextOut (hdc, x + 22 * cxCaps + 40 * cxChar, y, 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) ;
}
虽然使用了ScrollWindow函数当最后两个参数设置为NULL,依然是整个客户区设置为无效。ScrollWindow函数说明如下:

BOOL ScrollWindow(
    HWND hWnd,  //客户区域将被滚动的窗口的句柄。
    int XAmount,//指定水平滚动的距离,以设备单位计。
    int YAmount, //指定垂直滚动的距离,以设备单位计。
    CONST RECT *IpRect, //指向RECT结构的指针,该结构指定了将要滚动的客户区范围。若此参数为NULL,则整个客户区域将被滚动。
    CONST RECT *lpClipRect); //指向RECT结构的指针,该结构指定了要滚动的裁剪区域。




















你可能感兴趣的:(Win笔记)