熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

c#自定義控件中事件的處理

2022-06-13   來源: .NET編程 

  using System;  

  using SystemCollectionsGeneric; using SystemComponentModel; using SystemData; using SystemDrawing; using SystemText; using SystemWindowsForms; namespace ClientControl { //定義委托 public delegate void NewsClickEventHandle(object senderNewsEventArg args); public partial class NewsStage : Control { //定義事件 public event NewsClickEventHandle NewsClicked; private Graphics g; private bool isMouseOn = false; public NewsStage() { InitializeComponent(); //事件觸發這樣少了事件注冊我們在其他窗體中引用控件時只需要注冊事件和編輯事件處理程序即可可以對比上一篇博客 thisClick += new EventHandler(NewsStage_Click); thisMouseMove += new MouseEventHandler(NewsStage_MouseMove); thisMouseLeave += new EventHandler(NewsStage_MouseLeave); } void NewsStage_MouseLeave(object sender EventArgs e) { isMouseOn = false; thisInvalidate(); } void NewsStage_MouseMove(object sender MouseEventArgs e) { isMouseOn = true; thisInvalidate(); } //新聞被點擊 事件觸發方法 void NewsStage_Click(object sender EventArgs e) { if (_NewsID>=&&_NewsTitle!="") { NewsEventArg myArgs = new NewsEventArg(_NewsID_NewsTitle); NewsClicked(this myArgs); } } private int _NewsID = ; [Description("新聞ID") Category("Appearance")] public int NewsID { get { return _NewsID; } set { _NewsID = value; thisInvalidate(); } } /// <summary> /// 新聞標題 /// </summary> private string _NewsTitle = ""; [Description("新聞標題") Category("Appearance")] public string NewsTitle { get { return _NewsTitle; } set { _NewsTitle = value; thisInvalidate(); } } private Color _MouseOnColor = new Color(); [Description("鼠標劃上的樣色") Category("Appearance")] public Color MouseOnColor { get { return _MouseOnColor; } set { _MouseOnColor = value; } } protected override void OnPaint(PaintEventArgs pe) { baseOnPaint(pe); g = thisCreateGraphics(); if (isMouseOn) { gDrawString(_NewsTitle thisFont new SolidBrush(this_MouseOnColor) new PointF( )); } else { gDrawString(_NewsTitle thisFont new SolidBrush(thisForeColor) new PointF( )); } } protected void Dispose() { gDispose(); } } public partial class NewsEventArg : EventArgs { public int NewsID = ; public string NewsTitle = ""; public NewsEventArg(int newsIDstring newsTitle){ NewsID = newsID; NewsTitle = newsTitle; } } }


From:http://tw.wingwit.com/Article/program/net/201311/14278.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.