// 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类,自己处理,可能封装性会更好一些
在各位的指点下,我在派生类中响应单击消息,并重载了=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); } 却发现没有效果,请高手指点。
你跟踪下OnCtlColor函数,看看进去了没,没进去当然没效果了
void ItemTextStatic::OnKillFocus(CWnd* pNewWnd) { CStatic::OnKillFocus(pNewWnd);// TODO: Add your message handler code here m_BKflag=false; pNewWnd->InvalidateRect(NULL); } 没效果?
{
CDBlistDlg* pDlg=(CDBlistDlg*)(AfxGetApp()->m_pMainWnd);
CMystatic* pSC=(CMystatic*)pDlg->GetDlgItem(nID);
CDC*pDC=pSC->GetDC();
pDC->SetBkColor(RGB(0,0,255));
}
我这样做但一单击程序就错误
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类,自己处理,可能封装性会更好一些
这样做是不对的, 你不是重载了CStatic了吗? 在CMyStatic里响应单击事件, 然后自绘。
如果我建立的控件较多,是否有更好的方法?
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));}
为何没反应?如何在此消息响应函数中实现我的目的?
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);
}
却发现没有效果,请高手指点。
{
CStatic::OnKillFocus(pNewWnd);// TODO: Add your message handler code here
m_BKflag=false;
pNewWnd->InvalidateRect(NULL);
}
没效果?
m_BKflag=false;
pOldWnd->InvalidateRect(NULL);
http://topic.csdn.net/u/20101220/18/93254441-a21f-4b85-9b27-9e966e5b0afe.html
focus.