如何使3态复选框在Indeterminate状态下使用不同的位图?
我想改变我的3态复选框使用不同的图片;这些控件是Win98风格的,这种复选框的不确定状态很难与禁用的复选框区分开来(这可能是为什么他们为WinXP样式控件更改了这个,但由于项目中的其他细节,我不能使用那些)。我正在使用Visual C++ 2010,并且在VS的资源编辑器中定义了一个8x8位图。位图的ID是IDB_INDET_CHECK。
我不太确定像这样的标准“技术”是什么;我只是开始进入操作Windows控件和MFC。我的第一次尝试是创建一个类CTriButton,它从CButton派生,重写DrawItem函数,并尝试自己绘制它。然后我使用SubclassDlgItem将我的窗口中的一个复选框转换为这个类(我认为是这样?)。这...有点起作用?复选框不再出现,如果我点击应该出现的位置,一个空的复选框框架出现,但没有其他事情发生(我的代码中的调试消息也没有被发送)。
以下是相关代码,虽然我不确定任何这些是否正确。首先,是窗口的OnInitDialog代码。
接下来是我自定义按钮的
我想改变我的3态复选框使用不同的图片;这些控件是Win98风格的,这种复选框的不确定状态很难与禁用的复选框区分开来(这可能是为什么他们为WinXP样式控件更改了这个,但由于项目中的其他细节,我不能使用那些)。我正在使用Visual C++ 2010,并且在VS的资源编辑器中定义了一个8x8位图。位图的ID是IDB_INDET_CHECK。
我不太确定像这样的标准“技术”是什么;我只是开始进入操作Windows控件和MFC。我的第一次尝试是创建一个类CTriButton,它从CButton派生,重写DrawItem函数,并尝试自己绘制它。然后我使用SubclassDlgItem将我的窗口中的一个复选框转换为这个类(我认为是这样?)。这...有点起作用?复选框不再出现,如果我点击应该出现的位置,一个空的复选框框架出现,但没有其他事情发生(我的代码中的调试消息也没有被发送)。
以下是相关代码,虽然我不确定任何这些是否正确。首先,是窗口的OnInitDialog代码。
BOOL CAffixFilterDlg::OnInitDialog() // CAffixFilterDlg is my CDialog-derived window
{
CDialog::OnInitDialog(); // call basic version
// subclass a CButton-derived control with CTriButton
if ( CBipedHead.SubclassDlgItem(IDC_HEAD, this) ) // CBipedHead is a CTriButton member of CAffixFilterDlg, IDC_HEAD is a checkbox
SetWindowLong(CBipedHead.m_hWnd, GWL_STYLE, CBipedHead.GetStyle() | BS_OWNERDRAW); // set the ownerdraw style
else // subclassing didn't work
_ERROR("Subclassing failed."); // I do not see this error message, so SubclassDlgItem worked?
// initialization continues, but is not relevant...
UpdateWindow();
Invalidate();
return TRUE;
}
接下来是我自定义按钮的
DrawItem
代码。void CTriButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
_DMESSAGE("Drawing TriButton"); // never see this message
CDC dc;
dc.Attach(lpDrawItemStruct->hDC); //Get device context object
int nWidth = GetSystemMetrics(SM_CXMENUCHECK);
int nMargin = ( nWidth - 8 ) / 2;
CRect textRt = lpDrawItemStruct->rcItem;
textRt.right = textRt.right - nWidth - nMargin;
CString text;
GetWindowText(text);
UINT textDrawState = DST_TEXT;
if ( lpDrawItemStruct->itemState & ODS_DISABLED )
textDrawState |= DSS_DISABLED;
dc.DrawState(CPoint(textRt.left, textRt.top), textRt.Size(), text, textDrawState, TRUE, 0, (CBrush*)NULL);
CRect rt = lpDrawItemStruct->rcItem; // initial rect is for entire button
rt.left = rt.right - nWidth; // set left margin
LONG center = ( rt.bottom + rt.top ) / 2;
rt.top = center - nWidth/2;
rt.bottom = center + nWidth/2;
UINT checkDrawState = DFCS_BUTTONCHECK;
if ( lpDrawItemStruct->itemState & ODS_DISABLED )
checkDrawState |= DFCS_INACTIVE;
if ( lpDrawItemStruct->itemState & ODS_CHECKED )
checkDrawState |= DFCS_CHECKED;
else if ( GetCheck() == BST_INDETERMINATE ) {
_VMESSAGE("Indeterminate; custom draw.");
CBitmap indet_check = CBitmap();
indet_check.LoadBitmap(IDB_INDET_CHECK);
CPoint pt = CPoint(rt.left + nMargin, rt.top + nMargin);
CSize sz = CSize(8, 8);
dc.DrawState(pt, sz, &indet_check, DST_BITMAP|DSS_NORMAL);
}
dc.DrawFrameControl(rt, DFC_BUTTON, checkDrawState);
}