subclass Button class and override OnPaint method

解决方案 »

  1.   

    感谢您使用微软产品。在VC#中,您可以通过继承System.Windows.Forms.Button,然后定制其Paint事件句柄,如this.Paint += new System.Windows.Forms.PaintEventHandler(this.mybutton_Paint);。在mybutton_Paint(object sender, System.Windows.Forms.PaintEventArgs e)方法中,可以实现自画控件。
    下面提供一段示例程序,供您参考:
    using System;
    using System.Drawing;namespace CustomButton
    {
    /// <summary>
    /// Summary description for myButton.
    /// </summary>
    public class MyButton : System.Windows.Forms.Button 
      { 
      public MyButton() 
      { 
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.mybutton_Paint); 
      } 
      private void mybutton_Paint(object sender, System.Windows.Forms.PaintEventArgs e) 
      { 
      //custom drawing 
      Pen pen = new Pen(Color.Red); 
      pen.Width = 8; 
      e.Graphics.DrawLine(pen, 7, 4, 7, this.Height - 4); 
      pen.Width = 1; 
    pen.Color = Color.Blue;
    e.Graphics.DrawEllipse(pen, this.Width-16,4,8,this.Height-8);
    Icon myIcon = new Icon("c:\\homeworks\\myIcon.ico");
    e.Graphics.DrawIcon(myIcon,this.Width - 50 ,1);
      } 
      } 
    }
    — 微软全球技术中心 VB支持中心本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
    为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。