c# 自訂控制項

來源:互聯網
上載者:User

標籤:預設   添加引用   orm   繪製   draw   顏色   pre   property   ted   

 

  在自訂控制項的過程中,可以優先考慮從現有的控制項中進行派生,並添加所需要的功能。(製作效果,當滑鼠指標移動到控制項時,控制項顏色就會發生改變,當滑鼠離開時就恢複原來的顏色)

  首先添加一個新的類。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ComponentModel;using System.Drawing;using System.Windows.Forms;using System.Windows.Forms.Design;namespace Contol顏色{    //[DefaultProperty("HoverColor")]    //[Designer(typeof(ColourControl))]    public class ColourControl : Control    {        #region         //重寫該屬性以設定控制項預設大小        protected override System.Drawing.Size DefaultSize        {            get            {                return new Size(100, 100);            }        }        //當滑鼠指標移動到控制項地區內時的顏色        public Color HoverColor        {            get { return M_hoverColor; }            set            {                M_hoverColor = value;                Invalidate();//強制重新繪製            }        }        #endregion        #region        //該變數標識滑鼠指標是否已經進入控制項的地區        private bool isMouseEnter = false;        //當滑鼠進入控制項地區後的背景顏色        Color M_hoverColor;        #endregion        #region 方法        protected override void OnMouseEnter(EventArgs e)        {            //標識滑鼠指正已經進入地區            isMouseEnter = true;            //強制重新繪製            Invalidate();            base.OnMouseEnter(e);        }        protected override void OnMouseLeave(EventArgs e)        {            //標識滑鼠指標是否已經離開控制項            isMouseEnter = false;            //強制重新繪製            Invalidate();            base.OnMouseLeave(e);        }        protected override void OnPaint(PaintEventArgs e)        {            //用於填充控制項背景地區的畫刷            SolidBrush brush = new SolidBrush(BackColor);            if (isMouseEnter)            {                brush.Color = HoverColor;            }            e.Graphics.FillRectangle(brush, e.ClipRectangle);            //釋放畫刷資源            brush.Dispose();        }        #endregion    }}

   在方案總管視窗中設定:引用---->添加引用,勾選

  點擊重建項目。找到

 

c# 自訂控制項

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.