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

M

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?
 
A

Armin Zingler

Martijn said:
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
 
J

Jack Jackson


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.
 
A

Armin Zingler

Jack said:
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
 
H

Herfried K. Wagner [MVP]

Jack Jackson said:
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.
 
B

Ben Voigt [C++ MVP]

Herfried said:
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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top