【问题标题】:C# AutoCAD | How to properly set the Icon on MenuItem in ContextMenuExtensionC# AutoCAD |如何在 ContextMenuExtension 中正确设置 MenuItem 上的图标
【发布时间】:2022-09-30 01:41:26
【问题描述】:

我试图在 ContextMenu 扩展中为 MenuItem 设置一个图标,但该图标设置有缺陷:

原始图标(16x16):

设置图标的代码:

polyline_item1.Icon = ACADConstructionPit.Properties.Resources.exchangeProfile;

如何在 ContextMenuExtension 中正确设置 MenuItem 上的图标?

    标签: c# autocad autocad-plugin


    【解决方案1】:

    使用模板路径从文件中获取图像的代码 (%appdata%/Autodesk/ApplicationPlugins/POW_UPS.bundle/UI/_Images/):

    /// <summary>
        /// Get Icon for AutoCAD ContextMenuExtension: MenuItem.Icon 
        /// </summary>
        /// <param name="inIconFileName"></param>
        /// <returns></returns>
        public static System.Drawing.Icon GetIconForAppFromPathFile(string inIconFileName, string inFolderPath = null)
        {
            System.Drawing.Icon _ImageSourceForApp = null;
            try
            {
                if (string.IsNullOrEmpty(inIconFileName)) { return _ImageSourceForApp; }
    
                if (inFolderPath == null)
                {
                    // path to image folder: %appdata%/Autodesk/ApplicationPlugins/POW_EPS.bundle/UI/_Images/
                    var _PathToFolderIcons = $"{Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Autodesk", "ApplicationPlugins", "POW_EPS.bundle", "UI", "_Images")}";
                    if (string.IsNullOrEmpty(_PathToFolderIcons)) { return _ImageSourceForApp; }
                    inFolderPath = _PathToFolderIcons;
                }
    
                IEnumerable<string> _AllPathIcons = Directory.GetFiles(inFolderPath, "*.png");
                string _PathIconFile = _AllPathIcons.FirstOrDefault(i => i.Contains(inIconFileName));
                Bitmap _BitmapFromFile = new Bitmap(_PathIconFile);
                Bitmap _ResizedBitmap = new Bitmap(_BitmapFromFile, new Size(16, 16));
                _ImageSourceForApp = Icon.FromHandle(_ResizedBitmap.GetHicon());
    
            }
            catch { }
            return _ImageSourceForApp;
        }
    

    图标安装示例:

    menu_item.Icon = GetIconForAppFromPathFile("exchangeProfile.png");
    

    但是,不幸的是,并非所有图标都可以正确安装:

    原始图像(32x32):

    【讨论】:

      猜你喜欢
      • 2015-11-04
      • 1970-01-01
      • 1970-01-01
      • 2012-06-27
      • 1970-01-01
      • 2016-01-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多