個性控制項的製作

來源:互聯網
上載者:User
    這是一個個性張揚的年代,我也耍點個性做幾個個性的控制項玩玩,今天沒事情幹,做了幾個,就是把控制項換換衣服,變變臉。先讓大家看看我的個性控制項(以Button為例,其他的控制項類似)。
       
       就做這幾個樣子的吧,不論什麼形狀都可以,還可以把按鈕做出字元型,等等。本來想做個五角星的,計算座標太麻煩了,就算了吧。如果那位有興趣可以實現它。
     下面我把上面做這些個性控制項的主要內容給介紹一下。
      控制項重寫實際上就是繼承控制項類,重載他的函數,所謂的控制項,就是可以看的見的組件,組件是控制項的基類,以textbox為例,控制項類的家族關係是這樣的:
System.Object
      System.MarshalByRefObject
         System.ComponentModel.Component
            System.Windows.Forms.Control
               System.Windows.Forms.TextBoxBase
                  System.Windows.Forms.TextBox
                        System.Windows.Forms.DataGridTextBox
                        System.Windows.Forms.DataGridViewTextBoxEditingControl
我們發現TextBox還有兩個子類。
我們在控制項開發的時候,如我開發textbox,我可以以TextBox為基類,也可以以TextBoxBase,甚至以Control為基類。
C/S架構的控制項的其時間很豐富,不必要我們重新寫什麼,我們可以重寫paint函數實現控制項的變臉。
建立一個工程,在工程裡面添加一個類,名為mybutton,代碼為:using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.Drawing.Drawing2D;

namespace CheckboxRodio
{
    class MyButton:Button
    {
        public MyButton()
        {
            this.Paint += new PaintEventHandler(MyButton_Paint);
           // base.Paint+=new PaintEventHandler(MyButton_Paint);
            //this.MouseMove += new MouseEventHandler(MyButton_MouseMove);
            //this.MouseClick += new MouseEventHandler(MyButton_MouseClick);
        }
        void MyButton_Paint(object sender, PaintEventArgs e)
        {
            //建立路徑對象
            GraphicsPath MyPath = new GraphicsPath();
            Pen p = new Pen(Color.Red, 5);
            SolidBrush sb = new SolidBrush(Color.Gold);
            int w=this.Width;
            int h=this.Height;

            //五角星
            //Point p1=new Point(w/2,3);
            //Point p2 = new Point(w/3, h-4);
            //Point p3=new Point(w-4,h/3);
            //Point p4=new Point(3,h/3);
            //Point p5=new Point(2*w/3,h-4);
            //MyPath.AddPolygon(ps);

            //四菱形
            Point p1 = new Point(w / 2, 3);
            Point p2 = new Point(3, h / 2);
            Point p3 = new Point(w / 2, h - 6);
            Point p4 = new Point(w - 6, h / 2);
            Point[] ps ={ p1, p2, p3,p4,p1 };
            MyPath.AddPolygon(ps);

            //三角形
            //Point p1 = new Point((w-9)/ 2, 3);
            //Point p2 = new Point(3, h-6);
            //Point p3 = new Point(w-6, h - 6);
            //Point[] ps ={ p1, p2, p3, p1 };
            //MyPath.AddPolygon(ps);

            //橢圓
             //MyPath.AddEllipse(5, 5, w-10,h-10);
            //MyPath.AddLines(ps);
           
            this.Region = new Region(MyPath);
        
            e.Graphics.FillEllipse(sb, 20, 20, 20, 20);//.DrawEllipse();
        }
    }
}

        然後編譯一下,看看能不能成功,若個成功,開啟form表單類,這時候顯示工具列,開啟工具列,可以看見mybutton控制項就在你的工具列裡喲!要使用它,那是太容易了,拖到表單上就OK啦。由於我沒有重寫控制項的屬性,所以,屬性框裡面沒有變化。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.