mfc-Tenth Lesson (i) Study notes

Source: Internet
Author: User

F Programming Exercises-tenth lesson

Knowledge List

    • Drawing of graphs
    • Settings dialog box
    • Color dialog box
    • Font dialog box
    • How to display a bitmap in a window

(1) Add a menu

Drawing

(2) Add four items to this menu

Dots, lines, rectangles, ellipses

(3) Add a command response for each of the four menu items

    • Note: Class name selection:View class

Here are the four new functions in the view class

F Programming Exercises-tenth lesson

Knowledge List

    • Drawing of graphs
    • Settings dialog box
    • Color dialog box
    • Font dialog box
    • How to display a bitmap in a window

(1) Add a menu

Drawing

(2) Add four items to this menu

Dots, lines, rectangles, ellipses

(3) Add a command response for each of the four menu items

    • Note: Class name selection:View class

Here are the four new functions in the view class

1 voidCgraphicview::ondot ()2 {3     //todo:add Your command handler code here4     5 }6 7 voidCgraphicview::online ()8 {9     //todo:add Your command handler code hereTen      One } A  - voidCgraphicview::onrectangle () - { the     //todo:add Your command handler code here -      - } -  + voidcgraphicview::onellipse () - { +     //todo:add Your command handler code here A      at}

When the user clicks on the menu item, we should, in the corresponding function, save the user's choice, then in the subsequent drawing, we can according to Bai Chun down the selection, draw the graph. So how to save the user's choice?

    • Workaround: Add a member variable in the view class, as shown in:

When a member variable is added to a class, it is necessary to initialize it in the constructor of the class, as follows:

1 Cgraphicview::cgraphicview ()2 {3     //Todo:add Construction Code here4M_ndrawtype=0;5 }6 depending on the user's choice, we set the variable to a different value, the code is as follows:7 8 voidCgraphicview::ondot ()9 {Ten     //todo:add Your command handler code here OneMndrawtype=1; A } - voidCgraphicview::online () - { the     //todo:add Your command handler code here -Mndrawtype=2; - } - voidCgraphicview::onrectangle () + { -     //todo:add Your command handler code here +Mndrawtype=3; A } at voidcgraphicview::onellipse () - { -  -       //todo:add Your command handler code here -Mndrawtype=4;  -  in}
Determine the image

We can use two points to determine our graphics.

    • Click (Do not release) is for a point
    • Loosen for a different point

Naturally: We are going to capture the above two messages separately

    • On the view class, right-click, select Add Windows message Hinder
    • Select: WM_LBUTTONDOWM andWM_LBOTTONUP
    • Edit Code

Again, we should save the message when the left mouse button is clicked, as before, add a member variable to the view class and complete the initialization in the view class's constructor.

1 Cgraphicview::cgraphicview ()2 {3     //Todo:add Construction Code here4M_ndrawtype=0;5m_ptorigin=0;6 }7 All we have to do now is to save the information obtained by the new function in the new variable.8 voidCgraphicview::onlbuttondown (UINT nflags, CPoint point)9 {Ten     //todo:add your message handler code here and/or call default Onem_ptorigin=Point ; A Cview::onlbuttondown (nflags, point); -}

The next step is drawing, first of all, drawing, we need DC,

    • CCLIENTDC DC; Defines an object of a CCLIENTDC

    • materialized to which graphics, it depends on the user's choice, we have saved the user's choice in the variable m_ndrawtype So, here is a switch statement to judge

Next is the user's choice, we need a different way to deal with

      1. If the user chooses a point: about setting a point, here we need to use a function
      2. >

SetPixel (Point point, Colorref crcolor)

    1. If the user chooses a line: Here are two functions * DC. Moveto (m_nptorigin) * DC. Lineto (Point)

  • If the user chooses a rectangle:DC. Rectangle (CRect (m_nptorigin,point))

    There are some places to be aware of this function:
    Parameter requirements: Pointer to CRect, but what we're passing here is a rectangular object, which is the object of CRect (he can construct a rectangle object by two points).

    因为在Rectangle()函数里有这么一条:  如果发现参数是CRect对象而非指针,那么就会隐式的将它的参数转换为一个指向CRect的指针

  • If the user chooses an ellipse:

    dc. Ellipse (CRect (m_nptorigin,point));

    The specific code snippet is as follows:

    1 voidCgraphicview::onlbuttonup (UINT nflags, CPoint point)2 {3     //todo:add your message handler code here and/or call default4CCLIENTDC DC ( This);5     Switch(M_ndrawtype)6     {7      Case 1:8dc. SetPixel (Point,rgb (255,0,0));//Red9          Break;Ten      Case 2: One DC. MoveTo (m_ptorigin); A DC. LineTo (point); -          Break; -      Case 3: the DC. Rectangle (CRect (M_ptorigin,point)); -          Break; -      Case 4: - DC. Ellipse (CRect (M_ptorigin,point)); +          Break; -     } + Cview::onlbuttonup (nflags, point); A}

    Change the drawing color of a graphic
    We know that the color of the line is determined by the color of the handle in the DC, so we can do this:

      1. Create a handle and select a color for the handle
      2. Add this handle to our device description table

    The functions are as follows:
    CPen pen(PS_SOLID,1,RGB(255,0,0));

    参数一:画柄的类型,这里我们选择实线参数二:画出线的宽度参数三:颜色

    The second step is to add the handles we created to the device description table

      • dc.SelectObject(&pen);

    Code snippet:

    //创建画柄CPen pen(PS_SOLID,1,REG(255,0,0));//添加画柄dc.SelectObject(&pen);
    Sets the fill of the brush to be transparent

    The function GetStockObject(NULL_BRUSH) returns a handle to a transparent brush, and then we can use the CBrush::Fromhandle() function, which takes a handle to a brush, returns a CBrush pointer, and we also need to cast the parameter type to the Hbrush type. Because CBrush::Fromhandle() the parameter type of the function is Hbrush, and then it is added to our Device description table using the previous method.

    Settings dialog box

    (1) We want to have a dialog resource, after creating the dialog resource, then create a class for him, the base class is selected as Dialog
    (2) Add two controls: Static text box, edit box
    (3) Associate a variable to this edit box

    Let's take a look at how to associate a variable with a control

    1 Right-click in the edit box 2 Select ClassWizard 3 Click member Variables 4 views Add variables

(4) Next, let's create a menu item that pops up when the user clicks the item.
(5) Add a command response to this menu item (in fact, add a function)

    • Note The class name selects the view class;
    • To have this dialog box displayed in the view class, you must include the header file of the class associated with the dialog box.

Code Snippets

//构建对象CSettting dlg;//显示对话框dlg.DoModal();

After the dialog pops up, the user can set up, and the next step is to save the values the user wants to enter in the view class so that when we create the handle, we can set the relevant information according to the user's input.

Of course, when to save it? Can not be a user input, we will save it, when the user click on the OK button We, we will do the information to save This also requires us to make a judgment before saving the information

User input information, we judge to save, then we save the information where? This also requires us to add a member variable in the view class to hold our information

Code Snippets

1 voidcgraphicview::onsetting ()2 {3     //todo:add Your command handler code here4 csettting dlg;5     //Judging If you click on the OK button6     if(idok==dlg. DoModal ())7     {8Mnlinewidth=Dlg.mnlinewidth;9     }Ten}

Note: In the experiment also found that if we click on the settings again, the previously set value will be changed to 0, just because again click on the settings, he will re-call the function, re-create the dialog object, will call the constructor of this class, then we set the value is 0, in fact, this problem we encountered before, As long as we add the keyword to the object's statement before it is created, it is static, so that when we create the object again, it will not be reinitialized.

mfc-Tenth Lesson (i) Study notes

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.