MFC实现STATIC TEXT控件的鼠标事件解决方案

研究了N久才解决的一个问题,看来要想很好地驾驭MFC没这么容易~~ 以下是我解决问题的例子

//添加头文件 MyStatic.h


#ifndef MYSTATIC_H
#define MYSTATIC_H

#pragma   on

ce  

#include   "afxwin.h"  
   
  //   自定义消息  
  #define   WM_STATICMOUSE   WM_USER+5
  #define   WM_STATICLBUTTONDOWN   WM_USER+4
 
  class   CMyStatic   :  
  public   CStatic  
  {  
  public:  
  CMyStatic(void);  
  ~CMyStatic(void);  

  DECLARE_MESSAGE_MAP()  

  afx_msg   void   OnMouseMove(UINT   nFlags,   CPoint   point);  
  afx_msg   void   OnLButtonDown(UINT   nFlags,   CPoint   point);
  };  


#endif


//添加 实现文件 MyStatic.cpp

#include   "StdAfx.h"  
#include   "MyStatic.h"  
   
  CMyStatic::CMyStatic(void)  
  {  
  }  
   
  CMyStatic::~CMyStatic(void)  
  {  
  }  

  BEGIN_MESSAGE_MAP(CMyStatic,   CStatic)  

   ON_WM_MOUSEMOVE()  
   ON_WM_LBUTTONDOWN()

  END_MESSAGE_MAP()  
   
void   CMyStatic::OnMouseMove(UINT   nFlags,   CPoint   point)  
{  
  //   TODO:   在此添加消息处理程序代码和/或调用默认值  
   
  if(this->GetParent()   !=NULL)  

      GetParent()->SendMessage(WM_STATICMOUSE);  
   

CStatic::OnMouseMove(nFlags,   point);  
}  


 void   CMyStatic::OnLButtonDown(UINT   nFlags,   CPoint   point)
 {
     if(this->GetParent()   !=NULL)  
   
       GetParent()->SendMessage(WM_STATICLBUTTONDOWN);
 
  CStatic::OnLButtonDown(nFlags,point);
 }



//对话框头文件 ChartDlg.h
// ChartDlg.h : 头文件
//
#include "OpenGL.h"
#include "MyEdit.h"
#include "MyStatic.h"
#pragma once


// CChartDlg 对话框
class CChartDlg : public CDialog
{

public:
.
.
.
CMyStatic m_mystatic;
.
.
.
afx_msg   LRESULT   OnStaticMouseMove(WPARAM   wParam,   LPARAM   lParam);  
afx_msg   LRESULT   OnStaticLButtonDown(WPARAM   wParam,   LPARAM   lParam);  
};


//对话框实现文件 ChartDlg.cpp
// ChartDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "Chart.h"
#include "ChartDlg.h"
 

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


.
.
.

BEGIN_MESSAGE_MAP(CChartDlg, CDialog)
.
.
.

  ON_MESSAGE(WM_STATICMOUSE,   OnStaticMouseMove)
  ON_MESSAGE(WM_STATICLBUTTONDOWN,   OnStaticLButtonDown)
.
.
.
 
END_MESSAGE_MAP()

.
.
.


BOOL CChartDlg::OnInitDialog(){

 m_mystatic.SubclassDlgItem(IDC_OPENGL,this);  //IDC_OPENGL是static text的id


}
LRESULT  CChartDlg::OnStaticMouseMove(WPARAM   wParam,   LPARAM   lParam)
{
//先得到当前鼠标坐标
CPoint point;
GetCursorPos(&point);

//然后得到static控件rect。
CRect rect;
::GetClientRect(GetDlgItem(IDC_STATIC1)->GetSafeHwnd(), &rect);

//然后把当前鼠标坐标转为相对于rect的坐标。
::ScreenToClient(GetDlgItem(IDC_STATIC1)->GetSafeHwnd(), &point);

 
     if(rect.PtInRect(point))
    {
    CString strPos;
    strPos.Format(L"%d:%d", point.x, point.y);
    GetDlgItem(IDC_STATIC2)->SetWindowTextW(strPos);
    }

  return TRUE;
}

 LRESULT   CChartDlg::OnStaticLButtonDown(WPARAM   wParam,   LPARAM   lParam)
 {
    // MessageBox(L"wokao");
     //先得到当前鼠标坐标
    CPoint point;
    GetCursorPos(&point);
// 然后得到static控件rect。
CRect rect;
::GetClientRect(GetDlgItem(IDC_STATIC1)->GetSafeHwnd(), &rect);

//然后把当前鼠标坐标转为相对于rect的坐标。
::ScreenToClient(GetDlgItem(IDC_STATIC1)->GetSafeHwnd(), &point);

 
     if(rect.PtInRect(point))
    {
    CString strPos;
    strPos.Format(L"%d:%d", point.x, point.y);
    MessageBox(strPos);
    }



  return TRUE;
 
 }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值