智能可變大小的控件:一個控件製作的全過程(C#)

第一步:
確定這個控件需要用來做什麼的,我們想對這個控件進行什麼樣的操作,這個控件需要什麼屬性;
第二步:
明確我們要做的控件之後就要開始進行實質性的技術攻關了,就是你要定義好各種要做的方法,明確這些方法是否是技術可及的;
第三步:
方法實現階段,將原先已經解決的技術關鍵點和應處理的各個小範圍處理應用到整個工程;
以下我做的一個控件的代碼:
///
///此控件達到的功能是可以隨意控制控件的大小,所在容器的位置,並且此控件實現了
///在鼠標放上去的時候能夠自動變大(需要改變大小可以自己再添加屬性,本控件默認長寬增長30個像素),並且會產生標籤顯示控件的信息(可控)
///在鼠標離開後會自動變回原來默認的大小,並有畫字畫圖功能(這部分是根據傳入有限集合進行智能繪畫功能,出入集合自行解決,本控件將其省略)
///
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace AnalysisSystem.Object
{
    public partial class DeviceControl : UserControl
    {
        #region 定義各種屬性   

        private bool _showToolTip;//默認false
        private bool _InControl;//鼠標是否放在控件上的標誌位

        //設置窗口的大小
        private Size _SetSize;

        public Size SetSize
        {
            get { return _SetSize; }
            set { _SetSize = value; }
        }
        //將此控件放入容器的大小
        private Point _ContainerSize;

        public Point ContainerSize
        {
            get { return _ContainerSize; }
            set { _ContainerSize = value; }
        }
       
        //設置控件在容器中的位置
        private Point _showLocation;

        public Point ShowLocation
        {
            get { return _showLocation; }
            set { _showLocation = value; }
        }

        //傳入控制屬性類(有限集合,省略)
...........................................

        #endregion

        public DeviceControl()
        {
            InitializeComponent();
            //透明設置(在本控件中由於使用了g.clear()方法而失效,可用畫布疊加解決,暫未實現)
            SetStyle(ControlStyles.SupportsTransparentBackColor
                | ControlStyles.UserPaint
                | ControlStyles.AllPaintingInWmPaint
                | ControlStyles.Opaque, true);
            BackColor = Color.Transparent;
           

        }

        #region 定義一些執行函數中需要用到的方法

        //生成信息標籤
        private ToolTip Get_Tooltip(string Title)
        {
            ToolTip Node_Box = new ToolTip();
            Node_Box.AutoPopDelay = 5000;
            Node_Box.InitialDelay = 100;
            Node_Box.ReshowDelay = 300;
            Node_Box.ShowAlways = true;
            Node_Box.IsBalloon = true;
            Node_Box.OwnerDraw = true;
            Node_Box.ToolTipIcon = ToolTipIcon.Info;
            Node_Box.ToolTipTitle = Title;
            Node_Box.UseFading = true;
            return Node_Box;
        }
        //產生三角形的各個點
        private PointF[] GetTrianglePointF()
        {
            PointF point1 = new PointF(ClientSize.Width / 2, 0.0F);
            PointF point2 = new PointF(0.0F, ClientSize.Height);
            PointF point3 = new PointF(ClientSize.Width, ClientSize.Height);
            PointF[] curvePoints =
             {
                 point1,
                 point2,
                 point3,
             };
            return curvePoints;
        }
        //產生多邊形的各個點
        private PointF[] GetPolygonPointF()
        {
            PointF point1 = new PointF(ClientSize.Width / 2, 0.0F);
            PointF point2 = new PointF(0.0F, ClientSize.Height / 3);
            PointF point3 = new PointF(ClientSize.Width / 4, ClientSize.Height);
            PointF point4 = new PointF(ClientSize.Width / 5 + ClientSize.Width / 5 + ClientSize.Width / 5 + ClientSize.Width / 5, ClientSize.Height);
            PointF point5 = new PointF(ClientSize.Width, ClientSize.Height / 3);
            PointF[] curvePoints =
             {
                 point1,
                 point2,
                 point3,
                 point4,
                 point5
             };
            return curvePoints;
        }
        /// <summary>
        /// 畫字
        /// </summary>
        /// <param name="g">圖對象</param>
        /// <param name="Word">要畫的字符串</param>
        /// <returns>畫好的圖對象</returns>

        private Graphics DrawWord(Graphics g, string Word)
        {
            // Create font and brush.
            Font drawFont = new Font("Arial", 8);
            SolidBrush drawBrush = new SolidBrush(Color.Black);

            // Create point for upper-left corner of drawing.
            float x = 0.0F;
            float y = ClientSize.Height / 2;

            // Draw string to screen.
            g.DrawString(Word, drawFont, drawBrush, x, y);//往控件裏面寫字
            return g;
        }
        #endregion


        #region 控件使用到的一些消息響應方法

        private void DeviceControl_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen mypen = new Pen(Color.Black, 2);

            g.Clear(Color.White);//將背景清除並且填充白色
            //g.DrawImage(this.BackgroundImage, 0, 0, this.Width, this.Height);

            //依照不同的屬性類(集合)畫不同的圖案
...................................................................................


            // 圖像變大超出容器範圍則必須對其位置進行調整
            if (this._ContainerSize != null)//如果有設置此控件放入的容器大小
            {
                if ((base.Left + this.ClientSize.Width) > this._ContainerSize.X)//超出右邊邊界
                {
                    base.Left = this._ContainerSize.X - this.ClientSize.Width - 10;
                }
                else
                {
                    if (base.Left != _showLocation.X && !this._InControl)
                        base.Left = _showLocation.X;
                }
                if ((base.Top + this.ClientSize.Height) > this._ContainerSize.Y)//超出下邊邊界
                {
                    base.Top = this._ContainerSize.Y - this.ClientSize.Height - 10;
                }
                else
                {
                    if (base.Top != _showLocation.Y && !this._InControl)
                        base.Top = _showLocation.Y;
                }
            }
        }
        //鼠標放到控件上面時要做的動作
        private void DeviceControl_MouseHover(object sender, EventArgs e)
        {
            if (!_showToolTip)
            {
                ToolTip t = this.Get_Tooltip(...);
                 string info="";//標籤要顯示的內容
                t.SetToolTip(this, info);//this即是綁定到本身
                this._showToolTip = true;
            }
            this.Invalidate();//清除已畫的畫布上的圖形
            this.Size = new System.Drawing.Size(_SetSize.Width + 30, _SetSize.Height + 30);//重繪控件的大小,默認是擴大30個像素
            this._InControl = true;


        }      
        //鼠標移開時要做的動作
        private void DeviceControl_MouseLeave(object sender, EventArgs e)
        {
            this.Invalidate();//清除已畫的畫布上的圖形
            this.Size = new System.Drawing.Size(_SetSize.Width , _SetSize.Height );//重繪控件的大小
            this._InControl = false;
        }
        #endregion
    }
}

發佈了29 篇原創文章 · 獲贊 3 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章