• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Getting Size & Location of ChildView :: MFC

kuphryn

Senior member
Hi.

I am using AppWizard single-document type setup. I need to get the rectange of the ChildView. All I am trying to do is change the background color of the ChildView. So ineed to get the exact size and location of the ChildView related to the MainFrame. I tried using this:

-----
CWnd *pWnd = AfxGetMainWnd();
CRect rectCView;
pWnd->get(rectCView);
CBrush *pOldBrush, newBrush(RGB(0, 0, 0));
CDC *pDC = pWnd->GetDC();
pOldBrush = pDC->SelectObject(&newBrush);
pDC->Rectangle(rectCView);
pDC->SelectObject(pOldBrush);
pWnd->ReleaseDC(pDC);
-----

The problem with the code segment above is that it repaint everything inside the MainFrame including the toolbar and statusbar. I would like it to paint the ChildView.

Thanks,
Kuphryn
 
Normally you have the child window paint itself the proper color, by handling either the WM_PAINT message (CFormView or CDialog window) or WM_ERASEBKGND (CView). e.g.

void CSideEditFormView : : onPaint()
{
CPaintDC dc(this); // device context for painting
CBrush cream(PALETTERGB(224,224,224));
CRect invalid_rect;
dc.GetClipBox(&invalid_rect);
dc.FillRect(&invalid_rect, &cream);
}
 
Thanks!

Nice!

I have two questions.

First, in this line:

-----
CBrush cream(PALETTERGB(224,224,224));
-----

Is that the same as this?

-----
CBrush cream(RGB(224, 224, 224));
-----

Lastly, what does this line do?

-----
dc.GetClipBox(&invalid_rect);
-----

Kuphryn


 
Add WM_ERASEBKGND handler to the view class and have it set the background color to whatever you want.
 
No difference between RGB amd PALETTERGB *if* your desktop is set to 16-bit/24-bit color (no palette).

In the programming nightmare that is 256-color mode every running application fights for control of what shades are set for 236 of those colors. Then if your app is using a palette to control some colors you need PALETTERGB to find one of your custom colors. You should probably ignore all of this and just use RGB() instead 🙂

dc.GetClipBox(&invalid_rect); returns the smallest rectangle that encloses all of the invalid parts of the window. For example if you've closed a messagebox and it was only covering part of your view window then only part of the window is invalid / in need of repainting. Using this instead of GetClientRect is a little faster since less screen clipping is needed.
 
Okay. As for WM_ERASEBKGND, how do I start the message (i.e. have windows send the ChildView the WM_ERASEBKGND message)?

I am not familiar with WM_ERASEBKGND at all.

Kuphryn
 
Both the Petzold and Prosise books describe WM_ERASEBKGND and Prosise has pages on OnEraseBkgnd. You can force a repaint by calling Invalidate(), e.g. pChild->Invalidate()
 
WM_ERASEBKGND is automatically sent to the window before it's repainted. You don't need to do anything.
 
Back
Top