双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

自定义控件的事件

kxyzjm62 kxyzjm62 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
最近在学习自定义控件,参考网上的文章和代码写了一个控件,希望在达到定时时间是引发一个事件,现在的问题是:到时间后,事件不能引发。代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

//
using System.Timers;

//

namespace WindowsFormsControlLibrary1

{
[ToolboxBitmap(typeof(System.Windows.Forms .Timer))]

public partial class Clocks : UserControl
{
public event EventHandler Clocks_TimerEvent;//定义事件



private DateTime Time;
private Color colFColor;
private Color colBColor;
public Pen HPn;
public Pen MPn;
public Pen SPn;

public int HW;
public int MW;
public int SW;

public Boolean BL;
public String DT;
public DateTime XTS;

public Image FImageFil;

public Color ClockFColor;
//public Color ClockBColor;
public Clocks()
{
InitializeComponent();
this.HourColor = Color.Red;//给属性赋初值
this.MinoutColor =Color .Blue ;
this.SecondColor = Color.Black;
this.HW =1;
BL = false;
DT ="1900-02-21 12:23:33";
;

}
//添加属性
//时针颜色
[DefaultValue("Black"), Description("分针颜色"), Category("Appearance")] //Behavior在行为分类,Appearance在字体分类WindowStyle、Layout、Format、Action 百度查找CategoryAttribute 类
public Color MinoutColor
{
get
{
return colBColor;
}
set
{
colBColor = value;
}
}


//分针颜色
[DefaultValue("Black"), Description("时针颜色"), Category("Appearance")]
public Color HourColor
{
get
{
return colFColor;
}
set
{
colFColor = value;
}
}
//
[DefaultValue("Black"), Description("秒针颜色"), Category("Appearance")]
//DefaultValue的作用:不是提供设计时的默认值,他的作用是 当你在IDE的属性栏里 输入或选择的值 与 你DefaultValue相同时,这个值不变黑【加粗显示】
public Color SecondColor
{
get
{
return ClockFColor;
}
set
{
ClockFColor = value;
}
}

//时针宽度属性
[DefaultValue("1"), Description("时针宽度"), Category("Appearance")]
public int HourWide
{
get
{
return HW;
}
set
{
if (value > 1 && value < 16) //限定时针宽度的取值范围
HW = value;
else
HW = 1;
}
}

//
//添加图像属性
[DefaultValue(" "), Description("指定图像文件"), Category("Appearance")]
public Image ImageFile
{
get
{
return FImageFil;
}
set
{
FImageFil = value;
}

}

//添加文件属性
//添加一个是否启动定时的属性
[DefaultValue("False"), Description("设置是否启动定时"), Category("Appearance")]
public bool TimerE
{
get
{
return BL;
}
set
{
BL = value;
}

}
//
//添加一个事件的属性
[DefaultValue("12:00:00"), Description("设置定时时间"), Category("Appearance")]
public String DTimer
{
get
{
return DT;
}
set
{
DT = value;
}
}

//
private void timer1_Tick(object sender, EventArgs e)
{
this.Time = DateTime.Now;
this.Refresh(); //刷新界面,是表针走动
//定时事件
if (TimerE)
{
XTS = System.DateTime.Now;


if (XTS == Convert.ToDateTime(DTimer))
{
Clocks_TimerEvent (this, e);


}

}



}

private void UserClock_Paint(object sender, PaintEventArgs e)
{
Graphics dc = e.Graphics;
Pen pn = new Pen(ForeColor);
this.HPn = new Pen(HourColor,HW );
this.MPn = new Pen(MinoutColor);
this.SPn = new Pen(SecondColor);

SolidBrush br = new SolidBrush(ForeColor);
InitCoordinates(dc); //设置回话坐标变换
DrawDots(dc, br); //画点
DrawHourHand(dc, pn); //画时针
DrawMinuteHand(dc, pn);//画分针
DrawSecondHand(dc, pn); //画秒针

}
public void InitCoordinates(Graphics dc)
{
if (this.Width == 0 || this.Height == 0) return; //如果空间的宽高有一个为零,就返回
dc.TranslateTransform(this.Width / 2, this.Height / 2);//将控件的坐标原点设置为控件中心
dc.ScaleTransform(this.Width / 250F, this.Height / 250F);//调整比例大小

}
public void DrawDots(Graphics dc, Brush brush)
{
int iSize=5;
for (int i = 0; i <= 59; i++)
{
if (i % 5 == 0)
iSize = 15;
else
iSize = 5;
dc.FillEllipse(brush, -iSize / 2, -100 - iSize / 2, iSize, iSize);//画圆点
dc.RotateTransform(6);//旋转6度
}

}
protected virtual void DrawHourHand(Graphics grfx,Pen pn)
{
GraphicsState gs=grfx.Save();
grfx.RotateTransform(360.0F * Time.Hour / 12 + 30.0F * Time.Minute / 60);
grfx.DrawLine(this.HPn , 0, 0, 0, -50);
grfx .Restore(gs);
}
protected virtual void DrawMinuteHand(Graphics grfx, Pen pn)
{
GraphicsState gs = grfx.Save();
grfx.RotateTransform(360.0F * Time.Minute / 60 + 6.0F * Time.Second / 60);
grfx.DrawLine(this.MPn , 0, 0, 0, -70);
grfx.Restore(gs);
}
protected virtual void DrawSecondHand(Graphics grfx, Pen pn)
{
GraphicsState gs = grfx.Save();
grfx.RotateTransform(360.0F * Time.Second / 60 );
grfx.DrawLine(SPn , 0, 0, 0, -100);
grfx.Restore(gs);
}


}
}
...全文

相关推荐

自定义控件添加事件

Winfrom中自定义控件-- 添加事件示例,自定义控件 为两个普通按钮组成,其他窗体添加自定义控件后,分别触发自定义控件中的事件...

奔跑吧sunwugang

自定义控件事件添加指南

在开发应用程序时,往往需要特定的用户界面元素来实现独特的交互功能。创建自定义控件是达成这一目标的有效途径。本章将带您了解自定义控件创建的基本步骤,包括从设计到实现的整个流程。在开发自定义控件之前,重要的是理解用户的需求。您需要明确控件将要执行的功能,以及它应如何与用户交互。在设计阶段,可以绘制草图,规划控件的外观和行为,这将帮助您在编码阶段更高效地实现需求。继承是面向对象编程(OOP)的基本特性之一,它允许开发者创建新类(派生类或子类)继承自现有的类(基类或超类)。

高天艳阳

C# 自定义控件的属性和添加事件

C# 自定义控件的属性 添加自定义控件的事件

break001

C#创建自定义控件及添加自定义属性和事件使用实例详解

主要给大家介绍了关于C#创建自定义控件及添加自定义属性和事件使用的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用C#具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧

weixin_38640117

c# 自定义用户控件例(含自定义方法和事件)

自己编的c#自定义控件的示例,含有自定义事件,可以导入到工具栏

sixiangzhe2
评论 11
收藏 已收藏
分享
返回顶部
kxyzjm62
C# 111,129 成员 642,540 内容