📄 alarmclockcontrol.cs
字号:
// Return component's timezone offset
return Convert.ToInt32(this.timeZoneModifiers[extendee]);
}
public void SetTimeZoneModifier(Control extendee, object value) {
// If property isn't provided
if( value == null ) {
// Remove it
this.timeZoneModifiers.Remove(extendee);
if( !this.DesignMode ) {
extendee.Click -= this.extendee_Click;
}
}
else {
// Add the offset as an integer
timeZoneModifiers[extendee] = Convert.ToInt32(value);
if( !this.DesignMode ) {
extendee.Click += this.extendee_Click;
}
}
}
[Category("Appearance")]
[Description("Determines the clock face type to display.")]
[DefaultValue(ClockFace.Both)]
[Editor(typeof(FaceEditor), typeof(UITypeEditor))]
public ClockFace Face {
get { return this.face; }
set {
this.face = value;
this.Invalidate();
}
}
[Category("Appearance")]
[Description("Sets the color and size of the Hour Hand.")]
public Hand HourHand {
get { return this.hourHand; }
set {
this.hourHand = value;
this.Invalidate();
}
}
private bool ShouldSerializeHourHand() {
// Only serialize non-default values
return ((this.hourHand.Color != Color.Black) || (this.hourHand.Width != 1));
}
private void ResetHourHand() {
this.hourHand = new Hand(Color.Black, 1);
this.Invalidate();
}
[Category("Appearance")]
[Description("Sets the color and size of the Minute Hand.")]
public Hand MinuteHand {
get { return this.minuteHand; }
set {
this.minuteHand = value;
this.Invalidate();
}
}
private bool ShouldSerializeMinuteHand() {
// Only serialize non-default values
return ((this.minuteHand.Color != Color.Black) || (this.minuteHand.Width != 1));
}
private void ResetMinuteHand() {
this.minuteHand = new Hand(Color.Black, 1);
this.Invalidate();
}
[Category("Appearance")]
[Description("Sets the color and size of the Second Hand.")]
public Hand SecondHand {
get { return this.secondHand; }
set {
this.secondHand = value;
this.Invalidate();
}
}
private bool ShouldSerializeSecondHand() {
// Only serialize non-default values
return ((this.secondHand.Color != Color.Red) || (this.secondHand.Width != 1));
}
private void ResetSecondHand() {
this.secondHand = new Hand(Color.Red, 1);
this.Invalidate();
}
[Category("Appearance")]
[Description("The digital time format, constructed from .NET format specifiers.")]
[DefaultValue("dd/MM/yyyy hh:mm:ss tt")]
[Editor(typeof(DigitalTimeFormatEditor), typeof(UITypeEditor))]
public string DigitalTimeFormat {
get { return this.digitalTimeFormat; }
set {
this.digitalTimeFormat = value;
this.Invalidate();
}
}
[Category("Notification")]
[Description("Fired when the Alarm property is changed.")]
public event AlarmChangedEventHandler AlarmChanged;
[Category("Notification")]
[Description("Fired when the Alarm goes off.")]
public event AlarmSoundedEventHandler AlarmSounded;
[Category("Notification")]
[Description("Fired when a message to self needs to be read.")]
public event MessageNotificationEventHandler MessageNotification;
public void DelayPrimaryAlarm(double minutes) {
if( this.primaryAlarmSet ) {
this.primaryAlarm = this.primaryAlarm.AddMinutes(minutes);
}
}
public void DelaySecondaryAlarm(double minutes) {
if( this.backupAlarmSet ) {
this.backupAlarm = this.backupAlarm.AddMinutes(minutes);
}
}
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
// Get specified date/time if control in design-time,
// or current date/time if control is in run-time
DateTime now;
if( this.DesignMode ) {
// Get pretty date/time for design-time
now = new DateTime(2002, 12, 31, 15, 00, 20, 0);
}
else {
// Get current date/time and apply the time zone modifier
now = DateTime.Now.AddHours(timeZoneModifier);
}
// Calculate required dimensions
//Size faceSize = this.ClientRectangle.Size;
Size faceSize = this.DisplayRectangle.Size;
int xRadius = faceSize.Width / 2;
int yRadius = faceSize.Height / 2;
double degrees;
int x;
int y;
// Make things pretty
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
// Paint either Digital or Analog face
using( Pen facePen = new Pen(Color.Black, 2) ) {
if( face != ClockFace.Digital ) {
g.DrawEllipse(facePen, facePen.Width + this.Padding.Left, facePen.Width + this.Padding.Top, faceSize.Width - facePen.Width * 2, faceSize.Height - facePen.Width * 2);
using( SolidBrush faceBrush = new SolidBrush(Color.White) ) {
g.FillEllipse(faceBrush, facePen.Width + this.Padding.Left, facePen.Width + this.Padding.Top, faceSize.Width - facePen.Width * 2, faceSize.Height - facePen.Width * 2);
}
}
else {
g.DrawRectangle(facePen, 1, 1, faceSize.Width + 1 - facePen.Width * 2, faceSize.Height + 1 - facePen.Width * 2);
using( SolidBrush faceBrush = new SolidBrush(Color.White) ) {
g.FillRectangle(faceBrush, 1 + this.Padding.Left, 1 + this.Padding.Top, faceSize.Width + 1 - facePen.Width * 2, faceSize.Height + 1 - facePen.Width * 2);
}
}
}
// Paint analog clock?
if( face != ClockFace.Digital ) {
// Hour hand
using( Pen hourHandPen = new Pen(this.hourHand.Color, this.hourHand.Width) ) {
degrees = (90.0 - ((now.Hour / 3.0) + (now.Minute / 180.0)) * 90.0) * (Math.PI / 180.0);
x = (int)Math.Round((xRadius / 3.0) * Math.Cos(degrees));
y = (int)-(Math.Round((yRadius / 3.0) * Math.Sin(degrees)));
g.DrawLine(hourHandPen, xRadius + this.Padding.Left, yRadius + this.Padding.Top, x + xRadius + this.Padding.Left, y + yRadius + this.Padding.Top);
}
// Minute hand
using( Pen minuteHandPen = new Pen(this.minuteHand.Color, this.minuteHand.Width) ) {
degrees = (90.0 - (now.Minute / 15.0) * 90.0) * (Math.PI / 180.0);
x = (int)Math.Round((xRadius / 2.0) * Math.Cos(degrees));
y = (int)-(Math.Round((yRadius / 2.0) * Math.Sin(degrees)));
g.DrawLine(minuteHandPen, xRadius + this.Padding.Left, yRadius + this.Padding.Top, x + xRadius + this.Padding.Left, y + yRadius + this.Padding.Top);
}
// Paint second hand, if so configured
using( Pen secondHandPen = new Pen(this.secondHand.Color, this.secondHand.Width) ) {
degrees = (90.0 - (now.Second / 15.0) * 90.0) * (Math.PI / 180.0);
x = (int)Math.Round((2.0 * xRadius / 3.0) * Math.Cos(degrees));
y = (int)-(Math.Round((2.0 * yRadius / 3.0) * Math.Sin(degrees)));
g.DrawLine(secondHandPen, xRadius + this.Padding.Left, yRadius + this.Padding.Top, x + xRadius + this.Padding.Left, y + yRadius + this.Padding.Top);
}
}
// Paint clock text
if( this.face != ClockFace.Analog ) {
using( StringFormat format = new StringFormat() )
using( Brush brush = new SolidBrush(this.ForeColor) ) {
format.Alignment = StringAlignment.Center;
string nowFormatted = now.ToString(this.digitalTimeFormat);
SizeF size = g.MeasureString(nowFormatted, this.Font);
// Paint digital time
g.DrawString(nowFormatted, this.Font, brush, xRadius + this.Padding.Left, (yRadius * 1.6F) + this.Padding.Top, format);
}
}
//// Calling the base class OnPaint
//base.OnPaint(pe);
}
private void timer_Tick(object sender, EventArgs e) {
// Don't do anything if executing at design-time
//if( this.DesignMode ) return;
// Refresh clock face
this.Invalidate();
// Check to see whether we're within 1 second of the primary alarm
double primarySeconds = (DateTime.Now - this.primaryAlarm).TotalSeconds;
if( (primarySeconds >= 0) && (primarySeconds <= 1) ) {
DateTime alarm = this.primaryAlarm;
this.primaryAlarm = DateTime.Now; // Reset alarm
this.primaryAlarmSet = false; // Show alarm only once
if( this.AlarmSounded != null ) {
// Sound alarm asynch so clock can keep ticking
this.AlarmSounded.BeginInvoke(this, new AlarmSoundedEventArgs(this.primaryAlarm, AlarmType.Primary), null, null);
}
}
// Check to see whether we're within 1 second of the backup alarm
double backupSeconds = (DateTime.Now - this.backupAlarm).TotalSeconds;
if( (backupSeconds >= 0) && (backupSeconds <= 1) ) {
DateTime alarm = this.backupAlarm;
this.backupAlarm = DateTime.Now; // Reset alarm
this.backupAlarmSet = false; // Show alarm only once
if( this.AlarmSounded != null ) {
// Sound alarm asynch so clock can keep ticking
this.AlarmSounded.BeginInvoke(this, new AlarmSoundedEventArgs(this.backupAlarm, AlarmType.Backup), null, null);
}
}
// Check to see whether we're within 1 second of any messages to self
MessageToSelf msgToRemove = null;
DateTime now = DateTime.Now;
foreach( MessageToSelf msg in messagesToSelf ) {
double msgSeconds = (now - msg.Occurence).TotalSeconds;
if( (msgSeconds >= 0) && (msgSeconds <= 1) ) {
if( this.AlarmSounded != null ) {
// Send message asynch so clock can keep ticking
this.MessageNotification.BeginInvoke(this, new MessageNotificationEventArgs(msg.Message), null, null);
}
break;
}
}
if( msgToRemove != null ) messagesToSelf.Remove(msgToRemove);
}
private void extendee_Click(object sender, System.EventArgs e) {
// Update the time-zone
this.timeZoneModifier = this.GetTimeZoneModifier((Control)sender);
}
private void alarmClockControl_PaddingChanged(object sender, EventArgs e) {
this.Invalidate();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -