1.1.3-learning opencv and MFC mixed programming-drawing tool through the dialog box tool parameter settings draw curve drawing correction

Source: Internet
Author: User

Source code: http://download.csdn.net/detail/nuptboyzhb/3961688

L dialog box

1. "insert"-> "resource"-> "dialog box"

2. The properties of the dialog box are as follows:

Double-click the dialog box to create a dialog box class. Name: lineparameter

3. Add # include "resource. H" to the lineparameter. h header file. Otherwise, an error occurs: idd_dialog4 is not defined.

4. Add the following member variables to the lineparameter class:

Int m_colorr;

Int m_colorg;

Int m_colorb;

Int m_penwidth;

5. initialize the variable in the constructor:

Lineparameter: lineparameter (cwnd * pparent/* = NULL */)

: Cdialog (lineparameter: IDD, pparent)

{

// {Afx_data_init (lineparameter)

// Note: The classwizard will addmember initialization here

//} Afx_data_init

// Initialize data

M_colorb = 0;

M_colorg = 0;

M_colorr = 0;

M_penwidth = 1;

}

6. In the data exchange function, exchange the data of member variables and corresponding controls:

Voidlineparameter: dodataexchange (cdataexchange * PDX)

{

Cdialog: dodataexchange (PDX );

// {Afx_data_map (lineparameter)

// Note: The classwizard will addddx and DDV cballs here

Ddx_text (PDX, idc_color_r, m_colorr );

Ddv_minmaxint (PDX, m_colorr, 0,255 );

Ddx_text (PDX, idc_color_g, m_colorg );

Ddv_minmaxint (pdx, m_colorg, 0,255 );

Ddx_text (PDX, idc_color_ B, m_colorb );

Ddv_minmaxint (PDX, m_colorb, 0,255 );

Ddx_text (PDX, idc_width, m_penwidth );

Ddv_minmaxint (PDX, m_penwidth, 1, 50 );

//} Afx_data_map

}

7. Add a message processing function for the confirmation button:

Voidlineparameter: onok ()

{

// Todo: add extra validation here

Updatedata (true); // write data on the control to the member variable

If (m_colorr> = 0 & m_colorr <= 255) & (m_colorg> = 0 & m_colorg <= 255) & (m_colorb> = 0 & m_colorb <= 255 ))

{

Cdialog: onok (); // close the dialog box

}

Else

{

MessageBox ("enter a number between 0 and 25", "error message", mb_iconexclamation | mb_ OK );

}

}

L use dialog box

1. Add member variables in the cxxxxxxview class and initialize them in the constructor;

Int m_linewidth;

Int m_colorb;

Int m_colorg;

Int m_colorr;

2. Add the lineparameter dialog box object in the message processing function of the "straight line", "circle", and "rectangle" menu items respectively, and pass the data entered in the dialog box to the corresponding member variables:

Lineparameter DLG;

If (DLG. domodal ()! = Idok)

{

Return;

}

M_colorr = DLG. m_colorr;

M_colorg = DLG. m_colorg;

M_colorb = DLG. m_colorb;

M_linewidth = DLG. m_penwidth;

// Delete dialog box

Deletedlg;

3. Modify the onmousemove message processing function and assign the value of the corresponding member variables of the cxxxxview class to the parameters required for drawing (RGB color and line width)

Correct drawing problems:

Question:

The running result shows that the point pressed by the mouse is significantly different from the point displayed on the image. The point when the mouse moves is exactly the same as the point on the image. Therefore, consider the point after the first mouse movement as the starting point of the draw line:

1. Add the cxxxxview member variable onmousemoveflag to record the number of mouse moves. Initialize it to 0 in the constructor;

2. Each time you run the onmousemove message processing function, this sign is incremented by one.

3. When onmousemoveflag is enabled, save the point to the starting point:

My_cvpoint1 = cvpoint (point. X, point. y );

4. In order to prevent the onmousemoveflag from increasing too quickly and overflow, assign it to 3 in the subsequent statement: onmousemoveflag = 3;

5. initialize the variable every time you press the mouse or move the mouse up.

Onmousemoveflag = 0;

Draw a curve

1. Add the "curve" menu item. Set the corresponding properties, add the Class Wizard, and edit the message response function:

Voidccvmfcview: onmydrawcurve ()

{

// Todo: add your command handler code here

My_draw_flag = 4;

Lineparameter DLG;

If (DLG. domodal ()! = Idok)

{

Return;

}

M_colorr = DLG. m_colorr;

M_colorg = DLG. m_colorg;

M_colorb = DLG. m_colorb;

M_linewidth = DLG. m_penwidth;

// Delete dialog box

Deletedlg;

}

2. Modify the onmousemove message processing function. Add case 4: Message Processing:

Case 4: // when you select to draw a curve

Cvflip (workimg );

Cvline (workimg, my_cvpoint1, iner_point, cvscalar (colorb, colorg, colorr), thickness );

Cvflip (workimg );

My_cvpoint1 = cvpoint (point. X, point. y );

Invalidate (); // redraw

Break;

 

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.