- 积分
- 2765
- 明经币
- 个
- 注册时间
- 2015-12-1
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
本帖最后由 wyb36870 于 2023-4-12 15:09 编辑
直接贴上代码,不懂的可以问我
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.GraphicsInterface;
- using Copper.AutocadApi.Properties;
- using System.Drawing;
- namespace Copper.AutocadApi.ExtendedMethod;
- public static class Utils
- {
- private static ImageBGRA32 _FormatBrushImage = null;
- public static void CreateFormatBrushCursor()
- {
- if (_FormatBrushImage==null)
- {
- using var bmp =Resources.FormatBrush;//把透明背景的图片放到资源里面直接调用
- for (var i = 0; i < bmp.Width; i++)
- {
- for (var j = 0; j < bmp.Height; j++)
- {
- var pixel = bmp.GetPixel(i, j);
- 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)
- {
- bmp.SetPixel(i, j, Color.Magenta);
- }
- }
- }
- _FormatBrushImage = Autodesk.AutoCAD.Internal.Utils.ConvertBitmapToAcGiImageBGRA32(bmp);
- }
- new CursorBadgeUtilities().AddSupplementalCursorImage(_FormatBrushImage, 1);
- }
- public static void DeleteFormatBrushCursor()
- {
- var cbu = new CursorBadgeUtilities();
- if (cbu.HasSupplementalCursorImage() && _FormatBrushImage!=null)
- {
- cbu.RemoveSupplementalCursorImage(_FormatBrushImage);
- }
- }
- }
这里有个关键点,要注意图的背景色更改为Color.Magenta,如下
- bmp.SetPixel(i, j, Color.Magenta);
复制代码 调用时把方法放在你需要更改鼠标样式的位置,如下
- [CopperCmd("0DF476F9-87DF-46B8-917A-FD3A49F5CF9F", CommandFlags.Redraw)]
- public class CopyPropertiesCmd : AutocadCommand
- {
- protected override void Execute()
- {
- ......
- if (!this.TryCreateFilterForSelectBlockByName(new List<string> { bref.GetBlockName() }, out var filter)) return;
- var sourseFilter = new SelectionFilter(filter);
- var sourseOption = new PromptSelectionOptions
- {
- ......
- };
- Utils.CreateFormatBrushCursor();//此处设置图标
- var objSel = Ed.GetSelection(sourseOption, sourseFilter);
- Utils.DeleteFormatBrushCursor();//此处取消
- if (objSel.Status != PromptStatus.OK) return;
- var targetIds = objSel.Value.GetObjectIds();
- foreach (var id in targetIds)
- {
- ... ...
- }
- }
- }
复制代码
本做法参考了keanw的博客地址如下:
https://www.keanw.com/2014/05/ad ... 2015-using-net.html
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.GraphicsInterface;
- using Autodesk.AutoCAD.Internal;
- using Autodesk.AutoCAD.Runtime;
- using System.Drawing;
- namespace CursorBadges
- {
- public class Commands
- {
- private ImageBGRA32 _img = null;
- [CommandMethod("ACB")]
- public void AddCursorBadge()
- {
- if (_img == null)
- {
- using (var bmp = new Bitmap(85, 85))
- {
- using (var g = Graphics.FromImage(bmp))
- {
- // The transparent colour used for these badges is
- // Magenta, I've found. Let's use that as a background
- // and then draw three concentric, filled circles
- // (looking a bit like the RAF logo)
- g.Clear(Color.Magenta);
- g.FillEllipse(Brushes.Blue, 5, 5, 60, 60);
- g.FillEllipse(Brushes.White, 15, 15, 40, 40);
- g.FillEllipse(Brushes.Red, 25, 25, 20, 20);
- }
- _img = Utils.ConvertBitmapToAcGiImageBGRA32(bmp);
- }
- }
- // Adding the badge with a priority of 1 means that it
- // will get replaced by selection-related badges. Changing
- // this to 3 is enough to give it precedence over these
- // (a higher number still will increase the chances
- // of it staying shown)
- var cbu = new CursorBadgeUtilities();
- cbu.AddSupplementalCursorImage(_img, 1);
- }
- [CommandMethod("RCB")]
- public void RemoveCursorBadge()
- {
- var cbu = new CursorBadgeUtilities();
- if (cbu.HasSupplementalCursorImage() && _img != null)
- cbu.RemoveSupplementalCursorImage(_img);
- }
- }
- }
|
|