栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

Revit二次开发知识分享(十二)给Revit发送Esc按键

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Revit二次开发知识分享(十二)给Revit发送Esc按键

这里提供一个有时可行,有时不灵的办法,仅供参考学习。发现有改进的地方,可以联系博主一起讨论改进,上代码。

先整一个帮助的类里面包含键盘的消息

    public static class Helper
    {
        [DllImport("user32.dll")]
        private static extern IntPtr SetActiveWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        private static extern void keybd_event(Keys bVk, byte bScan, uint dwFlags, uint dwExtraInfo);

        /// 
        /// 发送键盘消息
        /// 
        /// 
        /// 
        public static void SendKeys(IntPtr proc, Keys key)
        {
            SetActiveWindow(proc);
            SetForegroundWindow(proc);
            keyevent(key, 0, 0, 0);
            keyevent(key, 0, 2, 0); 
            keyevent(key, 0, 0, 0);
            keyevent(key, 0, 2, 0);
        }

然后就是在主体程序中使用

 public class SendEsc : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;
            FamilySymbol fs = doc.GetElement(new ElementId(91406)) as FamilySymbol;
            commandData.Application.Application.DocumentChanged += AddElement;
            try
            {
                uidoc.PromptForFamilyInstancePlacement(fs);
            }
            catch 
            {

                
            }
            commandData.Application.Application.DocumentChanged -= AddElement;
            return Result.Succeeded;
        }

        private void AddElement(object sender, Autodesk.Revit.DB.Events.DocumentChangedEventArgs e)
        {
            Helper.SendKeys(Autodesk.Windows.ComponentManager.ApplicationWindow, System.Windows.Forms.Keys.Escape);
        }
    }

这里举得例子是用了上次的放置实例的例子,里面的documentchange事件是监控如果增加一个实例的时候,就自动传回两次Esc键,结束命令。今天的分享就到这里。
bug以修复
转载至:https://blog.csdn.net/Oneal5354/article/details/109406494

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/911198.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号