自定义组件 UITypeEditor

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
namespace MyLib
{
    // This UITypeEditor can be associated with Int32, Double and Single
    // properties to provide a design-mode angle selection interface.
    //[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
    public class AngleEditor : System.Drawing.Design.UITypeEditor
    {
        public AngleEditor()
        {
        }
        // Indicates whether the UITypeEditor provides a form-based (modal) dialog,
        // drop down dialog, or no UI outside of the properties window.
        public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }
        // Displays the UI for value selection.
        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value)
        {
            // Return the value if the value is not of type Int32, Double and Single.
            if (value.GetType() != typeof(double) && value.GetType() != typeof(float) && value.GetType() != typeof(int))
                return value;
            // Uses the IWindowsFormsEditorService to display a
            // drop-down UI in the Properties window.
            IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
            if (edSvc != null)
            {
                // Display an angle selection control and retrieve the value.
                AngleControl angleControl = new AngleControl((double)value);
                edSvc.DropDownControl(angleControl);
                // Return the value in the appropraite data format.
                if (value.GetType() == typeof(double))
                    return angleControl.angle;
                else if (value.GetType() == typeof(float))
                    return (float)angleControl.angle;
                else if (value.GetType() == typeof(int))
                    return (int)angleControl.angle;
            }
            return value;
        }
        // Draws a representation of the property's value.
        public override void PaintValue(System.Drawing.Design.PaintValueEventArgs e)
        {
            int normalX = (e.Bounds.Width / 2);
            int normalY = (e.Bounds.Height / 2);
            // Fill background and ellipse and center point.
            e.Graphics.FillRectangle(new SolidBrush(Color.DarkBlue), e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height);
            e.Graphics.FillEllipse(new SolidBrush(Color.White), e.Bounds.X + 1, e.Bounds.Y + 1, e.Bounds.Width - 3, e.Bounds.Height - 3);
            e.Graphics.FillEllipse(new SolidBrush(Color.SlateGray), normalX + e.Bounds.X - 1, normalY + e.Bounds.Y - 1, 3, 3);
            // Draw line along the current angle.
            double radians = ((double)e.Value * Math.PI) / (double)180;
            e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Red), 1), normalX + e.Bounds.X, normalY + e.Bounds.Y,
                e.Bounds.X + (normalX + (int)((double)normalX * Math.Cos(radians))),
                e.Bounds.Y + (normalY + (int)((double)normalY * Math.Sin(radians))));
        }
        // Indicates whether the UITypeEditor supports painting a
        // representation of a property's value.
        public override bool GetPaintValueSupported(System.ComponentModel.ITypeDescriptorContext context)
        {
            return true;
        }
    }
    // Provides a user interface for adjusting an angle value.
    internal class AngleControl : System.Windows.Forms.UserControl
    {
        // Stores the angle.
        public double angle;
        // Stores the rotation offset.
        private int rotation = 0;
        // Control state tracking variables.
        private int dbx = -10;
        private int dby = -10;
        private int overButton = -1;
        public AngleControl(double initial_angle)
        {
            this.angle = initial_angle;
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        }
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            // Set angle origin point at center of control.
            int originX = (this.Width / 2);
            int originY = (this.Height / 2);
            // Fill background and ellipse and center point.
            e.Graphics.FillRectangle(new SolidBrush(Color.DarkBlue), 0, 0, this.Width, this.Height);
            e.Graphics.FillEllipse(new SolidBrush(Color.White), 1, 1, this.Width - 3, this.Height - 3);
            e.Graphics.FillEllipse(new SolidBrush(Color.SlateGray), originX - 1, originY - 1, 3, 3);
            // Draw angle markers.
            int startangle = (270 - rotation) % 360;
            e.Graphics.DrawString(startangle.ToString(), new Font("Arial", 8), new SolidBrush(Color.DarkGray), (this.Width / 2) - 10, 10);
            startangle = (startangle + 90) % 360;
            e.Graphics.DrawString(startangle.ToString(), new Font("Arial", 8), new SolidBrush(Color.DarkGray), this.Width - 18, (this.Height / 2) - 6);
            startangle = (startangle + 90) % 360;
            e.Graphics.DrawString(startangle.ToString(), new Font("Arial", 8), new SolidBrush(Color.DarkGray), (this.Width / 2) - 6, this.Height - 18);
            startangle = (startangle + 90) % 360;
            e.Graphics.DrawString(startangle.ToString(), new Font("Arial", 8), new SolidBrush(Color.DarkGray), 10, (this.Height / 2) - 6);
            // Draw line along the current angle.
            double radians = ((((angle + rotation) + 360) % 360) * Math.PI) / (double)180;
            e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Red), 1), originX, originY,
                originX + (int)((double)originX * (double)Math.Cos(radians)),
                originY + (int)((double)originY * (double)Math.Sin(radians)));
            // Output angle information.
            e.Graphics.FillRectangle(new SolidBrush(Color.Gray), this.Width - 84, 3, 82, 13);
            e.Graphics.DrawString("Angle: " + angle.ToString("F4"), new Font("Arial", 8), new SolidBrush(Color.Yellow), this.Width - 84, 2);
            // Draw square at mouse position of last angle adjustment.
            e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black), 1), dbx - 2, dby - 2, 4, 4);
            // Draw rotation adjustment buttons.
            if (overButton == 1)
            {
                e.Graphics.FillRectangle(new SolidBrush(Color.Green), this.Width - 28, this.Height - 14, 12, 12);
                e.Graphics.FillRectangle(new SolidBrush(Color.Gray), 2, this.Height - 13, 110, 12);
                e.Graphics.DrawString("Rotate 90 degrees left", new Font("Arial", 8), new SolidBrush(Color.White), 2, this.Height - 14);
            }
            else
                e.Graphics.FillRectangle(new SolidBrush(Color.DarkGreen), this.Width - 28, this.Height - 14, 12, 12);
            if (overButton == 2)
            {
                e.Graphics.FillRectangle(new SolidBrush(Color.Green), this.Width - 14, this.Height - 14, 12, 12);
                e.Graphics.FillRectangle(new SolidBrush(Color.Gray), 2, this.Height - 13, 116, 12);
                e.Graphics.DrawString("Rotate 90 degrees right", new Font("Arial", 8), new SolidBrush(Color.White), 2, this.Height - 14);
            }
            else
                e.Graphics.FillRectangle(new SolidBrush(Color.DarkGreen), this.Width - 14, this.Height - 14, 12, 12);
            e.Graphics.DrawEllipse(new Pen(new SolidBrush(Color.White), 1), this.Width - 11, this.Height - 11, 6, 6);
            e.Graphics.DrawEllipse(new Pen(new SolidBrush(Color.White), 1), this.Width - 25, this.Height - 11, 6, 6);
            if (overButton == 1)
                e.Graphics.FillRectangle(new SolidBrush(Color.Green), this.Width - 25, this.Height - 6, 4, 4);
            else
                e.Graphics.FillRectangle(new SolidBrush(Color.DarkGreen), this.Width - 25, this.Height - 6, 4, 4);
            if (overButton == 2)
                e.Graphics.FillRectangle(new SolidBrush(Color.Green), this.Width - 8, this.Height - 6, 4, 4);
            else
                e.Graphics.FillRectangle(new SolidBrush(Color.DarkGreen), this.Width - 8, this.Height - 6, 4, 4);
            e.Graphics.FillPolygon(new SolidBrush(Color.White), new Point[] { new Point(this.Width - 7, this.Height - 8), new Point(this.Width - 3, this.Height - 8), new Point(this.Width - 5, this.Height - 4) });
            e.Graphics.FillPolygon(new SolidBrush(Color.White), new Point[] { new Point(this.Width - 26, this.Height - 8), new Point(this.Width - 21, this.Height - 8), new Point(this.Width - 25, this.Height - 4) });
        }
        protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
        {
            // Handle rotation adjustment button clicks.
            if (e.X >= this.Width - 28 && e.X <= this.Width - 2 && e.Y >= this.Height - 14 && e.Y <= this.Height - 2)
            {
                if (e.X <= this.Width - 16)
                    rotation -= 90;
                else if (e.X >= this.Width - 14)
                    rotation += 90;
                if (rotation < 0)
                    rotation += 360;
                rotation = rotation % 360;
                dbx = -10;
                dby = -10;
            }
            else
                UpdateAngle(e.X, e.Y);
            this.Refresh();
        }
        protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                UpdateAngle(e.X, e.Y);
                overButton = -1;
            }
            else if (e.X >= this.Width - 28 && e.X <= this.Width - 16 && e.Y >= this.Height - 14 && e.Y <= this.Height - 2)
                overButton = 1;
            else if (e.X >= this.Width - 14 && e.X <= this.Width - 2 && e.Y >= this.Height - 14 && e.Y <= this.Height - 2)
                overButton = 2;
            else
                overButton = -1;
            this.Refresh();
        }
        private void UpdateAngle(int mx, int my)
        {
            // Store mouse coordinates.
            dbx = mx;
            dby = my;
            // Translate y coordinate input to GetAngle function to correct for ellipsoid distortion.
            double widthToHeightRatio = (double)this.Width / (double)this.Height;
            int tmy;
            if (my == 0)
                tmy = my;
            else if (my < this.Height / 2)
                tmy = (this.Height / 2) - (int)(((this.Height / 2) - my) * widthToHeightRatio);
            else
                tmy = (this.Height / 2) + (int)((double)(my - (this.Height / 2)) * widthToHeightRatio);
            // Retrieve updated angle based on rise over run.
            angle = (GetAngle(this.Width / 2, this.Height / 2, mx, tmy) - rotation) % 360;
        }
        private double GetAngle(int x1, int y1, int x2, int y2)
        {
            double degrees;
            // Avoid divide by zero run values.
            if (x2 - x1 == 0)
            {
                if (y2 > y1)
                    degrees = 90;
                else
                    degrees = 270;
            }
            else
            {
                // Calculate angle from offset.
                double riseoverrun = (double)(y2 - y1) / (double)(x2 - x1);
                double radians = Math.Atan(riseoverrun);
                degrees = radians * ((double)180 / Math.PI);
                // Handle quadrant specific transformations.
                if ((x2 - x1) < 0 || (y2 - y1) < 0)
                    degrees += 180;
                if ((x2 - x1) > 0 && (y2 - y1) < 0)
                    degrees -= 180;
                if (degrees < 0)
                    degrees += 360;
            }
            return degrees;
        }
    }
    public class AngleEditorTestControl : System.Windows.Forms.UserControl
    {
        private double int_angle;
        [BrowsableAttribute(true)]
        [Editor(typeof(AngleEditor), typeof(System.Drawing.Design.UITypeEditor))]
        public double Angle
        {
            get
            { return int_angle; }
            set
            { int_angle = value; }
        }
        public AngleEditorTestControl()
        {
            int_angle = 90;
            this.Size = new Size(190, 42);
            this.BackColor = Color.Beige;
        }
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            if (this.DesignMode)
            {
                e.Graphics.DrawString("Use the Properties Window to access", new Font("Arial", 8), new SolidBrush(Color.Black), 3, 2);
                e.Graphics.DrawString("the AngleEditor UITypeEditor by", new Font("Arial", 8), new SolidBrush(Color.Black), 3, 14);
                e.Graphics.DrawString("configuring the \"Angle\" property.", new Font("Arial", 8), new SolidBrush(Color.Black), 3, 26);
            }
            else
                e.Graphics.DrawString("This example requires design mode.", new Font("Arial", 8), new SolidBrush(Color.Black), 3, 2);
        }
    }
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索new
, color
, this
, graphics
, width
, altium designer14
height
sapui5自定义组件、c uitypeeditor、uitypeeditor、vue ui组件库、react ui组件库,以便于您获取更多的相关知识。

时间: 2024-12-09 06:11:19

自定义组件 UITypeEditor的相关文章

自定义组件之属性(Property)的性质(Attribute)介绍(一)

自定义组件之 属性(Property)的性质(Attribute)介绍 属性(property)作为c#语言中一个重要的组成部分,尤其是在我们自己编写组件的时候显得更加重要.我相信大家一定对其有一定的了解.但是大家是否注意到了一个非常关键得细节问题呢?那就是在大家使用任何得组件的时候都需要通过属性浏览器给每一属性赋值,而且更加友好的是对于每种不同类型属性都会自己的形式.比如:数字类型.字符串类型是默认简单的输入的形式,而如Font.Color类型的属性则可以对话框或下拉列表框的形式.不知道大家是

自定义组件及其内组件大小的正确设置

http://blog.csdn.net/xiaodao1986/article/details/8481288 一.自定义组件大小的设置 不管你怎么设置,自定义组件似乎总是match_parent.我们需要覆盖onMeasure方法,并在其内正确设置组件大小. [java] view plaincopy @Override   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {       //注意:为

动态改变自定义组件!

动态 1.在叶面上放上一个容器PlaceHolder 2.可以动态在容器中加载所需的自定义组件,或用户组件 示例: 现在容器中加载一个TextBox,之后回传再换为Button. 代码如下: protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1; protected System.Web.UI.WebControls.Button Button1; private void Page_Load(object sender, Sy

Flex自定义组件外观

Flex 是现今最受欢迎的 RIA 开发技术之一,它凭借其优秀的用户体验获得 许多用户的青睐,因此也吸引了众多的程序员投入 Flex 学习的洪流之中.Flex 之所以如此受欢迎,一大部分原因是因为 Flex 的界面效果非常出众.Flex 提 供了自定义组件外观的众多方法,使得 Flex 程序界面丰富多彩.本文通过简单 实用的程序例子(包括冒泡信息提示框.图像圆角.动态按钮.网格背景),使 用位图.矢量图.SWF 文件自定义皮肤,使用 Mask 制作遮掩效果,讲解了其中 几种自定义组件外观方法的优

PHP开发框架Yii Framework教程(10) UI组件 自定义组件

在介绍Yii内置UI组件之前,先介绍一下如何自定义组件,这样也有助于理解CWidget的用法,自定义组件就是重载 CWidget 的init() 和 run() 方法. class MyWidget extends CWidget { public function init() { // 此方法会被 CController::beginWidget() 调用 } public function run() { // 此方法会被 CController::endWidget() 调用 } } 本

NetBeans 6.0可视Mobile设计器自定义组件:PIM浏览器

可视 Mobile 设计器(VMD) 是 NetBeans Mobility 包中的图形界面,允许您使用拖放组件来设计移动应用程序.VMD 允许使用 Mobility 包提供的组件或您自己设计的组件来定义应用程序流程和设计 GUI.VMD 包含许多标准 User Interface (UI) 组件,可用于创建 Lists.Alerts.Forms 和 Images 等应用程序.它还包括一些自定义组件,可用于简化一些比较复杂的功能的创建,比如等待屏幕.启动屏幕.表格项目等. PIM 浏览器是自定义

Flex自定义组件开发之日周月日期选择日历控件

原文:Flex自定义组件开发之日周月日期选择日历控件           使用过DateField的我们都知道,DateField 控件是用于显示日期的文本字段,字段右侧带有日历图标.当用户在控件边框内的任一位置单击时,将弹出一个 DateChooser 控件,显示当月的所有日期.如果未选择日期,则该文本字段为空白,并且 DateChooser 控件中将显示当前日期的月份.当 DateChooser 控件处于打开状态时,用户可以在各个月份和年份之间滚动,并选择某个日期.选择日期后,DateCho

布局-android 将自定义组件添加到xml后,程序崩溃

问题描述 android 将自定义组件添加到xml后,程序崩溃 activity_main中的代码是这样的 xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id

android-将surfaceview自定义组件利用(Context context,AttributeSet attrs)还想传递其他参数

问题描述 将surfaceview自定义组件利用(Context context,AttributeSet attrs)还想传递其他参数 android中利用自定义组件,我想将一个surfaceview作为组件,写到layout中,surfaceview利用(Context context,AttributeSet attrs)构造函数之后,但是,我想将调用它的activity当做参数传进来,我使用了puzzleActivity=(PuzzleActivity)context转化,发现不行,那我