473,657 Members | 2,711 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 CWinFormsContro l
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 1897
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

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_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
Sep 14 '06 #3

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

Similar topics

3
3024
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 has three HtmlTableCell objects, and each cell contains a single control added to the HtmlTableCell's Controls collection. The basic table renders correctly, but the controls within the HtmlTableCell objects do not; the cells are just empty. (Just...
2
1110
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 it? plz help me out
3
1082
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 method so if Hyperling ImageUrl is set then add ImageWitdh and ImageHeight attributes to image. Thank you!
5
2629
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. The form can then be deployed to their intranet. The form designer is somewhat WYSIWYG but it contains a heap of 'edit', 'delete' and reordering buttons that are not seen when the form is acutally being used. I would love to have a preview...
4
2055
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 authentication and not "server authentification" The code i wrote is quite straight forward. (See listing below) With server authentication, the server's application will contain a certificate created specifically for that server. The client...
5
2331
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 problem in this scenario: <myprefix:mycontrol runat="server"> <%= SomeVariable %> </myprefix:mycontrol>
2
1135
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 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.
0
2331
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 domain and get certificates from CA. I know this can be done through Windows configuration, but it's not good. I want to understand what is going behind, so I want to develop my own code. In brief, My question is : I want to do certificate enrollment...
0
8407
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8319
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8837
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8739
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7347
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2739
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.