How to change the background color of CIPAddressCtrl? - c++

I tried to change the background color and text color of the IP Address Control in MFC.
Once, I have changed the text color and background color, but the points between the Edit controls are not displayed.
My code is as follows:
BEGIN_MESSAGE_MAP(KIPAddressCtrl, CIPAddressCtrl)
ON_WM_CTLCOLOR()
ON_WM_PAINT()
END_MESSAGE_MAP()
// KIPAddressCtrl message handlers
HBRUSH KIPAddressCtrl::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
m_brush.DeleteObject();
m_brush.CreateSolidBrush(RGB(255, 255, 0));
CRect rc;
CWindowDC dc(this);
GetWindowRect(&rc);
dc.Draw3dRect(0, 0, rc.Width(), rc.Height(), RGB(0, 0, 255), RGB(0, 0, 255));
pDC->SetTextColor(RGB(255, 0, 0));
pDC->SetBkColor(RGB(255, 255, 0));
return (HBRUSH)m_brush;
}
void KIPAddressCtrl::OnPaint()
{
CRect rc;
CPaintDC dc(this);
GetClientRect(&rc);
dc.FillSolidRect(&rc, RGB(255, 255, 0));
//CIPAddressCtrl::OnPaint();
}
My result is
But I want to
How to solve this?

The reason why the separators disappear is because of the FillSolidRect call in the KIPAddressCtrl::OnPaint() handler, overwriting anything the default implementation had done.
Coloring the IP Address Control the way you want isn't supported. While the embedded Edit Controls request a color from their parent (the IP Address Control), the control itself will not send respective messages to its parent that would allow you to apply your customization.
In other words: While the four embedded edit controls can be customized, the area not covered by the edit controls cannot. That's unfortunate, but it is what it is.

Related

Create CBitmap from CDC?

Using C++/MFC and GDI (not GDI+), the overall goal is to create an patterned HBRUSH, which will be used in OnCtlColor to outline an edit control in red, with the ability to turn the outline on and off. To do this, you attach a bitmap to an HBRUSH using CreatePatternBrush. Here is the code for doing that, using a stored bitmap resource:
CDialog::OnInitDialog();
BOOL ok = redBoxBitmap.LoadBitmap(MAKEINTRESOURCE(IDB_mespe_EditBox_Red));
ok = redBoxBrush.CreatePatternBrush(&redBoxBitmap);
and in OnCtlColor
HBRUSH CModelEditorSpecies::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr;
int ctrlID=pWnd->GetDlgCtrlID();
if(ctrlID==IDC_MyEditControl)
hbr=(HBRUSH) redBoxBrush;
else
hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
return hbr;
}
The above code all works as desired. However, it depends on the bitmap being sized to the edit control. What I need now is the ability to create the bitmap within the C++ program, sized to the client area of the control, which depends on both the design size of the control (in the dialog editor), and the user's setting of text size in Windows 10 settings.
I cannot find a straightforward way of either constructing a bitmap, or, better, creating an empty one of the proper size (can do), selecting it into a CDC (can do), drawing the red box into it (can do), then extracting the update bitmap from the CDC (how to do?).
Can anyone suggest either how to create the bitmap programmatically, or suggest a better method of getting an edit control boxed in red when the program calls for that?
Added in response to #Constantine Georgiou's answer of 3/9:
New code:
CBitmap redBoxBitmap; // member variables of class CModelEditorSpecies
CBrush redBoxBrush;
BOOL CModelEditorSpecies::OnInitDialog()
{
CDialog::OnInitDialog();
BOOL ok;
CRect r; defaultSpecies1Ctrl.GetClientRect(&r);
xx(r.Width(), r.Height()/*, redBoxBrush*/);
ok = redBoxBrush.CreatePatternBrush(&redBoxBitmap);
//...
}
void CModelEditorSpecies::xx(const int w, const int h)
{
CDC *pDC=GetDC();
redBoxBitmap.CreateCompatibleBitmap(pDC, w, h);
// Create a red pen
CPen redPen;
redPen.CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
// Draw the bitmap - red pen & default background brush
CBitmap *pOldBitmap=pDC->SelectObject(&redBoxBitmap);
pDC->SelectObject(&redPen);
CBrush editBoxBrush;
editBoxBrush.CreateSysColorBrush(COLOR_WINDOW);
pDC->SelectObject(&editBoxBrush);
pDC->Rectangle(0, 0, w, h);
pDC->SelectObject(pOldBitmap);
// Create the edit-control custom brush
redBoxBrush.CreatePatternBrush(&redBoxBitmap);
return;
}
This code produces an all-black edit control, as if the bitmap being used were monochrome. That would be expected if drawing in the dc does not affect the bitmap, or, if drawing in a dc-compatible bitmap does not use the colors in redPen and editBoxBrush, as suggested by #IInspectable.
Here's how to create the brush - used the Win32 functions instead of their MFC wrappers, but you can figure it out.
BOOL CModelEditorSpecies::OnInitDialog()
{
CDialogEx::OnInitDialog();
// Get edit-control's size
RECT rc;
GetDlgItem(IDC_MyEditControl)->GetClientRect(&rc);
// Create the bitmap and a memory-DC
HDC hDC = ::GetDC(HWND_DESKTOP);
HDC mDC = CreateCompatibleDC(hDC);
HBITMAP hBmp = CreateCompatibleBitmap(hDC, rc.right, rc.bottom);
::ReleaseDC(HWND_DESKTOP, hDC);
// Create a red pen
HPEN hPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
// Draw the bitmap - red pen & default background brush
HBITMAP hOldBmp = (HBITMAP) SelectObject(mDC, hBmp);
HPEN hOldPen = (HPEN) SelectObject(mDC, hPen);
HBRUSH hOldBr = (HBRUSH) SelectObject(mDC, GetSysColorBrush(COLOR_WINDOW));
Rectangle(mDC, rc.left, rc.top, rc.right, rc.bottom);
SelectObject(mDC, hOldBr);
SelectObject(mDC, hOldPen);
SelectObject(mDC, hOldBmp);
// Create the edit-control custom brush
redBoxBrush = CreatePatternBrush(hBmp);
// Clean-up - the SysColorBrush doesn't need to be deleted
DeleteObject(hPen);
DeleteObject(hBmp);
DeleteDC(mDC);
return TRUE;
}
HBRUSH CModelEditorSpecies::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
return (nCtlColor == CTLCOLOR_EDIT && pWnd->GetDlgCtrlID() == IDC_MyEditControl) ?
redBoxBrush : CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
}
Also check the alternatives I suggested in the comments. This one will be drawing into the edit-control's client area.
EDIT:
The code I posted above does work, and after it runs all left is the HBRUSH handle (pattern-brush for your edit box). I can't see why you insist using the MFC wrappers instead. They are "higher-level" thin wrappers, so... "thin" actually, that you still have to perform almost exactly the same operations (create, select into a DC, perfrom some drawing operation, select out of the DC) as the GDI ones. The only operation you don't need to perform is delete the resource (the object's destructor will call DeleteObject() for you).
Anyways, if you prefer MFC over GDI, let's see what's wrong with your code. It has quite a few issues:
First and foremost, you need a memory DC to draw on a bitmap, the window DC you get by calling GetDC() draws on a window's surface.
The DC you get by calling GetDC() must be returned to the system (ReleaseDC()), because pDC is just a pointer, and the compiler won't call the destructor.
The objects you select into a DC must be selected out before it is destroyed, otherwise memory-leaks may be occurring.
So your code should be changed as shown below:
void CModelEditorSpecies::xx()
{
CRect r;
GetDlgItem(IDC_MyEditControl)->GetClientRect(&r);
// Create the bitmap and a memory-DC
CBitmap redBoxBitmap;
CDC mDC, *pDC = GetDC();
redBoxBitmap.CreateCompatibleBitmap(pDC, r.Width(), r.Height());
mDC.CreateCompatibleDC(pDC);
ReleaseDC(pDC);
// Create a red pen and get the default background brush
CPen redPen;
redPen.CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
CBrush editBoxBrush;
editBoxBrush.CreateSysColorBrush(COLOR_WINDOW);
// Draw the bitmap - red pen & default background brush
CBitmap *pOldBitmap = mDC.SelectObject(&redBoxBitmap);
CPen *pOldPen = mDC.SelectObject(&redPen);
CBrush *pOldBrush =mDC.SelectObject(&editBoxBrush);
mDC.Rectangle(r);
mDC.SelectObject(pOldBrush);
mDC.SelectObject(pOldPen);
mDC.SelectObject(pOldBitmap);
// Create the edit-control custom brush
redBoxBrush.CreatePatternBrush(&redBoxBitmap);
}
It's basically the same as the GDI version.

Redraw shapes in MFC

I'm trying to develop a painter app using MFC with C++.
So I set up a view in which I do the actual painting. However, when I open the color button I get "leftovers" of the menu on my canvas view.. I have no idea how to erase them.. I tried using SaveDC and RestoreDC to restore it to the previous state but no luck there. As I understand it is meant to restore properties of the device context such as pen & brush but it has no use for me...
I also need this feature so when I put up a rectangle, I could present a preview for it, but the "previews` are again presented like leftovers.
My view's OnEraseBkgbd;
BOOL CanvasView::OnEraseBkgnd(CDC* pDC)
{
if (!isBackgroundInit)
{
CRect rect;
GetClientRect(&rect);
CBrush myBrush(RGB(255, 255, 255)); // dialog background color
CBrush* pOld = pDC->SelectObject(&myBrush);
BOOL bRes = pDC->PatBlt(0, 0, rect.Width(), rect.Height(), PATCOPY);
pDC->SelectObject(pOld); // restore old brush
isBackgroundInit = true;
return bRes;
}
return 0;
}
My OnPaint:
void CanvasView::OnPaint()
{
CDialogEx::OnPaint();
//UpdateData(true);
CRect rect;
GetClientRect(&rect);
if (dc == nullptr)
{
dc = new CClientDC(this);
//dc->CreateCompatibleDC(dc);
HDC hdc = CreateCompatibleDC(*dc);
this->hdc = &hdc;
} else
{
BitBlt(*dc, 0, 0, (int)rect.Width(), (int)rect.Height(), *hdc, 0, 0, SRCCOPY);
}
}
How it looked at first:
How it looks when opening a color button:
How it looks after closing the color button menu(leftovers marked with red arrows):
When trying to put up a rectangle:
Any idea how to fix this and really restore drawing?
I am not sure I understand what you are trying to do.
CView has a virtual method called OnDraw. This is the method you must override:
void CMyView::OnDraw(CDC* pDC)
{
CMyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// first erase the entire client rectangle
CRect cr;
GetClientRect( &cr );
pDC->FillSolidRect( cr, RGB( 255, 255, 255 ) );
// your actual drawing goes here
pDC->Rectangle( 0, 0, 100, 100 );
}
You will also have to write OnEraseBkgnd:
BOOL CMyView::OnEraseBkgnd( CDC* /*pDC*/ )
{
return TRUE; // we fill the client rectangle in OnDraw
}
Also, it looks like you are trying to implement an ad-hoc memory DC. There is already one: CMemDC.

it's possible draw a rectangle on Edit control with ES_PASSWORD style?

i'm trying draw a rectangle roundend around of control, i've done something like this:
LRESULT CALLBACK WindPorc(HWND hwnd, UINT umsg, WPARAM wparam, LPARAM lparam)
{
case WM_CTLCOLOREDIT:
{
// device handle
HDC Chdc = (HDC)wparam;
// handle to control
HWND CHand = (HWND)lparam;
// new object pen...
HPEN penx = CreatePen(PS_SOLID, 5, RGB(0, 255, 255));
// apply pen to device handle and back up the original handle
HGDIOBJ objb = SelectObject(Chdc, penx);
// auto...
RECT rectx;
GetClientRect(CHand, &rectx);
// Now draw the rect with round borders...
RoundRect(Chdc, (rectx.left -2), (rectx.top - 2), (rectx.right + 2), (rectx.bottom + 2), 5, 5);
// reset device hand...
SelectObject(Chdc, objb);
// Set text color...
SetTextColor(Chdc, RGB(0, 255, 255));
// clean up...
DeleteObject(penx);
DeleteObject(objb);
// I should return a brush for the bk, but this don't care now...
//return;
}
}
void InitUI()
{
// just the edit control, isn't need the RegisterClass etc...
HWND Edit1 = CreateWindowEx
(
NULL,
L"EDIT",
NULL,
WS_CHILD | WS_VISIBLE |
ES_LEFT | ES_MULTILINE, // Just a demonstration, it should be ES_PASSWORD
10, 120, 200, 22,
winx, // parent window HWND...
(HMENU)TEXTBOX_1, // id
(HINSTANCE)GetWindowLong(winx, GWL_HINSTANCE),
NULL
);
}
ok, this would look like this:
but if i change the style on CreateWindowEx(); to ES_PASSWORD the edges disappear, leaving something like this:
Why do they disappear?
I would say the implementation is wrong in multiple ways:
Chdc is a DC that can draw on the client area of the edit control only, while you want to draw on the parent window's (dialog's) client area.
GetClientRect() always returns (0,0) for the top-left corner, as the rectangle is relative to... the edit control itself.
WM_CTLCOLOREDIT may not be the best place to draw the rectangle, you need to make some tests to check when this message is actually called.
If you wish to keep the implementation this way, you should:
Draw using the parent window's DC, not the edit control's one.
Instead of GetClientRect() call GetWindowRect() and then ScreenToClient() to make coordinates relative to the parent window's client area.
Use the WM_PAINT message (and the BeginPaint()/EndPaint() functions) to get a DC for the parent window.
I would consider a different implementation though, create an Owner-Drawn Static Control, some few pixels larger than the edit control, placed somehow "around" the edit control (set the WS_CLIPSIBLINGS style to make sure that the edit control stays on top of the static one). Drawing should be carried-out processing the WM_DRAWITEM message. I think this implementation is more robust.

Coloring the entire background of an MFC static label

This Answer is really great if you want to change the background color of a "conventional" text label. But what if you want to put a border around that text label and expand its size so that the text is swimming in a veritable sea of color? It only paints the text background in the required color, and leaves the rest of the expanded control with the standard button face. How can one make the color consistent across the entire control?
Note: The attractive feature (to me anyway) about the above answer is that it makes use of OnCtlColor(), which provides a pointer to the CWnd control concerned. So there is no need to create a subclass of CStatic to handle the color change. An answer that avoids creating such a subclass would be preferred.
I'm not very sure about OP's Note section. Still posting this code for his help.
HBRUSH CSampleDlg::OnCtlColor(CDC* pDC, CWnd *pWnd, UINT nCtlColor)
{
switch (nCtlColor)
{
case CTLCOLOR_STATIC:
{
CRect rcWindow(0, 0, 220, 40);
//::GetWindowRect(pWnd->GetSafeHwnd(), &rcWindow);
pDC->FillSolidRect(rcWindow, RGB(49, 49, 49));
pDC->SetTextColor(RGB(255, 255, 255));
return (HBRUSH)GetStockObject(NULL_BRUSH);
}
default:
{
return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
}
}
}
You can make the static control invisible in the resource editor then paint it from CMyDialog.
void CMyDialog::OnPaint()
{
CDialog::OnPaint();
paintstatic(IDC_STATIC1);
}
void CMyDialog::paintstatic(int id)
{
CClientDC dc(this);
CRect rc;
CWnd *child = GetDlgItem(id);
child->GetWindowRect(&rc);
CPoint offset(0, 0);
ClientToScreen(&offset);
rc.OffsetRect(-offset);
dc.FillSolidRect(rc, RGB(0, 255, 128));
CFont *font = GetFont();
dc.SelectObject(font);
CString text;
child->GetWindowText(text);
dc.DrawText(text, rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}

Custom CTreeCtrl - How to modify text / selection color

First of all, i would customize - among other things - color of text and selection color(text background).
For example, text color should be blue; color of text background should be transparent.
So, I have overriden OnPaint() method;
I call SetTextColor() and SetBkColor() functions, but unfortunately I always get invalid colors, or I get an annoying "infinite loop flash" effect.
Here you can see his complete implementation.
void CustomTree::OnPaint()
{
CPaintDC dc(this);
CDC memDC;
memDC.CreateCompatibleDC(&dc);
CRect rcClip, rcClient;
dc.GetClipBox( &rcClip );
GetClientRect(&rcClient);
CBitmap bitmap;
bitmap.CreateCompatibleBitmap( &dc, rcClient.Width(), rcClient.Height() );
memDC.SelectObject( &bitmap );
CRgn rgn;
rgn.CreateRectRgnIndirect( &rcClip );
memDC.SelectClipRgn(&rgn);
rgn.DeleteObject();
/* WHAT IS the correct usage of SetText/Bk Color? */
// ::SetTextColor(memDC, RGB(0, 0, 255));
// ::SetBkColor(memDC, RGB(0, 0, 255));
// COLORREF col = SetTextColor(RGB(0,0,255));
// COLORREF co2 = memDC.SetTextColor(RGB(0,0,255));
// First let the control do its default drawing.
CWnd::DefWindowProc(WM_PAINT, (WPARAM)memDC.m_hDC, 0);
// do some others stuffs...
dc.BitBlt(rcClip.left, rcClip.top, rcClip.Width(), rcClip.Height(), &memDC,
rcClip.left, rcClip.top, SRCCOPY);
memDC.DeleteDC();
}
Where is the error?
Thanks
IT
If you want to change color and font for a treeview, you have to catch and respond to NM_CUSTOMDRAW. Simply setting the properties before calling the default is insufficient.