也是十年前学绘图做的小玩意儿。已经没用了,翻出来给人分享学习资料。C#能干的有很多,只是代码开放意识弱。

一样的用绘图实现轮子控件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
namespace UserClock
{
public class UserClock:UserControl
{
///
/// 记录时间
///
private DateTime time;
///
/// Timer控件
///
private Timer timer1;
///
/// 中间点颜色
///
private Color dotColor=Color.Blue;
///
/// 时针颜色
///
private Color hourHandColor = Color.DimGray;
///
/// 分针颜色
///
private Color miniteHandColor = Color.LightSlateGray;
///
/// 秒针颜色
///
private Color secondHandColor = Color.DarkBlue;
///
/// 指示是否画图
///
bool isDraw = false;
private System.ComponentModel.IContainer components;
///
/// 设置时针颜色
///
public Color SecondHandColor
{
get { return secondHandColor; }
set { secondHandColor = value; }
}
///
/// 设置分针颜色
///
public Color MiniteHandColor1
{
get { return miniteHandColor; }
set { miniteHandColor = value; }
}
///
/// 设置秒针颜色
///
public Color HourHandColor1
{
get { return hourHandColor; }
set { hourHandColor = value; }
}
///
/// 设置动点颜色
///
public Color DotColor
{
get { return dotColor; }
set { dotColor = value; }
}
///
/// 构造函数
///
public UserClock()
{
InitializeComponent();
}
///
/// 初始化
///
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// UserClock
//
this.BackColor = System.Drawing.Color.Transparent;
this.ForeColor = System.Drawing.Color.MediumSlateBlue;
this.Name = "UserClock";
this.ResumeLayout(false);
}
///
/// TimeTick事件
///
///
///
private void timer1_Tick(object sender, EventArgs e)
{
this.time = DateTime.Now;
BufferDraw();
}
///
/// 双缓冲绘图
///
private void BufferDraw()
{
Bitmap bitmap = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
Graphics g = Graphics.FromImage(bitmap);
g.SmoothingMode = SmoothingMode.HighQuality;
g.Clear(this.BackColor);
Draw(g);
Graphics g1 = this.CreateGraphics();
g1.DrawImage(bitmap, 0, 0);
g1.Dispose();
g.Dispose();
bitmap.Dispose();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (isDraw)
{
BufferDraw();
}
isDraw = true;
}
///
/// 画图函数
///
///
private void Draw(Graphics pDC)
{
Pen pen = new Pen(this.ForeColor);
Pen hourPen = new Pen(hourHandColor);
Pen minetePen = new Pen(miniteHandColor);
Pen secondPen = new Pen(secondHandColor);
SolidBrush brush = new SolidBrush(this.ForeColor);
InitCoordinates(pDC);
DrawDots(pDC, brush);
//DrawHourHand(pDC, hourPen);
//DrawMineteHand(pDC, minetePen);
DrawSecondHand(pDC, secondPen);
}
///
/// 初始化画图环境
///
///
protected void InitCoordinates(Graphics pDC)
{
if (this.Width == 0 || this.Height == 0)
{
return;
}
pDC.TranslateTransform(this.Width/2,this.Height/2);//平移坐标原点
pDC.ScaleTransform(this.Height / 250F, this.Width / 250F);//调整比例大小
}
///
/// 画表盘
///
///
///
protected void DrawDots(Graphics pDC, Brush brush)
{
int size;
pDC.FillEllipse(brush, -4, -4, 8, 8);
for (int i = 0; i <= 59; i++)
{
if (i % 5 == 0)
{
size = 10;
}
else
{
size = 5;
}
pDC.FillEllipse(brush,-size /2,-100-size /2,size ,size );
pDC.RotateTransform(6);
}
}
///
/// 画时针
///
///
///
protected void DrawHourHand(Graphics pDC,Pen pen)
{
SolidBrush brush = new SolidBrush(hourHandColor);
GraphicsState state = pDC.Save();
pDC.RotateTransform(360.0F * time.Hour / 12 + 30F * time.Minute / 60);
pDC.DrawLine(pen, -3, 0, 0, -50);
pDC.DrawLine(pen, 3, 0, 0, -50);
pDC.FillEllipse(brush, -4 / 2, -50 - 4 / 2, 4, 4);
pDC.Restore(state);
}
///
/// 画分针
///
///
///
protected void DrawMineteHand(Graphics pDC, Pen pen)
{
SolidBrush brush = new SolidBrush(miniteHandColor);
GraphicsState state = pDC.Save();
pDC.RotateTransform(360.0F * time.Minute / 60 + 6.0F * time.Second / 60);
pDC.DrawLine(pen, -2, 0, 0, -70);
pDC.DrawLine(pen, 2, 0, 0, -70);
pDC.FillEllipse(brush, -4 / 2, -70 - 4 / 2, 4, 4);
pDC.Restore(state);
}
///
/// 画秒针
///
///
///
protected void DrawSecondHand(Graphics pDC, Pen pen)
{
//int size ;
//if (time.Second % 5 == 0)
//{
// size = 15;
//}
//else
//{
// size = 5;
//}
SolidBrush brush = new SolidBrush(secondHandColor);
GraphicsState state = pDC.Save();
pDC.RotateTransform(360.0F * time.Second / 60);
pDC.DrawLine(pen, -1, 0, 0, -100);
pDC.DrawLine(pen, 1, 0, 0, -100);
pDC.FillEllipse(brush, -5 / 2, -100 - 5 / 2, 5, 5);
pDC.Restore(state);
}
}
}
然后就直接拖控件啊,再使用屏保鼠标移动事件即可

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace TestClock
{
public partial class ScreenSaver : Form
{
Point point;
private const int SW_HIDE = 0;//API参数表示隐藏窗口
private const int SW_SHOW = 5;//API参数表示用当前的大小和位置显示窗口
[DllImportAttribute("user32.dll")]
private static extern int FindWindow(string ClassName, string WindowName);
[DllImport("user32.dll")]
private static extern int ShowWindow(int handle,int cmdShow);
bool isNew = true;
public ScreenSaver()
{
InitializeComponent();
}
private void ScreenSaver_Load(object sender, EventArgs e)
{
Cursor.Hide();
this.BackColor = Color.Black;
//this.ShowInTaskbar = false;//使程序不在任务栏里显示
ShowWindow(FindWindow("Shell_TrayWnd", null), SW_HIDE);
}
private void ScreenSaver_KeyDown(object sender, KeyEventArgs e)
{
Cursor .Show ();
ShowWindow(FindWindow("Shell_TrayWnd", null), SW_SHOW);
Application .Exit();
}
private void ScreenSaver_MouseMove(object sender, MouseEventArgs e)
{
if (isNew)
{
point = e.Location;
isNew = false;
}
if (System.Math.Abs(point.X - e.X) > 10 || System.Math.Abs(point.Y - e.Y ) > 10)
{
Cursor.Show();
ShowWindow(FindWindow("Shell_TrayWnd", null), SW_SHOW);
Application.Exit();
}
}
}
}
供喜欢的老铁参照