zoukankan      html  css  js  c++  java
  • 【Windows程序设计】与文字输出相关的函数及使用方法

    1、TextOut( hdc, x, y, psText, iLength); 

    是最常见的文本输出函数。它向client area输出一段文本。psText是指向字符串的一个指针,iLength代表字符串的长度。x和y分别代表文本输出的起始位置。hdc代表handle to a device context。

    2、获取Device context handle

    方法一:

    hdc = BeginPaint(hwnd, &ps);

    EndPaint(hwnd, &ps);

    这两组函数是成对出现的。

    BeginPaint函数使用的注意事项:

    The BeginPaint function sets the clipping region of the device context to exclude any area outside the update region.

    The update region is set by the InvalidateRect function and by the system after sizing, moving, creating, scrolling, or any other operation that affects the client area.

    If the update region is marked for erasing, BeginPaint sends a WM_ERASEBKGND message to the window.

    An application should not call BeginPaint except in response to a WM_PAINT message. //响应WM_PAINT消息时,才可以调用BeginPaint函数

    Each call to BeginPaint must have a corresponding call to the EndPaint function.

    If the caret is in the area to be painted, BeginPaint hides the caret to prevent it from being erased.

    If the window's class has a background brush, BeginPaint uses that brush to erase the background of the update region before returning.

    方法二:

    hdc = GetDC(hwnd);

    ReleaseDC(hwnd, hdc);

    这两组函数也是成对出现。

    GetDC使用注意事项:

    The GetDC function retrieves a common, class, or private device context depending on the class style specified for the specified window.

    For common device contexts, GetDC assigns default attributes to the device context each time it is retrieved.

    For class and private device contexts, GetDC leaves the previously assigned attributes unchanged.

    After painting with a common device context, the ReleaseDC function must be called to release the device context.

    Class and private device contexts do not have to be released.

    The number of device contexts is limited only by available memory.

    3、Paint Information Structure

    typedef struct tagPAINTSTRUCT {  
     HDC hdc;  
     BOOL fErase;  
     RECT rcPaint; 
     BOOL fRestore;   
     BOOL fIncUpdate;  
     BYTE rgbReserved[32];
     } PAINTSTRUCT;

    调用BeginPaint函数时,Windows填充该结构体的各个值。该结构体的前三个变量是允许我们使用的,后面的变量windows自身使用。hdc代表device context handler,fErase指示是否需要擦除无效区域的背景部分,若为0表示不擦除,若为1表示擦除。rcPaint类型为RECT,它定义了无效区域的边界。这个无效区域就是需要重绘的区域。rcPaint还代表clipping rectangle。

    4、InvalidateRect(hwnd, NULL, TRUE);

    它使得整个的client area区域无效。

    5、GetTextMetrics(hdc, &tm);

    当使用TextOut函数输出多行文本时,需要知道文字的属性,例如文字的高度,宽度,字体等,这样有助于我们在显示器上整齐排列文本。GetTextMetrics函数可获取文本的这些属性,并把这些值保存在结构体变量tm中。

    typedef struct tagTEXTMETRIC { 
      LONG tmHeight;  
      LONG tmAscent; 
      LONG tmDescent; 
      LONG tmInternalLeading; 
      LONG tmExternalLeading; 
      LONG tmAveCharWidth;    
      LONG tmMaxCharWidth;  
    ... } TEXTMETRIC; 

    TEXTMETRIC的属性很多,这里只列出经常使用的几个。

    因为某个窗口的字体属性一般不会变化,所以一般在WM_CREATE消失中获取字体的相关信息,并保存在某一静态变量中。

    6、格式化输出文本

    程序中常常需要显示格式化的数字跟简单的字符串。Windows中不能使惯用的工具(可爱的printf函数)来完成这项工作,但是可以使用sprintf和Windows版的sprintf-wsprintf。这些函数与printf相似,只是把格式化字符串放到字符串中。然后,可以用TextOut将字符串输出到显示器上。非常方便的是,从sprintf和wsprintf传回的值就是字符串的长度。您可以将这个值传递给TextOut作为iLength参数,即TextOut的最后一个变量。

    TextOut (hdc, x, y, szBuffer, wsprintf (szBuffer, TEXT ("The sum of %i and %i is %i"), iA, iB, iA + iB)) ;
    7、SetTextAlign(hdc, TA_RIGHT | TA_TOP);
    格式化输出文本,TA_RIGHT代表右对齐
    8、WM_SIZE消息
    当window的size发生变化时,Windows会发送WM_SIZE消息,其中消息的lParam参数包含client area的宽度和高度。其中低16位存储宽度,高16位存储高度。
    可使用宏LOWORD和HIWORD分别获取其低位与高位数据。
    case WM_SIZE:
           cxClient = LOWORD(lParam);
           cyClient = HIWORD(lParam);

    9、SetScrollRange(hwnd, iBar, iMin, iMax, bRedraw);

    默认情况下,scroll bar的range是0到100,iBar代表SB_VERT或SB_HORZ, iMin和iMax是新的range,bRedraw为TRUE时,Windows需要按照新的range重绘scroll bar。

    10、SetScrollPos(hwnd, iBar, iPos, bRedraw);

    该函数可以重新设置thumb的位置, iPos是range内的一个数。

    11、Scroll Bar Message

    当鼠标点击scroll bar或者拖动thumb时,Windows会发送WM_VSCROLL或者WM_HSCROLL消息。这里我们可忽略lParam参数,只分析wParam参数。wParam的低16为一个数字,它指示鼠标对scroll bar进行了什么操作,这个值又称为notification code。

    12、SetScrollInfo和GetScrollInfo函数

    SetScrollInfo(hwnd, iBar, &si, bRedraw);
    GetScrollInfo(hwnd, iBar, &si);

    iBar参数是SB_VERT或SB_HORZ,它还可以是用于滚动条控制的SB_CTL。SetScrollInfo的最后一个参数可以是TRUE或FALSE,指出了是否要Windows重新绘制计算了新信息后的滚动条。两个函数的第三个参数是SCROLLINFO结构,定义为:

    typedef struct tagSCROLLINFO
            
    {        
        UINT cbSize ;// set to sizeof (SCROLLINFO)
        UINT fMask ;  // values to set or get
        int  nMin ;      // minimum range value
        int  nMax ;   // maximum range value
        UINT nPage ;  // page size
        int  nPos ;   // current position
        int  nTrackPos ;// current tracking position
    }
    SCROLLINFO, * PSCROLLINFO ;

    在调用SetScrollInfo或GetScrollInfo之前,必须将cbSize字段设定为结构的大小:si.cbSize = sizeof(si); 把fMask字段设定为一个以上以SIF前缀开头的旗标,并且可以使用C的位操作OR运算子(|)组合这些旗标。

  • 相关阅读:
    【HTML】使用css3和html给网站添加上春节灯笼特效
    【CSS】学习笔记2 字体设置
    【CSS】学习笔记3 段落设置
    【CSS】学习笔记1 使用CSS样式表
    正则
    图片懒加载
    uni-app-组件
    uni-app-condition(条件)启动模式
    uni-app-页面
    uni-app项目目录和开发规范
  • 原文地址:https://www.cnblogs.com/qi09/p/2375851.html
Copyright © 2011-2022 走看看