0

紹介と関連情報:

共通のコントロールをテーマにする必要がありますが、テキストの色と透明な背景が異なります。この質問で十分に文書化されている問題に遭遇しました。

処理によってある程度の進歩があり、最初にチェックボックス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) );

と に共通のコントロール 6 を含めまし#pragma commentInitCommonControlsEx()

質問:

現時点で必要なのは、 に適切な状態を渡すことだけDrawThemeBackgroundです。誰かがこれで私を助けることができますか?

ありがとうございました。

よろしくお願いします。

4

1 に答える 1

2

NM_CUSTOMDRAW gives you state information about the control being drawn. The NMCUSTOMDRAW::uItemState field is a bitmask that can hold multiple values at a time, but you are not taking that into account. You need to use the & bitwise operator to check for the presence of specific values.

Change this:

// 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;
    }
}

To something more like this instead:

int stateID;  // parameter for DrawThemeBackground

UINT uiItemState = ((LPNMCUSTOMDRAW)lParam)->uItemState;
bool bChecked = (uiItemState & CDIS_CHECKED);

if (uiItemState & CDIS_HOT)
    stateID = bChecked ? CBS_CHECKEDHOT : CBS_UNCHECKEDHOT;

else if (uiItemState & CDIS_SELECTED)
    stateID = bChecked ? CBS_CHECKEDPRESSED : CBS_UNCHECKEDPRESSED;

else
    stateID = bChecked ? CBS_CHECKEDNORMAL : CBS_UNCHECKEDNORMAL;
于 2014-03-28T22:34:37.513 に答える