Sun Xin video tutorial Lesson 10-drawing, color dialog box, font dialog box, changing the font and color of controls, and displaying bitmap in the dialog box

Source: Internet
Author: User
Tags transparent image

How to use a custom paint brush (color, line width, line shape ). How to add the option menu and option Setting dialog box to the program, how to use the standard color dialog box, how to use the font dialog box, and how to preview in the option dialog box. Implement data exchange in the Options dialog box and window class. How to change the background color of the dialog box and control, how to change the text color of the control, and special processing of the button control. How to display a bitmap in a window.

1. Drawing: select a menu item to draw the image you need (points, straight lines, rectangles, and ovans ):

A. First, you need a variable to save the user's selection and coordinates: Add a variable m_ndrawtype and m_ptorigin to the View class;

B. Add a message response function for each menu item;

Void cid102view: ondot () {// todo: add the command handler code m_ndrawtype = 1 here; // This value is 1 when the user selects a vertex !} Void cid102view: Online () {// todo: add the command handler code m_ndrawtype = 2 here; // This value is 2 when the user selects a straight line !} Void cid102view: onrectangle () {// todo: add the command handler code m_ndrawtype = 3 here; // This value is 3 when the user selects a rectangle !} Void cid102view: onellipse () {// todo: add the command handler code m_ndrawtype = 4 here; // The value is 4 when the user selects an elliptic !} Void cid102view: onlbuttondown (uint nflags, cpoint point) {// todo: add the message processing program code and/or call the default value m_ptorigin = point; cview: onlbuttondown (nflags, point );}

C. Draw a picture by capturing the mouse message:

Void cid102view: onlbuttondown (uint nflags, cpoint point) {// todo: add the message processing program code and/or call the default value m_ptorigin = point; cview: onlbuttondown (nflags, point);} void cid102view: onlbuttonup (uint nflags, cpoint point) {// todo: add the message processing program code here and/or call the default cclientdc (this ); cpen pen (ps_solid, 1, RGB (255, 0, 0); // create a red brush DC. selectObject (& pen); // select the paint brush from the device description table cbrush * pbrush = cbrush: fromhandle (hbrush) getstockobject (null_brush )); // create a transparent image brush DC. selectObject (pbrush); // select the paint brush into the device description table switch (m_ndrawtype) {Case 1: // complete the painting point function DC. setpixel (point, RGB (255, 0, 0); break; Case 2: // completes the DC function of linear painting. moveTo (m_ptorigin); DC. lineto (point); break; Case 3: // complete the rectangle painting function DC. rectangle (crect (m_ptorigin, point); break; Case 4: // complete the Elliptical Function DC. ellipse (crect (m_ptorigin, point); break; default: break;} cview: onlbuttonup (nflags, point );}

2. Create a dialog box:

A. You need to insert a dialog box resource first, and then add a new class for the dialog box resource;

B. you can call up the dialog box by clicking the menu item button and set the value on the dialog box. You need to add a variable m_nlinewidth to the control, in the View class, you also need to set a variable to save the selected value m_nlinewidth;

C. Add the message response function of the menu item:

Void cid102view: onsetting () {// todo: add the command processing program code csettingdlg DLG here; // declare the object DLG of a dialog box. m_nlinewidth = m_nlinewidth; // pass the previously set value back to the variable above the dialog box! If (idok = DLG. domodal () // call up the dialog box {m_nlinewidth = DLG. m_nlinewidth; // Save the selection value on the dialog box }}

The Processing Method of other controls is similar to that of the preceding controls, so it is not too long!

3. Create a color dialog box: it is mainly created through a ccolordialog class, and the color button on the menu item is used to respond to the message function! Remember to set variables in the View class to save the selection of color values!

Void cid102view: oncolor () {// todo: add the command handler code ccolordialog DLG here; // create the DLG object in the color dialog box. m_cc.flags | = cc_rgbinit | cc_fullopen; // The method in this area should be noted! DLG. m_cc.rgbresult = m_clr; If (idok = DLG. domodal () // generate a color dialog box! {M_clr = DLG. m_cc.rgbresult; // Save the selected user color !}}

4. Create a font dialog box: the font dialog box is displayed by the menu item, and the font you select is displayed in the View class! Similar to creating a color dialog box!

Void cid102view: onfont () {// todo: add the command handler code cfontdialog DLG here; // class declaration in the font dialog box if (idok = DLG. domodal () // create a font dialog box {If (m_font.m_hobject) // determine whether the font m_font.deleteobject () has been selected; // cut off contact m_font.createfontindirectw (DLG. m_cf.lplogfont); // initialize the font object m_strfontname = DLG. m_cf.lplogfont-> lffacename; // Save the selected font! Invalidate (); // invalidate the window !}}

 

Void cid102view: ondraw (CDC * PDC) {cid102doc * pdoc = getdocument (); assert_valid (pdoc); If (! Pdoc) return; // todo: add the drawing code cfont * poldfont = PDC-> SelectObject (& m_font); PDC-> textoutw (0, 0, m_strfontname); PDC-> SelectObject (poldfont );}

It includes two handling errors in the selected Font: m_font.deleteobject ();

5. Create an example dialog box: mainly completed in the onpain () function of the DLG class:

Void csettingdlg: onpaint () {cpaintdc DC (this); // device context for painting // todo: add the message processing program code updatedata () here (); // The control value is reflected in the variable and must be called! Cpen pen (m_nlinestyle, m_nlinewidth, m_clr); // create a brush DC. selectObject (& pen); crect rect; getdlgitem (idc_sample)-> getwindowrect (& rect); // obtain the rectangular area screentoclient (& rect) in the example box ); // convert the screen coordinates to the coordinates of the customer zone! DC. moveTo (rect. left + 20, rect. top + rect. height ()/2); // draw the DC chart in the example box. lineto (rect. right-20, rect. top + rect. height ()/2); // do not call cdialog: onpaint ()} for drawing messages ()}

6. Modify the background color of the dialog box and its controls: This is mainly done in the dialog box class. In the dialog box class, add a function onctlcolor () and complete the code in the function:

Hbrush csettingdlg: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor) {hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor); // todo: change any properties of DC here // change the background color of the dialog box! If (pwnd-> getdlgctrlid () = idc_line) // determines whether the ID matches the {PDC-> settextcolor (RGB (255, 0, 0 )); // change the text color on the control! PDC-> setbkmode (transparent); // you can change the background mode to transparent! Return m_brush;} // change the font color of the control if (pwnd-> getdlgctrlid () = idc_line_width) {PDC-> settextcolor (RGB (255, 0, 0 )); // change the text color on the control! PDC-> setbkmode (transparent); // you can change the background mode to transparent! PDC-> setbkcolor (RGB (0, 0,255); // set the background color to Blue! Return m_brush;} // change the font of the control if (pwnd-> getdlgctrlid () = idc_text) {PDC-> SelectObject (& m_font);} // todo: if the default paint brush is not required, return another paint brush return HBr; // return m_brush; // return the paint brush created by yourself! Changes the background color of the entire dialog box !}

Modifying the text color and background color of buttons in the dialog box is troublesome. You need to add a cbutton class and then create a variable associated with the cbutton class for the buttons, add a function drawitem () to its cbutton class ():

Void ctestbtn: drawitem (lpdrawitemstruct) {// todo: add your code to draw the specified uint ustyle = dfcs_buttonpush; // This Code only works with buttons. assert (lpdrawitemstruct-> ctltype = odt_button); // If drawing selected, add the pushed style to drawframecontrol. if (lpdrawitemstruct-> itemstate & ods_selected) ustyle | = dfcs_pushed; // draw the button frame.: drawframecontrol (lpdrawitemstruct-> HDC, & lpdrawitemstruct-> rcitem, dfc_button, ustyle); // get the button's text. cstring strtext; getwindowtext (strtext); // draw the button text using the text color red. colorref croldcolor =: settextcolor (lpdrawitemstruct-> HDC, RGB (255, 0, 0);: drawtext (lpdrawitemstruct-> HDC, strtext, strtext. getlength (), & lpdrawitemstruct-> rcitem, dt_singleline | dt_vcenter | dt_center);: settextcolor (lpdrawitemstruct-> HDC, croldcolor );}

7. how to display bitmap in a window: You can add the wm_erasebkgnd () function in the View class to complete the four steps, or in the ondraw function of the dialog box, because the View class works well, the following code is written in the View class:

Bool cid102view: onerasebkgnd (CDC * PDC) {// todo: add the message processing program code and/or call the default value // create a bitmap cbitmap bitmap; bitmap. loadbitmapw (idb_bitmap1); bitmap BMP; bitmap. getbitmap (& BMP); // obtain the bitmap struct. // create a structure compatible with dccdc dccompatible and dccompatible. createcompatibledc (PDC); // select the bitmap to the compatible DC dccompatible. selectObject (& Bitmap); // paste the bitmap compatible with DC to the current DC crect rect; getclientrect (& rect); // PDC-> bitblt (0, 0, rect. width (), rect. height (), & dccompatible, 0, 0, srccopy); PDC-> stretchblt (0, 0, rect. width (), rect. height (), & dccompatible, 0, 0, BMP. bmwidth, BMP. bmheight, srccopy); Return true; // return cview: onerasebkgnd (& Bitmap );}

 

 

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.