cad.net 利用win32api实现不重复打开dwg路径的文件夹(资源管理器)

这里地址的方法也是可用的,但是net3.5不能使用  

为此我选择使用win32api的方式来遍历当前桌面所有资源管理器

 

        /// 
        /// 不重复打开dwg路径的资源管理器
        /// 
        [CommandMethod("JJ_OpenDwgFilePath")]
        public static void JJ_OpenDwgFilePath()
        {
            Database db = HostApplicationServices.WorkingDatabase;//当前的数据库
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            ed.WriteMessage("\n****惊惊盒子-打开当前Dwg的目录");
            try
            {
                string dwgname = CadSystem.Getvar("dwgname");

                var filename1 = Intranet.GetUNCPath(db.Filename);
                var filename2 = CadSystem.Getvar("dwgprefix") + dwgname;

                var regex = new Regex(".dwt");//正则 
                //符合.dwt默认面板 || 文件不存在(这个才支持局域网)
                if (regex.IsMatch(filename1) || !File.Exists(filename2))
                {
                    ed.WriteMessage("\n你没有保存文件!");
                }
                else
                {
                    ShellWindows wins = new ShellWindows();//这个名字是为了兼容高版本(没想到吧
                    foreach (InternetExplorer item in wins)
                    {
                        if (item.LocationURL + "\\" + dwgname == filename2)
                        {
                            item.Quit();//关闭 
                        }
                    }
                    Process.Start("explorer", "/select," + filename2);//重开一个,防止选择状态被改变 
                }
            }
            catch (System.Exception e)
            {
                ed.WriteMessage(e.Message);
                throw e;
            }
        }

 

 

迭代器使用:

    public struct InternetExplorer
    {
        public IntPtr HWND { get; set; }//句柄
        public string LocationURL { get; set; }//文件夹路径
        public void Quit()//关闭文件夹
        {
            Win32api.QuitToolbar(HWND);
        }
    }

    /// 
    /// 遍历桌面资源管理器
    /// 
    public class ShellWindows : IEnumerable
    {
        private readonly ArrayList list;
        public IEnumerator GetEnumerator()
        {
            return (list as IEnumerable).GetEnumerator();
        }

        /// 
        /// 获取桌面所有文件夹的路径
        /// 
        /// 
        public ShellWindows()
        {
            var allDesktopWindows = Win32api.GetAllDesktopWindows();
            var lst = new List();
            foreach (var item in allDesktopWindows)
            {
                if (item.className == "CabinetWClass")
                {
                    string a = item.windowName;
                    var fi = Win32api.FindWindowEx(item.hwnd, 0, "WorkerW", null);
                    if (fi != IntPtr.Zero)
                    {
                        fi = Win32api.FindWindowEx(fi, 0, "ReBarWindow32", null);
                        if (fi != IntPtr.Zero)
                        {
                            fi = Win32api.FindWindowEx(fi, 0, "Address Band Root", null);
                            if (fi != IntPtr.Zero)
                            {
                                fi = Win32api.FindWindowEx(fi, 0, "msctls_progress32", null);
                                if (fi != IntPtr.Zero)
                                {
                                    fi = Win32api.FindWindowEx(fi, 0, "Breadcrumb Parent", null);
                                    if (fi != IntPtr.Zero)
                                    {
                                        fi = Win32api.FindWindowEx(fi, 0, "ToolbarWindow32", null);//资源管理器
                                        //知识:toolbar上的按钮没有handler,要用发送通知信息
                                        if (fi != IntPtr.Zero)
                                        {
                                            StringBuilder sb = new StringBuilder(256);
                                            //获取窗口名称-路径地址
                                            Win32api.GetWindowText(fi, sb, sb.Capacity);
                                            string path = sb.ToString();
                                            path = path.Substring(4, path.Length - 4);//4表示"地址: "长度

                                            InternetExplorer ie = new InternetExplorer
                                            {
                                                HWND = item.hwnd,
                                                LocationURL = path
                                            };
                                            lst.Add(ie);
                                        }
                                    }
                                }
                            }
                        }

                    }
                }
            }
            list = new ArrayList(lst);
        }
    }
View Code

 

win32api使用:

#if !HC2019
#else
using GrxCAD.DatabaseServices;
using GrxCAD.EditorInput;
using GrxCAD.Geometry;
using GrxCAD.ApplicationServices;
using GrxCAD.Runtime;
#endif
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
namespace JingJingBoxDD
{ 
    public class Win32api
    {
        // https://blog.csdn.net/bcbobo21cn/article/details/50930221

        public delegate bool WNDENUMPROC(IntPtr hwnd, int lParam);

        /// 
        /// 置前窗口
        /// 
        /// 
        /// 
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern bool SetForegroundWindow(IntPtr hwnd);

        /// 
        /// 枚举窗口 
        /// 
        /// 
        /// 
        /// 
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, int lParam);

        /// 
        /// 获取窗口Text 
        /// 
        /// 
        /// 
        /// 
        /// 
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int GetWindowText(IntPtr hwnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);

        /// 
        /// 获取窗口类名
        /// 
        /// 
        /// 
        /// 
        /// 
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int GetClassName(IntPtr hwnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);

        /// 
        /// 窗口隐藏
        /// 
        /// 窗口句柄
        /// 
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern bool IsWindowVisible(IntPtr hwnd);

        /// 
        /// 查找子窗口
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern IntPtr FindWindowEx(IntPtr hwnd, uint hwndChildAfter, string lpszClass, string lpszWindow);

        ///https://jingyan.baidu.com/article/c45ad29cd5fb58051653e278.html
        /// 
        /// 发送消息
        ///  
        ///   
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

        /// 
        /// 发送消息
        ///  
        ///   
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, ref Rectangle lParam);

        /// 
        /// 关闭文件夹
        /// 
        /// 
        public static void QuitToolbar(IntPtr hwnd)
        {
            // https://docs.microsoft.com/zh-cn/windows/desktop/winmsg/wm-close
            const int WM_CLOSE = 0x0010;
            SendMessage(hwnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
        }

        //窗口样式 
        public struct WindowInfo
        {
            public IntPtr hwnd;
            public string windowName;
            public string className;
        }

        /// 
        /// 枚举所有桌面窗口
        /// 
        /// 
        public static WindowInfo[] GetAllDesktopWindows()
        {
            //用来保存窗口对象 列表
            var wndList = new List();

            //枚举所有桌面窗口
            EnumWindows(delegate (IntPtr hWnd, int lParam)
            {
                WindowInfo wnd = new WindowInfo();
                StringBuilder sb = new StringBuilder(256);

                //获得HWND
                wnd.hwnd = hWnd;

                //获取窗口名称
                GetWindowText(hWnd, sb, sb.Capacity);
                wnd.windowName = sb.ToString();

                //获取窗口类
                GetClassName(hWnd, sb, sb.Capacity);
                wnd.className = sb.ToString();

                //添加到列表中
                wndList.Add(wnd);
                return true;
            }, 0);

            return wndList.ToArray();
        }
    }

}
View Code

 

这里有讨论如何通过shell32设置选择文件夹,但是我的程序是关掉所有相同的,再打开一个新的.就没有实现了

https://stackoverflow.com/questions/8182494/how-can-i-set-an-existing-explorer-exe-instance-to-select-a-file
//然后置前窗口 Win32api.SetForegroundWindow(item.HWND); 

你可能感兴趣的:(cad.net 利用win32api实现不重复打开dwg路径的文件夹(资源管理器))