创建一个MFC单文档应用程序

  1. 创建插入符

    在窗口创建完成之后,再创建插入符,在View类当中去完成,增加一个Windows Message Handle…..,WM_CREATE消息处理函数。

    1.创建一个文字插入符

    CClientDC dc(this);//构造一个DC

    /* The CClientDC class is derived from CDC and takes care of calling the Windows functionsGetDC at construction time andReleaseDC at destruction time. This means that the device context associated with a CClientDC object is the client area of a window. */

    TEXTMETRIC tm;

    /* The TEXTMETRIC structure contains basic information about a physical font. All sizes are given in logical units; that is, they depend on the current mapping mode of the display context.

    typedef struct tagTEXTMETRIC { // tm

    LONG tmHeight;

    LONG tmAscent;

    LONG tmDescent;

    LONG tmInternalLeading;

    LONG tmExternalLeading;

    LONG tmAveCharWidth;

    LONG tmMaxCharWidth;

    LONG tmWeight;

    LONG tmOverhang;

    LONG tmDigitizedAspectX;

    LONG tmDigitizedAspectY;

    BCHAR tmFirstChar;

    BCHAR tmLastChar;

    BCHAR tmDefaultChar;

    BCHAR tmBreakChar;

    BYTE tmItalic;

    BYTE tmUnderlined;

    BYTE tmStruckOut;

    BYTE tmPitchAndFamily;

    BYTE tmCharSet;

    } TEXTMETRIC; */

    dc.GetTextMetrics(&tm);

    /* The GetTextMetrics function fills the specified buffer with the metrics for the currently selected font.

    BOOL GetTextMetrics(

    HDC hdc, // handle to device context

    LPTEXTMETRIC lptm // pointer to text metrics structure

    ); */

    CreateSolidCaret(tm.tmAveCharWidth/8,tm.tmHeight);//创建文字插入符

    /* void CreateSolidCaret( int nWidth, int nHeight );

    Parameters

    nWidth

    Specifies the width of the caret (in logical units). If this parameter is 0, the width is set to the system-defined window-border width.

    nHeight

    Specifies the height of the caret (in logical units). If this parameter is 0, the height is set to the system-defined window-border height.

    Creates a solid rectangle for the system caret and claims ownership of the caret. The caret shape can be a line or block. */

    ShowCaret();

    /* The ShowCaret function makes the caret visible on the screen at the caret's current position. When the caret becomes visible, it begins flashing automatically.

     

    BOOL ShowCaret(

    HWND hWnd // handle of window with caret

    ); */

     

    2.创建一个位图插入符

    在View类中加入一个CBitmap 成员变量,

    //    CBitmap bitmap;//这个对象与资源相关,析构时就删除了

        bitmap.LoadBitmap(IDB_BITMAP1);//IDB_BITMAP1是在资源中导入的位图

    /* BOOL LoadBitmap( LPCTSTR lpszResourceName );

    BOOL LoadBitmap( UINT nIDResource );

    Return Value

    Nonzero if successful; otherwise 0.

    Parameters

    lpszResourceName

    Points to a null-terminated string that contains the name of the bitmap resource.

    nIDResource

    Specifies the resource ID number of the bitmap resource.

    Loads the bitmap resource named by lpszResourceName or identified by the ID number in nIDResource from the application's executable file. The loaded bitmap is attached to the CBitmap object.

    If the bitmap identified by lpszResourceName does not exist or if there is insufficient memory to load the bitmap, the function returns 0. */

        CreateCaret(&bitmap);//创建位图插入符

    /* The CreateCaret function creates a new shape for the system caret and assigns ownership of the caret to the specified window. The caret shape can be a line, a block, or a bitmap.

    BOOL CreateCaret(

    HWND hWnd, // handle to owner window

    HBITMAP hBitmap, // handle to bitmap for caret shape

    int nWidth, // caret width

    int nHeight // caret height

    ); */

        ShowCaret();

    3.使输出的文字与图形始终能够被看到

    在View类中提供的OnDraw函数来始终显示我们要输出的字符串,加入

        CString str;

    /* The CString class is derived from the CData class. The data in this object is a null-terminated string. */

        str="HELLO ,NICE TO MEET YOU !";

        pDC->TextOut(0,140,str);//pDC是OnDraw函数传入的

    /* The TextOut function writes a character string at the specified location, using the currently selected font, background color, and text color.

    BOOL TextOut(

    HDC hdc, // handle to device context

    int nXStart, // x-coordinate of starting position

    int nYStart, // y-coordinate of starting position

    LPCTSTR lpString, // pointer to string

    int cbString // number of characters in string

    ); */

    4.通过输出字符中表中的字符输出,在资源中插入字符串,

        CSize sz=pDC->GetTextExtent(str);//返回一个CSize对象,即返回特定字符串的宽度和高度

/* Return Value: The dimensions of the string (in logical units) in a CSize object.

Call this member function to compute the width and height of a line of text using the current font to determine the dimensions. The information is retrieved from m_hAttribDC, the attribute device context.

The current clipping region does not affect the width and height returned by GetTextExtent. */

    str.LoadString(IDS_HELLOHELLO);//HELLOHELLO是字符串表ID

/* Reads a Windows string resource, identified by nID, into an existing CString object. */

    pDC->TextOut(0,320,str);

5.//在路径层或说路径支架中输出文字

    pDC->BeginPath();

/* The BeginPath function opens a path bracket in the specified device context.

Opens a path bracket in the device context. After a path bracket is open, an application can begin calling GDI drawing functions to define the points that lie in the path. An application can close an open path bracket by calling the EndPath member function. When an application calls BeginPath, any previous paths are discarded. */

    pDC->Rectangle(0,140,sz.cx,140+sz.cy);//在路径层中画一个矩形

    pDC->EndPath();

    pDC->SelectClipPath(RGN_XOR);//剪切区域,还有GRN_DIFF、RGN_AND、RGN_OR等

/* The SelectClipPath function selects the current path as a clipping region for a device context, combining the new region with any existing clipping region by using the specified mode.

BOOL SelectClipPath(

HDC hdc, // handle of device context

int iMode // clipping mode

);

Parameters

hdc :Handle to the device context of the path.

iMode :Specifies the way to use the path. The following values are allowed:

RGN_AND The new clipping region includes the intersection (overlapping areas) of the current clipping region and the current path.

RGN_COPY The new clipping region is the current path.

RGN_DIFF The new clipping region includes the areas of the current clipping region with those of the current path excluded.

RGN_OR The new clipping region includes the union (combined areas) of the current clipping region and the current path.

RGN_XOR The new clipping region includes the union of the current clipping region and the current path but without the overlapping areas.

Return Values:If the function succeeds, the return value is nonzero.If the function fails, the return value is zero. */

2.//画网格状的线条覆盖输出的文字

    for(int i=0;i<300;i+=10)

    {

        //画横线

        pDC->MoveTo(0,i);//横坐标是0,纵坐标是300

        pDC->LineTo(300,i);

        //画竖线

        pDC->MoveTo(i,0);

        pDC->LineTo(i,300);

    }

3.//在屏幕上输出字符串

在View类中捕获一个WM_CHAR消息,增加一个CString 成员变量m_strLine,并在其构造函数在将置空,m_strLine="";

//在输入文字的时候移动插入符,在View类捕获一个WM_LBUTTONDOWN消息,

SetCaretPos(point);//移动插入符

/* The SetCaretPos function moves the caret to the specified coordinates. If the window that owns the caret was created with the CS_OWNDC class style, then the specified coordinates are subject to the mapping mode of the device context associated with that window.

SetCaretPos moves the caret whether or not the caret is hidden. */

m_strLine.Empty();//清空m_strLine中保存的字符

//将当前鼠标点击的位置保存下来,加一个CStrng成员变量m_ptOrigin,并在构造函数中将它赋零

m_ptOrigin=point; //将当前鼠标点击的位置保存下来

4.//响应键盘输入消息,在View类中加一个WM_CHAR消息

    CClientDC dc(this);//定义这个CHAR响应的DC对象

    CFont font;//定义一个字体的对象

    font.CreatePointFont(300,"微软简行楷",NULL);

/* This function provides a simple way to create a font of a specified typeface and point size. It automatically converts the height in nPointSize to logical units using the CDC object pointed to by pDC. */

    CFont *pOldFont=dc.SelectObject(&font);//将字体选到设备描述表中

    TEXTMETRIC tm;//定义一个字体度量对象

    dc.GetTextMetrics(&tm); //返回获取的度量的值到tm对象中去

/* The GetTextMetrics function fills the specified buffer with the metrics for the currently selected font. */

//回车处理

    if(0x0d==nChar)

    {

        m_strLine.Empty();

        m_ptOrigin.y+=tm.tmHeight;

    }

//退格处理

    else if(0x08==nChar)

    {

        COLORREF clr=dc.SetTextColor(dc.GetBkColor());

/* The GetBkColor function returns the current background color for the specified device context. Returns the current background color. If the background mode is OPAQUE, the system uses the background color to fill the gaps in styled lines, the gaps between hatched lines in brushes, and the background in character cells. The system also uses the background color when converting bitmaps between color and monochrome device contexts.

Sets the text color to the specified color. The system will use this text color when writing text to this device context and also when converting bitmaps between color and monochrome device contexts. */

        dc.TextOut(m_ptOrigin.x,m_ptOrigin.y,m_strLine);

    m_strLine=m_strLine.Left(m_strLine.GetLength()-1);//对字符串取左边的部分

        dc.SetTextColor(clr);//将先前的颜色设回来

    }

    else

    {

        m_strLine+=nChar;//把字符加到m_strLine后边

    }

    CSize sz=dc.GetTextExtent(m_strLine);//获取文本在屏幕上显示的宽度

    CPoint pt;

    pt.x=m_ptOrigin.x+sz.cx;//加入一串字符后的x坐标

    pt.y=m_ptOrigin.y;//y坐标不变

    SetCaretPos(pt);//重置插入符

    dc.TextOut(m_ptOrigin.x,m_ptOrigin.y,m_strLine);

    dc.SelectObject(pOldFont);//把字体设回来

5.模拟字幕变色的功能

在View中增加一个WM_TIMER消息,增加一个int型成员变量m_nWidth,并在构造函数中赋初值等于零。

在OnCreate函数中设置一个定时器:

SetTimer(1,100,NULL);

/* The SetTimer function creates a timer with the specified time-out value.

UINT SetTimer(

HWND hWnd, // handle of window for timer messages

UINT nIDEvent, // timer identifier

UINT uElapse, // time-out value

TIMERPROC lpTimerFunc // address of timer procedure

);

An application can process WM_TIMER messages by including a WM_TIMER case statement in the window procedure or by specifying a TimerProc callback function when creating the timer. When you specify a TimerProc callback function, the default window procedure calls the callback function when it processes WM_TIMER. Therefore, you need to dispatch messages in the calling thread, even when you use TimerProc instead of processing WM_TIMER. */

在OnTimer函数中插入:

m_nWidth+=5;//按5个像数点增加

    CClientDC dc(this);

    TEXTMETRIC tm;

    dc.GetTextMetrics(&tm);

    CRect rect;

    rect.left=0;

    rect.top=320;

    rect.right=m_nWidth;

    rect.bottom=rect.top+tm.tmHeight;

    dc.SetTextColor(RGB(255,0,0));

    CString str;

    str.LoadString(IDS_HELLOHELLO);

    //dc.DrawText(str,rect,DT_LEFT);//从左边开始延伸显示

    dc.DrawText(str,rect,DT_CENTER);//从中间开始向两边延伸显示

/* The DrawText function draws formatted text in the specified rectangle. It formats the text according to the specified method (expanding tabs, justifying characters, breaking lines, and so forth). Call this member function to format text in the given rectangle. It formats text by expanding tabs into appropriate spaces, aligning text to the left, right, or center of the given rectangle, and breaking text into lines that fit within the given rectangle. The type of formatting is specified by nFormat.

int DrawText(

HDC hDC, // handle to device context

LPCTSTR lpString, // pointer to string to draw

int nCount, // string length, in characters

LPRECT lpRect, // pointer to struct with formatting dimensions

UINT uFormat // text-drawing flags

); */

    rect.top=350;

    rect.bottom=rect.top+tm.tmHeight;

    dc.DrawText(str,rect,DT_RIGHT);

    CSize sz=dc.GetTextExtent(str);

    if(m_nWidth>sz.cx)//比较在屏幕上显示的宽度是否大于字符串的宽度

    {

        m_nWidth=0;

        dc.SetTextColor(RGB(0,255,0));//设置文本的颜色

        dc.TextOut(0,320,str);

    }

OK. ^_^

源程序地址:http://down.qiannao.com/space/file/luowei505050/Text.rar/.page

创建插入符、输出不同效果的文字相关推荐

  1. 怎么在php中加入换行符,如何在PHP中创建换行符?

    为了创建换行符, PHP提供了nl2br()函数.它是PHP的内置函数, 用于在字符串中的所有换行符之前插入HTML换行符.虽然, 我们也可以在源代码中使用PHP换行符\ n或\ r \ n来创建换行 ...

  2. 如何为“选择”框创建占位符?

    我正在使用占位符进行文本输入,效果很好. 但是我也想为我的选择框使用一个占位符. 当然,我可以使用以下代码: <select><option value=""&g ...

  3. C语言-数据结构-单链表的初始化,插入和输出

    [问题描述] 设有头结点单链表,实现单链表的初始化.插入和输出算法. [输入形式] 第一行输入一个N(N大于等于1,小于1000),一个M(N大于等于1,小于1000): 第二行输入N个整数,以空格作 ...

  4. C语言-数据结构-可变长顺序表的初始化,插入和输出

    问题描述: 实现可变长顺序表的建表过程.任务要求:通过顺序表的初始化.插入算法,实现顺序表的建表,并依次输出顺序表元素. [输入形式] 第一行输入整数N(1<=N<=100),表示创建长度 ...

  5. HtmlCss光标(插入符caret)透明隐藏光标 221106笔记

    HtmlCss光标透明隐藏光标 221106笔记 光标有两种 文字插入符光标(caret), 一闪一闪的竖线 鼠标光标(cursor), 或者应该叫指针, 或叫鼠标指针 #mermaid-svg-cj ...

  6. C语言邻接表表示法创建无向图并输出

    C语言邻接表表示法创建无向图并输出 邻接表是图的一种链式存储结构,对图的每个顶点建立一个单链表,单链表第一个结点存放顶点信息,其余存放有关边信息. 邻接表由表头结点表和边表组成. 邻接表存储结构 #i ...

  7. window 创建虚拟盘符

    # 创建虚拟盘符 subst d: /w # 删除虚拟盘符 subst w: /d 应用场景 我把很多的库放在了自己的移动硬盘里 到一个新的电脑要使用这些库的时候,我常常要插上自己的移动硬盘 为保障每 ...

  8. 7-10 先序序列创建二叉树,输出先序序列、中序序列、后序序列并输出叶子结点数 (10 分)

    7-10 先序序列创建二叉树,输出先序序列.中序序列.后序序列并输出叶子结点数 (10 分) 对于给定的二叉树,输出其先序序列.中序序列.后序序列并输出叶子结点数. 输入格式: 二叉树的先序遍历序列. ...

  9. html如何为“选择”框创建占位符?

    我在文本输入中使用占位符,效果很好.但我也想为我的选择框使用一个占位符.当然,我可以使用以下代码: <select><option value="">Sel ...

最新文章

  1. shell-sed命令详解(转)
  2. linux星期六字符,linux shell系列10 判断某个月中的星期六和星期天
  3. 【Django】基于Django架构网站代码的目录结构
  4. 软件工程复习提纲——第八章
  5. SQLi LABS Less 14 报错注入+布尔盲注
  6. IntelliJ IDEA 2019.3 正式发布,给我们带来哪些新特性?| CSDN 博文精选
  7. 用有道ip地址查询接口的详细方法
  8. Ajax : load()
  9. linux 中文字体 推荐,适合阅读的中文字体
  10. CISSP的2021年认证大纲、CISSP学习大纲、中国考点,及如何考取成功
  11. 蓝电电池测试系统工步编辑软件,CT2001A
  12. 数据库安装、数据导入及格式转换20200514
  13. 大数据联姻“互联网+”驱动绿色变革
  14. 20小时写一篇文章,好看到爆炸的手机壁纸都在这些App里!
  15. Python之Excel 优雅操作手法 精选
  16. 如何进行接口测试(一篇学会)
  17. 巡查使:智能巡检领域“引航者”
  18. Android应用常用的加密方式
  19. Java云原生(Spring Native)开发初体验报告
  20. 一站式杀手级 AI 开发平台来袭!告别切换零散建模工具

热门文章

  1. Python爬取 | 王者荣耀英雄皮肤海报
  2. MongoDB从入门到实践(Docker安装及整合SpringBoot)
  3. 如何做好游戏内实时语音体验 1
  4. mysql实战45讲(15-22)
  5. 在python中调用abaqus和nastran的方法
  6. 暴雪娱乐公司_百度百科
  7. 航海日记手游如何在电脑上玩 航海日记模拟器玩法教程
  8. 2013年最精致的企业网站设计欣赏【系列四】
  9. python中argsort()函数的用法
  10. 我的世界java版记分板_我的世界基岩版计分板入门教程