cad.net 利用win32api实现不重复打开dwg路径的文件夹(资源管理器)
这里地址的方法也是可用的,但是net3.5不能使用
为此我选择使用win32api的方式来遍历当前桌面所有资源管理器
/// <summary>
/// 不重复打开dwg路径的资源管理器
/// </summary>
[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);
}
} /// <summary>
/// 遍历桌面资源管理器
/// </summary>
public class ShellWindows : IEnumerable
{
private readonly ArrayList list;
public IEnumerator GetEnumerator()
{
return (list as IEnumerable).GetEnumerator();
} /// <summary>
/// 获取桌面所有文件夹的路径
/// </summary>
/// <returns></returns>
public ShellWindows()
{
var allDesktopWindows = Win32api.GetAllDesktopWindows();
var lst = new List<InternetExplorer>();
foreach (var item in allDesktopWindows)
{
if (item.className == "CabinetWClass")
{
string a = item.windowName;
var fi = Win32api.FindWindowEx(item.hwnd, , "WorkerW", null);
if (fi != IntPtr.Zero)
{
fi = Win32api.FindWindowEx(fi, , "ReBarWindow32", null);
if (fi != IntPtr.Zero)
{
fi = Win32api.FindWindowEx(fi, , "Address Band Root", null);
if (fi != IntPtr.Zero)
{
fi = Win32api.FindWindowEx(fi, , "msctls_progress32", null);
if (fi != IntPtr.Zero)
{
fi = Win32api.FindWindowEx(fi, , "Breadcrumb Parent", null);
if (fi != IntPtr.Zero)
{
fi = Win32api.FindWindowEx(fi, , "ToolbarWindow32", null);//资源管理器
//知识:toolbar上的按钮没有handler,要用发送通知信息
if (fi != IntPtr.Zero)
{
StringBuilder sb = new StringBuilder();
//获取窗口名称-路径地址
Win32api.GetWindowText(fi, sb, sb.Capacity);
string path = sb.ToString();
path = path.Substring(, path.Length - );//4表示"地址: "长度 InternetExplorer ie = new InternetExplorer
{
HWND = item.hwnd,
LocationURL = path
};
lst.Add(ie);
}
}
}
}
} }
}
}
list = new ArrayList(lst);
}
}
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); /// <summary>
/// 置前窗口
/// </summary>
/// <param name="hwnd"></param>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool SetForegroundWindow(IntPtr hwnd); /// <summary>
/// 枚举窗口
/// </summary>
/// <param name="lpEnumFunc"></param>
/// <param name="lParam"></param>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, int lParam); /// <summary>
/// 获取窗口Text
/// </summary>
/// <param name="hwnd"></param>
/// <param name="lpString"></param>
/// <param name="nMaxCount"></param>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowText(IntPtr hwnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount); /// <summary>
/// 获取窗口类名
/// </summary>
/// <param name="hwnd"></param>
/// <param name="lpString"></param>
/// <param name="nMaxCount"></param>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetClassName(IntPtr hwnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount); /// <summary>
/// 窗口隐藏
/// </summary>
/// <param name="hwnd">窗口句柄</param>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool IsWindowVisible(IntPtr hwnd); /// <summary>
/// 查找子窗口
/// </summary>
/// <param name="hwnd"></param>
/// <param name="hwndChildAfter"></param>
/// <param name="lpszClass"></param>
/// <param name="lpszWindow"></param>
/// <returns></returns>
[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
/// <summary>
/// 发送消息
/// </summary>
/// <returns></returns>
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam); /// <summary>
/// 发送消息
/// </summary>
/// <returns></returns>
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, ref Rectangle lParam); /// <summary>
/// 关闭文件夹
/// </summary>
/// <param name="hwnd"></param>
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;
} /// <summary>
/// 枚举所有桌面窗口
/// </summary>
/// <returns></returns>
public static WindowInfo[] GetAllDesktopWindows()
{
//用来保存窗口对象 列表
var wndList = new List<WindowInfo>(); //枚举所有桌面窗口
EnumWindows(delegate (IntPtr hWnd, int lParam)
{
WindowInfo wnd = new WindowInfo();
StringBuilder sb = new StringBuilder(); //获得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;
}, ); return wndList.ToArray();
}
} }
这里有讨论如何通过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路径的文件夹(资源管理器)的更多相关文章
- [置顶] LOAD语句:利用MSSQL中的xp_cmdshell功能,将指定文件夹下的指定文件,生成mysql的LOAD语句
LOAD语句:利用MSSQL中的xp_cmdshell功能,将指定文件夹下的指定文件,生成mysql的LOAD语句 declare @sql varchar(4000), @dirpath varch ...
- (转)Intellij Idea工具栏添加打开选中文件的资源管理器位置
背景:在idea的view>toolbar上面添加工具按钮,能够简化操作,现在添加打开资源管理按钮,后续功能待研究 Intellij Idea工具栏添加打开选中文件的资源管理器位置 工具栏-右击 ...
- cad.net 利用win32api实现一个命令开关参照面板
首先我要判断是否已经打开了参照面板. 然而cad自己没有相关的系统变量.这时我就需要利用到win32api来判断程序是否打开了参照面板了. 首先学习的是 https://blog.csdn.net/b ...
- 打开"我的电脑"等特殊文件夹ShellExecute
procedure TForm2.btn10Click(Sender: TObject); begin ShellExecute(handle,'open','mailt', nil,nil,SW_S ...
- WPF 选择电脑文件显示路径,弹出资源管理器,打开文件
选择文件,将路径显示在名为txbx的textbox上 // 在WPF中, OpenFileDialog位于Microsoft.Win32名称空间 Microsoft.Win32.OpenFileDia ...
- [python IO学习篇]补充打开中文路径的文件
http://blog.csdn.net/mottolinux/article/details/525600621 关于Python编码的基本常识 在python里面 “明文”是unicode类型和s ...
- linux系统ubuntu中在命令行如何打开图形界面的文件夹
用linux查看文件列表之类的受到命令行限制,还是不太方便的.在文件夹中打开的话,切换路径又没有linux终端快,于是,需要在命令行窗口中打开文件夹.如何做呢? 来到终端命令行中,cd切换你的路径,使 ...
- 利用 pip 安装 Python 程序包到个人用户文件夹下
利用 --user 参数,即 pip install --user package_name 这样会将Python 程序包安装到 $HOME/.local 路径下,其中包含三个字文件夹:bin,lib ...
- C#利用浏览按钮获得文件路径和文件夹路径
生成文件夹路径 private void btnChoose_Click(object sender, EventArgs e) { using (OpenFileDialog ...
随机推荐
- SQL Server中多表连接时驱动顺序对性能的影响
本文出处:http://www.cnblogs.com/wy123/p/7106861.html (保留出处并非什么原创作品权利,本人拙作还远远达不到,仅仅是为了链接到原文,因为后续对可能存在的一些错 ...
- Spring再接触 IOC DI
直接上例子 引入spring以及Junite所需要的jar包 User.java package com.bjsxt.model; public class User { private String ...
- css选择器querySelector
* querySelector(css选择器)* 通过css选择器去获取一个元素* 它获取到的只有一个元素,如果说是有重复的,那它只取第一个** 主语* document 从整个文档里去获取元素* 父 ...
- html css col-md-offset
有的时候,我们不想让两个相邻的列挨在一起,这时候利用栅格系统的列偏移(offset)功能来实现,而不必再定义margin值.使用.col-md-offset-*形式的样式就可以将列偏移到右侧.例如,. ...
- office 安装
在 gaobo百度云下载安装包. 自定义安装,并在自定义界面选择安装路径. 破解:
- ajax请求导致status为canceled(无任何回调数据)的原因
1.故障现象 一个普通的ajax请求,请求能够到达controller,也能正常处理业务,但是ajax的回调函数为空,即没有任何状态和数据返回,使用谷歌浏览器查看请求状态如下图: 出现该错误:简单来说 ...
- Swagger注解
swagger注解说明 1.与模型相关的注解,用在bean上面 @ApiModel:用在bean上,对模型类做注释: @ApiModelProperty:用在属性上,对属性做注释 2.与接口相关的注 ...
- yii2 basic版本的一些配置
1.nginx配置 重写规则 修改访问模式为 http://wh.store/admin/index 文件位置: /home/wwwroot/default/yii2-app-basic/config ...
- FortiGate端口聚合配置
1.端口聚合(LACP)应用场景 该功能高端设备上支持,FortiGate60D.FortiGate90D和FortiGate240D等低端型号不支持. 1.在带宽比较紧张的情况下,通过逻辑聚合可以扩 ...
- Spring Boot+Quartz实现一个实时管理的定时任务
转载 https://www.cnblogs.com/wujiwen/p/9615120.html 项目实践过程中碰到一个动态管理定时任务的需求:针对每个人员进行信息的定时更新,具体更新时间可随时调整 ...