最近做一个项目,有一个功能点需要获取当前打开的文件夹,网上查资料+自己摸索,整理出如下代码,鉴于网上完整的代码比较少,顾贴出来,以供参考。如有更好的建议,欢迎留言。
class Program
{
public delegate bool CallBack(int hwnd, int y);
//该函数枚举所有屏幕上的顶层窗口,并将窗口句柄传送给应用程序定义的回调函数。
//回调函数返回FALSE将停止枚举,否则EnumWindows函数继续到所有顶层窗口枚举完为止。
[DllImport("user32.dll")]
public static extern int EnumWindows(CallBack x, int y);
//该函数将指定窗口的标题条文本(如果存在)拷贝到一个缓存区内
[DllImport("user32.dll")]
public static extern int GetWindowText(int hwnd, StringBuilder lptrString, int nMaxCount);
//该函数获得一个指定子窗口的父窗口句柄
[DllImport("user32.dll")]
public static extern int GetParent(int hwnd);
//该函数获得给定窗口的可视状态。
[DllImport("user32.dll")]
public static extern bool IsWindowVisible(int hwnd);
//获取窗体类名
[DllImport("User32.Dll ")]
public static extern void GetClassName(IntPtr hwnd, StringBuilder s, int nMaxCount);
//获取窗体的子窗体句柄
[DllImport("User32.dll ")]
public static extern IntPtr FindWindowEx(IntPtr parent, IntPtr childe, string strclass, string FrmText);
///
/// 根据句柄获取类名
///
///
///
private string GetFormClassName(IntPtr ptr)
{
StringBuilder nameBiulder = new StringBuilder(255);
GetClassName(ptr, nameBiulder, 255);
return nameBiulder.ToString();
}
///
/// 根据句柄获取窗口标题
///
///
///
private string GetFormTitle(IntPtr ptr)
{
StringBuilder titleBiulder = new StringBuilder(255);
GetWindowText((int)ptr, titleBiulder, 255);
return titleBiulder.ToString();
}
public bool Report(int hwnd, int lParam)
{
int pHwnd = GetParent(hwnd);
//如果再没有父窗口并且为可视状态的窗口,则遍历
if (pHwnd == 0 && IsWindowVisible(hwnd) == true)
{
IntPtr cabinetWClassIntPtr = new IntPtr(hwnd);
string cabinetWClassName = GetFormClassName(cabinetWClassIntPtr);
//如果类名为CabinetWClass ,则为explorer窗口,可以通过spy++查看窗口类型
if (cabinetWClassName.Equals("CabinetWClass", StringComparison.OrdinalIgnoreCase))
{
//下面为一层层往下查找,直到找到资源管理器的地址窗体,通过他获取窗体地址
IntPtr workerWIntPtr = FindWindowEx(cabinetWClassIntPtr, IntPtr.Zero, "WorkerW", null);
IntPtr reBarWindow32IntPtr = FindWindowEx(workerWIntPtr, IntPtr.Zero, "ReBarWindow32", null);
IntPtr addressBandRootIntPtr = FindWindowEx(reBarWindow32IntPtr, IntPtr.Zero, "Address Band Root", null);
IntPtr msctls_progress32IntPtr = FindWindowEx(addressBandRootIntPtr, IntPtr.Zero, "msctls_progress32", null);
IntPtr breadcrumbParentIntPtr = FindWindowEx(msctls_progress32IntPtr, IntPtr.Zero, "Breadcrumb Parent", null);
IntPtr toolbarWindow32IntPtr = FindWindowEx(breadcrumbParentIntPtr, IntPtr.Zero, "ToolbarWindow32", null);
string title = GetFormTitle(toolbarWindow32IntPtr);
Console.WriteLine(title);
int index = title.IndexOf(‘:‘);
index++;
string path = title.Substring(index, title.Length - index);
Console.WriteLine(path);
}
}
return true;
}
static void Main(string[] args)
{
EnumWindows(new Program().Report, 0);
Console.ReadKey();
}
}