《Windows32 SDK教程》05章 指定位置显示文字


要在指定位置显示文字,用的更多的是TextOut()函数。它与DrawText()函数特点不同,作用也不同。

一、TextOut()函数的基本用法

TextOut()函数既不处理回车符、TAB键等,也不自动回行,以指定的位置作基准点,在其附近显示,默认时基准点是这一行字的左上角。要知道当前文字的对齐方式,可以用GetTextAlign()函数,本章并不讲解这个函数,而讲解与之相对的SetTextAlign()函数。

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{;
    PAINTSTRUCT ps;
    HDC hdc;
    TCHAR str1[64]="Wecome you to \nHttp://www.quanxue.cn/!";   //中间有回车“\n”
    TCHAR str2[64]="Wecome you to   Http://www.quanxue.cn/!";   //中间有TAB键

    switch (message)
    {
        case WM_PAINT:
            hdc = BeginPaint(hWnd, &ps);

            SetTextColor(hdc, 0x0000FF);
            SetBkColor(hdc, 0xCDFAFF);
            TextOut(hdc,30,20,str1,(int)strlen(str1));
            TextOut(hdc,30,50,str2,(int)strlen(str2));

            EndPaint(hWnd, &ps);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

二、文字的对齐方式

SetTextAlign()函数用来设置文字的对齐方式。下面我们以窗口正中间作为基准点来了解这个函数基本内容。效果图中的红点是运行后截图时标出的,告诉你基准点的位置。

说明
TA_BASELINE
(VTA_BASELINE)
The reference point will be on the base line of the text.
TA_BOTTOM下对齐
TA_TOP上对齐
TA_CENTER
(VTA_CENTER)
The reference point will be aligned horizontally with the center of the bounding rectangle
TA_LEFT左对齐
TA_RIGHT右对齐
TA_NOUPDATECPThe current position is not updated after each text output call. The reference point is passed to the text output function.
TA_RTLREADING(中东阿拉伯用)The text is laid out in right to left reading order, as opposed to the default left to right order. This applies only when the font selected into the device context is either Hebrew or Arabic.
TA_UPDATECPThe current position is updated after each text output call. The current position is used as the reference point.
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    TCHAR strLeft[64] = "文字左对齐";
    TCHAR strTop[64] = "文字上对齐";
    TCHAR strRightBottom[64] = "文字右下对齐";
    TCHAR strCenter[64] = "文字中对齐";
    PAINTSTRUCT ps;
    HDC hdc;
    RECT rc;
    int x,y;

    switch (message)
    {
        case WM_PAINT:
            hdc = BeginPaint(hWnd, &ps);
            GetClientRect(hWnd, &rc);
            x = rc.right / 2, y = rc.bottom / 2;
            //这2句是在窗口中间画横竖二条线,后面章节有详细说明
            MoveToEx(hdc,x,0,NULL), LineTo(hdc,x,rc.bottom);
            MoveToEx(hdc,0,y,NULL), LineTo(hdc,rc.right,y);

            SetTextAlign(hdc, TA_LEFT);
            TextOut(hdc,x, 10, strLeft,(int)strlen(strLeft));

            SetTextAlign(hdc, TA_BOTTOM|TA_RIGHT);
            TextOut(hdc,x,rc.bottom - 10, strRightBottom,(int)strlen(strRightBottom));

            SetTextAlign(hdc, TA_TOP);
            TextOut(hdc,10,y, strTop,(int)strlen(strTop));

            SetTextAlign(hdc, TA_BOTTOM|TA_RIGHT);
            TextOut(hdc,rc.right - 10,y, strRightBottom,(int)strlen(strRightBottom));

            SetTextAlign(hdc, VTA_CENTER);
            TextOut(hdc,x,y, strCenter,(int)strlen(strCenter));

            EndPaint(hWnd, &ps);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

三、设置文字的字间距

设置文字的字间距用SetTextCharacterExtra()函数,看一下例子就基本明白了。

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    TCHAR strLeft[64] = "文字左对齐";
    TCHAR strTop[64] = "文字上对齐";
    TCHAR strRightBottom[64] = "文字右下对齐";
    TCHAR strCenter[64] = "文字中对齐";
    PAINTSTRUCT ps;
    HDC hdc;
    RECT rc;
    int x,y,z;

    switch (message)
    {
        case WM_PAINT:
            hdc = BeginPaint(hWnd, &ps);
            GetClientRect(hWnd, &rc);
            x = rc.right / 2, y = rc.bottom / 2;
            //这2句是在窗口中间画横竖二条线,后面章节有详细说明
            MoveToEx(hdc,x,0,NULL), LineTo(hdc,x,rc.bottom);
            MoveToEx(hdc,0,y,NULL), LineTo(hdc,rc.right,y);

            z = SetTextCharacterExtra(hdc, 2);
            SetTextAlign(hdc, TA_LEFT);
            TextOut(hdc,x, 10, strLeft,(int)strlen(strLeft));

            SetTextCharacterExtra(hdc, 4);
            SetTextAlign(hdc, TA_BOTTOM|TA_RIGHT);
            TextOut(hdc,x,rc.bottom - 10, strRightBottom,(int)strlen(strRightBottom));

            SetTextCharacterExtra(hdc, 6);
            SetTextAlign(hdc, TA_TOP);
            TextOut(hdc,10,y, strTop,(int)strlen(strTop));

            SetTextCharacterExtra(hdc, 8);
            SetTextAlign(hdc, TA_BOTTOM|TA_RIGHT);
            TextOut(hdc,rc.right - 10,y, strRightBottom,(int)strlen(strRightBottom));

            SetTextCharacterExtra(hdc, -1);
            SetTextAlign(hdc, VTA_CENTER|VTA_BASELINE);
            TextOut(hdc,x,y, strCenter,(int)strlen(strCenter));

            SetTextCharacterExtra(hdc, z);    //恢复原来的字间距
            EndPaint(hWnd, &ps);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}