1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace Test
{
public static class NativeMethods
{
// declaration du délégué pour le callback de EnumChildWindows
public delegate int WindowEnumDelegate(IntPtr hwnd, int lParam);
[DllImport("user32.dll", SetLastError = true)]
public static extern int EnumChildWindows(IntPtr hwnd, WindowEnumDelegate del, int lParam);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int GetWindowText(IntPtr hWnd, [Out] StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam);
}
class Program
{
static void Main(string[] args)
{
string lpszParentClass = "ExploreWClass";
IntPtr ParenthWnd = IntPtr.Zero;
//trouve la fenêtre de explorer.exe
ParenthWnd = NativeMethods.FindWindow(lpszParentClass, null);
if (ParenthWnd.Equals(IntPtr.Zero))
Console.WriteLine("Explorer non trouvé");
else
{
// récupère le titre d'explorer.exe
string explorerTitle = GetWinText(ParenthWnd);
// instance de classe
EnumWindows ew = new EnumWindows(ParenthWnd, explorerTitle);
// récupération du chemin complet
ew.GetPathFromExplorerWindow();
//affiche chemin complet
if ((ew.FullPath != null) && (ew.FullPath != string.Empty))
Console.WriteLine(ew.FullPath);
}
}
public static string GetWinText(IntPtr hWin)
{
int length = NativeMethods.GetWindowTextLength(hWin);
StringBuilder sb = new StringBuilder(length + 1);
NativeMethods.GetWindowText(hWin, sb, sb.Capacity);
return sb.ToString();
}
}
public class EnumWindows
{
IntPtr m_hParent;
string m_title;
string m_fullPath;
const int WM_GETTEXT = 13;
const int WM_GETTEXTLENGTH = 14;
public string FullPath { get { return m_fullPath; } }
public EnumWindows(IntPtr hParent, string explorerTitle)
{
m_hParent = hParent;
m_title = explorerTitle;
}
public void GetPathFromExplorerWindow()
{
NativeMethods.WindowEnumDelegate callback = new NativeMethods.WindowEnumDelegate(EnumWindow);
NativeMethods.EnumChildWindows(this.m_hParent, callback, 0);
}
private int EnumWindow(IntPtr handle, int lParam)
{
//Console.WriteLine("handle : {0:x8}", handle.ToInt32());
string text = this.GetWinText(handle);
if (!String.IsNullOrEmpty(text))
{
if (text.Contains(this.m_title))
{
this.m_fullPath = text;
return 0;
}
else
return 1;
}
return 1;
}
private string GetWinText(IntPtr hWin)
{
StringBuilder sb;
IntPtr pLength = NativeMethods.SendMessage(hWin, WM_GETTEXTLENGTH, 0, null);
int length = pLength.ToInt32();
if (length > 0)
{
sb = new StringBuilder(length + 1);
pLength = NativeMethods.SendMessage(hWin, WM_GETTEXT, length+1, sb);
}
else
return string.Empty;
return sb.ToString();
}
}
} |
Partager