"Computer Graphics Course" two. MFC mouse response function simulation drawing software

Source: Internet
Author: User

in the previous article, we talked about MFC's basic functions of drawing graphics, including drawing lines, drawing rectangles, drawing ellipses, and drawing text, while simply understanding the knowledge of graphics by revolving around circles and translating rectangles. In this article I will introduce mouse responses and keyboard responses that allow students to implement a simple software similar to drawing, while giving full play to the students ' imagination and creating things themselves.
Previous article:
"Computer Graphics Course" I. How to use MFC basic drawing function




I. MFC project creation and mouse response

Create a new MFC single-Document Application "Mousepic".



Then, select View, Create Class Wizard, and the Fast key is ctrl+w. This is a very important point of knowledge of MFC, dialog box or single-Document Set button action, response functions are implemented through this operation.


in MFC ClassWizard, select Create the "Cmousepicview" class name for the project, and then select "Wm_lbuttondown" in the "Message" and the left mouse button to press the response action. Also, double-click it to add the function OnLButtonDown ().


Mouse Common message response:
WM_LBUTTONDBCLK Double-click the left mouse button
Wm_lbuttondown Press the left mouse button
Wm_lbuttonup Releasing the left mouse button
Wm_mousemove moving the mouse in the customer area
WM_RBUTTONDBCLK Double-click the right mouse button
Wm_rbuttondown Press the right mouse button
Wm_rbuttonup Releasing the right mouse button
        


Two. MFC implements mouse response actions

1. Press the left mouse button
The double-click function navigates to the "MousePicView.cpp" file, and you can now edit the OnLButtonDown () function. Where the CPoint point parameter records the current position of the left mouse button press, nflags represents the mask.



then add the following code:
Defines a variable of a point type that is used to save the location clicked when the user clicks on the interface CPoint m_point;//the left mouse button press void Cmousepicview::onlbuttondown (UINT nflags, CPoint point) {/ /Assign the position of the current click Point to point m_pointm_point = points; Cview::onlbuttondown (nflags, point);}

2. Left mouse button release
Use the same method to implement the left mouse button deallocation function in the Class Wizard, as shown in.

Adding code is mainly mouse release (bounce):
Mouse release: Record current coordinates void Cmousepicview::onlbuttonup (UINT nflags, CPoint point) {//Draw graphics CDC *p = GetDC ();p->moveto (m_point);    //mouse move to left click P->lineto (point);      Draw a line end to the mouse release point Cview::onlbuttonup (nflags, points);}
The mouse draws the graphic as shown below, but there are two questions: the drawing process is not visible and the result is just a straight line.



Therefore, it is necessary to use the mouse movement function implementation, in the process of mouse movement to draw, while introducing the bool type of variable, the mouse to determine the press or release, press the time to draw operations.


3. Left mouse button movement
Use the same method to implement the left mouse button release function in the Class Wizard.


The complete code looks like this:
//define a variable of point type , which is used to save the position when the user clicks on the interface CPoint m_point;//defines a Boolean variable m_click=true means mouse click false means mouse release bool m_click;//left mouse button press void Cmousepicview::o Nlbuttondown (UINT nflags, CPoint point) {//assigns the position of the current click to point m_pointm_point = Point;m_click = true; Cview::onlbuttondown (nflags, point);} Mouse release: Record current coordinates void Cmousepicview::onlbuttonup (UINT nflags, CPoint point) {//Draw graphics/*CDC *p = GetDC ();p->moveto (m_point    );      Move the mouse to the left button press the P->lineto point; Draw a line end to the mouse release point */m_click = false; Cview::onlbuttonup (nflags, point);} Mouse move draw graphic void Cmousepicview::onmousemove (UINT nflags, CPoint point) {//define brush and select CDC *p=getdc (); CPen Pen (ps_solid, 4, RGB (255,0,0));p->selectobject (pen),//mouse press to draw if (m_click==true) {P->moveto (m_point);p- >lineto (point); m_point = point;} Cview::onmousemove (nflags, point);} 
The drawing results are as follows, which is equivalent to a simple drawing software.


4. Supplementary knowledge
If the onmousemove () mouse move function is missing code m_point = point in the If judgment, it will have an unexpected effect, because you need to draw each time, the mouse moves the current dot coordinate points need to be assigned to the next draw starting coordinates, for P->moveto (m_point) used.


At the same time, you can draw circles, rectangles and other related shapes, not just the straight line.
Mouse move draw graphic void Cmousepicview::onmousemove (UINT nflags, CPoint point) {//define brush and select CDC *p=getdc (); CPen Pen (ps_solid, 1, RGB (255,0,0));p->selectobject (pen);//mouse down to draw if (m_click==true) {P->moveto (m_point);// P->lineto (point);p->rectangle (Point.x, point.y,point.x+20, point.y+30); m_point = point;} Cview::onmousemove (nflags, point);}
the output is as follows:


In drawing, the brush pen is defined, and the correct method needs to be released after the drawing is complete. The core code is as follows:
Define a brush draw rectangle      CPen mypen, *oldpen;       Mypen.createpen (Ps_dash, 2, RGB (0,0,255)); Dashed thick 2 blue      Oldpen = Pdc->selectobject (&mypen);        Old brush Assignment  //Draw graphics//clear      Pdc->selectobject (oldpen);      Mypen.deleteobject ();  


Three. MFC Keyboard response Function 1. Basic Knowledge
Windows defines device-independent encoding for each key, which is called a virtual code. With this virtual code, WINDWOS programmers can use this virtual code to program. Where the virtual code for some keys on the keyboard is as shown:

Windows key messages are common as follows:
WM_CHAR when tapping a character key on the keyboard, the message is generated
Wm_keydown any key (including character keys) is pressed, the message is generated, and if a character is pressed, a character message is generated at the same time as the message is generated.
Wm_keyup arbitrary corners (including character keys) are released to produce the message
Wm_syskeydown F10 is pressed or Alt is pressed at the same time as the other key
Wm_syskeyup F10 is released or Alt is released at the same time as another key

2. Button response Operation
Similarly, the key press function is established through the Class Wizard.

then add the following code, pressing any key, and the drawn rectangle pans to the right by 40 distance.
mouse button void Cmousepicview::onkeydown (UINT NChar, uint nrepcnt, uint nflags) {//Todo:add your message handler code here an d/or call DEFAULTCDC *p = GetDC ();p->moveto (m_point);       Keyboard Press M_point.x + = +;          Horizontal panning 40p->rectangle (M_point.x, M_point.y, m_point.x+20, m_point.y+30); Cview::onkeydown (NChar, nrepcnt, nflags);}
the drawing figure looks like this:


3. Responding to different keyboard operations
The uint nchar needs to be converted to char character type, then the disk is made, and WASD moves up and down to draw the ellipse.

mouse button void Cmousepicview::onkeydown (UINT NChar, uint nrepcnt, uint nflags) {//Todo:add your message handler code here an               d/or call DEFAULTCDC *p = GetDC (); Char CChar;      The currently pressed character CChar = char (NChar);       Converts the pressed key to a character//definition brush CPen mypen, *oldpen;  Mypen.createpen (Ps_dash, 2, RGB (0,0,255));           Dashed thick 2 Blue Oldpen = P->selectobject (&mypen);      Old brush assignment//painting brush CBrush mybrush, *oldbrush;      Mybrush.createsolidbrush (RGB (0,255,0));      Oldbrush = P->selectobject (&mybrush);       if (CChar = = ' D ') {P->moveto (m_point);          D Key Press M_point.x + = 40; Horizontal to right pan 40p->ellipse (M_point.x, M_point.y, m_point.x+20, m_point.y+40);}       if (CChar = = ' A ') {P->moveto (m_point);          A key presses m_point.x-= 40; Horizontal pan to the left 40p->ellipse (M_point.x, M_point.y, m_point.x+20, m_point.y+40);}       if (CChar = = ' S ') {P->moveto (m_point);          S key Press M_POINT.Y + = 50; Vertical down translation 50p->ellipse (M_point.x, M_point.y, m_point.x+20, m_point.y+40);}if (CChar = = ' W ') {P->moveto (m_point);          W Key Press M_point.y-= 50; Pan up 50p->ellipse (M_point.x, M_point.y, m_point.x+20, m_point.y+40) vertically;}      Clear P->selectobject (Oldpen);      Mypen.deleteobject ();      P->selectobject (Oldbrush); Mybrush.deleteobject (); Cview::onkeydown (NChar, nrepcnt, nflags);}
draw as shown:


4. Key cursor Selection
mouse button void Cmousepicview::onkeydown (UINT NChar, uint nrepcnt, uint nflags) {//cursor operation Char CChar;      The currently pressed character hcursor hcursor = 0;  Show cursor handle hcursor hprevcursor = 0;      Previous cursor Handle CChar = char (NChar);    Converts the pressed key to the character if (CChar = = ' A ') {//load arrow cursor Hcursor = AfxGetApp ()->loadstandardcursor (Idc_arrow);    } if (CChar = = ' B ') {//load arrow cursor Hcursor = AfxGetApp ()->loadstandardcursor (Idc_ibeam);    } if (CChar = = ' C ') {//load arrow cursor Hcursor = AfxGetApp ()->loadstandardcursor (idc_wait);        } if (CChar = = ' X ') {hcursor = AfxGetApp ()->loadstandardcursor (Idc_arrow);        Hprevcursor = SetCursor (hcursor);    if (hprevcursor) destroycursor (hprevcursor);            } else{if (hcursor) {hprevcursor = SetCursor (hcursor);        if (hprevcursor) destroycursor (hprevcursor); }}cview::onkeydown (NChar, nrepcnt, nflags);}


Four. MFC mouse drawing-student work show

Finally show the students do the results, although the code is very simple, the principle is very simple, but the students do really good, the original programming can be such ah, on the one hand to enhance students ' interest in learning, on the other hand to increase their programming ability.








or that sentence, very admire the creativity and imagination of students! And the programming lesson can be done so, improve the students ' programming ability, but also cultivate the interest of students. Hope the article is helpful to you ~
        (by:eastmount 2016-11-20 2:30 P.M.http://blog.csdn.net/eastmount/ )



"Computer Graphics Course" two. MFC mouse response function simulation drawing software

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.