用户控件之椭圆Button

 public partial class EllipseButton : UserControl
    {
        public EllipseButton()
        {
            InitializeComponent();
        }

        private string caption = "";
        public string Caption
        {
            set { caption = value; }
            get { return caption; }
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);

            Graphics g = this.CreateGraphics();
            LinearGradientBrush lBrush = new LinearGradientBrush(
               ClientRectangle,
               Color.White,
               Color.Blue, LinearGradientMode.Vertical);
            g.FillEllipse(lBrush, 0, 0, 100, 60);


            if (this.Caption != null)
            {
                // 绘制文本
                using (StringFormat f = new StringFormat())
                {
                    // 水平居中对齐
                    f.Alignment = System.Drawing.StringAlignment.Center;
                    // 垂直居中对齐
                    f.LineAlignment = System.Drawing.StringAlignment.Center;
                    // 设置为单行文本
                    //f.FormatFlags = System.Drawing.StringFormatFlags.NoWrap;
                    // 绘制文本
                    using (SolidBrush b = new SolidBrush(this.ForeColor))
                    {
                          g.DrawString(
                              this.Caption,
                              this.Font,
                              b,
                              new System.Drawing.RectangleF(
                                  0,
                                  0,
                                  this.ClientSize.Width,
                                  this.ClientSize.Height),
                              f);
                    }
                }
            }

        }

        protected override void OnMouseMove(MouseEventArgs e)
        {

            // Make the cursor the Hand cursor when the mouse moves
            Cursor = Cursors.Hand;

            // Call MyBase.OnMouseMove to activate the delegate.
            base.OnMouseMove(e);
        }


        /// <summary>
        /// 处理鼠标单击事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);
        }

    }

posted on 2009-06-23 17:53  heart-in-sky  阅读(181)  评论(0编辑  收藏  举报