c#自定義控件中事件的處理
來源:易賢網(wǎng) 閱讀:2276 次 日期:2014-08-29 10:07:30
溫馨提示:易賢網(wǎng)小編為您整理了“c#自定義控件中事件的處理”,方便廣大網(wǎng)友查閱!

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.事件觸發(fā),這樣少了事件注冊,我們在其他窗體中引用控件時只需要注冊事件和編輯事件處理程序即可,可以對比上一篇博客

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();

}

//新聞被點擊 事件觸發(fā)方法

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>

/// 新聞標(biāo)題

/// </summary>

private string _NewsTitle = "";

[Description("新聞標(biāo)題"), Category("Appearance")]

public string NewsTitle

{

get { return _NewsTitle; }

set

{

_NewsTitle = value;

this.Invalidate();

}

}

private Color _MouseOnColor = new Color();

[Description("鼠標(biāo)劃上的樣色"), 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;

}

}

}

更多信息請查看IT技術(shù)專欄

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機網(wǎng)站地址:c#自定義控件中事件的處理
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報警專用圖標(biāo)