"MFC" mouse, keyboard response events

Source: Internet
Author: User

For MFC this thing, you really need to talk to me a lot of how to class, what derivation, what the devil thing

Tell me how to make a small function, I'll remember it.

This issue for everyone to bring MFC mouse response events

This is also known as the "message map event" in MFC


I. BASIC OBJECTIVES

1, there is a basic MFC program, click "Close" then "close" This program, this is nothing to say, bring the "Cancel" button, the right-caption property of the change to "close" two words can be


2, the Mouse in the dialog box to move, then display its location information, this coordinate is the window internal coordinates


3, if the right mouse button to move the mouse, then in the title bar of the project will display the "right mouse button to move the mouse", release the right mouse button back to the same as


4, if you press the CTRL key on the keyboard or the SHIFT key in the dialog window to move the mouse, the title bar will also have a corresponding hint, whether the same press will also be prompted


5, left click the dialog box, will pop up the dialog box, show where the mouse location



Second, the production process

1, the first new MFC project, in addition to the "Cancel" button renamed to "Off", all delete, drag out a static text in the middle of the window, note that the static text size is large enough, otherwise can not accommodate the "mouse in x=?,y= position" such as the font, as shown, This no longer repeat, you can refer to my previous "MFC" Student information management, to achieve the list control node additions and deletions (click the Open link).



2. Note that you want to change the "arrange text" in the style of the static Text property to "center"



3, drag the control, start adding mouse response, keyboard response to the event, the following only add mouse response time, keyboard response can be placed in the mouse response to the implementation of the function, add response to the event of the process first select "View", "New Class Wizard"



4, then pop up the following dialog box, in the lower-right corner of the messages box, find Wm_lbuttondown and Wm_mousemove, double-click, or after clicking, select "Add Function", then click on Confirm or edit Code, This means: Tell MFC to click the left mouse button in this dialog box, move the mouse, respond to the processing function.




5, click on Edit code to automatically jump to the ClassView in the page, click "OK" to manually cut the past, at this time, in the ClassView in the just we added the "Response class", or I prefer to call it "response function", anyway, you know what is on the line, I know there's a theory. I am not professional, anyway you like. Click on these two response functions, write the code respectively, the two response event functions, respectively, in the original//todo:add your message handler code here and/or call default and Cdialog::onlbuttondown ( nflags, point); write code between



6, for the Void Cmousedlg::onlbuttondown (UINT nflags, CPoint Point) to complete the left-click dialog to show the mouse location where the mission,


See the following code:

void Cmousedlg::onlbuttondown (UINT nflags, CPoint point) {//Todo:add your message handler code here and/or call DEFAULTC String str;//can be used to remove the position of the mouse between the two member variables Point.x,point.y, notice that the head of this function, the system has been automatically brought into the mouse class CPoint pointstr. Format ("Left click, mouse in x=%d,y=%d position", point.x,point.y); AfxMessageBox (str); Cdialog::onlbuttondown (nflags, point);}


7, for void Cmousedlg::onmousemove (UINT nflags, CPoint point) is the function of the mouse movement.

Mouse response events with keyboard response time as below, not for what.

void Cmousedlg::onmousemove (UINT nflags, CPoint point) {//Todo:add your message handler code here and/or call default//with Position of the mouse and put it in the static text CString str;str. Format ("Mouse in x=%d,y=%d position", point.x,point.y); Setdlgitemtext (IDC_STATIC,STR);//If you press CTRL without pressing SHIFTIF (Mk_control & nflags) &&! ( Mk_shift & nflags))//settings dialog titled "Pressed CTRL at Move mouse" SetWindowText ("Pressed CTRL on Move mouse");//If you do not press CTRL and press Shiftelse if (! Mk_control & nflags) && (Mk_shift & nflags)) SetWindowText ("Press SHIFT while moving the Mouse");//If you press CTRL with Shiftelse if ( (Mk_control & nflags) && (Mk_shift & nflags)) setwindowtext ("Pressed Ctrl+shift on Move Mouse");//If you press the right mouse button else if ( Mk_rbutton & nflags) SetWindowText ("Press the right mouse button to move the mouse");//If nothing is pressed Elsesetwindowtext ("mouse"); Cdialog::onmousemove (nflags, point);}

After compiling here, the entire project has been completed and it must be noted that:

for void Cmousedlg::onlbuttondown (UINT nflags, CPoint point) with void Cmousedlg::onmousemove (UINT nflags, CPoint point) It is the mouse left click dialog box once with the mouse in the dialog box to move once to execute once.

CString STR, a string that the WIN32 program reads, sets its value in the same way as its own format () method, because its nature is a class, not a single variable, not directly equal to

"MFC" mouse, keyboard response events

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.