明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1048|回复: 0

Modifying AutoCAD tooltips using .NET

[复制链接]
发表于 2011-7-12 10:26 | 显示全部楼层 |阅读模式

作者:kean

原文地址:http://through-the-interface.typepad.com/through_the_interface/2011/07/modifying-autocad-tooltips-using-net.html

 

To continue on the theme established in the last post, today we’re going to go ahead and modify AutoCAD tooltips, prefixing them with a fixed string. This is the next step on the path to modifying them in a more meaningful way (to translate them into a different language, for instance).

Here’s some C# code that implements a command to prefix all AutoCAD tooltips with a silly string (“Kean says this command …“):

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;

using System.Collections.Generic;

using System.Windows.Controls;

using System.Windows;

 

namespace PrefixTooltips

{

  public class Commands

  {

    List<string> _handled = null;

 

    [CommandMethod("PREFTT")]

    public void PrefixTooltips()

    {

      Document doc =

        Autodesk.AutoCAD.ApplicationServices.Application.

          DocumentManager.MdiActiveDocument;

      Editor ed = doc.Editor;

 

      _handled = new List<string>();

      const string prefix = "Kean says this command ";

 

      Autodesk.Windows.ComponentManager.ToolTipOpened +=

        (s, e) =>

        {

          Autodesk.Internal.Windows.ToolTip tt =

            s as Autodesk.Internal.Windows.ToolTip;

          if (tt != null)

          {

            Autodesk.Windows.RibbonToolTip rtt =

              tt.Content as Autodesk.Windows.RibbonToolTip;

            if (rtt == null)

            {

              // Basic tooltip

 

              if (tt.Content is string)

              {

                string contents = (string)tt.Content;

 

                if (!contents.StartsWith(prefix))

                {

                  tt.Content = prefix + contents;

                }

              }

            }

            else

            {

              // Enhanced tooltips

 

              if (rtt.Content is string)

              {

                string contents = (string)rtt.Content;

 

                if (!_handled.Contains(rtt.Command))

                {

                  rtt.Content = prefix + contents;

                  _handled.Add(rtt.Command);

 

                  if (rtt.ExpandedContent != null)

                  {

                    TextBlock tb = rtt.ExpandedContent as TextBlock;

                    if (tb != null)

                    {

                      tb.Text = prefix + tb.Text;

                    }

                    else

                    {

                      StackPanel sp =

                        rtt.ExpandedContent as StackPanel;

                      if (sp != null)

                      {

                        foreach (UIElement elem in sp.Children)

                        {

                          tb = elem as TextBlock;

                          if (tb != null)

                          {

                            tb.Text = prefix + tb.Text;

                          }

                        }

                      }

                    }

                  }

                }

              }

            }

          }

        };

    }

  }

}

 

When we run the PREFTT command and hover over ribbon, toolbar or statusbar icons, we see our tooltips – and even their extended content – gets prefixed with our string:

We keep track of tooltips that have already been translated by adding their command-names to a list: we need some tracking mechanism to avoid modifying tooltips multiple times, but this isn’t really optimal (something a bit more intelligent would be better, as – for one thing – this mechanism doesn’t differentiate between toolbars and ribbonbars, which may have different tooltips). All the same, it works reasonably well to prove the concept.

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
"觉得好,就打赏"
还没有人打赏,支持一下

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-3-29 06:49 , Processed in 0.270950 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表