AutoCAD 通过COM调用Preference设置背景颜色

AutoCAD中访问系统设定Preference,需要调用COM API,添加 Autodesk.AutoCAD.Interop的引用:

 

直接上代码: 

 

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Interop;

using System.Drawing;
 

 

        [CommandMethod("GetBkClr")]

        public void GetBkGndColor()

        {

            // Access the Preferences object

            AcadPreferences acPrefComObj = (AcadPreferences)Application.Preferences;

    

            uint clr = acPrefComObj.Display.GraphicsWinLayoutBackgrndColor ;

            ed.WriteMessage(UIntToColor(clr).ToString());

        }

 

        [CommandMethod("SetBKClr", CommandFlags.Session)]

        public void SetBkGroundColor()

        {

            // Access the Preferences object

            AcadPreferences acPrefComObj = (AcadPreferences)Application.Preferences;

 

            // Use the CursorSize property to set the size of the crosshairs

            acPrefComObj.Display.CursorSize = 100;

 

            System.Drawing.Color bkClr = Color.FromArgb(0, 255, 255, 255);//white, do not use known color, use Argb instead.

 

            //acPrefComObj.Display.GraphicsWinLayoutBackgrndColor = ColorToUInt(bkClr);

            acPrefComObj.Display.GraphicsWinModelBackgrndColor = ColorToUInt(bkClr);

 

        }

 

        private uint ColorToUInt(Color color)

        {

            return (uint)((color.A << 24) | (color.R << 16) |

                          (color.G << 8) | (color.B << 0));

        }

 

        private System.Drawing.Color UIntToColor(uint color)

        {

            byte a = (byte)(color >> 24);

            byte r = (byte)(color >> 16);

            byte g = (byte)(color >> 8);

            byte b = (byte)(color >> 0);

            return Color.FromArgb(a, r, g, b);

        }

 

 

其中,转换函数来自 http://www.daniweb.com/software-development/csharp/code/217202

 

你可能感兴趣的:(Preference)