Home All Groups Group Topic Archive Search About

System.Windows.Forms.Control.Handle==HWND?

Author
9 Jun 2009 7:10 PM
Martijn Mulder
The System.Windows.Forms.Control-member Handle can be cast to HWND like this

HWND hwnd=(HWND)this.Handle.ToPointer();

But is it a valid HWND, that can be used in DirectX for example? Or can the
value of Handle change in a managed environment?

Author
9 Jun 2009 8:11 PM
Armin Zingler
Martijn Mulder wrote:
> The System.Windows.Forms.Control-member Handle can be cast to HWND
> like this
> HWND hwnd=(HWND)this.Handle.ToPointer();

yes

> But is it a valid HWND, that can be used in DirectX for example?

yes

> Or
> can the value of Handle change in a managed environment?

no

It is the unmanaged, native, window handle, just wrapped in an IntPtr (not
really wrapped because it _is_ the handle, anyway....


Armin
Are all your drivers up to date? click for free checkup

Author
9 Jun 2009 11:12 PM
Jack Jackson
On Tue, 9 Jun 2009 22:11:10 +0200, "Armin Zingler"
<az.nospam@freenet.de> wrote:

Show quoteHide quote
>Martijn Mulder wrote:
>> The System.Windows.Forms.Control-member Handle can be cast to HWND
>> like this
>> HWND hwnd=(HWND)this.Handle.ToPointer();
>
>yes
>
>> But is it a valid HWND, that can be used in DirectX for example?
>
>yes
>
>> Or
>> can the value of Handle change in a managed environment?
>
>no

Sometimes.  .NET allows you to change properties of controls that can
only be accomodated by destroying and re-creating the underlying
Windows control.  If you make one of those changes, the HWND will
change.

Show quoteHide quote
>
>It is the unmanaged, native, window handle, just wrapped in an IntPtr (not
>really wrapped because it _is_ the handle, anyway....
>
>
>Armin
Author
10 Jun 2009 2:16 AM
Armin Zingler
Jack Jackson wrote:
Show quoteHide quote
> On Tue, 9 Jun 2009 22:11:10 +0200, "Armin Zingler"
> <az.nospam@freenet.de> wrote:
>
>> Martijn Mulder wrote:
>>> The System.Windows.Forms.Control-member Handle can be cast to HWND
>>> like this
>>> HWND hwnd=(HWND)this.Handle.ToPointer();
>>
>> yes
>>
>>> But is it a valid HWND, that can be used in DirectX for example?
>>
>> yes
>>
>>> Or
>>> can the value of Handle change in a managed environment?
>>
>> no
>
> Sometimes.  .NET allows you to change properties of controls that can
> only be accomodated by destroying and re-creating the underlying
> Windows control.  If you make one of those changes, the HWND will
> change.

Good point. As long as the window lives it has the same handle. Of course,
with a new window, it gets a new handle. I understood the question more
related to native pointers getting invalid due to memory management and
reallocated objects. That's never the case (in this case). So, the handle
does not change because it's a managed environment, it's because of the
internal behavior of some controls. Anyway, you're right.


Armin
Author
11 Jun 2009 10:51 PM
Herfried K. Wagner [MVP]
"Jack Jackson" <jjackson-***@cinnovations.net> schrieb:
>>> Or
>>> can the value of Handle change in a managed environment?
>>
>>no
>
> Sometimes.  .NET allows you to change properties of controls that can
> only be accomodated by destroying and re-creating the underlying
> Windows control.  If you make one of those changes, the HWND will
> change.

ACK.  I haven't tested it with .NET 2.0, but changing a textbox' border
style at runtime caused the creation of a new textbox and a new handle.
That's just one sample.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
17 Jun 2009 4:28 PM
Ben Voigt [C++ MVP]
Herfried K. Wagner [MVP] wrote:
Show quoteHide quote
> "Jack Jackson" <jjackson-***@cinnovations.net> schrieb:
>>>> Or
>>>> can the value of Handle change in a managed environment?
>>>
>>> no
>>
>> Sometimes.  .NET allows you to change properties of controls that can
>> only be accomodated by destroying and re-creating the underlying
>> Windows control.  If you make one of those changes, the HWND will
>> change.
>
> ACK.  I haven't tested it with .NET 2.0, but changing a textbox'
> border style at runtime caused the creation of a new textbox and a
> new handle. That's just one sample.

There are some hooks to determine when this happens, I forget whether
there's an event, there definitely is a virtual method than can be
overridden....

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onhandlecreated.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onhandledestroyed.aspx

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.handlecreated.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.handledestroyed.aspx

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.createhandle.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.recreatinghandle.aspx

The docs aren't very specific, but it sounds like the HandleCreated event
might not fire on a recreation.  In that case you'd need to override the
CreateHandle method.

Ok, from Reflector it looks like OnHandleCreated is called from WM_CREATE,
so recreation should trigger it as well.

Of course, .NET also runs on platforms that don't use HWND at all...

Bookmark and Share