1

Topic: How to change background color for the button (CButton)?

There is button IDC_BUTTON1 to which there corresponds a variable m_IDC_BUTTON1 type CButton. For this button I changed height and width of the text as CMyProgramDlg:: OnInitDialog: CMyProgramDlg:: OnInitDialog () {//... memset (&lf,0,sizeof (lf); lf.lfHeight = 25; lf.lfWidth = 10; lf.lfWeight = FW_BOLD; lf.lfCharSet = ANSI_CHARSET; lf.lfOutPrecision = OUT_DEFAULT_PRECIS; lf.lfClipPrecision = CLIP_DEFAULT_PRECIS; lf.lfQuality = PROOF_QUALITY; lf.lfPitchAndFamily = VARIABLE_PITCH|FF_ROMAN; wcscpy (lf.lfFaceName, L "MS sans Serif"); pfont = new FOnt (); pfont-> CreateFontIndirect (&lf); m_IDC_BUTTON1.SetFont (pfont); return TRUE;} It works - the text in the button is displayed in the necessary size. But I wanted to change also background color in this button - for example, on RGB (12,149,210). I left Text color former - black (RGB (0,0,0)). In this connection I wrote function: HBRUSH CMyProgramDlg:: OnCtlColor (CDC *pDC, CWnd *pWnd, UITN nCtlColor) {HBRUSH hbr = CDialogEx:: OnCtlColor (pDC, pWnd, nCtlColor); if (pWnd-> GetDlgCtrlID () == IDC_BUTTON1) {pDC-> SetBkColor (RGB (12,149,210)); pDC-> SetBkMode (TRANSPARENT);}} However I do not have a change of background color of the button - the background remains former. How to me to solve this problem - that at me background color of the button, but thus text color changed remained black and the text was that size which I set?

2

Re: How to change background color for the button (CButton)?

With change of color of the button I understood: void CMyButton:: OnPaint () {CString strText; LOGFONT lf; memset (&lf,0,sizeof (lf)); lf.lfHeight = 25; lf.lfWidth = 10; lf.lfWeight = FW_BOLD; lf.lfCharSet = ANSI_CHARSET; lf.lfOutPrecision = OUT_DEFAULT_PRECIS; lf.lfClipPrecision = CLIP_DEFAULT_PRECIS; lf.lfQuality = PROOF_QUALITY; lf.lfPitchAndFamily = VARIABLE_PITCH | FF_ROMAN; wcscpy (lf.lfFaceName, L "MS Sans Serif"); CFont *pfont = new CFont (); pfont-> CreateFontIndirect (&lf); CDC *pDC = this-> GetDC (); RECT rect; GetClientRect (&rect); GetWindowText (strText); HBRUSH hBrush = CreateSolidBrush (RGB (141,179,226)); pDC-> SelectObject (hBrush); pDC-> Rectangle (rect.left,rect.top,rect.right-rect.left,rect.bottom-rect.top); pDC-> SetBkColor (RGB (141,179,226)); pDC-> SetTextColor (RGB (255,255,255)); pDC-> SelectObject (pfont); pDC-> DrawText (strText,&rect,DT_SINGLELINE |DT_CENTER|DT_VCENTER);} But I decided to go further - at pushing this button (that is at pushing of the left button of "mouse") the boundary of this button should be selected with a fat line, and at release of the left button of "mouse" the button boundary should be recovered. For this purpose I wrote: void CMyButton:: OnLButtonDown (UINT nFlags, CPoint point) {CButton:: OnLButtonDown (nFlags, point); HPEN hPen = CreatePen (PS_SOLID, 2, RGB (0,0,0));//fat black solid line RECT rect; GetClientRect (&rect); pDC-> MoveTo (rect.left, rect.top); pDC-> LineTo (rect.right, rect.top); pDC-> LineTo (rect.right, rect.bottom); pDC-> LineTo (rect.left, rect.bottom); pDC-> LineTo (rect.left, rect.top); pDC-> SetBkColor (RGB (255,255,255)); pDC-> SetTextColor (RGB (255,255,255)); RedrawWindow ();} At pushing of the left button of "mouse" it works - the button boundary is selected with a black fat solid line. But how to make so that at release of the left button of "mouse" the button boundary in an original form would be recovered?

3

Re: How to change background color for the button (CButton)?

Hello, RussianFellow, you wrote: RF> RF> HBRUSH CMyProgramDlg:: OnCtlColor (CDC *pDC, CWnd *pWnd, UITN nCtlColor) RF> {RF> HBRUSH hbr = CDialogEx:: OnCtlColor (pDC, pWnd, nCtlColor); RF> if (pWnd-> GetDlgCtrlID () == IDC_BUTTON1) RF> {pDC->> SetBkColor (RGB (12,149,210)); pDC->> SetBkMode (TRANSPARENT); RF>} RF>} RF> 1) It is a misprint in  or the result and truth is not returned? 2) anyway, OnCtlColor must return a handle to the brush that is to be used for painting the control background. And generally, https://msdn.microsoft.com/en-us/library/0wwk06hc.aspx