Monday, April 1, 2013

visual c++,mfc program to handle mouse events..

   /*TO HANDLE MOUSE EVENTS*/


#include<afxwin.h>
class myframe:public CFrameWnd
{
public:
    myframe()
    {
        Create(0,"HELLLO");
    }

    void OnLButtonDown()
    {
        MessageBox("Left Button Clicked","999");
    }

    void OnRButtonDown()
    {
        MessageBox("Right Button Clicked","555");
    }

    void OnRButtonup()
    {
        MessageBox("Right Button Released","mmmmmm");
    }

    void OnLButtonUp()
    {
        MessageBox("Left Button Released","nnnnnnn");
    }
    DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(myframe,CFrameWnd)
ON_WM_LBUTTONDOWN()
ON_WM_RBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_RBUTTONUP()
END_MESSAGE_MAP()

class myapp:public CWinApp
{
    virtual BOOL InitInstance()
    {
        m_pMainWnd=new myframe();
        m_pMainWnd->ShowWindow(1);
        m_pMainWnd->UpdateWindow();
        return TRUE;
    }
};
myapp app;

 Mouse event  Information

A MouseEventArgs is sent to the handlers of mouse events related to clicking a mouse button and tracking mouse movements. MouseEventArgs provides information about the current state of the mouse, including the location of the mouse pointer in client coordinates, which mouse buttons are pressed, and whether the mouse wheel has scrolled. Several mouse events, such as those that simply notify when the mouse pointer has entered or left the bounds of a control, send an EventArgs to the event handler with no further information.
If you want to know the current state of the mouse buttons or the location of the mouse pointer, and you want to avoid handling a mouse event, you can also use the MouseButtons and MousePosition properties of the Control class. MouseButtons returns information about which mouse buttons are currently pressed. The MousePosition returns the screen coordinates of the mouse pointer and is equivalent to the value returned by Position.
Because some mouse location information is in client coordinates and some is in screen coordinates, you may need to convert a point from one coordinate system to the other. You can do this easily by using the PointToClient and PointToScreen methods available on the Control class.
If you want to handle mouse click events in the proper order, you need to know the order in which click events are raised in Windows Forms controls. All Windows Forms controls raise click events in the same order when a mouse button is pressed and released (regardless of which mouse button), except where noted in the following list for individual controls. The following list shows the order of events raised for a single mouse-button click:
  1. MouseDown event.
  2. Click event.
  3. MouseClick event.
  4. MouseUp event.
Following is the order of events raised for a double mouse-button click:
  1. MouseDown event.
  2. Click event.
  3. MouseClick event.
  4. MouseUp event.
  5. MouseDown event.
  6. DoubleClick event. (This can vary, depending on whether the control in question has the StandardDoubleClick style bit set to true. For more information about how to set a ControlStyles bit, see the SetStyle method.)
  7. MouseDoubleClick event.
  8. MouseUp event.

No comments:

Post a Comment