FindWindowW not working for form controls

I

ink

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")
 
C

Christian Resma Helle

There is a Handle property in .NET Compact Framework 2.0. You can just use
that instead of doing a FindWindow
 
I

ink

I am trying to get at it from a different application.





Christian Resma Helle said:
There is a Handle property in .NET Compact Framework 2.0. You can just use
that instead of doing a FindWindow

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


ink said:
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")
 
G

Guest

Look at EnumChildWindows - I'm sure that's what Spy is using.


--

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


ink said:
I am trying to get at it from a different application.





Christian Resma Helle said:
There is a Handle property in .NET Compact Framework 2.0. You can just
use that instead of doing a FindWindow

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


ink said:
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")
 
I

ink

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-827af03b7bda

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.


--

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


ink said:
I am trying to get at it from a different application.





Christian Resma Helle said:
There is a Handle property in .NET Compact Framework 2.0. You can just
use that instead of doing a FindWindow

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


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")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top