研究了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;
}
#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;
}