Article ID: 198732
Article Last Modified on 11/21/2006
// Override of AmbientUserMode, which will tell us whether if the UserMode
// ambient property is even implemented in the container.
bool CFormControl::AmbientUserMode(bool *bUserMode, bool bInit)
{
ASSERT (bUserMode);
if (!GetAmbientProperty(DISPID_AMBIENT_USERMODE,
VT_BOOL,
bUserMode) && bInit)
{
*bUserMode = true;
return false;
}else{
return true;
}
}
void CFormControl::PaintChildWindows(HWND hWndParent, CDC* pDC,
CPoint ptOffset)
{
for (HWND hWndChild = ::GetTopWindow(hWndParent);
hWndChild != NULL;
hWndChild = ::GetNextWindow(hWndChild, GW_HWNDNEXT))
{
CRect rect;
::GetWindowRect(hWndChild, rect); // wnd rect in screen coords
ScreenToClient(&rect); // relative to this view
HDC hdcOut = pDC->m_hDC;
#ifdef _DEBUG
CPoint pt = pDC->GetWindowOrg();
ASSERT(pt.x == 0 && pt.y == 0);
#endif
DWORD dwStyle = ::GetWindowLong(hWndChild, GWL_STYLE);
if (dwStyle & (WS_HSCROLL|WS_VSCROLL))
{
TRACE("Warning: printing control with scrollbars not
supported\n");
}
if (dwStyle & WS_BORDER)
{
// The only case we special case--manually drawn border.
::Rectangle(hdcOut, ptOffset.x + rect.left,
ptOffset.y + rect.top, ptOffset.x + rect.right,
ptOffset.y + rect.bottom);
rect.InflateRect(-1,-1); // One logical pixel.
}
pDC->SaveDC();
{
CPoint pt(ptOffset.x + rect.left, ptOffset.y + rect.top);
pDC->LPtoDP(&pt);
pDC->OffsetViewportOrg(pt.x, pt.y);
// Set the viewport origin so that the window origin
// can be changed by the control. Draw it using a
// non-virtual HDC.
// Error dialog in Excel95 shows up after this call when
// used on NT4SP3
::SendMessage(hWndChild, WM_PAINT, (WPARAM)hdcOut, 0L);
}
pDC->RestoreDC(-1);
if (::GetTopWindow(hWndChild) != NULL)
PaintChildWindows(hWndChild, pDC, ptOffset);
}
}
void CFormControl::OnDraw(CDC* pdc, const CRect& rcBounds,
const CRect& rcInvalid)
{
// If in design mode.
bool bUserMode;
if((AmbientUserMode(&bUserMode) && !bUserMode) || !m_hWnd) {
{
// This drawing code creates the window and has it draw its
// child windows into it.
_AFX_THREAD_STATE* pThreadState = AfxGetThreadState();
ASSERT(pThreadState != NULL);
pThreadState->m_pWndPark = new CWnd;
pThreadState->m_pWndPark->m_hWnd = pdc->GetWindow()->GetSafeHwnd();
RecreateControlWindow();
}
CBrush* pOldBrush;
CBrush bkBrush;
bkBrush.CreateSolidBrush(TranslateColor(GetBackColor()));
// Set the ForeColor property color and transparent background mode
// into the device context.
pdc->SetTextColor(TranslateColor(GetForeColor()));
pOldBrush = pdc->SelectObject(&bkBrush);
pdc->SetBkMode(TRANSPARENT);
// Paint the background using the BackColor property.
pdc->FillRect(rcBounds, &bkBrush);
CPen pen(PS_SOLID, 1, RGB(0,0,0)); // Solid black pen.
CPen* pOldPen = pdc->SelectObject(&pen);
PaintChildWindows(m_hWnd, pdc, CPoint(rcBounds.left, rcBounds.top));
ASSERT(pdc->GetWindowOrg() == CPoint(0,0));
pdc->SelectObject(pOldPen);
}
127192 How To Draw Controls in an OLE Metafile
143299 SAMPLE: CFormView-Based Server Shows Metafile When Not Active
Keywords: kbctrlcreate kbhowto kbinprocsvr KB198732