471,348 Members | 1,723 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Device Context

Is it possible to render a winform directly to a memory device context
instead of rendering to a display device context ?

Jun 27 '08 #1
6 5202
On Thu, 12 Jun 2008 18:22:00 -0700, ncolosi
<nc*****@discussions.microsoft.comwrote:
Is it possible to render a winform directly to a memory device context
instead of rendering to a display device context ?
In .NET, you render to a Graphics instance, not a DC. So technically, you
can do neither.

Depending on what you are _really_ trying to do, you may find the
Control.DrawToBitmap() method useful. Googling this newsgroup for other
threads that mention that method will turn up other techniques as well,
including Graphics.CopyFromScreen() and using p/invoke with the WM_PRINT
message.

Pete
Jun 27 '08 #2
Hi Peter, Thanks

The graphics object has a method called GetHdc() that gets a handle to the
current device context which then I can use in a win32 call to
createcompompatipledc.....

but I want to capture the form without it displaying to the physical display
if at all possible. I played around with Control.DrawToBitmap but the form is
still rendered to the physical display, I was just hoping I could change the
Device Context or something similar, whether in managed or unmanged code, so
that the Form would be rendered to a different type of device, so that the
form is captured but never seen on the display.

I went as far as to instantiate the new form call the show() method, capture
the
screen, then set the form to .visible = false; You don't see the form but
there is a
definite ugliness of seeing the screen flicker.

"Peter Duniho" wrote:
On Thu, 12 Jun 2008 18:22:00 -0700, ncolosi
<nc*****@discussions.microsoft.comwrote:
Is it possible to render a winform directly to a memory device context
instead of rendering to a display device context ?

In .NET, you render to a Graphics instance, not a DC. So technically, you
can do neither.

Depending on what you are _really_ trying to do, you may find the
Control.DrawToBitmap() method useful. Googling this newsgroup for other
threads that mention that method will turn up other techniques as well,
including Graphics.CopyFromScreen() and using p/invoke with the WM_PRINT
message.

Pete
Jun 27 '08 #3
On Thu, 12 Jun 2008 18:56:06 -0700, ncolosi
<nc*****@discussions.microsoft.comwrote:
The graphics object has a method called GetHdc() that gets a handle to
the
current device context which then I can use in a win32 call to
createcompompatipledc.....
Yes it does and yes you can. And yet, that has nothing to do with your
question.
but I want to capture the form without it displaying to the physical
display
if at all possible. I played around with Control.DrawToBitmap but the
form is
still rendered to the physical display,
DrawToBitmap() does not itself prevent the form from being shown
on-screen. In fact, it is generally limited to drawing the form as it's
shown on-screen, including any clipping that might occur (I know for
controls within a form this is true, and it might be for the whole form
too).
I was just hoping I could change the
Device Context or something similar, whether in managed or unmanged
code, so
that the Form would be rendered to a different type of device, so that
the
form is captured but never seen on the display.
I mentioned WM_PRINT. That might accomplish what you want, if you send it
to a form that's not visible but has otherwise been created. You may have
to call Control.CreateControl() first since you're not actually showing
the form.

I prefer not to second-guess, but the fact is what you're trying to do is
pretty unusual (as evidenced by lack of support in the API for it). I
don't know what your ultimate goal here is, but you may want to rethink
it. :)

Pete
Jun 27 '08 #4
Hi Peter,

What I meant by the first statement was a nice way of saying that a Device
Context does exist in .net, and the Graphics object does encapsulate it's
functionality, that's all I meant by that scenario.

I do like your second option with WM_PAINT, though.

On MSDN documentation it says that there are 4 types of Device Contexts, 2
obvious
Display, Printer, the third being a memory DC.......
The documentation states that if you use a memory DC and have your
application write to this vs a Display DC, the output is rendered, captured
and never shows, because you by-passed the Display device. Maybe I read to
much into this, but it sounded as though there was a way to set this Device
Context so that your app renders to a device of choosing. Unfortunitley the
documentation pretty much stopped there, and not going into how to change the
default funtionality of rendering to a Display DC.

Thanks

Nick

"Peter Duniho" wrote:
On Thu, 12 Jun 2008 18:56:06 -0700, ncolosi
<nc*****@discussions.microsoft.comwrote:
The graphics object has a method called GetHdc() that gets a handle to
the
current device context which then I can use in a win32 call to
createcompompatipledc.....

Yes it does and yes you can. And yet, that has nothing to do with your
question.
but I want to capture the form without it displaying to the physical
display
if at all possible. I played around with Control.DrawToBitmap but the
form is
still rendered to the physical display,

DrawToBitmap() does not itself prevent the form from being shown
on-screen. In fact, it is generally limited to drawing the form as it's
shown on-screen, including any clipping that might occur (I know for
controls within a form this is true, and it might be for the whole form
too).
I was just hoping I could change the
Device Context or something similar, whether in managed or unmanged
code, so
that the Form would be rendered to a different type of device, so that
the
form is captured but never seen on the display.

I mentioned WM_PRINT. That might accomplish what you want, if you send it
to a form that's not visible but has otherwise been created. You may have
to call Control.CreateControl() first since you're not actually showing
the form.

I prefer not to second-guess, but the fact is what you're trying to do is
pretty unusual (as evidenced by lack of support in the API for it). I
don't know what your ultimate goal here is, but you may want to rethink
it. :)

Pete
Jun 27 '08 #5
Hey Peter,

Lets take this from a different angle. Lets say I have Device X, and I've
created
a device driver for device X, how do I get a .Net application to render it's
contents to Device X .VS. the default device the Display ?

Thanks

Nick

"Peter Duniho" wrote:
On Thu, 12 Jun 2008 18:56:06 -0700, ncolosi
<nc*****@discussions.microsoft.comwrote:
The graphics object has a method called GetHdc() that gets a handle to
the
current device context which then I can use in a win32 call to
createcompompatipledc.....

Yes it does and yes you can. And yet, that has nothing to do with your
question.
but I want to capture the form without it displaying to the physical
display
if at all possible. I played around with Control.DrawToBitmap but the
form is
still rendered to the physical display,

DrawToBitmap() does not itself prevent the form from being shown
on-screen. In fact, it is generally limited to drawing the form as it's
shown on-screen, including any clipping that might occur (I know for
controls within a form this is true, and it might be for the whole form
too).
I was just hoping I could change the
Device Context or something similar, whether in managed or unmanged
code, so
that the Form would be rendered to a different type of device, so that
the
form is captured but never seen on the display.

I mentioned WM_PRINT. That might accomplish what you want, if you send it
to a form that's not visible but has otherwise been created. You may have
to call Control.CreateControl() first since you're not actually showing
the form.

I prefer not to second-guess, but the fact is what you're trying to do is
pretty unusual (as evidenced by lack of support in the API for it). I
don't know what your ultimate goal here is, but you may want to rethink
it. :)

Pete
Jun 27 '08 #6
On Fri, 13 Jun 2008 06:59:00 -0700, ncolosi
<nc*****@discussions.microsoft.comwrote:
Lets take this from a different angle. Lets say I have Device X, and
I've created
a device driver for device X, how do I get a .Net application to render
it's
contents to Device X .VS. the default device the Display ?
That depends on what you mean by "contents". If it's your own class, you
just draw to the appropriate DC at the appropriate time.

If it's some other class, you hope that it supports WM_PRINT. Because if
it doesn't, short of somehow hooking into Windows's own screen
invalidation and redraw implementation, you don't have enough control over
those other classes.

Pete
Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Abd | last post: by
1 post views Thread by Ben Taylor | last post: by
reply views Thread by sandy_pt_in | last post: by
1 post views Thread by Dennis | last post: by
reply views Thread by =?Utf-8?B?bmNvbG9zaQ==?= | last post: by

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.