Static Control Background Color with C++

前端 未结 3 1482
我寻月下人不归
我寻月下人不归 2020-12-02 02:19

I am creating a basic GUI with the Windows API and I have run into an issue. It starts with a main window that opens with a custom background color I set (RGB(230,230

相关标签:
3条回答
  • 2020-12-02 03:02

    For static text controls there's no permanent way to set the text color or their background. Even if you want to apply the changes to a single static control; you would still have to handle WM_CTLCOLORSTATIC notification message in parent dlgproc just when the control is about to be drawn.

    This is due to the DefWindowProc overwriting your changes to the device context each time it handles WM_CTLCOLORSTATIC as stated in the MSDN:

    By default, the DefWindowProc function selects the default system colors for the static control.

    static HBRUSH hBrush = CreateSolidBrush(RGB(230,230,230));
    
    case WM_CTLCOLORSTATIC:
    {
        if (settingstext == (HWND)lParam)
    
                  //OR if the handle is unavailable to you, get ctrl ID
    
        DWORD CtrlID = GetDlgCtrlID((HWND)lParam); //Window Control ID
        if (CtrlID == IDC_STATIC1) //If desired control
        {
           HDC hdcStatic = (HDC) wParam;
           SetTextColor(hdcStatic, RGB(0,0,0));
           SetBkColor(hdcStatic, RGB(230,230,230));
           return (INT_PTR)hBrush;
        }
    }
    

    If you're looking to make the control's background transparent over a parent dialog you could use SetBkMode(hdcStatic, TRANSPARENT).

    0 讨论(0)
  • 2020-12-02 03:06

    Have you considered subclassing the static window and doing owner draw?

    0 讨论(0)
  • 2020-12-02 03:12

    I think there is a permanent way to do it.

    Just after you create the label,use GetDC() function to get the Device Context. Then use:

    SetTextColor(hdcStatic, RGB(0,0,0));
    SetBkColor(hdcStatic, RGB(230,230,230)); // Code Copied from the above answer by cpx.
    

    And it should do .

    0 讨论(0)
提交回复
热议问题