鼠标单击和双击处理

/****************************************************************************
* ===========================================================================
*
* 文件名: mousehandle.h
* 描述: 
*
* 版本: 1.0
* 创建时间: 2013-4-5 11:22:5
* 编译环境: Visual Studio 2005
*
* 作者: 张永昌
* 单位: 华中师范大学计算机科学系
*
* ===========================================================================
*****************************************************************************/
#ifndef MOUSEHANDLE_H_
#define MOUSEHANDLE_H_


#ifndef MESSAGE_TEST
#define MESSAGE_TEST
#endif


class IMouseListener
{
public:
	virtual void MouseDown(int nX, int nY) = 0;
	virtual void MouseUp(int nX, int nY) = 0;
	virtual void MouseDbClick(int nX, int nY) = 0;
	virtual void MouseMove(int nX, int nY) = 0;
	virtual ~IMouseListener();
};


class IEnventHandle
{
public:
	virtual void Click(int nX, int nY) = 0;
	virtual void DbClick(int nX, int nY) = 0;
	virtual ~IEnventHandle();
};
#endif
#include "stdafx.h"
#include "mousehandle.h"

IMouseListener::~IMouseListener()
{
}

IEnventHandle::~IEnventHandle()
{
}
/****************************************************************************
* ===========================================================================
*
* 文件名: mousehandleimp.h
* 描述: 
*
* 版本: 1.0
* 创建时间: 2013-4-6 21:32:1
* 编译环境: Visual Studio 2005
*
* 作者: 张永昌
* 单位: 华中师范大学计算机科学系
*
* ===========================================================================
*****************************************************************************/
#ifndef MOUSEHANDLEIMP_H_
#define MOUSEHANDLEIMP_H_
#include "mousehandle.h"

class MouseListener : public IMouseListener
{
public:
	MouseListener(IEnventHandle *pEnventHandle);

	// Interface for mouse
public:
	virtual void MouseDown(int nX, int nY);
	virtual void MouseUp(int nX, int nY);
	virtual void MouseDbClick(int nX, int nY);
	virtual void MouseMove(int nX, int nY);

public:
	virtual ~MouseListener();

private:
	// Internal mouse thing type
	typedef enum _eMouseThing
	{
		CLICKTHING = 0,
		DBCLICKTHING = 1
	}eMouseThing;

	eMouseThing m_MouseThing;
	IEnventHandle *m_pEnventHandle;

	// Point buffer
	int m_nCachePointX;
	int m_nCachePointY;

};

class ControlPointHandle : public IEnventHandle
{
public:
	ControlPointHandle();

	// Interface for MouseListener
public:
	virtual void Click(int nX, int nY);
	virtual void DbClick(int nX, int nY);

	virtual ~ControlPointHandle();

private:

	typedef enum _eSelectState
	{
		NOTBEGIN = 0,
		BEGIN = 1,
		END = 2
	}eSelectState;

	eSelectState m_SelectState;
};

#endif

#include "stdafx.h"
#include "mousehandleimp.h"
#include "controlpointimp.h"

MouseListener::MouseListener(IEnventHandle *pEnventHandle)
{
    m_pEnventHandle = pEnventHandle;
}

void MouseListener::MouseDown(int nX, int nY)
{
    m_nCachePointX = nX;
    m_nCachePointY = nY;
}

void MouseListener::MouseUp(int nX, int nY)
{
    MSG msg;
    DWORD dwBegin = GetTickCount();
    DWORD dwClickInterval = 0;

    while (true)
    {
        if (::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
        {
            if (msg.message == WM_LBUTTONDBLCLK)
            {
                m_MouseThing = DBCLICKTHING;
                break;
            }
        }

        dwClickInterval = GetTickCount() - dwBegin;

        if (dwClickInterval > GetDoubleClickTime())
        {
            m_MouseThing = CLICKTHING;
            m_pEnventHandle->Click(m_nCachePointX, m_nCachePointY);
            break;
        }
        else
        {
            continue;
        }
    }
}

void MouseListener::MouseDbClick(int nX, int nY)
{
    MSG msg;
    DWORD dwBegin = GetTickCount();
    DWORD dwClickInterval = 0;

    while (true)
    {
        if (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            if (msg.message == WM_LBUTTONUP && m_MouseThing == DBCLICKTHING)
            {
                m_pEnventHandle->DbClick(m_nCachePointX, m_nCachePointY);
                break;
            }
        }

        dwClickInterval = GetTickCount() - dwBegin;

        if (dwClickInterval > GetDoubleClickTime())
        {
            m_MouseThing = CLICKTHING;
            break;
        }
        else
        {
            continue;
        }
    }

    m_MouseThing = CLICKTHING;
}

void MouseListener::MouseMove(int nX, int nY)
{
}

MouseListener::~MouseListener()
{
}

ControlPointHandle::ControlPointHandle()
{
}

ControlPointHandle::~ControlPointHandle()
{
}

void ControlPointHandle::Click(int nX, int nY)
{
	CString sTemp;
	sTemp.Format(_T("Click\n"));
	TRACE(sTemp);

    if (NOTBEGIN == m_SelectState)
    {
        m_SelectState = BEGIN;
    }
    else if (BEGIN == m_SelectState)
    {
        m_SelectState = BEGIN;
    }
}

void ControlPointHandle::DbClick(int nX, int nY)
{
	CString sTemp;
	sTemp.Format(_T("DbClick\n"));
	TRACE(sTemp);

    if (BEGIN == m_SelectState)
    {
        m_SelectState = NOTBEGIN;
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值