Change background color and name in MFC Control

I want to change the text color and background color for my EDIT CONTROL, STATIC CONTROL and BUTTON CONTROL in an MFC application. The control is in a dialog CDialogEx.

I am trying to add OnCtlColor (with a wizard in visual studio, in the WM_CTLCOLR message), but I cannot set the color of the static control and button controls.

I also set a breakpoint in the function OnCtlColor(in the IF construct), but I get nothing.

I also tried using the function SetTextColorby getting the control handle from GetDlgItem, but I cannot change the color as I want.

Please help me.

0
source share
1 answer

, OnCtlColor CDialog, .

, , :

  • , - , HBRUSH

    return (HBRUSH) m_brush.GetSafeHandle();

    , (m_brush ), ( ), .

    , , ,

    pDC->SetBkColor(RGB(0,0,255));

    , , ; .

  • pDC->SetTextColor(RGB(255,0,0));

, !

- MFC, - : CButton BS_GROUPBOX, nCtlColor CTLCOLOR_STATIC CTLCOLOR_BTN!

UINT nStyle = (UINT)(pWnd->GetStyle() & 0x0F);

if(nStyle == BS_GROUPBOX)
{
    return (HBRUSH) m_brush2.GetSafeHandle();
}

, , groupbox!

!

, , http://www.codeproject.com/Articles/29016/XGroupBox-an-MFC-groupbox-control-to-display-text : CStatic, OnPaint() DrawItem(). ON_WM_PAINT() . , OnEraseBkgnd() ON_WM_ERASEBKGND(). , XGroupBox DDX_ Control. , .

CButtons . CMFCButton DDX_Control . :

  • m_bTransparent TRUE ( afxbutton.cpp ) , , ( , )

  • SetFaceColor() SetTextColor() .

CMFCButton , CMFCVisualManager.

. CSpinButton CMFCSpinButon, .

OnCtlColor nCtlColor , dynamic_cast .

ON_WM_CTLCOLOR() .

1: http://social.msdn.microsoft.com/Forums/vstudio/en-US/53f47162-078a-418f-8067-ee61a81ceeac/checkbox-transparent-color-not-working-in-vs2008?forum=vcgeneral, Groupbox, :

class CMyGroupBox: public CButton
{
protected:
    virtual void PreSubclassWindow()
    {
        SetWindowTheme(*this, _T(""), _T(""));
        #pragma comment(lib, "UxTheme.lib")
    }
};

, DDX_Control , , SetTextColor. HBRUSH , , , , .

2: CMyGroupBox CMyButton, PreSubClassWindow , . , , .

3: - , pDC->SetBkColor(RGB(0,0,255));; , :( pDC->SetBkMode(TRANSPARENT);, : (

4:. , , PreSubClassWindow, , .

SetThemeAppProperties(0);
#pragma comment(lib, "UxTheme.lib")
AfxGetMainWnd()->SendMessage(WM_THEMECHANGED, 0U, 0L);

.

+2

Source: https://habr.com/ru/post/1691633/


All Articles