winform图片上这个控件该怎么制作

winform图片上这个控件该怎么制作,两条边框上有三个点,中间显示文本。

img

【以下回答由 GPT 生成】

这个问题可以通过自定义一个继承自Control的控件来解决。下面是一个示例代码,以实现这个控件:

首先,创建一个名为CustomControl的类,并继承自Control类。在该类中添加必要的属性和方法。

using System;
using System.Drawing;
using System.Windows.Forms;

namespace YourNamespace
{
    public class CustomControl : Control
    {
        // 控件的文本属性
        public string Text { get; set; }

        // 控件的字体属性
        public Font Font { get; set; }

        // 控件的边框颜色
        public Color BorderColor { get; set; }

        // 控件的边框宽度
        public int BorderWidth { get; set; }

        // 控件的圆点颜色
        public Color DotColor { get; set; }

        // 控件的圆点半径
        public int DotRadius { get; set; }

        // 控件的构造函数
        public CustomControl()
        {
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);

            // 默认属性值
            BorderColor = Color.Black;
            BorderWidth = 2;
            DotColor = Color.Red;
            DotRadius = 5;
            Font = new Font("Arial", 12);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            // 绘制边框
            using (Pen borderPen = new Pen(BorderColor, BorderWidth))
            {
                e.Graphics.DrawRectangle(borderPen, ClientRectangle);
            }

            // 绘制圆点
            int dotX = Width / 2 - DotRadius / 2;
            int dotY1 = Height / 4 - DotRadius / 2;
            int dotY2 =Height / 4 * 3 - DotRadius / 2;

            using (Brush dotBrush = new SolidBrush(DotColor))
            {
                e.Graphics.FillEllipse(dotBrush, dotX, dotY1, DotRadius, DotRadius);
                e.Graphics.FillEllipse(dotBrush, dotX, dotY2, DotRadius, DotRadius);
            }

            // 绘制文本
            using (Brush textBrush = new SolidBrush(ForeColor))
            {
                SizeF textSize = e.Graphics.MeasureString(Text, Font);
                int textX = (int)(Width / 2 - textSize.Width / 2);
                int textY = (int)(Height / 2 - textSize.Height / 2);
                e.Graphics.DrawString(Text, Font, textBrush, textX, textY);
            }
        }
    }
}

然后,在你的WinForm中使用该控件。在Form的代码中添加一个CustomControl控件,并设置相应的属性。

using System.Windows.Forms;

namespace YourNamespace
{
    public class MainForm : Form
    {
        public MainForm()
        {
            CustomControl customControl = new CustomControl();
            customControl.Text = "Hello World";
            customControl.Font = new Font("Arial", 14, FontStyle.Bold);
            customControl.BorderColor = Color.Red;
            customControl.BorderWidth = 4;
            customControl.DotColor = Color.Blue;
            customControl.DotRadius = 8;

            Controls.Add(customControl);
        }
    }
}

以上代码示例中,我们自定义了一个控件CustomControl,该控件具有文本、字体、边框颜色、边框宽度、圆点颜色和圆点半径等属性。在控件的OnPaint方法中,绘制了边框、圆点和文本。

在Form的代码中,我们创建了一个MainForm,并在其中使用了CustomControl,设定了相应的属性。

你可以将以上代码添加到你的项目中,并根据需要进行调整。这样,当你运行程序时,就会在窗体中看到一个带有边框和文本的控件,满足你的需求。



【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^

不知道你这个是在哪里看到的,其实可以用贴图的方法,用picturebox组合