重绘CheckBox遇到一个有关问题
重绘CheckBox遇到一个问题
比如有一个CheckBox控件 IDC_CHECK1 .h文件 CMyCheckBox m_check1;
cpp文件响应按钮点击事件比如:
void CXXXDlg::OnCheck1()
{
CButton *p1=(CButton*)GetDlgItem(IDC_CHECK1); //在这里设了一个断点
CButton *p2=(CButton*)GetDlgItem(IDC_CHECK2);
p1->SetCheck(1);
p2->SetCheck(1);
}
这时问题就来了,当我点击CheckBox按钮时,为什么进不去OnCheck1()这个函数呢?
附:我重绘CheckBox的代码是这样
------解决方案--------------------
是不是被void CMyCheckBox::OnClicked() 截掉了
------解决方案--------------------
两件事情做了没有?
1.消息映射
ON_BN_CLICKED(check资源编号, OnCheck1())
比如有一个CheckBox控件 IDC_CHECK1 .h文件 CMyCheckBox m_check1;
cpp文件响应按钮点击事件比如:
void CXXXDlg::OnCheck1()
{
CButton *p1=(CButton*)GetDlgItem(IDC_CHECK1); //在这里设了一个断点
CButton *p2=(CButton*)GetDlgItem(IDC_CHECK2);
p1->SetCheck(1);
p2->SetCheck(1);
}
这时问题就来了,当我点击CheckBox按钮时,为什么进不去OnCheck1()这个函数呢?
附:我重绘CheckBox的代码是这样
// MyCheckBox.cpp : implementation file
//
/////////////////////////////////////////////////////////////////////////////
// CMyCheckBox message handlers
void CMyCheckBox::PreSubclassWindow()
{
UINT nBS;
nBS= GetButtonStyle();
SetButtonStyle(nBS| BS_OWNERDRAW);
CButton::PreSubclassWindow();
}
void CMyCheckBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CRect rect = lpDrawItemStruct->rcItem;
CDC *pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
CRect ReRect,TextRect;
// CopyRect(ReRect,rect);
// ReRect.right=rect.Height();
ReRect.top=2;
ReRect.bottom=rect.bottom-2;
ReRect.left=rect.left=2;
ReRect.right=ReRect.bottom-ReRect.top;
CopyRect(TextRect,rect);
TextRect.left=ReRect.right+8;
CString strText;
GetWindowText(strText);
UINT state = lpDrawItemStruct->itemState;
if (state&ODS_DISABLED|| state & ODS_DEFAULT)
{
m_bDisable=TRUE;
}
else
{
m_bDisable=FALSE;
}
CBrush m_br;
m_br.CreateSolidBrush(m_BkColor);
pDC->FillRect(&rect,&m_br);
int nCheck=GetCheck();
if (nCheck)
{
DrawBitmap(pDC,IDB_BITMAP2,ReRect);
}
else
{
DrawBitmap(pDC,IDB_BITMAP3,ReRect);
}
CFont font;
font.CreateFont(m_FontSize,0,0,0,FW_NORMAL,FALSE,FALSE,0,GB2312_CHARSET,OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_MODERN,m_strFontName);
pDC->SelectObject(&font);
pDC->SetBkMode(TRANSPARENT);
if (m_bDisable)
{
pDC->SetTextColor(RGB(128,128,128));
}
else
{
pDC->SetTextColor(RGB(0,0,0));
}
pDC->DrawText(strText,&TextRect,DT_LEFT|DT_VCENTER|DT_SINGLELINE);
font.DeleteObject();
m_br.DeleteObject();
}
void CMyCheckBox::OnClicked()
{
int nState;
nState = GetCheck();
if(nState == 0)
nState = 1;
else
nState = 0;
SetCheck(nState);
}
LRESULT CMyCheckBox::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
if (message==BM_SETCHECK)
{
// m_nChecked=wParam;
// SetMyCheck(wParam);
SetCheck(wParam);SetMyCheck(wParam);
}
if (message==BM_GETCHECK)
{
// return GetCheck();
return GetMyCheck();
// return m_nChecked;
}
return CButton::DefWindowProc(message, wParam, lParam);
}
int CMyCheckBox::GetMyCheck()
{
if (m_nChecked)
{
return 1;
}
return 0;
}
void CMyCheckBox::SetMyCheck(int nState)
{
if (nState)
{
m_nChecked=1;
}
else
{
m_nChecked=0;
}
Invalidate();
}
void CMyCheckBox::SetFontBkColor(COLORREF rgb)
{
m_BkColor=rgb;
}
void CMyCheckBox::SetFontSize(int nSize)
{
m_FontSize=nSize;
}
void CMyCheckBox::SetFontName(CString strFontName)
{
m_strFontName=strFontName;
}
void CMyCheckBox::DrawBitmap(CDC *pDC,UINT nID,CRect &rect)
{
CDC memDC;
BITMAP bmpInfo;
CBitmap bmp;
bmp.LoadBitmap(nID);
bmp.GetBitmap(&bmpInfo);
memDC.CreateCompatibleDC(pDC);
CBitmap *pOldBmp=memDC.SelectObject(&bmp);
pDC->StretchBlt(rect.left,rect.top,rect.right,rect.bottom,
&memDC,0,0,bmpInfo.bmWidth,bmpInfo.bmHeight,SRCCOPY);
pDC->SelectObject(pOldBmp);
bmp.DeleteObject();
}
------解决方案--------------------
是不是被void CMyCheckBox::OnClicked() 截掉了
------解决方案--------------------
两件事情做了没有?
1.消息映射
ON_BN_CLICKED(check资源编号, OnCheck1())