HBRUSH CDialog23::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
int Element = pWnd->GetFocus()->GetDlgCtrlID();
if (pWnd->GetDlgCtrlID() == Element && nCtlColor != CTLCOLOR_STATIC)
{
pDC->SetBkMode(TRANSPARENT);
pDC->SetBkColor(RGB(255, 230, 153));
hbr = m_Gelb;
return hbr;
}
else
{
pDC->SetBkMode(TRANSPARENT);
pDC->SetBkColor(RGB(255, 255, 255));
hbr = m_Weiss;
return hbr;
}
}
With this code, my ComboBoxes are getting colored in yellow, when I set the focus to one of them. However, if the current focus is set to one of the ComboBoxes, all other ComboBoxes are getting colored yellow, when I hover over them using the cursor (without clicking them). Also, the List of the ComboBox doesn't get colored (they are formated as dropdown).
Any ideas?
This is because you are not checking combo box ID equals focused combo ID. The dropdown combo boxes never receive focus. It is complex control that contains child edit control and this control has ID=1001 (0x03E9). Hence you are getting focus on the window with 1001 ID and all windows by this ID also change the background.
Change logic and use windows handles instead of IDs.
As for the drop-down list is a popup window layered over the combo box window and you will have to change the background using a different approach. You have no control over drop list creation and the only way would be to dynamically subclass this window using a hook.
Related
I have a class that is derived from CComboBoxEx and I'm trying to change the background color. I was thinking that it would work like a ComboBox (using the SetBkColor function), but it doesn't change the background color.
Here's what I have tried :
BEGIN_MESSAGE_MAP(CMyComboBoxEx, CComboBoxEx)
ON_WM_CTLCOLOR()
END_MESSAGE_MAP()
void CMyComboBoxEx::SetBkColor(COLORREF backgroundColor)
{
m_backgroundColor = backgroundColor;
m_brBkgnd.DeleteObject();
m_brBkgnd.CreateSolidBrush(backgroundColor);
}
HBRUSH CMyComboBoxEx::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH brush = __super::OnCtlColor(pDC, pWnd, nCtlColor);
pDC->SetBkColor(RGB(255,0,0));
return brush;
}
I've tried with OnEraseBkgnd() too and it didn't worked either.
Do I need to subclass a derived CComboBox class and set the background color in that class?
Thx.
The problem here is that WM_CTLCOLOR messages are sent to the parent window (dialog box, probably) of your combo control, not to the control itself; also, in the case of the drop-down 'list-box' part of the combo, this message is not sent (as the dialog doesn't need to draw it unless the control has been activated).
The way I have achieved what you want is by making the control owner-draw and then (manually) drawing each item in the list.
First, you need to add the CBS_OWNERDRAWFIXED style to your control in the .rc/.rc2 script; like this, for a typical combo:
COMBOBOX IDC_IGONG, 224, 68, 52,120,
CBS_DROPDOWNLIST | CBS_HASSTRINGS | CBS_OWNERDRAWFIXED | WS_VSCROLL | WS_TABSTOP
Then, you need to add ON_WM_DRAWITEM() to the message map for your dialog class, and override its OnDrawItem() member. Note that the message is sent once for each item in the drop-down list, when the list is made visible by user-action:
void MyDialog::OnDrawItem(int nIDCtl, DRAWITEMSTRUCT *pDIS)
{
switch (pDIS->CtlType) { // You can switch on the ID if it's only one combo!
case ODT_COMBOBOX:
DrawDropDownBox(this, nIDCtl, pDIS);
break;
default:
CDialogEx::OnDrawItem(nIDCtl, pDIS);
break;
}
}
The DrawDropDownBox() does all the hard work:
void MyDialog::DrawDropDownBox(CWnd *box, int nID, DRAWITEMSTRUCT *pDIS)
{
CComboBox *pCBC = dynamic_cast<CMyComboBoxEx *>(box->GetDlgItem(nID));
if (pCBC == nullptr) return; // Skip if we can't get handle to the control
CDC *pDC = CDC::FromHandle(pDIS->hDC);
wchar_t buffer[4096]; // Or just char if you ain't using Unicode
if (pCBC->GetLBText(int(pDIS->itemID), buffer) == CB_ERR) return; // Maybe called during WM_DELETEITEM
int dcSave = pDC->SaveDC(); // Save DC state for later restoration
CPen pen(PS_SOLID, 0, ListColor); // ListColor is COLORREF for your desired b/g
if (pDIS->itemState & ODS_DISABLED) {
pDC->SelectStockObject(NULL_PEN);
pDC->SelectObject(BackBrush); // A CBrush for disabled: defined/created elsewhere
pDC->SetBkMode(TRANSPARENT);
}
else {
pDC->SelectObject(&pen);
pDC->SelectObject(ListBrush); // A CBrush that draws your desired b/g
pDC->SetBkMode(OPAQUE);
}
CRect rc(pDIS->rcItem); pDC->Rectangle(&rc); // This draws the b/g
if (pDIS->itemState & ODS_DISABLED) {
pDC->SetTextColor(GetSysColor(COLOR_GRAYTEXT));
}
else if (pDIS->itemState & ODS_SELECTED) { // Use Windows defaults if selected...
pDC->SetTextColor(GetSysColor(COLOR_HIGHLIGHTTEXT));
pDC->SetBkColor(GetSysColor(COLOR_HIGHLIGHT));
}
else {
pDC->SetTextColor(GetSysColor(COLOR_WINDOWTEXT));
pDC->SetBkColor(ListColor); // Custom b/g color
}
unsigned format = DT_SINGLELINE | DT_VCENTER; // You desired text alignment
pDC->DrawText(CString(buffer), rc, format);
pDC->RestoreDC(dcSave); // Restore DC's saved state...
pDC->Detach(); // ...then 'release it'
return;
}
The code shown handles both disabled combos and selected items in the list; you could possibly skip some of these, if you want to simplify the operation.
Feel free to ask for further explanation and/or clarification.
If it's all about just changing Bk color of the control, then you have to handle WM_CTLCOLOR message in control's parent window:
HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if (pWnd->GetDlgCtrlID() == IDC_MY_CONTROL)
{
pDC->SetBkColor(RGB(0, 0, 0)); //Black color
hbr = m_hbrBlack; //Black brush.
}
return hbr;
}
Otherwise, you have to draw your control totally yourself in your derived class, with CBS_OWNERDRAWFIXED or CBS_OWNERDRAWVARIABLE style, which is way more complicated, but ofc possible.
I'm surprised that all the answers you've got so far suggest either handling WM_CTLCOLOR in parent window or use one of OWNERDRAW styles.
Handling WM_CTLCOLOR in parent window means you'll need to duplicate that code in each parent window's class where you'll use such combobox. This is obviously a bad solution if you want to use combobox more than once.
Adding OWNERDRAW style may have impact on other existing controls that you'd like to subclass and you may need to handle additional problems. That's also far from a sinmple solution.
Fortunately, there's another way to solve it - use Message Reflection. And all you need to do is to add ON_WM_CTLCOLOR_REFLECT() entry to the message map and CtlColor handler.
In case of combobox control I'd do it like this:
MyComboBoxEx.h
class CMyComboBoxEx : public CComboBoxEx
{
public:
CMyComboBoxEx();
virtual ~CMyComboBoxEx();
protected:
CBrush m_BkBrush;
DECLARE_MESSAGE_MAP()
public:
afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
};
MyComboBoxEx.cpp
CMyComboBoxEx::CMyComboBoxEx()
{
m_BkBrush.CreateSolidBrush(RGB(0, 255, 0));
}
CMyComboBoxEx::~CMyComboBoxEx()
{
}
BEGIN_MESSAGE_MAP(CMyComboBoxEx, CComboBoxEx)
ON_WM_CTLCOLOR_REFLECT()
ON_WM_CTLCOLOR()
END_MESSAGE_MAP()
HBRUSH CMyComboBoxEx::CtlColor(CDC* pDC, UINT nCtlColor)
{
pDC->SetTextColor(RGB(255, 0, 0));
pDC->SetBkColor(RGB(0, 255, 0));
return m_BkBrush;
}
HBRUSH CMyComboBoxEx::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
return CtlColor(pDC, nCtlColor);
}
Here's how such combobox looks:
If you want to have custom color for borders and glyph then you need to handle WM_PAINT yourself.
I have window with gradient background. Combobox have own background brush.
How can I remove white corner in combobox? How can I change brush or another way.
On picture white corner marked by red frame.
I create combobox as:
DWORD dwStyle = WS_CHILD | CBS_DROPDOWNLIST;
if (m_bVisible) dwStyle |= WS_VISIBLE;
m_hWnd = CreateWindow(WC_COMBOBOX, NULL, dwStyle,
m_posX, m_posY, m_width, m_height, m_hParent, (HMENU)m_id, m_hInstance, NULL);
I tried changing background brush with message WM_CTLCOLOREDIT, but no effect:
case WM_CTLCOLOREDIT:
if ((HWND)lParam == m_hSrcListBox)
{
return (LRESULT)m_hBrush;
}
break;
=== SOLVED. WORK VERSION ===
First way.
In parent WndProc:
case WM_CTLCOLORSTATIC:
if ((HWND)lParam == m_hSrcListBox)
{
return (LRESULT)m_pSrcListBox->GetHbrush();
}
break;
In my class:
//
// CListBox::GetHbrush().
//
// Get brush.
//
HBRUSH CListBox::GetHbrush()
{
if (!m_hBrush)
{
m_hBrush = CreateTransparentBackgroundBrush(m_hParent, m_hWnd);
}
return m_hBrush;
}
Create transparent background:
//
// CListBox::CreateTransparentBackgroundBrush().
//
// Create transparent background for element.
//
HBRUSH CListBox::CreateTransparentBackgroundBrush(HWND parent, HWND client)
{
RECT rct;
POINT p1;
POINT p2;
GetWindowRect(client, &rct);
p1.x = rct.left;
p1.y = rct.top;
ScreenToClient(parent, &p1);
p2.x = rct.right;
p2.y = rct.bottom;
ScreenToClient(parent, &p2);
HDC hdcParent = GetDC(parent);
HDC hdcClient = GetDC(client);
HDC hdcmem = CreateCompatibleDC(hdcClient);
HBITMAP hbitmap = CreateCompatibleBitmap(hdcClient, p2.x - p1.x, p2.y - p1.y);
SelectObject(hdcmem, hbitmap);
BitBlt(hdcmem, 0, 0, p2.x - p1.x, p2.y - p1.y, hdcParent, p1.x, p1.y, SRCCOPY);
HBRUSH pattern = CreatePatternBrush(hbitmap);
DeleteDC(hdcmem);
DeleteObject(hbitmap);
ReleaseDC(client, hdcClient);
ReleaseDC(parent, hdcParent);
return pattern;
}
Second way.
In parent WndProc draw background in WM_ERASEBKGND message, then the corners will not.
case WM_ERASEBKGND:
m_hdc = (HDC)wParam;
// draw background.
return TRUE;
break;
The result of both methods:
For dialog boxes, handle WM_CTLCOLORDLG and return a background brush for the combobox
If you are displaying this combobox in a dialog, the trick is actually to handle the WM_CTLCOLORDLG message in your dialog's window procedure. In response to this message, you return a handle to a brush that the dialog box will use to paint its background.
case WM_CTLCOLORDLG:
{
// NOTE: This code is wrong because it creates a new brush object each time it processes
// the message, which it promptly leaks. It is merely for demonstration purposes.
// Normally, you would create the brush once, in response to WM_INITDIALOG,
// cache it away, and return that same cached handle each time, finally destroying
// the brush in response to WM_NCDESTROY.
HBRUSH hBrush = CreateSolidBrush(RGB(255, 120, 0));
return reinterpret_cast<INT_PTR>(hBrush);
}
This is the standard, documented way of changing the background color of a dialog box, and it also solves the problem with the combobox. Apparently, for whatever reason, combobox controls also use this brush to paint their background. I suppose they send a WM_CTLCOLORDLG message to their parent when they are painting themselves.
Of course, this limits you to the graphics capabilities of a GDI brush. You can draw any system or solid color that you want, or even use a hatch or pattern/bitmap brush, but there is no simple way of creating a gradient brush. (GDI+ has one, but not GDI.) Normally it wouldn't matter—you'd just call the GradientFill function in your WM_PAINT (or even WM_ERASEBKGND) message handler. That works fine for the dialog's background, but the combobox still draws its background with the brush returned by WM_CTLCOLORDLG, so it still has those 4 dots on its corners drawn in COLOR_3DFACE (which is the brush that the default dialog procedure returns).
Returning a null brush (NULL_BRUSH/HOLLOW_BRUSH) from WM_CTLCOLORDLG doesn't work, either. It changes the appearance slightly, such that the upper-right and lower-left corner pixels are now filled with something that looks like COLOR_3DSKSHADOW, but they are still visibly filled with a color other than the actual background gradient.
So if you really want it to look nice, you are left with only a single option: returning a handle to a GDI brush. And of course, it needs to be the same brush as is used to draw the dialog's background.
If you want a gradient fill, the only solution I can think of is using a pattern/bitmap brush, where the bitmap (DDB or DIB) is your gradient. Not great, but at least the days of Windows 9x limiting us to 8×8 patterns are long gone. Maybe someone more inventive than me can use this information to think of a better workaround?
For other windows, handle WM_CTLCOLORSTATIC and return a background brush for the combobox
All of that for a dialog box. But what about if you are displaying the combobox in a standard window (i.e., something other than a dialog box)? The WM_CTLCOLORDLG message is never sent in this case.
Instead, the combobox sends a WM_CTLCOLORSTATIC message to its parent window, and then uses the brush handle returned in response to that message to paint its background.
This is weird, I know. I only stumbled across it by conducting empirical tests, and I'm not sure quite sure what the rationale was. If I had to guess, I'd say that the CBS_DROPDOWNLIST style makes the combobox non-editable (i.e., it's not a true combobox because there is no Edit control), so instead of WM_CTLCOLOREDIT, it uses WM_CTLCOLORSTATIC. A disabled Edit box sends WM_CTLCOLORSTATIC, too, and so does a disabled combobox with the "normal" CBS_SIMPLE and CBS_DROPDOWN styles.
Weirder still, this only happens when the Aero theme is enabled (Vista and 7). It doesn't happen on Windows 10, or with the Luna theme (Visual Styles under XP), or with the Classic theme. (I didn't test on Windows 8 or 8.1.) Not that it matters, I suppose, since all of those other themes draw a simple rectangular combobox, leaving no corner pixels for the background to show through.
Whatever the logic, the solution remains to handle the WM_CTLCOLORSTATIC message and return the brush you wish the combobox to use to paint its background.
The same considerations apply here as those discussed above for the dialog box. If your window uses a solid-color background or a system color, you are home-free. Simply return a handle to the same brush that you set as the window class's background brush. If you want to use a gradient, you'll need to figure out a way to represent that gradient in the form of a GDI brush.
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDR_APPLICATION));
wcex.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDR_APPLICATION_SMALL));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_3DDKSHADOW + 1); // background brush
wcex.lpszMenuName = NULL;
wcex.lpszClassName = TEXT("My Colored Window Class");
RegisterClassEx(&wcex);
case WM_CTLCOLORSTATIC:
{
// NOTE: No leak here because we're using a system brush in this example.
return reinterpret_cast<LRESULT>(GetSysColorBrush(COLOR_3DDKSHADOW)); // background brush
}
How can i remove white corner in combobox?
I don't know if there is a more official way to get rid of it, but one option would be to create a Region with rounded corners using CreateRoundRectRgn(), and then apply it to the ComboBox using SetWindowRgn(). That will mask off the corners.
I develop a win32 app and create a color chooser using this example. I update if statement like this to change the background of my app when user click to "Ok" in color dialog box, but nothing change. Where is my mistake?
if (ChooseColor(&cc) == TRUE) {
HBRUSH hbrush = CreateSolidBrush(cc.rgbResult);
rgbCurrent = cc.rgbResult;
SetClassLongPtr(hWnd, GCLP_HBRBACKGROUND, (LONG)hbrush);
}
The following code would work.
First, SetClassLongPtr() returns the previous value, which is, in this case, the HBRUSH previously set to the window class(hWnd). You should delete the object to avoid memory leak.
After that, calling InvalidateRect() brings the color change into effect. Because the newly created brush will be used when the window needs to be repainted.
InvalidateRect() sends WM_ERASEBKGND to the window.
if (ChooseColor(&cc) == TRUE) {
HBRUSH hbrush = CreateSolidBrush(cc.rgbResult);
HBRUSH hOldBrush = (HBRUSH)SetClassLongPtr(hWnd, GCLP_HBRBACKGROUND, (LONG_PTR)hbrush);
DeleteObject(hOldBrush);
InvalidateRect(hWnd, NULL, 1);
}
I want to change text color of menu in mfc. I have searched a lot but didn't got a proper solution. Finally I was trying to use OnCtlColor which I generally use for setting the color of static text control. But I am confused here how to do the same for menu items because pWnd->GetDlgCtrlID() dont work on menu.My menu item id is ID_MENU_ITEM. My query is what should i write at "?" to get my job done. And if my approach is wrong please suggest me good alternative.
HBRUSH CMyClass::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr;
if ( ? == ID_MENU_ITEM)
{
pDC->SetTextColor(RGB(255, 0, 0));
hbr = (HBRUSH)m_whitebrush;
return hbr;
}
}
I have an Edit control that is read only that has text in it. I would like to switch the default gray background to white but have been having limited luck. In my first go, I was executing the following code during initialization of the dialog:
CEdit *m_ctrlEditOne = (CEdit*) GetDlgItem(IDC_EDIT1);
CDC *m_ctrlEEditWee = m_ctrlEditOne->GetDC();
m_ctrlEEditWee->SetBkColor(RGB(255,0,0));
Invalidate(true);
Another solution I tried was:
HBRUSH CTestingDlg::OnCtlColor(CDC* pDC, CWnd *pWnd, UINT nCtlColor)
{
if (pWnd->GetStyle() & ES_READONLY)
//if(pDC->GetRuntimeClass == & ES_READONLY)
{
switch (nCtlColor)
{
case CTLCOLOR_STATIC:
pDC->SetBkColor(RGB(255,255,255));
return (HBRUSH)GetStockObject(NULL_BRUSH);
default:
//return NULL;
return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
}
}
//return NULL;
return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
}
In the screenshot below, you can see that the text is inserted after the fact (this is what needs to happen) and appears highlighted in blue - I have no idea where to begin on how to make it just appear as normal, non-highlighted text. When clicking on it, it appears normally. In the bottom left corners of each edit control, one can see a square which should not be appearing there. Also, you can see some artifacts of what looks like to be a combo box dropdown selection appearing in the larger boxes.
I would appreciate any pointers on how to get rid of the artifacts and fix the highlighting issue with inserted text.
I do as shown below. It will change the background of the read only edit control IDC_EDIT1 to white. This is a copy-paste straight out of one of my projects.
m_whitebrush is a private member of CTestOnCtlClorDlg of type HBRUSH and must be initialized to NULL in the constructor of CTestOnCtlClorDlg.
HBRUSH CTestOnCtlClorDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
int id = pWnd->GetDlgCtrlID( ) ;
if (id == IDC_EDIT1)
{
pDC->SetTextColor(RGB(0, 0, 0));
pDC->SetBkColor(RGB(255,255,255));
if (!m_whitebrush)
m_whitebrush = CreateSolidBrush(RGB(255,255,255)) ;
hbr = m_whitebrush ;
}
return hbr;
}
void CTestOnCtlClorDlg::OnDestroy()
{
CDialog::OnDestroy();
if (m_whitebrush !=NULL)
{
DeleteObject(m_whitebrush) ;
m_whitebrush = NULL ;
}
}