473,407 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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 1131
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
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
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
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
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
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
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
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...
13
by: Bob Jones | last post by:
Here is my situation: I have an aspx file stored in a resource file. All of the C# code is written inline via <script runat="server"tags. Let's call this page B. I also have page A that contains...
0
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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
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...

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.