Mouse Programming under WindowsMouse:- A pointing device with one or more buttons.In computing, a mouse (plural mouses, mice, or mouse devices.) is a pointing device that functions by detecting two-dimensional motion relative to its supporting surface.
Physically, a mouse consists of an object held under one of the user's hands, with one or more buttons.
It sometimes features other elements, such as "wheels", which allow the user to perform various system-dependent operations, or extra buttons or features can add more control or dimensional input. The mouse's motion typically translates into the motion of a pointer on a display, which allows for fine control of a Graphical User Interface.
Mouse ActionsMouse Actions Button Down, Button Up Wheel movement Moving mouse Clicking:- Pressing and releasing a mouse button Dragging:- Moving mouse while a button is pressed down Double Clicking:- Clicking a button twice in succession Must occur within a set period of time and with mouse cursorin approximately the same place Form’s System Information class has two properties that give
thisinformation:– int DoubleClickTime– Size DoubleClickSize
Mouse Messages Mouse is the most common input device while working with
windows. As in the case of keyboard, the mouse input also comes in the
form of messages. There are about 20 messages which windows uses to report
events that involve the mouse. We will classify them into four groups:-
Group count MessageID
Client 10 WM_
Non-Client 10 WM_NC
Hit Testing 1 WM_NCHITTEST
ACTIVATION 1 WM_MOUSEACTIVATE
Windows Client-area Messages
Message Handler Sent When
WM_LBUTTONDOWN OnLButtonDown The left mouse button is pressed.
WM_LBUTTONUP OnLButtonUP The left mouse button is released.
WM_LBUTTONDBCLICK OnLButtonDBClick
WM_MBUTTONDOWN OnMButtonDown
WM_MBUTTONUP OnButtonUp
WM_MBUTTONDBCLICK OnMButtonDblClk
WM_RBUTTONDOWN OnRButtonDown
WM_RBUTTONUP OnRButtonUp
WM_RBUTTONDBCLK OnRButtonDbCLK
WM_MOUSEMOVE OnMouseMove The mouse is moved over the window’s client area
Write a program which displays a message wherever you click the left mouse button in the client area.
#include "afxwin.h"
#include "stdafx.h"
#include "resource.h"
class myframe:public CFrameWnd
{
public:
myframe()
{ Create(0,"On Single Left Mouse Button Click");
}
void OnLButtonDown(UINT flag,CPoint pt)
{ CClientDC d(this);
d.SetTextColor(RGB(255,0,0));
d.TextOut(pt.x,pt.y,“Hello",5);
}
DECLARE_MESSAGE_MAP()};BEGIN_MESSAGE_MAP(myframe,CFrameWnd)ON_WM_LBUTTONDOWN()END_MESSAGE_MAP()class myapp:public CWinApp{public:
int InitInstance(){
myframe *p;p=new myframe;p->ShowWindow(3);m_pMainWnd=p;return 1;
}};myapp a;
Non-Client Messages
• This is the group of ten messages which are sent to the window procedure whenever the mouse cursor overlies one of the non-client areas of window. This could be the caption bar, the menu one of the border, or even the scroll bar. All the ten non-client messages closely duplicate the corresponding client area messages
Windows Non-Client Messages
• WM_NCMOUSEMOVE
• WM_NCLBUTTONDOWN
• WM_NCLBUTTONDBCLK
• WM_NCMBUTTONDOWN
• WM_NCMBUTTONUP
• WM_NCMBUTTONDBCLK
• WM_NCRBUTTONDOWN
• WM_NCRBUTTONUP
• WM_NCRBUTTONDBCLK
Write a program to find out whether a mouse is a attached or not; and if it is attached, how many buttons are present on it.
#include"afxwin.h"#include"resource.h"#include"stdafx.h"class myframe: public CFrameWnd{public:
myframe(){ Create(0,"Mouse present/absent");}void test(){ int value, num;
char str[5];value=::GetSystemMetrics(SM_MOUSEPRESENT);if(value==1){
MessageBox("mouse is present");
num=::GetSystemMetrics(SM_CMOUSEBUTTONS);
sprintf(str,"%d",num);
MessageBox(str,"number of mouse button");
}
else
MessageBox("mouse is not present","Attention");
}
};
class myapp :public CWinApp{public:
int InitInstance(){
myframe *p;p=new myframe;p->ShowWindow(3);p->test();m_pMainWnd=p;return 1;
}};myapp a;
Top Related