Basic techniques of windows-text programming

Source: Internet
Author: User
Tags textout

basic techniques of windows-text programming
Here are some simple technical points for MFC text programming.
one. Insert character
(1). Create a text caret

#if 1    //创建插入符, 参数:宽,高    CreateSolidCaret(20100);    //必须显示出来,默认是隐藏的    ShowCaret();#endif

(2). Create a graphics caret

#if 1    //创建图形插入符,根据图片的ID加载    m_Bitmap.LoadBitmap(IDR_MAINFRAME);    CreateCaret(&m_Bitmap);    #endif

(3). Display the caret

    //插入符默认是隐藏的,我们需要显式的显示它    ShowCaret()

(4). Set the position of the caret

    //设置插入符的位置为左键按下的位置    SetCaretPos(point);

(5). Get the font information in the Device Description table and display the appropriate caret size

    TEXTMETRIC tm;//文本信息的结构体变量    dc.GetTextMetrics(&tm);//获得设备描述表中文本的信息    //宽度根据经验值一般要 / 8    CreateSolidCaret(tm.tmAveCharWidth / 8, tm.tmHeight);//根据字体的大小,创建合适的插入符    ShowCaret();//显示插入符

(6). Font-related information

Information about the TM structure

Two. Window redraw
When the window is redrawn in MFC, the corresponding view object receives the WM_PAINT message, which can be logically handled in the class's OnDraw function.
Here is an example of a string displayed on the screen:

void CTextView::OnDraw(CDC* pDC){    CTextDoc* pDoc = GetDocument();    ASSERT_VALID(pDoc);    if (!pDoc)        return;    // TODO:  在此处为本机数据添加绘制代码    #if 1        //构造字符串        CString str("Hello Wolrd");        //显示在50 50位置处        pDC->TextOut(5050, str);    #endif}

three. Simple use of the CString class
The CString class is a string class in MFC that can be used to manipulate strings conveniently in the MFC framework, rather than needing to manipulate pointers in C, but we also have to implement ideas for these functions, and think about how to implement some of the functions in CString with the idea of C.
The following are two common examples:

(1). Load string

#if 1    //在OnDraw函数中测试,这个函数会提供pDc这个设备描述表指针的参数    //构造字符串    str("Hello Wolrd");    //显示在50 50位置处    pDC->TextOut(5050str);//直接显示在屏幕上    str.LoadString(IDS_HELLO_WORLD);//通过MFC中的资源ID来加载字符串    pDC->TextOut(0200str);#endif

(2). Remove one character from a string

    CString Left(int nCount)//返回左边的nCount个字符构成的字符串  
    m_strLine = m_strLine.Left(m_strLine.GetLength1);//等价于去掉最后一个字符

four. Using the path layer in MFC
(1). The path layer is actually defined as the boundary of a text display, which can be displayed within the bounds, and cannot be displayed outside the path layer.

Instance:

    //开启路径层    pDC->BeginPath();    //在路径层中画出一个矩形    pDC->Rectangle(50, 50, 50 + sz.cx, 50 + sz.cy);    //关闭路径层    pDC->EndPath();

(2). Gets the height and width of the string gettextextent

    int nCount  const

We need to distinguish between this function and the function that gets the information of the font

    BOOLconst;

The difference.
This only gets the height and width information of the string, and the function above is to get details of the current font in the device description table, such as the height, width, average height, and so on for the specific font. It is necessary to understand the usage of these two functions and not to use them indiscriminately.

(3). Clipping area
The clipping area is essentially the drawing area, usually in the customer area of the screen, which is the area we operate.
To set the interop of the clipping region and the path layer

    //设置路径层和剪切区域进行互操作    pDC->SelectClipPath(RGN_DIFF);

five. Character input
When we enter characters in the view class, we receive the WM_CHAR message, and the keyboard message can be processed in the corresponding callback function OnChar (), where we need to do special handling of the carriage return and the deletion of the two characters.
Here is an example:

voidCtextview::onchar (UINT NChar, uint nrepcnt, uint nflags) {//TODO: Add the message Handler code here and or call the default valueCCLIENTDC DC ( This);//Get more information about the fontTextmetric TM; dc. GetTextMetrics (&AMP;TM);//Enter    if(0x0D= = NChar) {m_strline.empty ();    M_PTORIGIN.Y + = Tm.tmheight; }//Delete key    Else if(0x08= = NChar) {colorref clr = DC. SetTextColor (DC. GetBkColor ());//Set Current text background color to default window background colordc. TextOut (m_ptorigin.x, M_PTORIGIN.Y, m_strline);//Output text onceM_strline = M_strline.left (M_strline.getlength ()-1);//equivalent to removing the last characterdc.    SetTextColor (CLR); }//Other keys to add characters directly    Else{M_strline + =static_cast<Char> (NChar); }//output on the screendc.     TextOut (m_ptorigin.x, M_PTORIGIN.Y, m_strline); Cview::onchar (NChar, nrepcnt, nflags);}

Six. Set the font
In MFC we can use the CFont class to make the appropriate selection of fonts.
Here is an example:

    CFont font;    //创建具体的字体类型    font.CreatePointFont(300, L"华文行楷"nullptr);    //选入设备描述表中    CFont* pOldFont = dc.SelectObject(&font);    //记得要恢复设备描述表中之前的字体信息    dc.SelectObject(&font);

Seven. Timers
In MFC we can also set a timer like hardware to form a special effect on the interface.
We need to use SetTimer to set the timer, which will send the WM_TIMER message at the end of the timer, we need to capture this message in the application and handle it in the OnTimer callback function.

    //定时器ID,定时时间ms,回调函数置为空,会把WM_TIMER消息防盗消息队列中    SetTimer(1, 100, nullptr);

Timer processing function

voidCTextView::OnTimer(UINT_PTRnIDEvent){    // TODO:  在此添加消息处理程序代码和/或调用默认值     CView::OnTimer(nIDEvent);}

eight. Attach an ASCII table

Nine. Summary
Here are just a few basic techniques for text programming, and more technology needs to be found in real projects.
This is their own summary of the map, easy to summarize

The following resources to share to everyone: Https://yunpan.cn/cPYM8uu5XuZJH access password 8eb9

Basic techniques of windows-text programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.