Changing background of text in edit control - c++

Can you change the background of text in area of edit control that would stay static?

In the parent of the edit control, handle the WM_CTLCOLORSTATIC message, the wParam of this message is the HDC that the Edit control is about to draw with,
for most CTLCOLOR messages, if you set text and background colors into this DC, the control will use the colors you set.
You can also return an HBRUSH and the contol will use that for any brush painting that it wil do, but many controls don't use brushes much, so that will have limited effect for some
CTLCOLOR messages. Your best bet here is to return the DC brush, and set the DC Brush color to match the BkColor of the DC.
LRESULT lRet = 0; // return value for our WindowProc.
COLORREF crBk = RGB(255,0,0); // use RED for Background.
...
case WM_CTLCOLORSTATIC:
{
HDC hdc = (HDC)wParam;
HWND hwnd = (HWND)lParam;
// if multiple edits and only one should be colored, use
// the control id to tell them apart.
//
if (GetDlgCtrlId(hwnd) == IDC_EDIT_RECOLOR)
{
SetBkColor(hdc, crBk); // Set to red
SetDCBrushColor(hdc, crBk);
lRet = (LRESULT) GetStockObject(DC_BRUSH); // return a DC brush.
}
else
{
lRet = DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
break;

WM_CTLCOLORSTATIC is for static text control.
To be simple, you can do this in your winproc:
...
case WM_CTLCOLOREDIT:
{
HDC hdc = (HDC)wParam;
SetTextColor(hdc, yourColor); // yourColor is a WORD and it's format is 0x00BBGGRR
return (LRESULT) GetStockObject(DC_BRUSH); // return a DC brush.
}
...
If you have more than 1 edit control, you can use the item id and lParam to check which one need to be change.

WM_CTLCOLOREDIT allows you to set text and background color(+brush), if you want more control than that, you have to subclass and paint yourself

you could do something like this:
CBrush bkBrush;
RECT ctrlRect;
COLORREF crBk = RGB(255,0,0); // Red color
bkBrush.CreateSolidBrush(crBk);
CWnd* pDlg = CWnd::GetDlgItem(IDC_EDIT);
pDlg->GetClientRect(&ctrlRect);
pDlg->GetWindowDC()->FillRect(&ctrlRec, &bkBrush);
pDlg->GetWindowDC()->SetBkColor(crBk);
This should change the background color of the edit control

All you need is to set the required color in control's device context and pass an HBRUSH with same color in WM_CTLCOLOREDIT message. If you want to change both foreground & background colors, use SetTextColor t0 change the text color. But you must pass the background color HBRUSH. But if you want to change the text color only, then you must pass a DC_BRUSH with GetStockObject function.

Related

CComboBox - How to change the color of the static with Drop List Style

I want to know if it's possible to change the color of the text (and of the small arrow) and the background of the static in a CCombobox with the Drop List style.
My class is derived from CComboBox and I've tried with the function CtlColor and OnCtlColor, but nothing seems to change the color of the ComboBox.
Here's a picture of the control with the Drop List style :
I would like the text and the arrow to change to RGB(0, 255, 255) and the background to RGB(255,255,0).
Here's my function CtlColor() :
HBRUSH CColoredComboBox::CtlColor(CDC *pDC, UINT nCtlColor)
{
if (nCtlColor == CTLCOLOR_STATIC || nCtlColor == CTLCOLOR_EDIT)
{
pDC->SetBkColor(RGB(255,255,0));
pDC->SetTextColor(RGB(0, 255, 255));
}
return m_brBkgnd;
}
It works for the style Dropdown, but not for the Drop List.
Thank you.
There are two ways - easy and hard. The hard way is to complete DrawItem with ownerdraw to handle all the cases. The easy way is to put two combos on top of each other in your dialog resource and hide the one you're not going to use.
Maybe this can give you some ideas.
Don't know arrow color can be changed or not but, color of combobox can be changed. With help of OnChildNotify() function, you can retrieve child HDC and then particular child HDC can be changed.
/////////////////////////////////////////////////////////////////////////////
// CMyComboBox message handlers
BOOL CMyComboBox::OnChildNotify(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pLResult)
{
// TODO: Add your specialized code here and/or call the base class
if(WM_CTLCOLOREDIT != message)
return CComboBox::OnChildNotify(message, wParam, lParam, pLResult);
HDC hdcChild = (HDC)wParam;
if(NULL != hdcChild)
{
SetBkMode(hdcChild, TRANSPARENT);
SetTextColor(hdcChild, RGB(255, 255, 0));
SetBkColor(hdcChild, RGB(255, 0, 0));
*pLResult = (LRESULT)(m_Brush.GetSafeHandle());
}
return TRUE;
// return CComboBox::OnChildNotify(message, wParam, lParam, pLResult);
}
Result:

Win32 C++ Custom painting subclassed radiobutton initial background issue

I am trying to draw a radiobutton with a transparent background using a subclassed radiobutton & WM_PAINT. I know how to do this using the TransparentBlt function & an off-screen (back)buffer.
My problem is that intially the radiobutton by default draws some text & circle (see image 1). My WM_PAINT message only consists of BeginPaint() and EndPaint(). When i minimize the window and afterwards activate the window again, the default text & circle get replaced by a black square like you would expect (see image 2).
The yellow window is also painted in the same manner using WM_PAINT.
Both procedures return 1 for WM_ERASEBKGND and return 0 for WM_PAINT as required for custom painting & double buffering.
Is this normal behaviour? I found a "fix" by using WS_EX_TRANSPARENT, but I would like to understand first why the radiobutton initially gets painted like that to determine if this is the right fix for me.
Thanks in advance.
Radiobutton creation:
MControlRect rect(0, 0, 100, 20);
unsigned long style = WS_CHILD | BS_AUTORADIOBUTTON;
if (isGroupStarter) {
style += WS_GROUP;
}
::HWND hWnd = _create(pControlParent, WC_BUTTON, style, rect);
::WNDPROC systemProc = (::WNDPROC)GetWindowLong(hWnd, GWL_WNDPROC);
::SetWindowLong(hWnd, GWL_WNDPROC, (long)customRadiobuttonProcedure);
::SetWindowLong(hWnd, GWL_USERDATA, (long)systemProc);
::UpdateWindow(hWnd);
::ShowWindow(hWnd, SW_SHOW);
Radiobutton procedure:
switch (msg) {
case WM_ERASEBKGND:
{
return 1;
break;
}
case WM_NCPAINT:
{
return 0;
break;
}
case WM_PAINT:
{
::PAINTSTRUCT ps;
::HDC hdc = ::BeginPaint(hWnd, &ps);
::EndPaint(hWnd, &ps);
return 0;
break;
}
}
::WNDPROC defaultWindowProc = (::WNDPROC)::GetWindowLong(hWnd, GWL_USERDATA);
return ::CallWindowProc(defaultWindowProc, hWnd, msg, wParam, lParam);
Image 1: initial radiobutton painted
Image 2: radiobutton after minimize > show again

Changing win32's radio button text color

Upon color change, i listen to WM_CTLCOLORSTATIC and act accordingly:
LRESULT ProcessWindowMessage(_In_ HWND hWnd, _In_ UINT uMsg, _In_ WPARAM wParam, _In_ LPARAM lParam)
{
switch (uMsg)
{
case WM_CTLCOLORSTATIC:
LRESULT lBrush = ::DefWindowProc(hWnd, uMsg, wParam, lParam); // get default brush used so far
::SetBkMode((HDC)wParam, TRANSPARENT);
::SetTextColor((HDC)wParam, RGB(m_color.red, m_color.green, m_color.blue));
return lBrush;
}
}
This works well with regular static-texts: labels and so on, but has no effect on regular radio buttons.
During my debugging attempts, I've tried listening to:
WM_DRAWITEM - doesn't receive any events
WM_CTLCOLORBTN - receive events only for regular push buttons( OK / CANCEL)
WM_CTLCOLOREDIT - doesn't receive any events.
I'm subclassing to another window not generated / created by me, but constructed by my process.
#igal k said SetWindowTheme does not work. Since the comment is not enough for the sample code. I post it as an answer.
First the result.
Code:
OnInitDialog:
::SetWindowTheme(GetDlgItem(IDC_RADIO1)->GetSafeHwnd(), L"wstr", L"wstr");
HBRUSH CMFCApplication1Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
// Call the base class implementation first! Otherwise, it may
// undo what we're trying to accomplish here.
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// Are we painting the IDC_MYSTATIC control? We can use
// CWnd::GetDlgCtrlID() to perform the most efficient test.
if (pWnd->GetDlgCtrlID() == IDC_RADIO1)
{
// Set the text color to red
pDC->SetTextColor(RGB(255, 0, 0));
// Set the background mode for text to transparent
// so background will show thru.
pDC->SetBkMode(TRANSPARENT);
// Return handle to our CBrush object
hbr = m_brush;
}
return hbr;
}
If you are interested to draw the full radio button, what you have to do is set a custom window proc for the radio button.In that proc you get the WM_PAINT and WM_ERASEBKGND messages.
In the erase background you can just fill the background color for the control.
In WM_PAINT you do the drawing of the control , get the window text of the control and do a DrawText with color set to whatever you need using SetTextColor
Draw an image of radio (circle and a dot inside).You can get all the state variables of the control and draw according to it, like whether its selected it or not. Whenever you need a repaint, ie new text is set, just call invalidateRect to force a repaint..

transparency layered window white

I'm trying to make a layered window with c++ win32 but I'm having a problem with the drawing or "collision" of it
For reference the picture that I'm trying to display.
This is the basic creation of the window
//window
DWORD exFlags = 0;
if(m_bTransparent)
exFlags |= WS_EX_LAYERED;
Create(WS_POPUP, exFlags);
std::wstring sPic(L"power-disconnected.png");
m_pAlertPic = m_pPowerMon->GetGPPicMan()->LoadPicture(sPic.c_str());
// make the window layered when using transparency
if(m_bTransparent && m_pAlertPic != nullptr)
{
HDC hdcScreen = GetDC(GetHandle());
HDC hdc = CreateCompatibleDC(hdcScreen);
HBITMAP hbmpold = (HBITMAP)SelectObject(hdc, m_pAlertPic->GetBuffer());
POINT dcOffset = {0, 0};
SIZE size = {ww, wh};
BLENDFUNCTION bf = {AC_SRC_OVER, 0, (int) (2.55 * 100), AC_SRC_ALPHA}; // blend function combines opacity and pixel based transparency
UpdateLayeredWindow(GetHandle(), hdcScreen, NULL, &size, hdc, &dcOffset, RGB(255, 255, 255), &bf, ULW_ALPHA);
SelectObject(hdc, hbmpold);
DeleteDC(hdc);
ReleaseDC(GetHandle(), hdcScreen);
}
and the message loop
int WindowAlert::WndProc(Gnaq::WindowBase* pWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
Hide();
return 1;
case WM_PAINT:
// only draw when the widow is not transparent
// layered window redraw them self
if(!m_bTransparent)
m_pCanvas->Draw(m_pGraphics);
break;
case WM_LBUTTONUP:
pWnd->Hide();
m_bDismised = true;
break;
}
return DefWindowProcW(pWnd->GetHandle(), msg, wParam, lParam);
}
So this is the result I get
As you can see with this method I'm getting white borders where is should actually be fully transparent, but the semi transparent parts do work correctly.
here's what I've tried that gave me a "useful" change.
First I just tried to add the ULW_COLORKEY flag to hide the white color
UpdateLayeredWindow(GetHandle(), hdcScreen, NULL, &size, hdc, &dcOffset, RGB(255, 255, 255), &bf, ULW_ALPHA | ULW_COLORKEY);
And the result.
So this hides the white border but also all the white in the picture.
Next thing I've tried was using SetLayeredWindowAttributes in combination of UpdateLayeredWindow, without the ULW_COLORKEY flag
SetLayeredWindowAttributes(GetHandle(), 0xFFFFFF00, 255, LWA_COLORKEY);
Also in the window proc enable the paint, like this
case WM_PAINT:
m_pCanvas->Draw(m_pGraphics);
break;
This way I'm visually getting what I want like this
But the problem with his approach is that it the complete window is click able while with just using the UpdateLayeredWindow only the parts that should be fully transparent are click able like it should be. I also have the feeling with this last approach that it is more a "hack" than a decent approach.
So i hope that someone can tell me what I'm doing wrong.
The first way was the correct. The fault was in the bitmap, which didn't had premultiplied alpha

Static control set text color

I have a static control:
HWND hLabelControl=CreateWindowEx(WS_EX_CLIENTEDGE,"STATIC","",
WS_TABSTOP|WS_VISIBLE|WS_CHILD|SS_CENTER,0,0,24,24,
hwnd,(HMENU)hS1,GetModuleHandle(NULL),NULL);
I want when a button is pressed the color of the text in the static label to change to red for example.
How can I do this?
I know there is a
SetTextColor(
_In_ HDC hdc,
_In_ COLORREF crColor
);
function but I can't figure out how to get the HDC of the static control.
Thanks in advance.
EDIT:
This doesn't work:
HDC hDC=GetDC(hLabelControl);
SetTextColor(hDC,RGB(255,0,0));
Static controls send their parent a WM_CTLCOLORSTATIC message just before they paint themselves. You can alter the DC by handling this message.
case WM_CTLCOLORSTATIC:
if (the_button_was_clicked) {
HDC hdc = reinterpret_cast<HDC>(wParam);
SetTextColor(hdc, COLORREF(0xFF, 0x00, 0x00));
}
return ::GetSysColorBrush(COLOR_WINDOW); // example color, adjust for your circumstance
So the trick is to get the static control to repaint itself when the button is clicked. You can do this several different ways, but the simplest is probably to invalidate the window with InvalidateRect.