|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
System.Windows.Forms.Control.Handle==HWND?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 Mulder wrote:
> The System.Windows.Forms.Control-member Handle can be cast to HWND yes> like this > HWND hwnd=(HWND)this.Handle.ToPointer(); > But is it a valid HWND, that can be used in DirectX for example? yes> Or no> can the value of Handle change in a managed environment? It is the unmanaged, native, window handle, just wrapped in an IntPtr (not really wrapped because it _is_ the handle, anyway.... Armin On Tue, 9 Jun 2009 22:11:10 +0200, "Armin Zingler"
<az.nospam@freenet.de> wrote: Show quoteHide quote >Martijn Mulder wrote: Sometimes. .NET allows you to change properties of controls that can>> 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 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 Jack Jackson wrote:
Show quoteHide quote > On Tue, 9 Jun 2009 22:11:10 +0200, "Armin Zingler" Good point. As long as the window lives it has the same handle. Of course, > <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. 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" <jjackson-***@cinnovations.net> schrieb: ACK. I haven't tested it with .NET 2.0, but changing a textbox' border >>> 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. 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/> Herfried K. Wagner [MVP] wrote:
Show quoteHide quote > "Jack Jackson" <jjackson-***@cinnovations.net> schrieb: There are some hooks to determine when this happens, I forget whether >>>> 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'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...
Other interesting topics
Design Time Controls
Cannot use Mustinherit in a base form object properties presentation flicker when restraining one form to only move within the bounds of another Saving and retrieving WinForm template info Project consisting of multiple projects Validating Event Bug Win forms designer - controlling the designer code generation What control did Microsoft Use? Shortcut Keys - more than 26 |
|||||||||||||||||||||||