有一个STATIC控件,ID为IDC_STATIC1,怎样给它添加一个双击事件和右键事件?

解决方案 »

  1.   

    在static控件的属性中,
    选中“NOTIFY”就可以添加点击消息响应事件了
      

  2.   

    就一个CLICKED事件吗?能否添加别的事件?
      

  3.   

    可以的阿
    一种方法:对话框中重载onwndmsg消息,截获你所需要的消息
    另外一种方法:重载cstatic类,处理onlbuutonup等消息
    wish it helpful
      

  4.   

    很简单,用classwizard即可,
    1、用insert ->class添加一个基类为CStatic的类
    2、使用MFCClassWizard对话框,选择“Calss Info”标签,更改message filter的内容为window。再选择messageMaps标签,你就可看到有很多的消息映射了,双击需要的消息就可产生消息函数。
      

  5.   

    BEGIN_MESSAGE_MAP(CLeftView, CFormView) ON_BN_DOUBLECLICKED(IDC_STATIC1,onstatic)
    END_MESSAGE_MAP()
      

  6.   

    同意楼上的意见,
    但是这些都需要

    在static控件的属性中,
    选中“NOTIFY”就可以添加点击消息响应事件了

    或者直接自己写一个class!
      

  7.   

    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 风格,也可以用楼上的方法
      

  8.   

    还有,不能用Static控件的默认ID:  IDC_STATIC
      

  9.   

    我给你一个我的例子:透明static文本控件:
    // 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);
    }