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);

        /// <summary>
        /// 发送键盘消息
        /// </summary>
        /// <param name="proc"></param>
        /// <param name="key"></param>
        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

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

baobao熊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值