自定义屏幕保护


一、设计器页面及代码(Form2.Designer.cs):

自定义屏幕保护_第1张图片

namespace 自定义屏保
{
    partial class Form2
    {
        /// 
        /// Required designer variable.
        /// 
        private System.ComponentModel.IContainer components = null;

        /// 
        /// Clean up any resources being used.
        /// 
        /// true if managed resources should be disposed; otherwise, false.
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// 
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// 
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.label4 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label1 = new System.Windows.Forms.Label();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.SuspendLayout();
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.BackColor = System.Drawing.Color.Transparent;
            this.label4.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label4.ForeColor = System.Drawing.Color.White;
            this.label4.Location = new System.Drawing.Point(177, 137);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(55, 14);
            this.label4.TabIndex = 7;
            this.label4.Text = "label4";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.BackColor = System.Drawing.Color.Transparent;
            this.label3.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label3.ForeColor = System.Drawing.Color.White;
            this.label3.Location = new System.Drawing.Point(175, 110);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(55, 14);
            this.label3.TabIndex = 6;
            this.label3.Text = "label3";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.BackColor = System.Drawing.Color.Transparent;
            this.label2.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label2.ForeColor = System.Drawing.Color.White;
            this.label2.Location = new System.Drawing.Point(175, 85);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(55, 14);
            this.label2.TabIndex = 5;
            this.label2.Text = "label2";
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.BackColor = System.Drawing.Color.Transparent;
            this.label1.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label1.ForeColor = System.Drawing.Color.White;
            this.label1.Location = new System.Drawing.Point(175, 60);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(55, 14);
            this.label1.TabIndex = 4;
            this.label1.Text = "label1";
            // 
            // timer1
            // 
            this.timer1.Interval = 1;
            // 
            // Form2
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.BackgroundImage = global::自定义屏保.Properties.Resources.黑帖;
            this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
            this.ClientSize = new System.Drawing.Size(284, 261);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Name = "Form2";
            this.Text = "Form2";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Timer timer1;

    }
}

二、代码如下(Form2.cs):

   
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;
namespace 自定义屏保 {     public partial class Form2 : Form     {         int fontSize = 0;//字休大小
        public Form2()         {             InitializeComponent();             InitEvent();             StartRun();         }
        private void InitEvent()         {             this.SizeChanged += ((s, e) =>             {                 label1.Top = this.Height / 5;//文字从右到左滚动  <---                 label3.Top = (this.Height / 5) * 4;//文字从左到右滚动  --->                 label3.Left = 0 - label2.Width;                 label4.Left = (this.Width / 5) * 4;// 向下滚动                 label4.Top = 0 - label2.Height;//设置当前控件的显示位置                 label2.Left = this.Width / 5;//向上滚动             });
            this.KeyDown += ((a, b) =>             {                 if (b.KeyCode == Keys.Escape)                     this.Close();             });
            this.timer1.Tick += new EventHandler(timer1_Tick);         }
        private void StartRun()         {             this.FormBorderStyle = FormBorderStyle.None;//窗体无边框             this.WindowState = FormWindowState.Maximized;//窗体最大化             this.BackColor = Color.Black;//设置窗体背景为黑色             SetScrollingText();//设置窗体中各控件的样式             timer1.Start();//启动计时器         }
        ///         /// 在显示预览和屏保前,对窗体中的各控件进行设置         ///         /// 设置父级控件         public void SetScrollingText()         {             fontSize = 20;//设置字体大小             label1.Text = "我非英雄";//设置文本             label1.Font = new Font("宋体", fontSize, FontStyle.Bold);//设置字体样式             label2.Text = "广" + "\n" + "目" + "\n" + "无" + "\n" + "双";//设置纵向文本             label2.Font = new Font("宋体", fontSize, FontStyle.Bold);//设置字体样式             label3.Text = "蛋坏本我";//设置文本             label3.Font = new Font("宋体", fontSize, FontStyle.Bold);//设置字体样式             label4.Text = "张" + "\n" + "嚣" + "\n" + "限" + "\n" + "无";//设置纵向文本             label4.Font = new Font("宋体", fontSize, FontStyle.Bold);//设置字体样式         }
        private void timer1_Tick(object sender, EventArgs e)         {             //文字从右到左滚动             label1.Left = label1.Left - 2;//向左移动             if ((label1.Left + label1.Width) < 1)//当超出左边界时                 label1.Left = this.Width;//在右边出现             //文字从下到上滚动             label2.Top = label2.Top - 1;//向上移动             if ((label2.Top + label2.Height) < 1)//当超出上边界时                 label2.Top = this.Height;//在下边出现             //文字从左到右滚动             if (label3.Left > this.Width)//如果超出右边界                 label3.Left = 0 - label2.Width;//在左边出现             else                 label3.Left = label3.Left + 2;//向右移动             //文字从上到下滚动             if (label4.Top > this.Height)//如果超出下边界                 label4.Top = 0 - label4.Height;//在上边出现             else                 label4.Top = label4.Top + 1;//向下移动         }     } }


三、运行效果:

自定义屏幕保护_第2张图片


你可能感兴趣的:(C#)