MFC Dialog Box Drawing

Source: Internet
Author: User

Functions:

1. Draw a colored line in an area of the dialog box.

2. When the window is overwritten and restored, the image is automatically re-painted.

3. Change the button color.

Operation:

Create a dialog box program according to the wizard. Add a static text box to the resource with the ID idc_static.

Add the following content to dialogxxxdlg. h:

Cbrush m_brush; // used for custom colors
CDC m_memdc; // memory DC compatible with screen DC
Cbitmap m_bmp; // bitmap
Cwnd * m_pdrawwnd; // Object Pointer used to save the static text box
//
// Initialize data related to double buffering
Void initialdbb ();
// Drawing on Dual-buffer memory
Void drawonmem ();
// Draw a picture in a static area
Void drawonstaticarea ();
// Message response function for changing the color
Afx_msg hbrush onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor );

Add the following content to dialogxxxdlg. cpp:

1. Add message ing

On_wm_ctlcolor ()

2. Add the initialization code in oninitdialog ().

// Todo: add additional initialization code here
M_brush.createsolidbrush (RGB (, 0, 0); // initialize the paint brush
M_pdrawwnd = getdlgitem (idc_static); // get the static window object pointer
Initialdbb (); // initialize data related to the double buffer.

3. Rewrite function onctlcolor ()

// Change the button color
Hbrush cdialogdrawdlg: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor)
{
Hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor );
//
If (pwnd-> getdlgctrlid () = idok | pwnd-> getdlgctrlid () = idcancel)
{
PDC-> settextcolor (RGB (, 0, 0); // you can specify the text color.
PDC-> setbkmode (transparent); // set the text background to transparent
PDC-> setbkcolor (RGB (, 0, 0); // you can specify the background color.
Return m_brush;
}
//
Return HBr; // m_brush; // HBr
}

4. Add the function definition code declared in the header file


// Initialize elements related to double buffering
Void cdialogdrawdlg: initialdbb ()
{
Crect RT;
M_pdrawwnd-> getclientrect (& RT );
CDC * SDC = m_pdrawwnd-> getdc ();
// Create compatible memory DC for the screen DC
If (! M_memdc.createcompatibledc (SDC ))//
{
: Postquitmessage (0 );
}

// Create a bitmap. The value cannot be m_memdc; otherwise, no color is available.
M_bmp .createcompatiblebitmap (SDC, Rt. Width (), Rt. Height (); // m_memdc
// Select the canvas, m_pdrawwnd->
: SelectObject (m_memdc.getsafehdc (), m_bmp );
M_pdrawwnd-> releasedc (SDC );
}


// Drawing on Dual-buffer memory
Void cdialogdrawdlg: drawonmem ()
{
Crect rect;
M_pdrawwnd-> getclientrect (& rect );

// Colorref CRL = getsyscolor (color_3dface );
// M_memdc.fillsolidrect (rect, CRL );
// M_memdc.fillsolidrect (& rect, 0x00ffffff); // white fill. Note: This time it is painted in the memory device environment.
Cpen pen (ps_solid, 1, RGB (255, 0, 0 ));
M_memdc.selectobject (& pen );

// Drawing part
M_memdc.moveto (rect. Right, rect. Bottom );
M_memdc.lineto (rect. Left, rect. Top );
}
//
// Draw a picture in a static area
Void cdialogdrawdlg: drawonstaticarea ()
{
Cwnd * pwnd = getdlgitem (idc_static); // obtain the window object of the static text box
Crect rect;
Pwnd-> getclientrect (& rect );
//
CDC * PDC = pwnd-> getdc ();//
//
Drawonmem ();
// One-time paste the image drawn in the memory device environment to the screen
PDC-> bitblt (0, 0, rect. Width (), rect. Height (), & m_memdc, 0, 0, srccopy );
Pwnd-> releasedc (PDC );//
//
}

5. Add the drawing code to the onpaint () function (the red part is the added content)

Void cdialogdrawdlg: onpaint ()
{
If (isiconic ())
{
Cpaintdc DC (this); // device context used for plotting

Sendmessage (wm_iconerasebkgnd, reinterpret_cast <wparam> (DC. getsafehdc (), 0 );

// Center the icon in the rectangle of the Workspace
Int cxicon = getsystemmetrics (sm_cxicon );
Int cyicon = getsystemmetrics (sm_cyicon );
Crect rect;
Getclientrect (& rect );
Int x = (rect. Width ()-cxicon + 1)/2;
Int y = (rect. Height ()-cyicon + 1)/2;

// Draw the icon
DC. drawicon (X, Y, m_hicon );
}
Else
{
Cdialog: onpaint ();
// Redraw the image area
Paintstruct pS;
Crect RT;
M_pdrawwnd-> getclientrect (& RT );
CDC * PDC = m_pdrawwnd-> beginpaint (& PS );
Drawonmem ();
PDC-> bitblt (0, 0, Rt. Width (), Rt. Height (), & m_memdc, 0, 0, srccopy );
M_pdrawwnd-> endpaint (& PS );
}
}

You need to use the beginpaint () and endpaint () functions to obtain DC. The reason is that the main form also needs to draw and send messages to each control when processing the wm_paint message. If you do not need the beginpaint () and endpaint () functions to obtain the control DC, the wm_paint message in the message queue of the control will not be deleted, and the control will also call its own onpaint () function to re-paint the control, so that the image we have drawn will be overwritten.

Note: The drawonstaticarea () Here is not used. This is used for plotting in this area during expansion and is written here to facilitate future expansion. In this case, drawonmem () is not required in onpaint (). paste the previously drawn image directly.

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.