MFC-Simple Drawing

Source: Internet
Author: User
Tags transparent image

 

Ii. draw lines

1. Use the global functions of the SDK to implement the draw line function

HDC;
HDC =: getdc (m_hwnd );

Movetoex (HDC, m_ptorigin.x, m_ptorigin.y, null );
Lineto (HDC, point. X, point. y );

: Releasedc (m_hwnd, HDC );

Note: A device description table DC must be obtained for drawing. After drawing, you must release the device description table resources.

2. Use the CDC class of MFC to implement the draw line function

CDC * PDC = getdc ();

PDC-> moveTo (m_ptorigin );
PDC-> lineto (point );

Releasedc (PDC );

Cview: onlbuttonup (nflags, point );

Note: MFC provides a device description table encapsulation class CDC, which encapsulates all plotting-related operations and provides a data member m_hdc,

Used to save DC handles related to the CDC class

3. Use the cclientdc class of MFC to implement the draw line function

Cclientdc DC (this );
DC. moveTo (m_ptorigin );
DC. lineto (point );

Note: MFC provides the cclientdc class for drawing operations. This class inherits from the CDC class. It calls the getdc function during construction and the release function during analysis.

Function; when constructing a cclientdc object, a cwnd type pointer parameter is required. Passing this indicates the class window, and passing getparent () indicates

Frame window

4. Use the cwindowdc class of MFC to implement the line drawing function

Cwindowdc DC (this );
DC. moveTo (m_ptorigin );
DC. lineto (point );

Note: The cwindowdc class and cclient class are basically the same. The difference is that the class can access the entire window area, including the customer and non-customer areas of the framework window.

5. draw lines in the desktop window

Cwindowdc DC (getdesktopwindow ());
DC. moveTo (m_ptorigin );
DC. lineto (point );

Note: The cwnd: getdesktopwindow member function can obtain the handle of the Windows desktop window and obtain a device description related to the desktop window.

Table, you can draw in the desktop window

6. Draw colored lines

(1) the device description table contains a default black paint brush.

(2) MFC provides the cpen class for creating a paint brush object, which encapsulates operations related to the paint brush

(3) After creating a GDI object, the object will not take effect immediately. You must select the device description table to make it take effect in future painting operations.

(4) Generally, after drawing, you must use the SelectObject function to select the previous GDI object into the device description table so that

Restore to the previous status

(5) When the width of the paint brush is less than or equal to 1, the dotted line is valid.

(6) implementation code

Cpen pen (ps_solid, 1, RGB (255, 0, 0 ));
Cclientdc DC (this );
Cpen * poldpen = Dc. SelectObject (& pen );
DC. moveTo (m_ptorigin );
DC. lineto (point );
DC. SelectObject (poldpen );

3. Drawing with painter

1. Simple painter

(1) MFC provides the cbrush class, which is used to create a painting brush object and thus fill an area.

(2) fillrect, a member function of the object in the device description table, used to fill a specified rectangular area with a specified image brush

(3) implementation code

Cbrush brush (RGB (255, 0, 0 ));
Cclientdc DC (this );
DC. fillrect (crect (m_ptorigin, point), & brush );

2. Bitmap painter

(1) The cbrush class cbrush (cbitmap * pbitmap) constructor can create a bitmap image painting object.

(2) The cbitmap class is a bitmap class. When constructing such an object, you must call the loadbitmap initialization function in addition to the constructor.

(3) implementation code

Cbitmap bitmap;
Bitmap. loadbitmap (idb_bitmap1 );

Cbrush brush (& Bitmap );

Cclientdc DC (this );
DC. fillrect (crect (m_ptorigin, point), & brush );

3. Transparent image brush

(1) the device description table has a default white brush.

(2) The getstockobject function is used to obtain a white or black paint brush handle.

(3) Static members (static member variables and static member functions) belong to the class itself. When classes are loaded, they are allocated space.

Class Name: The variable name/function name. The instance member belongs to the data and method of the object, that is, the class object should be generated first, and then

Like to reference

(4) In static member functions, non-static members cannot be called, but static member variables can only be accessed.

(5) No matter what operations are used, the program code runs in the memory. We can access it only when it occupies a place in the memory. For example

If a member function or member variable is not generated in the memory, it cannot be accessed.

(6) implementation code

Cclientdc DC (this );
Cbrush * pbrush = cbrush: fromhandle (hbrush) getstockobject (null_brush ));
Cbrush * poldbrush = Dc. SelectObject (pbrush );
DC. rectangle (crect (m_ptorigin, point ));
DC. SelectObject (poldbrush );

4. Draw continuous lines

1. Move the message wm_mousemove with the mouse to draw continuous lines.

2. Implementation Code

Cclientdc DC (this );
If (m_bdraw)
{
DC. moveTo (m_ptorigin );
DC. lineto (point );
M_ptorigin = point;
}

5. draw lines with slices

Cclientdc DC (this );
Cpen pen (ps_solid, 1, RGB (255, 0, 0 ));
Cpen * poldpen = Dc. SelectObject (& pen );
If (m_bdraw)
{
DC. moveTo (m_ptorigin );
DC. lineto (point );
DC. lineto (m_ptold );
M_ptold = point;
}
DC. SelectObject (poldpen );

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.