AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > VC编程

Visual C++/MFC入门教程(三)

51自学网 2015-08-30 http://www.wanshiok.com

3.2 接收用户输入

在视中接收鼠标输入:

鼠标消息是我们常需要处理的消息,消息分为:鼠标移动,按钮按下/松开,双击。利用ClassWizard可以轻松的添加这几种消息映射,下面分别讲解每种消息的处理。

WM_MOUSEMOVE对应的函数为OnMouseMove( UINT nFlags, CPoint point ),nFlags表明了当前一些按键的消息,你可以通过“位与”操作进行检测。

  • MK_CONTROL Ctrl键是否被按下   Set if the CTRL key is down.

  • MK_LBUTTON 鼠标左键是否被按下   Set if the left mouse button is down.

  • MK_MBUTTON 鼠标中间键是否被按下   Set if the middle mouse button is down.

  • MK_RBUTTON 鼠标右键是否被按下   Set if the right mouse button is down.

  • MK_SHIFT Shift键是否被按下   Set if the SHIFT key is down.

point表示当前鼠标的设备坐标,坐标原点对应视左上角。

WM_LBUTTONDOWN/WM_RBUTTONDOWN(鼠标左/右键按下)对应的函数为OnLButtonDown/OnRButtonDown( UINT nFlags, CPoint point )参数意义和OnMouseMove相同。

WM_LBUTTONUP/WM_RBUTTONUP(鼠标左/右键松开)对应的函数为OnLButtonUp/OnRButtonUp( UINT nFlags, CPoint point )参数意义和OnMouseMove相同。

WM_LBUTTONDBLCLK/WM_RBUTTONDBLCLK(鼠标左/右键双击)对应的函数为OnLButtonDblClk/OnRButtonDblClk( UINT nFlags, CPoint point )参数意义和OnMouseMove相同。

下面我用一段伪代码来讲解一下这些消息的用法:

代码的作用是用鼠标拉出一个矩形global BOOL fDowned;//是否在拉动global CPoint ptDown;//按下位置global CPoint ptUp;//松开位置OnLButtonDown(UINT nFlags, CPoint point){ fDowned=TRUE; ptUp=ptDown=point; DrawRect();...}OnMouseMove(UINT nFlags, CPoint point){ if(fDowned) {  DrawRect();//恢复上次所画的矩形  ptUp=point;  DrawRect();//画新矩形 }}OnLButtonUp(UINT nFlags, CPoint point){ if(fDowned) {  DrawRect();//恢复上次所画的矩形  ptUp=point;  DrawRect();//画新矩形  fDowned=FALSE; }}DrawRect(){//以反色屏幕的方法画出ptDown,ptUp标记的矩形 CClientDC dc(this); MakeRect(ptDown,ptUp); SetROP(NOT); Rect();}

坐标间转换:在以上的函数中point参数对应的都是窗口的设备坐标,我们应该将设备坐标和逻辑坐标相区别,在Value

Description
0?Scan code (OEM-dependent value).
8Extended key, such as a function key or a key on the numeric keypad (1 if it is an extended key).
9?0Not used.
11?2Used internally by Windows.
13Context code (1 if the ALT key is held down while the key is pressed; otherwise 0).
14Previous key state (1 if the key is down before the call, 0 if the key is up).
15Transition state (1 if the key is being released, 0 if the key is being pressed).

 

 
 
说明
:本教程来源互联网或网友上传或出版商,仅为学习研究或媒体推广,wanshiok.com不保证资料的完整性。

上一篇:Visual C++/MFC入门教程(五)  下一篇:Visual C++/MFC入门教程(四)