WinForm 指示灯

8 篇文章 0 订阅
1 篇文章 0 订阅

WinForm 指示灯

参考:newideas07的博文 ovalshape C# 真正纯WinForm打造指示灯

今天一个WinForm项目需要使用到指示灯,由于没在控件属性的找到圆角设置。原本打算直接切两个图在PictureBox中显示。后来在网上找到 newideas07的博文 ovalshape C# 真正纯WinForm打造指示灯 ,因此根据该博文制作一个指示灯控件,以下为过程及代码。
效果图

1、新建自定义指示灯控件
在项目相应文件夹中 右键-添加-用户控件,输入名称:Lamp,点击确定。

在这里插入图片描述

2、设置属性和事件
设置控件大小为(50,50,设置控件绘制时事件 Lamp_Paint;

在这里插入图片描述
在这里插入图片描述

3、相关代码
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using MyControl.Datas.Enum;

namespace MyControl.Part
{
    public partial class Lamp:UserControl
    {

        //=================== construct function ==================	
        #region construct 指示灯构造
        /// <summary>
        /// 指示灯构造
        /// </summary>
        public Lamp()
        {
            this.InitializeComponent();
        }
        #endregion



        //=================== private fields ======================
        #region fields      
        private Status m_status;
        private Color m_centerColor = Color.White;
        private Color m_surroundColor = Color.Green;
        #endregion



        //=================== public properties ===================
        #region status 状态
        /// <summary>
        /// 状态
        /// </summary>
        public Status Status
        {
            get => this.m_status;
            set
            {
                this.m_status = value;
                this.SetStatus(value);
            }
        }
        #endregion



        //=================== event metod =========================
        #region OnPaint 绘制时控制边界圆角
        /// <summary>
        /// 绘制时控制边界圆角
        /// </summary>
        /// <param name="pevent"></param>
        protected override void OnPaint(PaintEventArgs pevent)
        {
            //使控件边界也为圆形
            var graphics = new GraphicsPath();
            graphics.AddEllipse(0,0,this.Width,this.Height);
            this.Region = new System.Drawing.Region(graphics);
            base.OnPaint(pevent);
        }
        #endregion

        #region Lamp_Paint 绘图时呈现中心颜色方式到指定颜色渐变效果
        /// <summary>
        /// 绘图时呈现中心颜色方式到指定颜色渐变效果
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Lamp_Paint(object sender,PaintEventArgs e)
        {
            //重绘时 画出中心放射颜色的圆形
            var path = new GraphicsPath();
            path.AddEllipse(0,0,this.Size.Width,this.Size.Height);
            var pthGrBrush = new PathGradientBrush(path)
            {
                CenterColor = m_centerColor
            };
            Color[] colors = { this.m_surroundColor };
            pthGrBrush.SurroundColors = colors;
            e.Graphics.FillEllipse(pthGrBrush,0,0,this.Size.Width,this.Size.Height);
        }
        #endregion



        //=================== public metod ========================



        //=================== private metod =======================
        #region SetStatus 设置状态
        /// <summary>
        /// 设置状态
        /// </summary>
        /// <param name="status"></param>
        private void SetStatus(Status status)
        {
            switch (this.Status)
            {
                case Status.OK:
                case Status.Open:
                    this.m_centerColor = Color.White;
                    this.m_surroundColor = Color.Green;
                    break;
                case Status.EX:
                case Status.ERR:
                    this.m_centerColor = Color.White;
                    this.m_surroundColor = Color.Red;
                    break;
                case Status.Stop:
                    this.m_centerColor = Color.White;
                    this.m_surroundColor = Color.Yellow;
                    break;
                case Status.None:
                case Status.Close:
                default:
                    this.m_centerColor = Color.FromArgb(255,249,249,249);//Color.White;
                    this.m_surroundColor = Color.Gray;
                    break;
            }
            this.Refresh();//刷新控件
        }
        #endregion
    }
}
枚举类 Enumeration
namespace MyControl.Datas.Enum
{
    #region Status 状态
    /// <summary>
    /// 状态
    /// </summary>
    public enum Status
    {
        #region None 无
        /// <summary>
        /// 无
        /// </summary>
        None,
        #endregion

        #region OK 正常
        /// <summary>
        /// 正常
        /// </summary>
        OK,
        #endregion

        #region EX 异常
        /// <summary>
        /// 异常
        /// </summary>
        EX,
        #endregion

        #region ERR 错误
        /// <summary>
        /// 错误
        /// </summary>
        ERR,
        #endregion

        #region Open 开启
        /// <summary>
        /// 开启
        /// </summary>
        Open,
        #endregion

        #region Close 关闭
        /// <summary>
        /// 开启
        /// </summary>
        Close,
        #endregion

        #region Stop 暂停
        /// <summary>
        /// 开启
        /// </summary>
        Stop,
        #endregion 
    }
    #endregion
}
调用效果
项目生成后,在设计页面,可以直接在【工具箱】找到自定义控件【Lamp】,与【公共控件】一样,
用拖来进设计页面即可。

在这里插入图片描述
或者使用代码添加

            this.lamp1 = new MyControl.Part.Lamp();
             this.lamp1.Location = new System.Drawing.Point(1, 1);
            this.lamp1.Name = "lamp1";
            this.lamp1.Size = new System.Drawing.Size(50, 50);
            this.lamp1.Status = MyControl.Datas.Enum.Status.None;
            this.Panel1.Controls.Add(this.lamp1);
效果图

参考:newideas07的博文 ovalshape C# 真正纯WinForm打造指示灯

  • 3
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
以下是实现WinForm跑马灯效果的方法: 1. 使用Timer控件和Label控件实现跑马灯效果: ```csharp using System; using System.Windows.Forms; namespace WindowsFormsApp { public partial class Form1 : Form { private Timer timer; private Label label; public Form1() { InitializeComponent(); InitializeTimer(); InitializeLabel(); } private void InitializeTimer() { timer = new Timer(); timer.Interval = 100; // 设置定时器间隔时间,单位为毫秒 timer.Tick += Timer_Tick; timer.Start(); } private void InitializeLabel() { label = new Label(); label.Text = "这是一个跑马灯效果的Label"; label.Location = new System.Drawing.Point(10, 10); label.AutoSize = true; Controls.Add(label); } private void Timer_Tick(object sender, EventArgs e) { // 每次定时器触发时,将Label的位置向左移动一定距离 label.Left -= 5; // 当Label完全移出窗口时,将其重新放置到窗口右侧 if (label.Right < 0) { label.Left = Width; } } } } ``` 2. 使用PictureBox控件和Timer控件实现跑马灯效果: ```csharp using System; using System.Windows.Forms; namespace WindowsFormsApp { public partial class Form1 : Form { private Timer timer; private PictureBox pictureBox; public Form1() { InitializeComponent(); InitializeTimer(); InitializePictureBox(); } private void InitializeTimer() { timer = new Timer(); timer.Interval = 100; // 设置定时器间隔时间,单位为毫秒 timer.Tick += Timer_Tick; timer.Start(); } private void InitializePictureBox() { pictureBox = new PictureBox(); pictureBox.Image = Properties.Resources.marquee_image; // 设置跑马灯图片 pictureBox.SizeMode = PictureBoxSizeMode.AutoSize; pictureBox.Location = new System.Drawing.Point(10, 10); Controls.Add(pictureBox); } private void Timer_Tick(object sender, EventArgs e) { // 每次定时器触发时,将PictureBox的位置向左移动一定距离 pictureBox.Left -= 5; // 当PictureBox完全移出窗口时,将其重新放置到窗口右侧 if (pictureBox.Right < 0) { pictureBox.Left = Width; } } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值