473,505 Members | 14,658 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Urgent Help Req'd: Rendering a .NET control to an MFC CDC.

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 CWinFormsControl
template.

How can I make this happen? Does anyone have any links to sample source
code?

Any help greatly appreciated.
Thanks,
Martin

Aug 29 '06 #1
2 1880
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********@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.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 CWinFormsControl
template.

How can I make this happen? Does anyone have any links to sample source
code?

Any help greatly appreciated.
Thanks,
Martin

Aug 29 '06 #2
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_ContainedControl->Visible = true;
IWin32Window^ win32Win =
cli::safe_cast<IWin32Window^>(m_ContainedControl.G etControl());
System::IntPtr ptr = win32Win->Handle;
HWND hwnd = (HWND)ptr.ToPointer();
CWnd* pWnd = CWnd::FromHandle(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.GetClipBox(&boundingBox);

CDC memDC;
CBitmap memBitmap;
memDC.CreateCompatibleDC(&netCtrlDC);
netCtrlDC.LPtoDP(&boundingBox);

memBitmap.CreateCompatibleBitmap(&netCtrlDC, boundingBox.Width(),
boundingBox.Height());

AutoSelector selectBitmap(&memDC, &memBitmap);
memDC.SetMapMode(netCtrlDC.GetMapMode());
memDC.SetWindowExt(netCtrlDC.GetWindowExt());
memDC.SetViewportExt(netCtrlDC.GetViewportExt());
netCtrlDC.DPtoLP(&boundingBox);
memDC.SetWindowOrg(boundingBox.left, boundingBox.top);
memDC.FillSolidRect(boundingBox, netCtrlDC.GetBkColor());

// Blit the contents of the .NetCtrl to the memDC
memDC.BitBlt(boundingBox.left,
boundingBox.top,
boundingBox.Width(),
boundingBox.Height(),
&netCtrlDC,
boundingBox.left,
boundingBox.top,
SRCCOPY);

// Debugging Crap Just Draw an X
AutoSelector selectPen(&memDC, BLACK_PEN); // Automatically Deslect
on exit
memDC.MoveTo(boundingBox.TopLeft());
memDC.LineTo(boundingBox.BottomRight());
memDC.MoveTo(boundingBox.right, boundingBox.top);
memDC.LineTo(boundingBox.left, boundingBox.bottom);
// End Debugging Crap

m_ContainedControl->Visible = false;

// Blit the contents of the MemDC to the real DC
CRect thisBoundingBox = GetBaseRgn().GetBounds();

pDC->BitBlt(thisBoundingBox.left,
thisBoundingBox.top,
thisBoundingBox.Width(),
thisBoundingBox.Height(),
&memDC,
boundingBox.left,
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********@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.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 CWinFormsControl
template.

How can I make this happen? Does anyone have any links to sample source
code?

Any help greatly appreciated.
Thanks,
Martin
Sep 14 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3018
by: David Whitney | last post by:
All: I have a control that renders a table. As the table is rendered, each row in the table is constructed by creating a run-time (dynamic) object that is derived from an HtmlTableRow. The row...
2
1104
by: | last post by:
how to highlight a row of my datagrid (web) . i dont want to use select button. upon clicking on the row , it shouls be highlighted and cursor shouls remain in the same row. how to acheive...
3
1074
by: James T. | last post by:
Hello! I am developing a custom server control that inherits from Hyperlink web control. I defined 2 new properties - ImageWidth and ImageHeight. My qustion is, how I can override Render...
5
2613
by: Dave A | last post by:
I am writing an ASP.NET tool that will allow the client to create their own online froms. ie the client can add tect boxes, text, drop downs,etc with absolutely no technical skill what so ever....
4
2044
by: jens Jensen | last post by:
Hello, I was given the task to build a .Net client that will talk to IBM integration server via HTTP post. The idea is that each http packet exchange should be authenticated via X09 "client...
5
2305
by: paul.hester | last post by:
Hi all, I have a custom control with an overridden Render method. Inside this method I'm rendering each control in its collection using their RenderControl method. However, I'm running into a...
2
1132
by: mmacrobert | last post by:
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...
0
2325
by: fireline1082 | last post by:
Hi every body I am doing a project in which I have a Windows Certificate Authority server and RADIUS server (IAS RADIUS server under windows ) . In this project the clients will login to the...
0
7216
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5613
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5028
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.