怎样给一个STATIC控件添加一个事件? 有一个STATIC控件,ID为IDC_STATIC1,怎样给它添加一个双击事件和右键事件? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在static控件的属性中,选中“NOTIFY”就可以添加点击消息响应事件了 就一个CLICKED事件吗?能否添加别的事件? 可以的阿一种方法:对话框中重载onwndmsg消息,截获你所需要的消息另外一种方法:重载cstatic类,处理onlbuutonup等消息wish it helpful 很简单,用classwizard即可,1、用insert ->class添加一个基类为CStatic的类2、使用MFCClassWizard对话框,选择“Calss Info”标签,更改message filter的内容为window。再选择messageMaps标签,你就可看到有很多的消息映射了,双击需要的消息就可产生消息函数。 BEGIN_MESSAGE_MAP(CLeftView, CFormView) ON_BN_DOUBLECLICKED(IDC_STATIC1,onstatic)END_MESSAGE_MAP() 同意楼上的意见,但是这些都需要“在static控件的属性中,选中“NOTIFY”就可以添加点击消息响应事件了”或者直接自己写一个class! A static control normally takes no input and provides no output; however, it can notify its parent of mouse clicks if it's created with SS_NOTIFY style.注意SS_NOTIFY可以在Create()中包括SS_NOTIFY 风格,也可以用楼上的方法 还有,不能用Static控件的默认ID: IDC_STATIC 我给你一个我的例子:透明static文本控件:// CStaticTran windowclass CStaticTran : public CStatic{// Constructionpublic: CStaticTran();// Attributespublic:// Operationspublic:// Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CStaticTran) //}}AFX_VIRTUAL// Implementationpublic: virtual ~CStaticTran(); // Generated message map functionsprotected: //{{AFX_MSG(CStaticTran) afx_msg void OnPaint(); afx_msg BOOL OnEraseBkgnd(CDC* pDC); //}}AFX_MSG DECLARE_MESSAGE_MAP()};/////////////////////////////////////////////////////////////////////////////// CStaticTranCStaticTran::CStaticTran(){}CStaticTran::~CStaticTran(){}BEGIN_MESSAGE_MAP(CStaticTran, CStatic) //{{AFX_MSG_MAP(CStaticTran) ON_WM_PAINT() ON_WM_ERASEBKGND() //}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CStaticTran message handlersvoid CStaticTran::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here CFont *fntold,*fnt=GetFont(); CRect rt; GetClientRect(&rt); int bkmodeold=dc.SetBkMode(TRANSPARENT); fntold=dc.SelectObject(fnt); CString szt; GetWindowText(szt); COLORREF clrold=dc.SetTextColor(RGB(255,255,255)); dc.DrawText(szt,rt,DT_VCENTER|DT_CENTER); dc.SetTextColor(RGB(0,0,0)); rt.left-=1;rt.right-=1;rt.top-=1;rt.bottom-=1; dc.DrawText(szt,rt,DT_VCENTER|DT_CENTER); dc.SetTextColor(clrold); dc.SelectObject(fntold); dc.SetBkMode(bkmodeold); // Do not call CStatic::OnPaint() for painting messages}BOOL CStaticTran::OnEraseBkgnd(CDC* pDC) { // TODO: Add your message handler code here and/or call default return TRUE;// return CStatic::OnEraseBkgnd(pDC);} typedef 定义函数的问题 地址家族是什么,解释下? 各位,MFC中的“视图、文档”根本难以看明白…… PostThreadMessage的一个问题 [难题]在Unicode环境下如何调用Ansi类型的DLL DLL 高手请进! TCHAR*如何转换成char*呀? VC中,如何在DLL工程和DLL测试工程中建立Depends关系?并能在测试工程中跟进Dll中去。一定给分! 以下模式识别案例与实际工作开发的案例有多大距离? 如何实现文件保存信息的获取 高难度高技术问题:利用CDC进行文本输出对齐,涉及文本表格问题 在AfxMessageBox在for循环中起什么作用??
选中“NOTIFY”就可以添加点击消息响应事件了
一种方法:对话框中重载onwndmsg消息,截获你所需要的消息
另外一种方法:重载cstatic类,处理onlbuutonup等消息
wish it helpful
1、用insert ->class添加一个基类为CStatic的类
2、使用MFCClassWizard对话框,选择“Calss Info”标签,更改message filter的内容为window。再选择messageMaps标签,你就可看到有很多的消息映射了,双击需要的消息就可产生消息函数。
END_MESSAGE_MAP()
但是这些都需要
“
在static控件的属性中,
选中“NOTIFY”就可以添加点击消息响应事件了
”
或者直接自己写一个class!
注意SS_NOTIFY可以在Create()中包括SS_NOTIFY 风格,也可以用楼上的方法
// CStaticTran windowclass CStaticTran : public CStatic
{
// Construction
public:
CStaticTran();// Attributes
public:// Operations
public:// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CStaticTran)
//}}AFX_VIRTUAL// Implementation
public:
virtual ~CStaticTran(); // Generated message map functions
protected:
//{{AFX_MSG(CStaticTran)
afx_msg void OnPaint();
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
//}}AFX_MSG DECLARE_MESSAGE_MAP()
};/////////////////////////////////////////////////////////////////////////////
// CStaticTranCStaticTran::CStaticTran()
{
}CStaticTran::~CStaticTran()
{
}
BEGIN_MESSAGE_MAP(CStaticTran, CStatic)
//{{AFX_MSG_MAP(CStaticTran)
ON_WM_PAINT()
ON_WM_ERASEBKGND()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CStaticTran message handlersvoid CStaticTran::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
CFont *fntold,*fnt=GetFont();
CRect rt;
GetClientRect(&rt);
int bkmodeold=dc.SetBkMode(TRANSPARENT); fntold=dc.SelectObject(fnt);
CString szt;
GetWindowText(szt);
COLORREF clrold=dc.SetTextColor(RGB(255,255,255));
dc.DrawText(szt,rt,DT_VCENTER|DT_CENTER); dc.SetTextColor(RGB(0,0,0));
rt.left-=1;rt.right-=1;rt.top-=1;rt.bottom-=1;
dc.DrawText(szt,rt,DT_VCENTER|DT_CENTER);
dc.SetTextColor(clrold);
dc.SelectObject(fntold);
dc.SetBkMode(bkmodeold);
// Do not call CStatic::OnPaint() for painting messages
}BOOL CStaticTran::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return TRUE;
// return CStatic::OnEraseBkgnd(pDC);
}