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

  • Thread starter Thread starter Martijn Mulder
  • Start date Start date
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?
 
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
 

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.
 
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
 
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.
 
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...
 
Back
Top