MFC dialog color settings

//Dialog color control code

  //Dialog variables

 HBRUSH m_brushBack;

HBRUSH FunctionWindowA::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
	{
		/*
		The root cause of the problem is that the dialog and its controls need to be set to different background colors to beautify the interface.
		Thus, the HBRUSH m_brushBack object is used, especially when the data is continuously updated through thread messages or timers,
		GDI objects will keep growing and exceed the system's GDI limit (9999), causing the program to crash.
       
        The essence of the problem is that components that are constantly changing content cannot use background brushes


	(1) In order to solve the problem of growing GDI objects, (GDI objects cannot exceed 9999)
   		Notes on setting the color of the control: (The essence of the problem is that the background brush cannot be used for components that constantly change content)
		1 The background brush uses the window member variable HBRUSH m_brushBack;
		2 When setting with a control type, the background brush cannot be used if the type contains controls that constantly refresh the content;
		3 Use the control ID to set the color and background of the control whose content remains unchanged;
		4 The IDs of the available labels are IDC_STATIC by default, and the color background is set for the labels whose content remains unchanged.

	(2) References

		1 Imitation of the light blue of QQ: COLORREF QQcolor=RGB(200,224,251)

		2 control types
    		CTLCOLOR_BTN button control
   			CTLCOLOR_DLG dialog
    		CTLCOLOR_EDIT edit box
    		CTLCOLOR_LISTBOX list control
    		CTLCOLOR_MSGBOX message control
    		CTLCOLOR_SCROLLBAR scroll bar control
    		CTLCOLOR_STATIC static control      
		   
		*/
	HBRUSH hbr = CDialog::OnCtlColor(pDC,pWnd, nCtlColor);
		
	COLORREF QQcolor=RGB(200,224,251);

    // set the ribbon background
    if (  (pWnd->GetDlgCtrlID() == IDC_STATICbg)
		  || (pWnd->GetDlgCtrlID() == IDC_STATICbg2)  
		  || (pWnd->GetDlgCtrlID() == IDC_STATICbg3) )
	{
        //pDC->SetTextColor(RGB(255,0,0)); //font color
		//pDC-> SetBkColor(RGB(255,255, 255)); //font background color
		//pDC-> SetBkMode(OPAQUE);
		m_brushBack= CreateSolidBrush(RGB(200,204,220)); //Create a brush
        return HBRUSH(m_brushBack);
    }

	 // Use the ID to set the color and background of the label. Generally, the ID of the label is IDC_STATIC;
	 //The content of these tags is fixed
    if (pWnd->GetDlgCtrlID() == IDC_STATIC)
	{
        pDC->SetTextColor(RGB(0,0,255)); //font color
		pDC-> SetBkColor(RGB(200,204,220)); //Font background color
		pDC-> SetBkMode(TRANSPARENT);
		m_brushBack= CreateSolidBrush(RGB(200,204,220)); //Create a brush
        return HBRUSH(m_brushBack);
    }

	//Use the type to set the font color of the label, you cannot use the background brush
	//The content of these tags is constantly changing
	if (nCtlColor==CTLCOLOR_STATIC)
	{
		pDC->SetTextColor(RGB(255,0,0)); //font color
		pDC-> SetBkColor(RGB(200,204,220)); //Font background color
		pDC-> SetBkMode(TRANSPARENT);	

	}
	
	//Edit box
	if (nCtlColor==CTLCOLOR_EDIT)
	{
		pDC->SetTextColor(RGB(0,0,255)); //font color
		pDC-> SetBkColor(RGB(255,255, 255)); //font background color
		pDC-> SetBkMode(TRANSPARENT);
		m_brushBack= CreateSolidBrush(RGB(200,224,251)); //Create a brush
        return HBRUSH(m_brushBack);

	}

	 //// Use the ID to set the background color of the window
  //  if (nCtlColor == CTLCOLOR_DLG) {
  //// pDC-> SetTextColor(RGB(255,0,0)); //font color
		////pDC-> SetBkColor(RGB(200,204,220)); //Font background color
		////pDC-> SetBkMode(TRANSPARENT);
		//m_brushBack= CreateSolidBrush(RGB(200,224,251)); //Create a brush
  //      return HBRUSH(m_brushBack);
  //  }
	//Can only return to the background brush hbr of the system
    return (HBRUSH) hbr; 	
}

// brush function

void FunctionWindowA::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	CRect rect;

	GetClientRect (rect);
	dc.FillSolidRect(rect,RGB(200,224,251));
	dc.FillPath();
		// TODO: add message handler code here
		// not called for drawing messages
	CDialog::OnPaint();
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325889016&siteId=291194637
Recommended