Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Get Control From Handle

59 views
Skip to first unread message

jonfroehlich

unread,
Jan 31, 2007, 1:08:30 AM1/31/07
to
I have not yet determined a clean way to invoke code on the UI thread
without access to a Form/Control. The Process class has a property
called "MainWindowHandle," which returns the window handle of the main
window of the associated process. So, for example,
Process.GetCurrentProcess().MainWindowHandle would return a Handle
(HWND) to the main window. But how can we translate a window handle to
a Form?

In .NET 2.0, this is relatively easy, we can simply call
Control.FromHandle(IntPtr handle). Thus, receiving access to the main
window is simply:
(Form)Control.FromHandle(Process.GetCurrentProcess().MainWindowHandle)

Unfortunately, however, .NET CF 2.0 does not expose the
Control.FromHandle method. I have not found anyone on the web who has
solved this problem. Given the limitations of CF, one hacky way to do
this would be to expose a static property on the MainForm class which
would return a reference to itself.

I was wondering if anyone else has come up with a separate solution.
Basically, I need to marshal calls into my UI thread, but I don't want
to pass around a reference to a From/Control.

A few relevant links:

1. https://forums.microsoft.com/MSDN/ShowPost.aspx?
PostID=855352&SiteID=1
2. http://groups.google.com/group/microsoft.public.dotnet.general/
browse_frm/thread/c65f0b7d652c3c3d/a9c80f50dd05af20?
lnk=st&q=MainWindowHandle+Form&rnum=4#a9c80f50dd05af20
3.
http://groups.google.com/group/microsoft.public.dotnet.framework.compactframework/browse_frm/thread/d6509bd2852d8b20/3ee2c216071da85b?lnk=gst&q=FromHandle&rnum=1#3ee2c216071da85b

Peter Foot [MVP]

unread,
Jan 31, 2007, 6:43:56 AM1/31/07
to
If you want to get your own main form, you are probably right, the best way
would be to have a static reference to your form somewhere in your app.

Peter

--
Peter Foot
Device Application Development MVP
www.peterfoot.net | www.inthehand.com

"jonfroehlich" <jonfro...@gmail.com> wrote in message
news:1170223710....@a34g2000cwb.googlegroups.com...

Neil Cowburn

unread,
Jan 31, 2007, 7:11:20 AM1/31/07
to
You should take a look at using SHChangeNotifyRegister with
SHCNE_MEDIAINSERTED. If you're not comfortable with P/Invoke, drop me a
mail.

--
Neil Cowburn
Principal Partner
OpenNETCF Consulting, LLC.

http://www.opennetcf.com/


"Peter Foot [MVP]" <feed...@nospam-inthehand.com> wrote in message
news:62E68EA8-0429-43F8...@microsoft.com...

jonfroehlich

unread,
Jan 31, 2007, 11:45:20 AM1/31/07
to
Neil,

I'm going to guess that your reply here is meant for a different
thread--it looks like you're answering a question about receiving a
notification when a device's storage card is inserted (I just wanted
to point that out to reduce confusion amongst those more uninformed
readers).

Peter, I was afraid you were going to say that. OK, I'll go ahead and
keep my current solution for now then.

On Jan 31, 4:11 am, "Neil Cowburn"


<neilc@[nospamthanks].opennetcf.com> wrote:
> You should take a look at using SHChangeNotifyRegister with
> SHCNE_MEDIAINSERTED. If you're not comfortable with P/Invoke, drop me a
> mail.
>
> --
> Neil Cowburn
> Principal Partner
> OpenNETCF Consulting, LLC.
>
> http://www.opennetcf.com/
>

> "Peter Foot [MVP]" <feedb...@nospam-inthehand.com> wrote in messagenews:62E68EA8-0429-43F8...@microsoft.com...


>
> > If you want to get your own main form, you are probably right, the best
> > way would be to have a static reference to your form somewhere in your
> > app.
>
> > Peter
>
> > --
> > Peter Foot
> > Device Application Development MVP
> >www.peterfoot.net|www.inthehand.com
>

> > "jonfroehlich" <jonfroehl...@gmail.com> wrote in message


> >news:1170223710....@a34g2000cwb.googlegroups.com...
> >>I have not yet determined a clean way to invoke code on the UI thread
> >> without access to a Form/Control. The Process class has a property
> >> called "MainWindowHandle," which returns the window handle of the main
> >> window of the associated process. So, for example,
> >> Process.GetCurrentProcess().MainWindowHandle would return a Handle
> >> (HWND) to the main window. But how can we translate a window handle to
> >> a Form?
>
> >> In .NET 2.0, this is relatively easy, we can simply call
> >> Control.FromHandle(IntPtr handle). Thus, receiving access to the main
> >> window is simply:
> >> (Form)Control.FromHandle(Process.GetCurrentProcess().MainWindowHandle)
>
> >> Unfortunately, however, .NET CF 2.0 does not expose the
> >> Control.FromHandle method. I have not found anyone on the web who has
> >> solved this problem. Given the limitations of CF, one hacky way to do
> >> this would be to expose a static property on the MainForm class which
> >> would return a reference to itself.
>
> >> I was wondering if anyone else has come up with a separate solution.
> >> Basically, I need to marshal calls into my UI thread, but I don't want
> >> to pass around a reference to a From/Control.
>
> >> A few relevant links:
>
> >> 1.https://forums.microsoft.com/MSDN/ShowPost.aspx?
> >> PostID=855352&SiteID=1

> >> 2.http://groups.google.com/group/microsoft.public.dotnet.general/
> >> browse_frm/thread/c65f0b7d652c3c3d/a9c80f50dd05af20?
> >> lnk=st&q=MainWindowHandle+Form&rnum=4#a9c80f50dd05af20
> >> 3.
> >>http://groups.google.com/group/microsoft.public.dotnet.framework.comp...


0 new messages