How to render a windows form control to a given DC in

  • Thread starter Thread starter Buddhist.CHinA
  • Start date Start date
B

Buddhist.CHinA

In my application, I mixed win32 GDI porgramming and windows.form
control. And I had to draw the windows.form control on a win32 DC. But
I don't know how to accomplish it.

Now, I have a workaround to complete this. Firstly, I got a bitmap of
the windows.control, then pass the bitmap to unmanaged code and redraw
it in given DC.

But I didn't consider it was a straight way.
I believe there should be better way to do it. But how to?

Thx in advance.
 
Hi,

This question has been asked many times.
If you use .NET2.0 you can probably use DrawToBitmap method declared on the
level of the control. This method internaly sends WM_PRINT message, but I
have found that using PrintWindow API in some cases gives better results.

Anyhow, there are controls (such as RichTextBox) that cannot be printed well
to a bitmap.
 
Great!

I just did with DrawToBitmap, then get the HBITMAP handle, that's very
convinient.
thx. But I didn't quite understand your saying about "WM_PRINT" msg. I
got the handle from DrawToBitmap, is there sth to do with "WM_PRINT"
msg in the implementation?
But to PrintWindow API, it's a MFC method, I can't leverage from it :(
 
Hi,

I'm just saying that DrawToBitmap is implemented to send WM_PRINT thus,
controls (such as RichTextBox) that doesn't repond to this message will
still not work with DrawToBitmap.

PrintWindow is not MFC method, rather it is Win32 API method and MFC has a
wrapper for it. You can use PrintWindow API form .NET code using PInvoke.
The difference is that PrintWindow sends WM_PAINT to the window, thus it
goes through all normal painting procedure.
 
Wow!
I'll try it, see if can reslove the RichTextBox printing issue.

Thx again.
 
Regarding the richtextbox, I managed to print it by adding the control on a
panel and calling PrintWindow providing the handler to the panel, not the
richtextbox.
 
Back
Top