m0_59342845 2023-09-06 11:25 采纳率: 81%
浏览 2
已结题

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

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

img

  • 写回答

2条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-09-06 14:07
    关注

    【以下回答由 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,设定了相应的属性。

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



    【相关推荐】



    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月21日
  • 已采纳回答 9月13日
  • 创建了问题 9月6日

悬赏问题

  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价
  • ¥15 在anaconda上pytorch和paddle paddle下载报错
  • ¥25 自动填写QQ腾讯文档收集表