Hi Andy,
I have tried what you said, but not having any success. I can't figure
out how to get the .NET control to actually do the drawing onto the MFC
DC. I thought that if I got the window handle (.net window) and its
associated DC then that would suffice. I had thought that if I had the
actual device context of the .NET control then it should already have
rendered it's content to the DC, but apparently this is not the case.
Perhaps you might be able to spot the problem in the code below
Thanks in advance,
Martin
m_ContainedCont rol->Visible = true;
IWin32Window^ win32Win =
cli::safe_cast< IWin32Window^>( m_ContainedCont rol.GetControl( ));
System::IntPtr ptr = win32Win->Handle;
HWND hwnd = (HWND)ptr.ToPoi nter();
CWnd* pWnd = CWnd::FromHandl e(hwnd);
ASSERT(pWnd);
CClientDC netCtrlDC(pWnd) ; // This will automatically GetDC and
ReleaseDC
// After creating and configuring the memDC
CRect boundingBox(0,0 ,0,0);
netCtrlDC.GetCl ipBox(&bounding Box);
CDC memDC;
CBitmap memBitmap;
memDC.CreateCom patibleDC(&netC trlDC);
netCtrlDC.LPtoD P(&boundingBox) ;
memBitmap.Creat eCompatibleBitm ap(&netCtrlDC, boundingBox.Wid th(),
boundingBox.Hei ght());
AutoSelector selectBitmap(&m emDC, &memBitmap);
memDC.SetMapMod e(netCtrlDC.Get MapMode());
memDC.SetWindow Ext(netCtrlDC.G etWindowExt());
memDC.SetViewpo rtExt(netCtrlDC .GetViewportExt ());
netCtrlDC.DPtoL P(&boundingBox) ;
memDC.SetWindow Org(boundingBox .left, boundingBox.top );
memDC.FillSolid Rect(boundingBo x, netCtrlDC.GetBk Color());
// Blit the contents of the .NetCtrl to the memDC
memDC.BitBlt(bo undingBox.left,
boundingBox.top ,
boundingBox.Wid th(),
boundingBox.Hei ght(),
&netCtrlDC,
boundingBox.lef t,
boundingBox.top ,
SRCCOPY);
// Debugging Crap Just Draw an X
AutoSelector selectPen(&memD C, BLACK_PEN); // Automatically Deslect
on exit
memDC.MoveTo(bo undingBox.TopLe ft());
memDC.LineTo(bo undingBox.Botto mRight());
memDC.MoveTo(bo undingBox.right , boundingBox.top );
memDC.LineTo(bo undingBox.left, boundingBox.bot tom);
// End Debugging Crap
m_ContainedCont rol->Visible = false;
// Blit the contents of the MemDC to the real DC
CRect thisBoundingBox = GetBaseRgn().Ge tBounds();
pDC->BitBlt(thisBou ndingBox.left,
thisBoundingBox .top,
thisBoundingBox .Width(),
thisBoundingBox .Height(),
&memDC,
boundingBox.lef t,
boundingBox.top ,
SRCCOPY);
Andy Bates wrote:
If your control is derived from the System.Windows. Forms.Control then you
can retrieve the window handle from the control via the IWin32Window
interface. This interface will give you access to the Handle method which
returns an IntPtr. This can be converted into a HWND which in turn can be
converted to a DC using HDC dc = GetDC(hwnd). The MFC application can then
create a BITMAP being the size of the control and blit the content into it;
alternatively you could blit the controls content directly into the printers
DC (providing no windows are over the top of it).
It should be possible with a little messing around to get the .NET control
to render to the printer DC. Depending on what your control provides it may
be easier to render the controls content to a BITMAP and then output that on
the DC in the MFC application (for print, display etc.).
HTH
- Andy
<mm********@gma il.comwrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.com.. .
We have an MFC application mixed with a few .NET controls. I would like
to be able to render the content of a .NET control to an MFC device
context - specifically a printing device context.
We wrote the MFC app and the .NET control, so we can add hook functions
into either module if necessary.
The .NET control is hosted in the MFC app using the CWinFormsContro l
template.
How can I make this happen? Does anyone have any links to sample source
code?
Any help greatly appreciated.
Thanks,
Martin