2004.7.15 [C#学习记录]使用C#编写一个自定义控件(高
2008-04-24 08:43
866 查看
using System;using System.Collections;using System.ComponentModel;using System.Drawing;using System.Data;using System.Windows.Forms;namespace WindowsControlLibrary1{/// /// UserControl1 的摘要说明。/// public class UserControl1 : System.Windows.Forms.UserControl{/// /// 必需的设计器变量。/// private System.ComponentModel.Container components = null;/// 私有域private string MyText;//存储关联的事件处理方法;private EventHandler OnMyTextChanged;public UserControl1(){// 该调用是 Windows.Forms 窗体设计器所必需的。InitializeComponent();// TODO: 在 InitializeComponent 调用后添加任何初始化MyText="Static Control";OnMyTextChanged=null;}/// /// 清理所有正在使用的资源。/// protected override void Dispose( bool disposing ){if( disposing ){if( components != null )components.Dispose();}base.Dispose( disposing );}#region Component Designer generated code/// /// 设计器支持所需的方法 - 不要使用代码编辑器 /// 修改此方法的内容。/// private void InitializeComponent(){// // UserControl1// this.Name = "UserControl1";this.Size = new System.Drawing.Size(248, 128);this.Load += new System.EventHandler(this.UserControl1_Load);this.Paint += new System.Windows.Forms.PaintEventHandler(this.UserControl1_Paint);}#endregionprivate void UserControl1_Paint(object sender, System.Windows.Forms.PaintEventArgs e){SizeF textSize=e.Graphics.MeasureString(MyText,Font);float xPos=(ClientRectangle.Width/2)-(textSize.Width/2);float yPos=(ClientRectangle.Height/2)-(textSize.Height/2);e.Graphics.DrawString(MyText,Font,new SolidBrush(Color.Black),xPos,yPos);}private void UserControl1_Load(object sender, System.EventArgs e){}//公开属性public String ControlText{get{return MyText;}set{MyText=value;//重画Invalidate();//如果关联了事件处理方法,则调用该方法if(OnMyTextChanged!=null){OnMyTextChanged.Invoke(this,EventArgs.Empty);}}}//声明了一个公开事件public event EventHandler MyTextChanged{add{OnMyTextChanged+=value;}remove{OnMyTextChanged=value;}}}}
相关文章推荐
- 2004.7.15 [C#学习记录]使用C#编写一个自定义控件
- 菜鸟C#学习记录:简单使用一个ListView控件
- CSharpGL(34)以从零编写一个KleinBottle渲染器为例学习如何使用CSharpGL
- 使用C#编写一个计时器-.NET教程,C#语言
- PHP7 学习笔记(七)如何使用zephir编译一个扩展记录
- 使用C#编写的一个定时关机程序
- 关于使用C#编写一个简单的日志类
- 学习API HOOK,编写了一个winsock 的封包抓取程序,可免费使用;
- 使用C#编写的一个定时关机程序
- C#使用Log4Net记录日志(想直接转载的,但是不支持转到这上面来,复制来的,作者勿怪,学习用)
- 使用C#编写的一个定时关机程序
- Delphi编写自定义控件以及接口的使用(做了一个TpgDbEdit)
- C#编译器优化那点事 c# 如果一个对象的值为null,那么它调用扩展方法时为甚么不报错 webAPI 控制器(Controller)太多怎么办? .NET MVC项目设置包含Areas中的页面为默认启动页 (五)Net Core使用静态文件 学习ASP.NET Core Razor 编程系列八——并发处理
- 【嵌入式系统学习记录】练习:编写一个函数,求字符数组的前n个字符中最大字符的地址并返回该地址,字符数组和n作为函数的参数;
- 使用C#编写一个用于客户端打印的ActiveX控件(1)
- 使用C#编写的一个定时关机程序
- c#如何使用socket发送一条记录再接收一条记录,服务器端及客户端如何编写,初学。。最好有代码,谢谢!
- 使用CLRMD编写一个自己的C#调试器
- 编写一个C#Windows服务程序,定时从数据库中拿出记录发送邮件。
- c#学习笔记之九 事件(event)的使用,一个简单的例子揭示其设计理念