zoukankan      html  css  js  c++  java
  • 文本输出方法与字体的设置

    学习目的

    掌握获取字体句柄的两种方法;调用CreateFont函数创建自定义字体; 调用SetTextColor和SetBkColor函数设置字体的颜色和背景色; 掌握文本输出的过程(获取字体信息, 格式化文本, 输出文本).

     

    1. 掌握获取字体句柄的方法:

    HFONT hF; //定义字体句柄变量hF

    hF=GetStockObject(); //获得系统字体句柄

    SelectObject(hdc,hF); //将字体选入设备环境

     

    2. CreateFont函数创建自定义字体

    HFont = CreateFont(

    int nHeight, // height of font

    int nWidth, // average character width

    int nEscapement, // angle of escapement

    int nOrientation, // base-line orientation angle

    int fnWeight, // font weight

    DWORD fdwItalic, // italic attribute option

    DWORD fdwUnderline, // underline attribute option

    DWORD fdwStrikeOut, // strikeout attribute option

    DWORD fdwCharSet, // character set identifier

    DWORD fdwOutputPrecision, // output precision

    DWORD fdwClipPrecision, // clipping precision

    DWORD fdwQuality, // output quality

    DWORD fdwPitchAndFamily, // pitch and family

    LPCTSTR lpszFace // typeface name

    );

    3.设置字体的颜色和背景色

    SetTextColor(hdc,crColor); //设置字体颜色

    SetBkColor(hdc,crColor); //设置背景色色

    4. 掌握文本输出的过程

    (1)获取字体信息

    GetTextMetrics(hdc,&tm);

    (2) 格式化文本

    用GetTextExtentPoint32()确定后续文本的坐标,然后根据当前字体信息确定换行后的坐标。

    (3) 输出文本

    使用TextOut或者DrawText输出文本:

    BOOL TextOut(
    

      HDC
    										hdc,           // handle to DC
    

      int
    										nXStart,       // x-coordinate of starting position
    

      int
    										nYStart,       // y-coordinate of starting position
    

      LPCTSTR
    										lpString,  // character string
    

    
    									 int
    									cbString       // number of characters
    

    );
    								

    int DrawText(  
    

    HDC hDC,          // handle to DC
    

      LPCTSTR lpString, // text to draw  
    									

    int nCount,       // text length
    									

      LPRECT lpRect,    // formatting dimensions
    									

      UINT uFormat
    									 // text-drawing options
    									

    );
    

    5.编写程序: 在窗口上设计一行文字, 要求文字能在窗口中向左滚动显示, 而且每显示一轮, 改变一次颜色和字体.

    程序主要代码如下:

    static int Flag=0;

    int X=0;

    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

    {

        int wmId, wmEvent;

        PAINTSTRUCT ps;

        HDC hDC;

        RECT clientRect;

        //TCHAR szHello[MAX_LOADSTRING];

        //LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

        HFONT hF;            

        TEXTMETRIC tm;    

        char text[]="Windows 程序设计实验三";        //输出的字符串.

        char chFont[7];                                //定义包含字体的字符串.

        int xOrg,yOrg,Y=0,nCharlen=strlen(text);

        switch (message)

        {

            case WM_CREATE:

            SetTimer(hWnd,1,50,NULL);        //设定定时器.每0.2秒发出WM_TIMER消息.

            break;

            case WM_COMMAND:

                wmId = LOWORD(wParam);

                wmEvent = HIWORD(wParam);

                // Parse the menu selections:

                switch (wmId)

                {

                    case IDM_ABOUT:

                     DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);

                     break;

                    case IDM_EXIT:

                     DestroyWindow(hWnd);

                     break;

                    default:

                     return DefWindowProc(hWnd, message, wParam, lParam);

                }

                break;

            case WM_PAINT:

                hDC = BeginPaint(hWnd, &ps);

                GetClientRect(hWnd,&clientRect);

                xOrg=(clientRect.left+clientRect.right)/2;

                yOrg=(clientRect.top+clientRect.bottom)/2;

     

                SetViewportOrgEx(hDC,0,yOrg,NULL);

                // TODO: Add any drawing code here...

     

                if(Flag%4==0)    //设置字体颜色.

                {

                    SetTextColor(hDC,RGB(255,0,0));

                    strcpy(chFont,"楷体");

                }

                else if(Flag%4==1)

                {

                    SetTextColor(hDC,RGB(0,255,0));

                    strcpy(chFont,"宋体");

                }

                else if(Flag%4==2)

                {

                    SetTextColor(hDC,RGB(0,0,255));

                    strcpy(chFont,"仿宋体");

                }

                else if(Flag%4==3)

                {

                    SetTextColor(hDC,RGB(255,255,0));

                    strcpy(chFont,"黑体");

                }

                hF=CreateFont(            //获得字体句柄.

                    100,                //字体高度

                    0,                    //系统自动调整宽度.

                    0,                    //文本水平

                    0,                    //字体倾斜度为0

                    400,                //字体粗度.400为正常.

                    0,                    //字体不倾斜.

                    0,                    //无下划线.

                    0,                    //无中划线.

                    GB2312_CHARSET,        //字符集

                    OUT_DEFAULT_PRECIS,    //默认输出精度.

                    CLIP_DEFAULT_PRECIS,//默认裁剪精度

                    DEFAULT_QUALITY,    //默认输出质量.

                    DEFAULT_PITCH|FF_DONTCARE,//默认间距

                    chFont);         //字体名称.

     

                SelectObject(hDC,hF);    //选入字体.

                GetTextMetrics(hDC,&tm);    //得到字体的信息.

                TextOut(hDC,X,Y-tm.tmHeight/2,&text[0],nCharlen); //输出.

     

                X-=10;

                if(X+tm.tmAveCharWidth*nCharlen<0){

                    Flag++;

                    if(Flag==4)

                        Flag=0;

                    X=clientRect.right;

                }

     

                DeleteObject(hF);    //删除字体.

                EndPaint(hWnd, &ps);

                break;

            case WM_TIMER:

            InvalidateRect(hWnd,NULL,1);    //刷新用户区.

            break;

            case WM_DESTROY:

                PostQuitMessage(0);

                break;

            default:

                return DefWindowProc(hWnd, message, wParam, lParam);

    }

    return 0;

    }

    编译并调试程序,

     

    滚动文字运行结果如下图:

     

    本次学习的涉及文本输出,需要注意Windows字符类型!

  • 相关阅读:
    One special dictionary
    What is mobile platform?
    Linux 学习 二, 安装JDK
    Design Pattern ->Abstract Factory
    Entity Framework 使用注意:Where查询条件中用到的关联实体不需要Include
    在使用ICSharpCode.SharpZipLib进行目录压缩后,再解压缩是提示这个错误Size mismatch: 4294967295;126976 70202;126976
    在C#中利用SharpZipLib进行文件的压缩和解压缩收藏
    JavaScript中的匿名函数及函数的闭包
    让ConfigurationManager打开任意的配置文件
    C# 读取与修改配置文件
  • 原文地址:https://www.cnblogs.com/leftshine/p/5698610.html
Copyright © 2011-2022 走看看