AutoCAD.NET文档事件:DocumentActivationChanged

本文深入探讨了DocumentCollection的DocumentActivationChanged事件,揭示了其在文档窗口失活或销毁时触发的特性,以及在图元编辑操作中意外频繁触发的问题。通过测试代码展示了文档级别事件的触发顺序,提醒开发者在使用事件时需谨慎。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于DocumentCollection 的 DocumentActivationChanged 事件,查阅资料找到的解释是:活动文档窗口失活或销毁时触发。作为文档级别的事件,我理解的是要切换或者打开关闭文档才会触发该事件,所以在做层表事件响应的时候订阅了该事件,用以刷新图层列表。

后来发现:在进行一些图元编辑操作(比如移动多段线节点啥)的时候,鼠标会异常卡顿。最开始很纳闷,以为是订阅了 Object 相关事件造成的,专门控制变量做了好一阵测试,最后发现上述事件才是罪魁祸首,以下测试代码:

        [CommandMethod("TTT")]
        public void Test()
        {
            Application.DocumentManager.DocumentActivationChanged +=
                DocumentManager_DocumentActivationChanged;
        }
        private int count = 0;
        private void DocumentManager_DocumentActivationChanged(object sender, DocumentActivationChangedEventArgs e)
        {
            count++;
            Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(
                "DocumentActivationChanged,Count: " + count + Environment.NewLine);
        }

然后结果是这样:

在这里插入图片描述
我他喵的,随便拖一个节点触发几百次,不卡才怪了。所以这个事件并不仅仅是文档级别,应该是某些窗口刷新都会触发,慎用。
然后,顺便测试了下文档级别全部事件(13个)的触发顺序:

		public void Test()
        {
            Application.DocumentManager.DocumentCreated += DocumentManager_DocumentCreated;
            Application.DocumentManager.DocumentActivated += DocumentManager_DocumentActivated;
            Application.DocumentManager.DocumentBecameCurrent += DocumentManager_DocumentBecameCurrent;
            Application.DocumentManager.DocumentCreateStarted += DocumentManager_DocumentCreateStarted;
            Application.DocumentManager.DocumentCreationCanceled += DocumentManager_DocumentCreationCanceled;
            Application.DocumentManager.DocumentDestroyed += DocumentManager_DocumentDestroyed;
            Application.DocumentManager.DocumentLockModeChangeVetoed += DocumentManager_DocumentLockModeChangeVetoed;
            Application.DocumentManager.DocumentLockModeChanged += DocumentManager_DocumentLockModeChanged;
            Application.DocumentManager.DocumentLockModeWillChange += DocumentManager_DocumentLockModeWillChange;
            Application.DocumentManager.DocumentToBeActivated += DocumentManager_DocumentToBeActivated;
            Application.DocumentManager.DocumentToBeDeactivated += DocumentManager_DocumentToBeDeactivated;
            Application.DocumentManager.DocumentToBeDestroyed += DocumentManager_DocumentToBeDestroyed;
            Application.DocumentManager.DocumentActivationChanged +=DocumentManager_DocumentActivationChanged;
        }
        private int step = 0;
        private string fileName=@"d:test.txt";

        private void DocumentManager_DocumentToBeDestroyed(object sender, DocumentCollectionEventArgs e)
        {
            step++;
            HtFileHelper.AppendText("step " + step + " :DocumentToBeDestroyed", fileName);
        }
        //后面就不一一列出....

文档从打开到关闭,大致顺序如下:

顺序事件
step 1 :DocumentLockModeWillChange
step 2 :DocumentLockModeChanged
。。。。。。
step 9 :DocumentActivationChanged
。。。。。。
step 26 :DocumentCreateStarted
。。。。。。
step 29 :DocumentToBeDeactivated
。。。。。。
step 34 :DocumentActivationChanged
step 35 :DocumentCreated
step 36 :DocumentActivated
step 37 :DocumentBecameCurrent
。。。。。。
step 55 :DocumentActivationChanged
。。。。。。
step 62 :DocumentBecameCurrent
。。。。。。
step 65 :DocumentToBeDestroyed
step 66 :DocumentDestroyed

省略号是Step1+Step2组合的重复,意义不大,就删掉了,测试并不严谨,仅供参考。


最后,想起之前一个前辈的告诫:慎用事件,在你不确定的时候

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值