• 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
        {
            /// 
            /// 记录时间
            /// 
            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);
            }
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266

    然后就直接拖控件啊,再使用屏保鼠标移动事件即可
    在这里插入图片描述

    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();
                }
            }
    
        
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64

    供喜欢的老铁参照

  • 相关阅读:
    javascript自定义事件的观察者模式写法和用法以及继承
    Java Double isNaN()方法具有什么功能呢?
    多线程与高并发(13)——Java常见并发容器总结
    发送 Splunk UBA 的anomalies and threats to Splunk ES
    python threading和multiprocessing模块基本用法实例分析
    Cesium快速上手9-Camera和Scene中的其他函数使用
    Perl 语言入门学习
    spring boot simple类型cache使用
    软件开发工程师笔试记录--关键路径,浮点数计算,地址变换,中断向量,I/O接口,海明码
    机器学习_LGB调参汇总(开箱即食)
  • 原文地址:https://blog.csdn.net/zhanglianzhu_91/article/details/126444384