Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Compact Framework / August 2007

Tip: Looking for answers? Try searching our database.

FindWindowW not working for form controls

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ink - 30 Aug 2007 11:16 GMT
Hi All

I have been trying for hours to get a handle to the controls on my form with
no luck.
I have tried a number of different veriations of FindWindow with no luck. It
seems to only work for top level Forms, I can get the handle to them fine,
but nothing on the form itself.

Some backgroung:
I am using Windows Mobile 5 with CF2.0
Have 2 applications AppMain and AppToTest.
I load up AppMain and on it there is a button, when I click the button I
start AppToTest as a Process using Process.Start("AppToTest Path","").
This all works fine. I can even get the MainWindowHandle from the process.
The problem comes when trying to get hold of the Handles for the Buttons or
Edit boxes on that AppToTest form.

I have opened Remote Spy and I can see them there but I can seem to get hold
of then.

How does every one else do it?
I don't even mined looping round all the forms controls getting the handles
that way but I cant even see how that could be done cose there doesn't seem
to be any enumWindows API.

Any help would be much appriciated.  Bellow I have attached some of the
fings I have tried and I always seem to get a 0 returned.

Thanks,
Ink

Some Examples of things I have tried.

 [DllImport("coredll.dll", EntryPoint = "FindWindowW", SetLastError =
true)]
       static extern IntPtr FindWindowW(string lpClassName, string
lpWindowName);

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

       [DllImport("coredll.dll", EntryPoint = "FindWindow", SetLastError =
true, CharSet = CharSet.Auto)]
       static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr
hwndChildAfter, string lpszClass, string lpszWindow);

FindWindowW(null, "Set TB Text")

FindWindowW("", "Set TB Text")

FindWindowW("Edit", null)

FindWindowW("Edit", "My Text Box")

FindWindowEx(2080908848, IntPtr.Zero, "Edit", "My Text Box")

FindWindowEx(2080908848, IntPtr.Zero, "Button", "Set TB Text")

FindWindowEx(2080908848, 0, "Button", "Set TB Text")
Christian Resma Helle - 30 Aug 2007 11:33 GMT
There is a Handle property in .NET Compact Framework 2.0. You can just use
that instead of doing a FindWindow

Signature

Regards,
Christian Resma Helle
http://christian-helle.blogspot.com

> Hi All
>
[quoted text clipped - 56 lines]
>
> FindWindowEx(2080908848, 0, "Button", "Set TB Text")
ink - 30 Aug 2007 11:44 GMT
I am trying to get at it from a different application.

> There is a Handle property in .NET Compact Framework 2.0. You can just use
> that instead of doing a FindWindow
[quoted text clipped - 60 lines]
>>
>> FindWindowEx(2080908848, 0, "Button", "Set TB Text")
<ctacke/> - 30 Aug 2007 13:34 GMT
Look at EnumChildWindows - I'm sure that's what Spy is using.

Signature

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com

>I am trying to get at it from a different application.
>
[quoted text clipped - 62 lines]
>>>
>>> FindWindowEx(2080908848, 0, "Button", "Set TB Text")
ink - 30 Aug 2007 14:02 GMT
Hi ctacke,

Already checked it and is not part of the CoreDLL. Check this web page out.
http://mulliner.org/pocketpc/GetFuncAddr.input_full_coredll.txt

and I tried testing the EnumWindows to work with no luck. I was under the
impression that with CF2 I would be able to use it because the documentation
said the Compact Framework 2.0 would support callbacks  for the
EnumWindowsProc but I have been unsuccessful in getting it to work.

And even if I got it to work I think it wouldn't pick up child windows this
is directly from the documentation.
"The EnumWindows function does not enumerate child windows, with the
exception of a few top-level windows owned by the system that have the
WS_CHILD style"

So that is a bummer.

So the next step I have tacken is to try and customise this clever blocks
briliant function to include child windows.
http://blog.opennetcf.org/ayakhnin/PermaLink.aspx?guid=ce482a27-4ae9-4555-b050-8
27af03b7bda


And it now seems to work.
I can enumerate to find the form I want and the enumerate the child windows
of that form outputting both the handle and the caption of each window on
the form.

I will attach the code that I have changed to get it to function how I want
at the bottom you will have to forgive the messy why I have done it because
it is just a profe of consept at the moment. To test it download the zip it
is in VS2003 format and replace his WindowsHelper class with mine from
below.

I realy can't belive that this is the best way of getting hold of child
windows though.
If any one has any better sugestions. Please I am listening.

Thanks,
Ink

//Start of Class
public class WindowHelper
{

 private const uint GW_HWNDFIRST = 0;
 private const uint GW_HWNDLAST = 1;
 private const uint GW_HWNDNEXT = 2;
 private const uint GW_HWNDPREV = 3;
 private const uint GW_OWNER = 4;
 private const uint GW_CHILD = 5;

 [DllImport("coredll.dll")]
 static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);

 [DllImport("coredll.dll", SetLastError=true)]
 static extern int GetWindowText(IntPtr hWnd, [Out] StringBuilder lpString,
int nMaxCount);

 [DllImport("coredll.dll", SetLastError=true, CharSet=CharSet.Auto)]
 static extern int GetWindowTextLength(IntPtr hWnd);

 [DllImport("coredll.dll")]
 static extern IntPtr GetParent(IntPtr hWnd);

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

 [DllImport("coredll.dll")]
 static extern IntPtr GetActiveWindow();

 public static Window[] EnumerateTopWindows()
 {
  ArrayList winList = new ArrayList();
  IntPtr hwnd = IntPtr.Zero;
  Window window = null;
  StringBuilder sb = null;

  // Get the first window
  hwnd = GetActiveWindow();
  hwnd = GetWindow(hwnd, GW_HWNDFIRST);

  while(hwnd != IntPtr.Zero)
  {
   IntPtr parentWin = GetParent(hwnd);
   // Make sure that the window doesn't a parent
   if ((parentWin == IntPtr.Zero))
   {
    int length       = GetWindowTextLength(hwnd);
    // Check it has caption text
    if (length > 0)
    {
     sb = new StringBuilder(length + 1);
     GetWindowText(hwnd, sb, sb.Capacity);

     if(sb.ToString()=="my Demo")
     {
      window = new Window();
      window.Handle = hwnd;

      window.Caption = sb.ToString() + " - " + hwnd.ToString();
      winList.Add(window);

      EnumChildWindows(winList,hwnd);
     }

    }
   }

   hwnd = GetWindow(hwnd, GW_HWNDNEXT);
  }

  return (Window[])winList.ToArray(typeof(Window));

 }

 public static void EnumChildWindows(ArrayList winList,IntPtr HWnd)
 {
  IntPtr hwnd = IntPtr.Zero;
  StringBuilder sb = null;
  Window window = null;

  hwnd = GetWindow(HWnd, GW_CHILD);

  while(hwnd != IntPtr.Zero)
  {

    int length       = GetWindowTextLength(hwnd);
    // Check it has caption text
    if (length > 0)
    {
     sb = new StringBuilder(length + 1);
     GetWindowText(hwnd, sb, sb.Capacity);

     window = new Window();
     window.Handle = hwnd;
     window.Caption = sb.ToString() + " - " + hwnd.ToString();
     winList.Add(window);

    }

   hwnd = GetWindow(hwnd, GW_HWNDNEXT);
  }

 }

}

//End of class

//============================================

> Look at EnumChildWindows - I'm sure that's what Spy is using.
>
[quoted text clipped - 65 lines]
>>>>
>>>> FindWindowEx(2080908848, 0, "Button", "Set TB Text")

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.