Source code: http://download.csdn.net/detail/nuptboyzhb/3961685
Drawing tools
1. draw a straight line
Add the "line" menu item and create a Class Wizard;
Add the member variable my_draw_flag to the cxxxxxxview class and initialize it to 0 in the constructor.
Export my_draw_flag = 1 to draw a straight line in the processing function of the 'line' menu item.
Add window message processing, wm_lbuttondown, wm_mousemove, and wm_lbuttonup
Add member variables
The value is initialized to 0 in the constructor;
Added the onlbuttondownrunflag and onlbuttonuprunflag member variables of the int type. And initialize it to 0 in the constructor of the View class;
Add iplimage member variable
Ø in the onlbuttondown (uint nflags, cpoint point) function, save the point when the mouse is pressed:
My_cvpoint1 = cvpoint (point. X, point. y );
Onlbuttondownrunflag = 1;
Onlbuttonuprunflag = 0;
Bufimg = cvcloneimage (workimg); // bufimg is used to save the image after each painting.
Ø in the onmousemove (uint nflags, cpoint point) function, edit the Code as follows:
If (onlbuttondownrunflag & onlbuttonuprunflag = 0)
{
Cvpointiner_point = cvpoint (point. X, point. y );
Iplimage * SRC;
If (! Workimg)
{
Return;
}
Intthickness, colorr, colorg, colorb;
Thickness = 2; // line width
Colorr = 0;
Colorg = 255;
Colorb = 0;
Src = cvcloneimage (bufimg );
Switch (my_draw_flag)
{
Case0:
Break;
Case1:
Cvflip (SRC );
Cvline (SRC, my_cvpoint1, iner_point, cvscalar (colorr, colorg, colorb), thickness );
Cvflip (SRC );
Workimg = cvcloneimage (SRC); // display the current line
Invalidate (); // redraw
Break;
}
Cvreleaseimage (& SRC );
}
Cscrollview: onmousemove (nflags, point );
Ø message processing function by mouse
Void ccvmfcview: onlbuttonup (uintnflags, cpoint point)
Onlbuttonuprunflag = 1;
Onlbuttondownrunflag = 0;
Draw a rectangle
1. Add the menu item to draw a rectangle
2. Set the attributes as follows:
3. Create a Class Wizard and edit the message response function:
Void ccvmfcview: onmydrawrectangle ()
{
// Todo: add your command handler code here
My_draw_flag = 2;
}
4. In the mousemove message response function, add
Cvflip (SRC );
Cvrectangle (SRC, my_cvpoint1, iner_point, cvscalar (colorr, colorg, colorb), thickness );
Cvflip (SRC );
Workimg = cvcloneimage (SRC); // display the current rectangle
Invalidate (); // redraw
Circle
The procedure for adding a menu is the same as that for adding a rectangle:
The key is to write the case 3 code.
Case 3:
Int r = 0;
R = (INT) SQRT (iner_point.x-my_cvpoint1.x) * (iner_point.x-my_cvpoint1.x)
+ (Iner_point.y-my_cvpoint1.y) * (iner_point.y-my_cvpoint1.y ));
Cvflip (SRC );
Cvcircle (SRC, my_cvpoint1, R, cvscalar (colorb, colorg, colorr), thickness );
Cvflip (SRC );
Workimg = cvcloneimage (SRC); // display the current rectangle
Invalidate (); // redraw
Break;