17 May 2007

Find a Window Handle

Une question fréquemment posée sur les newsgroups Windows Mobile : Comment trouver le handle d'une fenêtre?
A frequently asked question on WM newsgroup : How to find a Window Handle?

Cela peut être utile, si vous avez besoin:
- appeler une fonction native de l'API Windows avec un handle de fenêtre en paramètre (SetForeGroundWindow, ShowWindow...)
- envoyer un message Window à une fenêtre Window (WM_ACTIVATE, WM_CLOSE)
- interagir avec des fenêtre système comme la barre des tâches ou le SIP

That can be used, if you need:
- to call a native Windows API function with a window handle in parameter (SetForeGroundWindow, ShowWindow...)
- to send window message to an other window (WM_ACTIVATE, WM_CLOSE)
- to interact with a system window like the taskbar or the SIP

La fonction FindWindow a les paramètres suivants:
The FindWindow function has the following paramters:
HWND FindWindow(LPCTSTR lpClassName,
LPCTSTR lpWindowName);

Pour l'importer en C#:
To import in C#:
[DllImport("coredll.dll", EntryPoint="FindWindow")] public static extern int FindWindow(string lpClassName, string lpWindowName);
Pour vous aider à identifier les paramètres, un outil très interessant est disponible : Windows CE Remote Spy. Il permet de lister toutes les fenêtres du système et ensuite, vous pouvez identifier les deux paramètres nécessaires. Les deux copies d'écran montrent des exemples:

To help you to identify these parameters, there is a very interesting utility : Windows CE Remote Spy. It lists all the windows of the system and then, you can identify the two needed parameters. The two screenshots show examples:
1) Pour une application C# nommée WM6Test, vous devez appeler :
for a C# application named WM6Test, you must call
int h = FindWindow("#NETCF_AGL_BASE_", "WM6TEst");
2) Pour la barre des tâches:
for the taskbar:
int h = FindWindow("HHTaskBar", "");

J'espère que cela vous aidera, postez vos commentaires...

Hope this help you, post comments...