明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 673|回复: 4

关于CAD自定义鼠标样式

  [复制链接]
发表于 2023-4-12 15:05 | 显示全部楼层 |阅读模式
本帖最后由 wyb36870 于 2023-4-12 15:09 编辑

直接贴上代码,不懂的可以问我

  1. using Autodesk.AutoCAD.EditorInput;
  2. using Autodesk.AutoCAD.GraphicsInterface;
  3. using Copper.AutocadApi.Properties;
  4. using System.Drawing;

  5. namespace Copper.AutocadApi.ExtendedMethod;

  6. public static class Utils
  7. {
  8.     private static ImageBGRA32 _FormatBrushImage = null;
  9.     public static void CreateFormatBrushCursor()
  10.     {
  11.         if (_FormatBrushImage==null)
  12.         {
  13.             using var bmp =Resources.FormatBrush;//把透明背景的图片放到资源里面直接调用
  14.             for (var i = 0; i < bmp.Width; i++)
  15.             {
  16.                 for (var j = 0; j < bmp.Height; j++)
  17.                 {
  18.                     var pixel = bmp.GetPixel(i, j);
  19.                     if ((pixel.A == 0 && pixel.R == 0 && pixel.G == 0 && pixel.B == 0)|| pixel.A == 0 && pixel.R == 255 && pixel.G == 255 && pixel.B == 255)
  20.                     {
  21.                         bmp.SetPixel(i, j, Color.Magenta);
  22.                     }
  23.                 }
  24.             }
  25.             _FormatBrushImage = Autodesk.AutoCAD.Internal.Utils.ConvertBitmapToAcGiImageBGRA32(bmp);
  26.         }
  27.         new CursorBadgeUtilities().AddSupplementalCursorImage(_FormatBrushImage, 1);
  28.     }

  29.     public static void DeleteFormatBrushCursor()
  30.     {
  31.         var cbu = new CursorBadgeUtilities();

  32.         if (cbu.HasSupplementalCursorImage() && _FormatBrushImage!=null)
  33.         {
  34.             cbu.RemoveSupplementalCursorImage(_FormatBrushImage);
  35.         }
  36.     }
  37. }
这里有个关键点,要注意图的背景色更改为Color.Magenta,如下
  1. bmp.SetPixel(i, j, Color.Magenta);
复制代码
调用时把方法放在你需要更改鼠标样式的位置,如下
  1. [CopperCmd("0DF476F9-87DF-46B8-917A-FD3A49F5CF9F", CommandFlags.Redraw)]
  2. public class CopyPropertiesCmd : AutocadCommand
  3. {
  4.     protected override void Execute()
  5.     {
  6.          ......
  7.         if (!this.TryCreateFilterForSelectBlockByName(new List<string> { bref.GetBlockName() }, out var filter)) return;
  8.         var sourseFilter = new SelectionFilter(filter);
  9.         var sourseOption = new PromptSelectionOptions
  10.         {
  11.               ......
  12.         };
  13.         Utils.CreateFormatBrushCursor();//此处设置图标
  14.         var objSel = Ed.GetSelection(sourseOption, sourseFilter);
  15.         Utils.DeleteFormatBrushCursor();//此处取消
  16.         if (objSel.Status != PromptStatus.OK) return;
  17.         var targetIds = objSel.Value.GetObjectIds();
  18.         foreach (var id in targetIds)
  19.         {
  20.             ... ...
  21.         }
  22.     }
  23. }
复制代码


本做法参考了keanw的博客地址如下:
https://www.keanw.com/2014/05/ad ... 2015-using-net.html

  1. using Autodesk.AutoCAD.EditorInput;
  2. using Autodesk.AutoCAD.GraphicsInterface;
  3. using Autodesk.AutoCAD.Internal;
  4. using Autodesk.AutoCAD.Runtime;
  5. using System.Drawing;

  6. namespace CursorBadges
  7. {
  8.   public class Commands
  9.   {
  10.     private ImageBGRA32 _img = null;
  11.     [CommandMethod("ACB")]
  12.     public void AddCursorBadge()
  13.     {
  14.       if (_img == null)
  15.       {
  16.         using (var bmp = new Bitmap(85, 85))
  17.         {
  18.           using (var g = Graphics.FromImage(bmp))
  19.           {
  20.             // The transparent colour used for these badges is
  21.             // Magenta, I've found. Let's use that as a background
  22.             // and then draw three concentric, filled circles
  23.             // (looking a bit like the RAF logo)
  24.             g.Clear(Color.Magenta);
  25.             g.FillEllipse(Brushes.Blue, 5, 5, 60, 60);
  26.             g.FillEllipse(Brushes.White, 15, 15, 40, 40);
  27.             g.FillEllipse(Brushes.Red, 25, 25, 20, 20);
  28.           }
  29.           _img = Utils.ConvertBitmapToAcGiImageBGRA32(bmp);
  30.         }
  31.       }

  32.       // Adding the badge with a priority of 1 means that it
  33.       // will get replaced by selection-related badges. Changing
  34.       // this to 3 is enough to give it precedence over these
  35.       // (a higher number still will increase the chances
  36.       // of it staying shown)
  37.       var cbu = new CursorBadgeUtilities();
  38.       cbu.AddSupplementalCursorImage(_img, 1);
  39.     }

  40.     [CommandMethod("RCB")]
  41.     public void RemoveCursorBadge()
  42.     {
  43.       var cbu = new CursorBadgeUtilities();
  44.       if (cbu.HasSupplementalCursorImage() && _img != null)
  45.         cbu.RemoveSupplementalCursorImage(_img);
  46.     }
  47.   }

  48. }
发表于 2023-4-14 11:00 | 显示全部楼层
谢谢大佬分享!!!
发表于 2023-4-14 13:38 | 显示全部楼层

谢谢大佬分享!!!
发表于 2023-4-21 10:30 | 显示全部楼层
我喜欢VB,帮大家转个VB.Net 代码。
改成Bitmap.FromFile() 加载32位BMP


  1. Imports System.Drawing
  2. Imports Autodesk.AutoCAD.Runtime
  3. Imports Autodesk.AutoCAD.Internal
  4. Imports Autodesk.AutoCAD.GraphicsInterface
  5. Imports Autodesk.AutoCAD.EditorInput

  6. Namespace CursorBadges
  7.   Public Class Commands
  8.     Private _img As ImageBGRA32 = Nothing
  9.     <CommandMethod("ACB")> _
  10.     Public Sub AddCursorBadge()
  11.       If _img Is Nothing Then
  12.        'Using bmp As New Bitmap(85, 85)
  13.                 '    Using g As Object = Graphics.FromImage(bmp)
  14.                 '        ' The transparent colour used for these badges is
  15.                 '        ' Magenta, I've found. Let's use that as a background
  16.                 '        ' and then draw three concentric, filled circles
  17.                 '        ' (looking a bit like the RAF logo)
  18.                 '        g.Clear(Color.Magenta)
  19.                 '        g.FillEllipse(Brushes.Blue, 5, 5, 60, 60)
  20.                 '        g.FillEllipse(Brushes.White, 15, 15, 40, 40)
  21.                 '        g.FillEllipse(Brushes.Red, 25, 25, 20, 20)
  22.                 '    End Using
  23.                 '    _img = Utils.ConvertBitmapToAcGiImageBGRA32(bmp)
  24.                 'End Using
  25.                 Dim bmp As Bitmap = Bitmap.FromFile(My.Application.Info.DirectoryPath & "\images\CAD_Cursor.bmp")
  26.                 _img = Utils.ConvertBitmapToAcGiImageBGRA32(bmp)
  27.       End If

  28.       ' Adding the badge with a priority of 1 means that it
  29.       ' will get replaced by selection-related badges. Changing
  30.       ' this to 3 is enough to give it precedence over these
  31.       ' (a higher number still will increase the chances
  32.       ' of it staying shown)
  33.       Dim cbu As New CursorBadgeUtilities()
  34.       cbu.AddSupplementalCursorImage(_img, 1)
  35.     End Sub

  36.     <CommandMethod("RCB")> _
  37.     Public Sub RemoveCursorBadge()
  38.       Dim cbu As New CursorBadgeUtilities()
  39.       If cbu.HasSupplementalCursorImage() AndAlso _img IsNot Nothing Then
  40.         cbu.RemoveSupplementalCursorImage(_img)
  41.       End If
  42.     End Sub
  43.   End Class

  44. End Namespace
  1. Namespace Copper.AutocadApi.ExtendedMethod
  2.     Public NotInheritable Class Utils
  3.         Private Sub New()
  4.         End Sub
  5.         Private Shared _FormatBrushImage As ImageBGRA32 = Nothing
  6.         Public Shared Sub CreateFormatBrushCursor()
  7.             If _FormatBrushImage Is Nothing Then
  8.                 'Using bmp As Bitmap = Bitmap.FromFile(My.Application.Info.DirectoryPath & "\images\CAD_Cursor.bmp")  
  9.                 Using bmp As Bitmap = New Bitmap(CType(My.Resources.ResourceManager.GetObject("CAD_Cursor"), Image))
  10.                     '把透明背景的图片放到资源里面直接调用
  11.                     Dim i As Object = 0
  12.                     While i < bmp.Width
  13.                         Dim j As Object = 0
  14.                         While j < bmp.Height
  15.                             Dim pixel As Object = bmp.GetPixel(i, j)
  16.                             If (pixel.A = 0 AndAlso pixel.R = 0 AndAlso pixel.G = 0 AndAlso pixel.B = 0) OrElse pixel.A = 0 AndAlso pixel.R = 255 AndAlso pixel.G = 255 AndAlso pixel.B = 255 Then
  17.                                 bmp.SetPixel(i, j, Color.Magenta)
  18.                             End If
  19.                             j += 1
  20.                         End While
  21.                         i += 1
  22.                     End While
  23.                     _FormatBrushImage = Autodesk.AutoCAD.Internal.Utils.ConvertBitmapToAcGiImageBGRA32(bmp)
  24.                 End Using
  25.             End If
  26.             Dim cbu As New CursorBadgeUtilities()
  27.             cbu.AddSupplementalCursorImage(_FormatBrushImage, 1)
  28.         End Sub

  29.         Public Shared Sub DeleteFormatBrushCursor()
  30.             Dim cbu As Object = New CursorBadgeUtilities()
  31.             If cbu.HasSupplementalCursorImage() AndAlso _FormatBrushImage IsNot Nothing Then
  32.                 cbu.RemoveSupplementalCursorImage(_FormatBrushImage)
  33.             End If
  34.         End Sub
  35.     End Class
  36.     Public Class TcCursor
  37.         <CommandMethod("TcCursor", CommandFlags.Redraw)>
  38.         Sub TcCursor()
  39.             Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
  40.             Utils.CreateFormatBrushCursor()
  41.             '此处设置图标
  42.             Dim objSel As Object = acDoc.Editor.GetSelection()
  43.             Utils.DeleteFormatBrushCursor()
  44.             '此处取消
  45.             If objSel.Status <> PromptStatus.OK Then
  46.                 Return
  47.             End If
  48.             Dim targetIds As Object = objSel.Value.GetObjectIds()
  49.             For Each id As Object In targetIds
  50.                 MsgBox(id.ToString)
  51.             Next
  52.         End Sub
  53.     End Class
  54. End Namespace



您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-5-3 03:25 , Processed in 0.216414 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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