PC Review


Reply
Thread Tools Rate Thread

Control.FromHandle for Compact Framework

 
 
jhow
Guest
Posts: n/a
 
      12th Aug 2009
I am trying to find a way to create a .NET form or control from a
window outside my own application (I know the handle of the window).
The desktop way would be to simply call Control.FromHandle, but this
method does not exist in the Compact Framework.

Are there any workarounds for this?

 
Reply With Quote
 
 
 
 
Paul G. Tobey [eMVP]
Guest
Posts: n/a
 
      12th Aug 2009
What do you want to *do* with it? That should control what you do next...

Paul T.

"jhow" <(E-Mail Removed)> wrote in message
news:4578b4c3-657b-4e88-8b9f-(E-Mail Removed)...
>I am trying to find a way to create a .NET form or control from a
> window outside my own application (I know the handle of the window).
> The desktop way would be to simply call Control.FromHandle, but this
> method does not exist in the Compact Framework.
>
> Are there any workarounds for this?
>



 
Reply With Quote
 
jhow
Guest
Posts: n/a
 
      14th Aug 2009
I have a small popup window in my application that I would like to
make a child of the startbar. I believe if I do this, it will stop the
window being hidden by the startbar.

As a result, I would like to get the handle of the startbar, create a
control from it and set it as the Owner of my popup.
 
Reply With Quote
 
Chris Tacke, MVP
Guest
Posts: n/a
 
      14th Aug 2009
Sounds like a kludge to me, but you don't need Control.FromHandle for that
anyway. You're P/Invoking to find the Start bar's handle, just P/Invoke
SetParent to reparent your dialog.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com

"jhow" <(E-Mail Removed)> wrote in message
news:1a231fe7-da81-4b7b-b554-(E-Mail Removed)...
>I have a small popup window in my application that I would like to
> make a child of the startbar. I believe if I do this, it will stop the
> window being hidden by the startbar.
>
> As a result, I would like to get the handle of the startbar, create a
> control from it and set it as the Owner of my popup.


 
Reply With Quote
 
jhow
Guest
Posts: n/a
 
      15th Aug 2009
Thanks Chris, yes it is a kludge, but I've racked my brains over this
one and it's the only way I can think of. Using the Topmost property
only works until some other window such as the Startbar bumps it off
the top of the z-order. Do you have any better ideas?

I tried your SetParent solution but this fails for some reason. Any
ideas why?

[DllImport("Coredll.dll", EntryPoint="FindWindow",
CharSet=CharSet.Unicode)]
public static extern IntPtr FindWindow(string _WindowClassName, string
_WindowName);

[DllImport("Coredll.dll", EntryPoint="SetParent", CharSet =
CharSet.Unicode)]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr
hWndParent);

_pickerForm = new PickerForm();

//not necessary, but just in case
_pickerForm.Owner = null;

//returns a valid handle
IntPtr handle = FindWindow("HHTaskBar", null);

//shows "0" indicating failure
MessageBox.Show(SetParent(_pickerForm.Handle, handle).ToString());

//even this.Handle doesn't work
MessageBox.Show(SetParent(_pickerForm.Handle, this.Handle).ToString
());
 
Reply With Quote
 
jhow
Guest
Posts: n/a
 
      19th Aug 2009
Is there anybody out there?
 
Reply With Quote
 
Paul G. Tobey [eMVP]
Guest
Posts: n/a
 
      19th Aug 2009
I don't believe that this is going to be possible. You've got a process
boundary there that should not be broken and that I'm certain is not
designed to be broken by the .NET CF. Here's what I'd do:

Forget about managed code and try this in native code. If you can't make
that work (and that would not surprise me one bit), trying to do it from
managed code is a waste of time.

If it doesn't work, think about other ways that you might achieve what you
need. You need a pop up window of some sort associated with the taskbar,
right? What about a notification balloon associated with an icon in the
system tray? This breaks through the process barrier and has the shell
present what you give it and then sends you messages associated with user
actions. It's also supported...

Paul T.

"jhow" <(E-Mail Removed)> wrote in message
news:9fa11a35-3352-4931-aa5e-(E-Mail Removed)...
> Is there anybody out there?



 
Reply With Quote
 
jhow
Guest
Posts: n/a
 
      20th Aug 2009
A notification window is not quite what I'm looking for and I know
what I am trying to do is possible, because I've seen it done in at
least 3 other applications before. It is quite a common feature in
dictionary applications.

What I'm looking for is something like this:
http://www.pda4x.com/attachmentes/Da...reenshot_2.jpg

or the same popup dragged to a different position on the startbar
here:
http://wronek.wdfiles.com/local--files/mdict/mdict.JPG

If you look at the start bar on the first image, you will see a small
popup window (cum button) (aka picker window) to the right of the
title "MDict". This popup can be freely dragged about anywhere in the
startbar region, and, despite what happens to the start bar, it always
stays "on top".

Other dictionary applications allow their picker windows to be dragged
about anywhere within the entire screen area and also manage to always
stay topmost.

I'm just puzzled as to how they do it. Do these images and this
explanation shed any light to you on a possible solution?
 
Reply With Quote
 
Paul G. Tobey [eMVP]
Guest
Posts: n/a
 
      20th Aug 2009
It looks to me like there's either a system tray item being added or just a
front-most window that gets pushed to the top of the stack all the time. If
you just say, "I need a floating window to always be on top of everything,
even the start menu.", the first thing that comes to mind is "topmost". Of
course, other topmost windows will try to come to the front when you select
them, but I don't see any reason why you couldn't push yourself back on top
as soon as you detect that.

Paul T.

"jhow" <(E-Mail Removed)> wrote in message
news:2eb4d810-5d64-4f60-96c8-(E-Mail Removed)...
>A notification window is not quite what I'm looking for and I know
> what I am trying to do is possible, because I've seen it done in at
> least 3 other applications before. It is quite a common feature in
> dictionary applications.
>
> What I'm looking for is something like this:
> http://www.pda4x.com/attachmentes/Da...reenshot_2.jpg
>
> or the same popup dragged to a different position on the startbar
> here:
> http://wronek.wdfiles.com/local--files/mdict/mdict.JPG
>
> If you look at the start bar on the first image, you will see a small
> popup window (cum button) (aka picker window) to the right of the
> title "MDict". This popup can be freely dragged about anywhere in the
> startbar region, and, despite what happens to the start bar, it always
> stays "on top".
>
> Other dictionary applications allow their picker windows to be dragged
> about anywhere within the entire screen area and also manage to always
> stay topmost.
>
> I'm just puzzled as to how they do it. Do these images and this
> explanation shed any light to you on a possible solution?



 
Reply With Quote
 
Christopher Fairbairn [MVP]
Guest
Posts: n/a
 
      21st Aug 2009
Hi,

"jhow" <(E-Mail Removed)> wrote in message
news:2eb4d810-5d64-4f60-96c8-(E-Mail Removed)...
>A notification window is not quite what I'm looking for and I know
> what I am trying to do is possible, because I've seen it done in at
> least 3 other applications before. It is quite a common feature in
> dictionary applications.


They probably are doing this by reparenting their window to the window which
represents the navbar.

Within C# / .NET CF it's all a bit of a hack (and I wouldn't really even
recommend doing it at the native level...) but if you follow these
instructions hopefully it's something close to what you want...

Start off by adding a new form to your project (I called it "IconForm"), and
set the following properties.

FormBorderStyle = none
MaximizeBox = false
ControlBox = false
Size = 24,24

Also make sure you delete the MainMenu instance.

Then add the following class to your project

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

public static class FormExtensions
{
public static void ReparentToNavBar(this Form form, Point location)
{
IntPtr hWndParent = FindWindow("HHTaskBar", null);
Reparent(form, hWndParent, location);
}

// Reparents "form" to be a child of "hWndParent" positioned
// using the x,y co-ordinates specified by "location".
public static void Reparent(this Form form, IntPtr hWndParent, Point
location)
{
IntPtr hWndChild = form.Handle;

// Remove the WS_POPUP window style and
// add the WS_CHILD window style
uint style = GetWindowLong(hWndChild, GWL_STYLE);
style &= ~WS_POPUP;
style |= WS_CHILD;
SetWindowLong(hWndChild, GWL_STYLE, style);

// Reparent the window
SetParent(hWndChild, hWndParent);

// Adjust the positioning of the form
form.Location = location;
}

private const uint WS_POPUP = 0x80000000;
private const uint WS_CHILD = 0x40000000;

private const uint WS_EX_TOPMOST = 0x00000008;

private const int GWL_STYLE = -16;
private const int GWL_EXSTYLE = -20;

[DllImport("coredll.dll")]
private static extern uint GetWindowLong(IntPtr hWnd, int nIndex);

[DllImport("coredll.dll")]
private static extern uint SetWindowLong(IntPtr hWnd, int nIndex, uint
dwNewLong);

[DllImport("coredll.dll")]
private static extern IntPtr FindWindow(string lpClassName, string
lpWindowName);

[DllImport("coredll.dll")]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr
hWndNewParent);
}

Once this has been done, you should be able to add a button click event
handler or something similiar to invoke the code to add your custom window
to the nav bar. A code snippet similiar to the following should be
sufficient:

Form f = new IconForm(); // or what ever you named your custom form
f.Show();
f.ReparentToNavBar(new Point(45, 0));

And to remove it, you would simply execute

f.Close();
f.Dispose();

You should see that this window appears on the top of your screen, and
should be fairly visible throughout most applications (except those which
purposely hide the navbar). Its rather hacky though and potentially prone to
breaking.

Hope this helps,
Christopher Fairbairn


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calendar control in compact framework 1.1 =?Utf-8?B?c2FtZWVy?= Microsoft Dot NET Compact Framework 7 8th Feb 2007 10:39 PM
webbrowser control in compact framework =?Utf-8?B?TmFyYXNpbWhhbQ==?= Microsoft C# .NET 1 10th Nov 2006 06:54 PM
Compact Framework .NET V2 & WebBrowser Control =?Utf-8?B?UEpUZXdrZXNidXJ5?= Microsoft Dot NET 0 8th Mar 2005 02:21 PM
Compact Framework and Tab Control David Pope Microsoft Dot NET 0 7th Feb 2005 11:53 PM
Compact Framework - Custom Control Keith Microsoft VB .NET 3 30th May 2004 03:16 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:19 PM.