c#自訂控制項中事件的處理

來源:互聯網
上載者:User
參考http://blog.csdn.net/aofengdaxia/article/details/5890464
using System;  using System.Collections.Generic;  using System.ComponentModel;  using System.Data;  using System.Drawing;  using System.Text;  using System.Windows.Forms;    namespace ClientControl  {      //1.定義委託    public delegate void NewsClickEventHandle(object sender,NewsEventArg args);      public partial class NewsStage : Control      {          //2.定義事件        public event NewsClickEventHandle NewsClicked;          private Graphics g;          private bool isMouseOn = false;          public NewsStage()          {              InitializeComponent();             //3.事件觸發,這樣少了事件註冊,我們在其他表單中引用控制項時只需要註冊事件和編輯事件處理常式即可,可以對比上一篇部落格            this.Click += new EventHandler(NewsStage_Click);              this.MouseMove += new MouseEventHandler(NewsStage_MouseMove);              this.MouseLeave += new EventHandler(NewsStage_MouseLeave);          }            void NewsStage_MouseLeave(object sender, EventArgs e)          {              isMouseOn = false;              this.Invalidate();          }            void NewsStage_MouseMove(object sender, MouseEventArgs e)          {              isMouseOn = true;              this.Invalidate();          }            //新聞被點擊  事件觸發方法        void NewsStage_Click(object sender, EventArgs e)          {              if (_NewsID>=0&&_NewsTitle!="")              {                  NewsEventArg myArgs = new NewsEventArg(_NewsID,_NewsTitle);                  NewsClicked(this, myArgs);              }          }                private int _NewsID = 0;            [Description("新聞ID"), Category("Appearance")]          public int NewsID          {              get { return _NewsID; }              set              {                  _NewsID = value;                  this.Invalidate();              }          }            /// <summary>          /// 新聞標題          /// </summary>          private string _NewsTitle = "";            [Description("新聞標題"), Category("Appearance")]          public string NewsTitle          {              get { return _NewsTitle; }              set              {                  _NewsTitle = value;                  this.Invalidate();              }          }              private Color _MouseOnColor = new Color();          [Description("滑鼠划上的樣色"), Category("Appearance")]          public Color MouseOnColor          {              get { return _MouseOnColor; }              set              {                  _MouseOnColor = value;              }          }          protected override void OnPaint(PaintEventArgs pe)          {              base.OnPaint(pe);              g = this.CreateGraphics();              if (isMouseOn)              {                  g.DrawString(_NewsTitle, this.Font, new SolidBrush(this._MouseOnColor), new PointF(0, 0));              }              else              {                  g.DrawString(_NewsTitle, this.Font, new SolidBrush(this.ForeColor), new PointF(0, 0));              }                        }          protected void Dispose()          {              g.Dispose();          }                }      public partial class NewsEventArg : EventArgs      {          public int NewsID = 0;          public string NewsTitle = "";            public NewsEventArg(int newsID,string newsTitle){              NewsID = newsID;              NewsTitle = newsTitle;          }      }  } 

聯繫我們

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