Windows Programming _ sun Xin C ++ lesson10 graphic drawing and various dialog boxes

Source: Internet
Author: User

Windows Programming _ sun Xin C ++ lesson10 graphic drawing and various dialog boxes

Highlights of this section:
1. Drawing
2. graphic drawing parameter settings
3. coloring of the dialog box Control
4. Create a self-drawn button
5. Texture operations (display bitmap to view)
//************************************** ************************************
1. Drawing
Point, straight line, rectangle, and elliptic
Lab code:
//************************************** ************************************
Void cgraphicview: onlbuttonup (uint nflags, cpoint point)
{
// Todo: add your message handler code here and/or call default
Cclientdc DC (this );
// You do not need to switch the paint brush line to determine its definition.
// Set the width of the paint brush based on the user's width.
// Select the paint brush color from the color dialog box
Cpen pen (m_nlinestyle, m_nlinewidth, m_clr );
Cpen * poldpen = Dc. SelectObject (& pen );
Switch (m_ndrawtype)
{
Case 0:
DC. setpixel (point, RGB (255, 0, 0 ));
Break;
Case 1:
DC. moveTo (m_ptorigin );
DC. lineto (point );
Break;
Case 2:
DC. rectangle (crect (m_ptorigin, point ));
Break;
Case 3:
DC. ellipse (crect (m_ptorigin, point ));
Break;
Default:
Break;
}
DC. SelectObject (poldpen );
Cview: onlbuttonup (nflags, point );
}
//************************************** ************************************
The drawing effect is as follows:

2. graphic drawing parameter settings
(1) linear settings and example Functions
// Set the line type by associating member variables
Void cgraphicview: onset ()
{
// Todo: add your command handler code here
Cdlgsetting DLG;
DLG. m_nlinewidth = m_nlinewidth;
DLG. m_nlinestyle = m_nlinestyle;
DLG. m_clr = m_clr;
If (idok = DLG. domodal ())
{
M_nlinewidth = DLG. m_nlinewidth;
M_nlinestyle = DLG. m_nlinestyle;
}
}
// Use the example control as an example to select to redraw a straight line
Void cdlgsetting: onpaint ()
{
Cpaintdc DC (this); // device context for painting
 
// Todo: add your message handler code here
Updatedata ();
Cpen pen (m_nlinestyle, m_nlinewidth, m_clr); // The parameter is selected by the reselect
Cpen * poldpen = Dc. SelectObject (& pen );
Crect rect;
Getdlgitem (idc_sample)-> getwindowrect (& rect); // screen Coordinate
Screentoclient (& rect); // The Screen coordinate conversion is the customer coordinate.
DC. moveTo (rect. Left + 20, rect. Top + rect. Height ()/2); // draw the sample line
DC. lineto (rect. Right-20, rect. Top + rect. Height ()/2 );
DC. SelectObject (poldpen );
// Do not call cdialog: onpaint () for painting messages
}
(2) Color Selection
// Use the system color dialog box
Void cgraphicview: oncolordlg ()
{
// Todo: add your command handler code here
Ccolordialog colordlg;
// Colordlg. m_cc.flags = cc_rgbinit; // error because the previous mark is cleared
Colordlg. m_cc.flags | = cc_fullopen | cc_rgbinit;
Colordlg. m_cc.rgbresult = m_clr;
If (idok = colordlg. domodal ())
{
M_clr = colordlg. m_cc.rgbresult;
}
}
(4) font selection
// Use the system font dialog box
Void cgraphicview: onfont ()
{
// Todo: add your command handler code here
Cfontdialog fontdlg;
Logfont lf;
If (idok = fontdlg. domodal ())
{
If (m_font.m_hobject)
M_font.deleteobject ();
Memcpy (& lf, fontdlg. m_cf.lplogfont, sizeof (logfont ));
M_font.createfontindirect (& lf); // associate the font
M_strfontname = lf. lffacename;
}
Invalidate ();
}
//************************************** * ************************************ 8
3. coloring of the dialog box Control
// Process the wm_ctlcolor message
Hbrush cdlgsetting: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor)
{
Hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor );
 
// Todo: change any attributes of the DC here
If (idc_line_style = pwnd-> getdlgctrlid () // linear control coloring
{
PDC-> settextcolor (RGB (255, 0, 0 ));
PDC-> setbkmode (transparent );
Return m_brush;
}
If (idc_line_width = pwnd-> getdlgctrlid () // color the line width control
{
PDC-> settextcolor (RGB (255, 0, 0 ));
PDC-> setbkcolor (RGB (0, 0, 255 ));
Return m_brush;
}
If (idc_text = pwnd-> getdlgctrlid () // test the text box and select the font
PDC-> SelectObject (& m_font );
// Todo: return a different brush if the default is not desired
Return HBr;
}
// The control of the coloring dialog box is as follows:

4. Create a self-drawn button
Insert a ctestbutton class, select cbutton as the base class, And reload the virtual function.
Virtual void drawitem (lpdrawitemstruct); // copy the sample code from msdn.
5. Texture operations (display bitmap to view)
Texture operation steps:
Step 1: Create a bitmap
Step 2: Create compatible DC
Step 3: select the bitmap to compatible DC
Step 4: paste DC-compatible bitmaps to the current DC
The experiment code is as follows:
//************************************** *****************************
// Called when the background is erased
Bool cgraphicview: onerasebkgnd (CDC * PDC)
{
// Todo: add your message handler code here and/or call default
Cbitmap BMP;
If (BMP. loadbitmap (idb_bitmap1) // load bitmap
{
Bitmap BMP Info;
BMP. getbitmap (& BMP info); // retrieves bitmap Information

CDC dccompatible;
Dccompatible. createcompatibledc (PDC); // create compatible DC

Cbitmap * poldbitmap = dccompatible. SelectObject (& BMP); // select the bitmap to compatible DC.

Crect rect;
Getclientrect (& rect );
PDC-> bitblt (, rect. Width (), rect. Height (), & dccompatible, srccopy); // 1:1 proportional copy
// PDC-> stretchblt (0, 0, rect. Width (), rect. Height (), & dccompatible,
//, BMP info. bmwidth, BMP info. bmheight, notsrccopy); // stretch copy
Dccompatible. SelectObject (poldbitmap );
Return true;
}
Else
Return cview: onerasebkgnd (PDC );
}
//************************************** *****************************

The parameter in dwrop in bitblt is set to srccopy (do not change the copy). The texture effect is as follows:

The parameter in dwrop in bitblt is set to notsrccopy (reverse effect). The texture effect is as follows:

//************************************** ****************************************
Summary:
1. Master the usage of several system dialogs, especially how to set the initial values and obtain the returned values.
2. Control coloring in the dialog box
3. Master the texture operations compatible with DC (here is a question: compatible with DC and who are compatible with why? To be resolved)

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.