VC++6.0建立了基于对话框的应用程序,单击某按钮,在对话框某位置处动态建立一个静态控件
ID为ID_STATIC(类为CMystatic:CStatic)
,要实现单击该静态控件使其呈蓝色,文本显示为“hello”,如何实现?

解决方案 »

  1.   

    在CMystatic类中响应鼠标单击消息 然后改变颜色和文字
      

  2.   

    void CDBlistDlg::OnTextStaticClicked(UINT nID)
    {
    CDBlistDlg* pDlg=(CDBlistDlg*)(AfxGetApp()->m_pMainWnd);
    CMystatic* pSC=(CMystatic*)pDlg->GetDlgItem(nID);
    CDC*pDC=pSC->GetDC();
    pDC->SetBkColor(RGB(0,0,255));
    }
    我这样做但一单击程序就错误
      

  3.   

    //CStatic控件勾上notify属性,或者使用GetDlgItem(IDC_STATIC1)->ModifStyle(0, SS_NOTIFY);
    BOOL bClick; // 成员变量,初始化为FALSE
    HBRUSH CXXDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
    {
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

    // TODO: Change any attributes of the DC here
    if(IDC_STATIC1 == pWnd->GetDlgCtrlID() && bClick)
    {
    pDC->SetTextColor(RGB(0, 0, 255));
    }
    // TODO: Return a different brush if the default is not desired
    return hbr;
    }// CStatic控件的BN_CLICKED消息,修改BOOL标记值,然后强制刷新
    void CXXDlg::OnStatic1() 
    {
    // TODO: Add your control notification handler code here
    bClick = TRUE;
    CWnd* pStatic = GetDlgItem(IDC_STATIC1);
    CRect rc;
    pStatic->GetWindowRect(rc);
    ScreenToClient(rc);
    InvalidateRect(rc);
    }
    当然你可以重写CStatic类,自己处理,可能封装性会更好一些
      

  4.   


    这样做是不对的, 你不是重载了CStatic了吗? 在CMyStatic里响应单击事件, 然后自绘。
      

  5.   

    to:VisualEleven
    如果我建立的控件较多,是否有更好的方法?
      

  6.   

    我试试看,但我我想把这个类封装后,想使用STatic控件一样,在对话框中响应单击,然后改变它的属性
      

  7.   

    再说清楚些:VC++6.0建立了基于对话框的应用程序,单击某按钮,在对话框某位置处动态建立8个静态控件(根据需要可能更多,在此设为8个)
    ID为ID_CS_TEXT_ITEM_1+i(i=0~7,类为ItemStatic:CStatic),在对话框类中添加了该类静态控件的单击消息
    afx_msg void OnTextStaticClicked(UINT nID);和ON_CONTROL_RANGE(BN_CLICKED,ID_CS_TEXT_ITEM_1,ID_CS_TEXT_ITEM_1+7,OnTextStaticClicked)要实现单击该静态控件使其呈蓝色,文本显示为“hello”,如何实现?
    void CDBlistDlg::OnTextStaticClicked(UINT nID)
    { CDBlistDlg* pDlg=(CDBlistDlg*)(AfxGetApp()->m_pMainWnd);
    ItemStatic* pSC=(ItemStatic*)pDlg->GetDlgItem(nID);
    CDC*pDC=pSC->GetDC();
    pDC->SetBkColor(RGB(0,0,255));}
    为何没反应?如何在此消息响应函数中实现我的目的?
      

  8.   

    在各位的指点下,我在派生类中响应单击消息,并重载了=WM_CTLCOLOR消息,实现了要求,代码如下:
    void ItemTextStatic::OnClicked() 
    {
    // TODO: Add your control notification handler code here
        m_BKflag=true;
    CWnd* pSC=this;
    pSC->SetWindowText("hello");
    pSC->InvalidateRect(NULL,TRUE);
    }HBRUSH ItemTextStatic::CtlColor(CDC* pDC, UINT nCtlColor) 
    {
    // TODO: Change any attributes of the DC here
    if(m_BKflag==false)
    {
    pDC->SetBkMode(TRANSPARENT);//OPAQUE
    pDC->SetTextColor(m_clrTextRed);//text 
    pDC->SetBkColor(m_clrBkgndWhite);// text   bkgnd 
            return   m_brBkgndWhite;
    }
    else
    {
    pDC->SetBkMode(TRANSPARENT);//OPAQUE
    pDC->SetBkColor(m_clrBkgndBlue);// text   bkgnd
            return  m_brBkgndBlue;
    }
      
    // TODO: Return a non-NULL brush if the parent's handler should not be called
    //return NULL;
    }
    但要实现控件失去焦点(鼠标点击其他控件或位置),背景色变回白色,我响应WM_KILLFOCUS消息:
    void ItemTextStatic::OnKillFocus(CWnd* pNewWnd) 
    {
    CStatic::OnKillFocus(pNewWnd);

    // TODO: Add your message handler code here
    m_BKflag=false;
    pNewWnd->InvalidateRect(NULL);
    }
    却发现没有效果,请高手指点。
      

  9.   

    你跟踪下OnCtlColor函数,看看进去了没,没进去当然没效果了
      

  10.   

    void ItemTextStatic::OnKillFocus(CWnd* pNewWnd)  
    {
    CStatic::OnKillFocus(pNewWnd);// TODO: Add your message handler code here
    m_BKflag=false;
    pNewWnd->InvalidateRect(NULL);
    }
    没效果?
      

  11.   

    那只能在click的那个中,找到要变的那个,然后:
    m_BKflag=false;
    pOldWnd->InvalidateRect(NULL);
      

  12.   

    参考:
    http://topic.csdn.net/u/20101220/18/93254441-a21f-4b85-9b27-9e966e5b0afe.html
      

  13.   

    You would not get WM_SETFOCUS to a static control, because static controls never get
    focus.