[MFC] SDI allows text to move with the mouse on the image background

Source: Internet
Author: User
Tags drawtext

SDI is short for single-document interface applications. In this article, you need to display an image in the view area, display text on the image surface, and move the text following the mouse. Think about it and you can judge that this problem is divided into the following parts: 1. display images; 2. Locate the mouse position; 3. Insert text; 4. automatically move text. Perform the following steps.

1. First, open an image and display it in "open" mode. For convenience, only the following code is added to the OnDraw function in the CYourView class:

    (GetDocument()->GetPathName() != )=->GetPathName(), IMAGE_BITMAP, , |&&=->BitBlt(, , m_bitmap.bmWidth, m_bitmap.bmHeight, &MemDC, , 

M_bitmap is a BITMAP object. The image display method is common and is not described here.

2. You need to know that there is a message processing function OnMouseMove to process the mouse moving message. Therefore, you must first add this function, which can be found in Class Wizard. Add this function first.

OnMouseMove(UINT nFlags, CPoint point);

The second CPoint object of this function is the location of the mouse. You can use the following method to find the corresponding coordinates:

Xcoords == point.y;

 

3. Then we need to insert the text. It is very easy to insert the text at a specific position in SDI. You can use the CDC-> DrawText method. This function allows you to add formatted text in a specified rectangle, a bit like the insert dialog box in word. The syntax is as follows:

 DrawText( CString& str, LPRECT lpRect, UINT nFormat);

The first parameter is the CString object to be output, the second parameter is the rectangular area of the displayed text, and the last parameter is the text format, such as center left. An example is as follows:

CClientDC cdc(, CRect(left, top, reight, bottom), DT_LEFT);


 

4. After the text is inserted, the text will be moved. First, I think of the movement of images in SDI. For example, draw a Rectangle CDC-> Rectangle method and move the Rectangle. There is a common way to draw an image by moving the image by using the CDC-> SetROP2 (R2_XORPEN) method. You can use a paint brush or screen color to clear the image. Therefore, the image is moved as follows:

(1) exclusive or plotting

(2) original location again or drawing

(3) place a picture in a new location

(4 )...

By combining drawing with OnMouseMove, you can easily move the image following the mouse. But can the same method be used for text movement? The answer is no. Because DrawText is essentially different from other drawing functions, because it does not use a paint brush, the SetROP2 method has no effect on the text. However, with the mobile image method, it is easy to think that the essence of the so-called mobile (graphics or text) is to first clear the content of the original location, then re-draw the image in the new location (as if it was nonsense ).

Therefore, the key to moving text is how to clear the original text. (The method for inserting an empty text using DrawText is different because it does not affect the inserted text .) One easy way to think of is to copy the original position (the rectangle corresponding to DrawText), and re-draw the text after inserting the text. Then, the text is cleared by overwriting. So we have the following method.

First, add a function called ScreenShot.

CBitmap* CYourView::ScreenShot(CDC* pDC, CBitmap *memBitmap,  Xcoords,  Ycoords,  Width, ->, 

The processing process of this function is very simple. It saves the image content inside the rectangle with the specified position and size.

The code for moving the text following the mouse is in the OnMouseMove function:

==(m_first_show_flag == = Xcoords+= Ycoords-= ScreenShot(&cdc, &memBitmap, OldX, OldY, ,             cdc.DrawText(,CRect(OldX,OldY, OldX+, OldY+            m_first_show_flag = ->GetObject ((BITMAP),&&*pOldBitmap=&MemDC,,            OldX = Xcoords+= Ycoords-= ScreenShot(&cdc, &memBitmap, OldX, OldY, , ,CRect(OldX, OldY, OldX+, OldY+),DT_LEFT);

Because the state of the first mouse move is a little different from that of the next one, it is said to be processed separately in the Code. Therefore, in the OnDraw function, if (GetDocument ()-> GetPathName ()! = "") Statement to add the following content:

        m_loaded_flag = (m_first_show_flag == = 

The text that follows the mouse is displayed only after the image is loaded. And when the image is moved, refresh to clear the text.

In general, the entire process is relatively simple. I have seen an example of text moving with the mouse using OnTimer. In fact, it is to constantly refresh the screen to display different texts, A background image may flash badly. You need to use other methods to avoid blinking. However, using my method does not solve this problem. A small amount of re-painting has no visual impact.

In addition, pay attention to the use of CDC object recycling, if you use CDC * pDC = this-> DetDC () to create a device context in this way, you need to use pDC-> DeleteDC () to delete the DC when you do not need it. Otherwise, it will cause serious memory leakage. However, the cdc object system created using CClientDC CDC (this) can be automatically recycled, so this method is used in the above Code.

 

 

 

 

 

 

 

 

 

 

 

 

Related Article

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.