简介及相关资料:
我需要有主题的公共控件,但具有不同的文本颜色和透明背景。我遇到了这个问题中详细记录的问题。
我通过处理取得了一些进展,NM_CUSTOMDRAW
并决定先完成复选框。
问题:
我在确定复选框的状态时遇到了困难,所以我无法传递正确的参数DrawThemeBackground()
。
代码胜于雄辩,所以这里是片段:
case WM_NOTIFY:
{
if( ((LPNMHDR)lParam)->code == NM_CUSTOMDRAW )
{
switch( ((LPNMHDR)lParam)->idFrom )
{
case IDC_CHECK1:
{
switch( ((LPNMCUSTOMDRAW)lParam)->dwDrawStage )
{
case CDDS_PREERASE:
{
HRESULT hr = DrawThemeParentBackground(
((LPNMCUSTOMDRAW)lParam)->hdr.hwndFrom
((LPNMCUSTOMDRAW)lParam)->hdc,
&((LPNMCUSTOMDRAW)lParam)->rc );
if( FAILED(hr) ) // if failed draw without theme
{
SetWindowLongPtr( hDlg, DWLP_MSGRESULT
(LONG_PTR)CDRF_DODEFAULT );
return TRUE;
}
HTHEME hTheme = OpenThemeData(
((LPNMCUSTOMDRAW)lParam)->hdr.hwndFrom,
L"BUTTON" );
if( ! hTheme ) // if failed draw without theme
{
CloseThemeData(hTheme);
SetWindowLongPtr( hDlg, DWLP_MSGRESULT
(LONG_PTR)CDRF_DODEFAULT );
return TRUE;
}
// draw the state-->this is the problem part
// I thought this might be useful
LRESULT state = SendMessage(
((LPNMCUSTOMDRAW)lParam)->hdr.hwndFrom,
BM_GETSTATE, 0, 0 );
int stateID; // parameter for DrawThemeBackground
switch( ((LPNMCUSTOMDRAW)lParam)->uItemState )
{
case CDIS_HOT:
{
if( IsDlgButtonChecked( hDlg, ((LPNMCUSTOMDRAW)lParam)->hdr.idFrom ) )
stateID = CBS_CHECKEDHOT;
else
stateID = CBS_UNCHECKEDHOT;
break;
}
case CDIS_DEFAULT:
{
if( IsDlgButtonChecked( hDlg, ((LPNMCUSTOMDRAW)lParam)->hdr.idFrom ) )
stateID = CBS_CHECKEDNORMAL;
else
stateID = CBS_UNCHECKEDNORMAL;
break;
}
case CDIS_FOCUS:
{
if( IsDlgButtonChecked( hDlg, ((LPNMCUSTOMDRAW)lParam)->hdr.idFrom ) )
stateID = CBS_CHECKEDNORMAL;
else
stateID = CBS_UNCHECKEDNORMAL;
break;
}
case CDIS_SELECTED:
{
if( IsDlgButtonChecked( hDlg, ((LPNMCUSTOMDRAW)lParam)->hdr.idFrom ) )
stateID = CBS_CHECKEDPRESSED;
else
stateID = CBS_UNCHECKEDPRESSED;
break;
}
}
RECT r;
SIZE s;
// get check box dimensions so we can calculate
// rectangle dimensions for text
GetThemePartSize( hTheme,
((LPNMCUSTOMDRAW)lParam)->hdc,
BP_CHECKBOX, stateID, NULL,
TS_TRUE ,&s );
r.left = ((LPNMCUSTOMDRAW)lParam)->rc.left;
r.top = ((LPNMCUSTOMDRAW)lParam)->rc.top;
r.right = ((LPNMCUSTOMDRAW)lParam)->rc.left + s.cx;
r.bottom = ((LPNMCUSTOMDRAW)lParam)->rc.top + s.cy;
DrawThemeBackground( hTheme, ((LPNMCUSTOMDRAW)lParam)->hdc,
BP_CHECKBOX, stateID, &r, NULL );
// adjust rectangle for text drawing
((LPNMCUSTOMDRAW)lParam)->rc.left += 2 + s.cx;
DrawText( ((LPNMCUSTOMDRAW)lParam)->hdc,
L"Example text", -1,
&((LPNMCUSTOMDRAW)lParam)->rc,
DT_SINGLELINE | DT_VCENTER );
CloseThemeData(hTheme);
SetWindowLongPtr( hDlg, DWLP_MSGRESULT
(LONG_PTR)CDRF_SKIPDEFAULT );
return TRUE;
}
}
}
}
}
}
break;
文本颜色和文本背景在WM_CTLCOLORSTATIC
处理程序中设置:
case WM_CTLCOLORSTATIC:
{
SetTextColor( (HDC)wParam, RGB( 255, 0, 0 ) );
SetBkMode( (HDC)wParam, TRANSPARENT );
}
return (INT_PTR)( (HBRUSH)GetStockObject(NULL_BRUSH) );
我在#pragma comment
和中包含了常用控件 6 InitCommonControlsEx()
。
问题:
我现在需要的是为DrawThemeBackground
. 有人可以帮我弄这个吗?
谢谢你。
最好的祝福。