C#实现轮子效果的屏保

也是十年前学绘图做的小玩意儿。已经没用了,翻出来给人分享学习资料。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
    {
        /// <summary>
        /// 记录时间
        /// </summary>
        private DateTime time;
        /// <summary>
        /// Timer控件
        /// </summary>
        private Timer timer1;
        /// <summary>
        /// 中间点颜色
        /// </summary>
        private Color dotColor=Color.Blue;
        /// <summary>
        /// 时针颜色
        /// </summary>
        private Color hourHandColor = Color.DimGray;
        /// <summary>
        /// 分针颜色
        /// </summary>
        private Color miniteHandColor = Color.LightSlateGray;
        /// <summary>
        /// 秒针颜色
        /// </summary>
        private Color secondHandColor = Color.DarkBlue;
        /// <summary>
        /// 指示是否画图
        /// </summary>
        bool isDraw = false;

        private System.ComponentModel.IContainer components;
        /// <summary>
        /// 设置时针颜色
        /// </summary>
        public Color SecondHandColor
        {
            get { return secondHandColor; }
            set { secondHandColor = value; }
        }

        /// <summary>
        /// 设置分针颜色
        /// </summary>
        public Color MiniteHandColor1
        {
            get { return miniteHandColor; }
            set { miniteHandColor = value; }
        }

        /// <summary>
        /// 设置秒针颜色
        /// </summary>
        public Color HourHandColor1
        {
            get { return hourHandColor; }
            set { hourHandColor = value; }
        }

        /// <summary>
        /// 设置动点颜色
        /// </summary>
        public Color DotColor
        {
            get { return dotColor; }
            set { dotColor = value; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public UserClock()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        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);

        }

        /// <summary>
        /// TimeTick事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            this.time = DateTime.Now;
            BufferDraw();


        }

        /// <summary>
        /// 双缓冲绘图
        /// </summary>
        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;
            
        }
        /// <summary>
        /// 画图函数
        /// </summary>
        /// <param name="pDC"></param>
        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);
        }

        /// <summary>
        /// 初始化画图环境
        /// </summary>
        /// <param name="pDC"></param>
        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);//调整比例大小
        }

        /// <summary>
        /// 画表盘
        /// </summary>
        /// <param name="pDC"></param>
        /// <param name="brush"></param>
        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);
            }
        }

        /// <summary>
        /// 画时针
        /// </summary>
        /// <param name="pDC"></param>
        /// <param name="pen"></param>
        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);
        }

        /// <summary>
        /// 画分针
        /// </summary>
        /// <param name="pDC"></param>
        /// <param name="pen"></param>
        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);
        }

        /// <summary>
        /// 画秒针
        /// </summary>
        /// <param name="pDC"></param>
        /// <param name="pen"></param>
        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();
            }
        }

    
    }
}

供喜欢的老铁参照