Winform控件优化之圆角按钮1

yizhihongxing

Winform控件优化之圆角按钮1

Winform中的按钮控件在设计时为了美观通常会考虑使用圆角按钮。本文将介绍Winform控件圆角按钮的实现方法,涉及Winform控件创建、绘制和事件处理等方面。

1. 预备知识

本文需要了解以下知识点:

  • C#基础语法
  • Winform控件的创建和使用
  • GDI+绘图基础知识

2. 创建圆角按钮

首先,在Winform窗体中创建一个Button控件,然后将其FlatStyle属性设置为Flat:

Button button = new Button();
button.FlatStyle = FlatStyle.Flat;

接着,为了使按钮变成圆角,我们需要在按钮的Paint事件中进行绘制。在该事件中,我们可以使用GraphicsPath对象绘制圆角矩形,然后使用Graphics对象填充矩形和绘制按钮文本,代码如下:

private void button_Paint(object sender, PaintEventArgs e)
{
    Button button = sender as Button;
    Graphics g = e.Graphics;
    g.Clear(button.BackColor);
    using (GraphicsPath path = new GraphicsPath())
    {
        path.AddArc(button.ClientRectangle.X, button.ClientRectangle.Y, button.CornerRadius, button.CornerRadius, 180, 90);
        path.AddArc(button.ClientRectangle.X + button.ClientRectangle.Width - button.CornerRadius, button.ClientRectangle.Y, button.CornerRadius, button.CornerRadius, 270, 90);
        path.AddArc(button.ClientRectangle.X + button.ClientRectangle.Width - button.CornerRadius, button.ClientRectangle.Y + button.ClientRectangle.Height - button.CornerRadius, button.CornerRadius, button.CornerRadius, 0, 90);
        path.AddArc(button.ClientRectangle.X, button.ClientRectangle.Y + button.ClientRectangle.Height - button.CornerRadius, button.CornerRadius, button.CornerRadius, 90, 90);
        path.CloseFigure();

        g.FillPath(new SolidBrush(button.BackColor), path);
        if (button.Enabled)
        {
            g.DrawString(button.Text, button.Font, new SolidBrush(button.ForeColor), button.ClientRectangle, new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
        }
        else
        {
            g.DrawString(button.Text, button.Font, new SolidBrush(Color.Gray), button.ClientRectangle, new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
        }
    }
}

在上述代码中,我们定义了一个名为CornerRadius的属性,用于指定按钮的圆角半径,默认值为5。然后我们绘制了4个圆角弧线和一个矩形,组成了一个圆角矩形。最后使用FillPath方法填充绘制的图形。

3. 设置圆角按钮的样式

除了使用代码进行样式设置之外,我们还可以通过控件的属性设置进行样式设置。例如,我们可以在应用程序的资源文件中定义按钮的圆角样式,如下图所示:

Winform控件优化之圆角按钮1

然后在代码中应用该样式,如下所示:

button.FlatStyle = FlatStyle.Flat;
button.FlatAppearance.BorderSize = 0;
button.BackgroundImage = Properties.Resources.ButtonBackground;
button.BackgroundImageLayout = ImageLayout.Stretch;
button.Cursor = Cursors.Hand;

上述代码中,我们使用了各种属性对控件进行设置,包括背景、边框、光标等。

4. 示例说明

下面分别给出两个Winform控件圆角按钮的实现示例,其中一个是使用代码实现设置按钮样式,另一个是使用资源文件实现按钮样式。

示例一

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

namespace WinformCornerButtonExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitButton1();
        }

        private void InitButton1()
        {
            Button button = new Button();
            button.Location = new Point(50, 50);
            button.Size = new Size(100, 50);
            button.FlatStyle = FlatStyle.Flat;
            button.FlatAppearance.BorderSize = 0;
            button.BackColor = Color.MediumSeaGreen;
            button.ForeColor = Color.White;
            button.Font = new Font("Microsoft YaHei", 12);
            button.Text = "Button 1";
            button.Cursor = Cursors.Hand;
            button.CornerRadius = 20;
            button.Paint += button_Paint;
            Controls.Add(button);
        }

        private void button_Paint(object sender, PaintEventArgs e)
        {
            Button button = sender as Button;
            Graphics g = e.Graphics;
            g.Clear(button.BackColor);
            using (GraphicsPath path = new GraphicsPath())
            {
                path.AddArc(button.ClientRectangle.X, button.ClientRectangle.Y, button.CornerRadius, button.CornerRadius, 180, 90);
                path.AddArc(button.ClientRectangle.X + button.ClientRectangle.Width - button.CornerRadius, button.ClientRectangle.Y, button.CornerRadius, button.CornerRadius, 270, 90);
                path.AddArc(button.ClientRectangle.X + button.ClientRectangle.Width - button.CornerRadius, button.ClientRectangle.Y + button.ClientRectangle.Height - button.CornerRadius, button.CornerRadius, button.CornerRadius, 0, 90);
                path.AddArc(button.ClientRectangle.X, button.ClientRectangle.Y + button.ClientRectangle.Height - button.CornerRadius, button.CornerRadius, button.CornerRadius, 90, 90);
                path.CloseFigure();

                g.FillPath(new SolidBrush(button.BackColor), path);
                g.DrawString(button.Text, button.Font, new SolidBrush(button.ForeColor), button.ClientRectangle, new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
            }
        }
    }
}

上述代码实现了一个绿色的带圆角的Button控件,如下所示:

Winform控件优化之圆角按钮1

示例二

首先,我们需要在资源文件中定义一张圆角样式的背景图片。接着,在代码中使用该背景图片实现Button控件的样式。

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

namespace WinformCornerButtonExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitButton2();
        }

        private void InitButton2()
        {
            Button button = new Button();
            button.Location = new Point(50, 50);
            button.Size = new Size(100, 50);
            button.FlatStyle = FlatStyle.Flat;
            button.FlatAppearance.BorderSize = 0;
            button.BackgroundImage = Properties.Resources.ButtonBackground;
            button.BackgroundImageLayout = ImageLayout.Stretch;
            button.Font = new Font("Microsoft YaHei", 12);
            button.Text = "Button 2";
            button.Cursor = Cursors.Hand;
            button.Paint += button_Paint;
            Controls.Add(button);
        }

        private void button_Paint(object sender, PaintEventArgs e)
        {
            Button button = sender as Button;
            e.Graphics.DrawString(button.Text, button.Font, new SolidBrush(button.ForeColor), button.ClientRectangle, new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
        }
    }
}

上述代码实现了一个带圆角的Button控件,使用了定义好的资源文件,如下所示:

Winform控件优化之圆角按钮1

5. 总结

本文介绍了Winform控件圆角按钮的实现方法,并提供了两个示例帮助读者了解如何使用代码和资源文件实现圆角按钮。读者可以根据自己的需要进行选择,使用代码或资源文件实现样式设置。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Winform控件优化之圆角按钮1 - Python技术站

(0)
上一篇 2023年6月27日
下一篇 2023年6月27日

相关文章

  • ios的collection控件的自定义布局实现与设计

    ios的collection控件是一种在iOS应用中广泛使用的UI界面组件,可以用于展示各种类型的数据,如图片、文字、视频等。自定义布局实现与设计是指开发者可以通过编写代码实现对collection控件的样式、布局、交互等方面进行自定义,以满足特定的需求。 以下是自定义布局实现与设计的完整攻略: 第一步:创建自定义布局类 创建一个新的继承自UICollect…

    other 2023年6月25日
    00
  • Todo清单怎么用 Todo清单常见问题汇总

    Todo清单怎么用 简介 Todo清单是一种简单而常用的待办事项管理工具。它可以帮助用户快速记录需要完成的任务,并将它们分类、标注,方便用户在日常生活和工作中灵活高效地进行任务管理。下面是Todo清单的基本使用方法和常见问题汇总。 基本使用方法 安装Todo清单APP 在手机应用商店中搜索Todo清单,下载并安装。 创建新的任务 在Todo清单主页面,点击“…

    other 2023年6月27日
    00
  • Win10正式版ESD升级镜像官方下载地址汇总(64为/32位)

    Win10正式版ESD升级镜像官方下载地址汇总(64位/32位)攻略 本攻略将详细介绍如何获取Win10正式版ESD升级镜像的官方下载地址,并提供两个示例说明。 步骤一:访问官方网站 首先,打开你的网络浏览器,并访问微软官方网站。你可以在以下网址找到官方下载页面: https://www.microsoft.com/zh-cn/software-downlo…

    other 2023年8月4日
    00
  • 关于Android输入法弹窗bug的优雅处理

    在Android应用程序中,有时会遇到输入法弹窗导致界面错位或遮挡的问题。为了优雅地处理这个问题,可以按照以下完整攻略进行操作: … … 在AndroidManifest.xml文件中,为对应的Activity添加android:windowSoftInputMode属性,并设置为adjustResize。 <activity … andr…

    other 2023年9月5日
    00
  • php弹窗案例

    以下是关于“php弹窗案例”的完整攻略,包括php弹窗的基本知识、实现弹窗的方法和两个示例。 php弹窗的基本知识 PHP是一种流行的服务器端脚本语言可以用于创建动态网页和应用程序。PHP可以与HTML、CSS和JavaScript前端技术结合使用,实现各种功能,括弹窗。 实现弹窗的方法 以下是使用PHP实现弹窗的基本骤: 创建一个包含弹窗内容的HTML页面…

    other 2023年5月7日
    00
  • 详解Spring Boot加载properties和yml配置文件

    关于“详解Spring Boot加载properties和yml配置文件”的攻略,我将分为以下几个部分进行详细讲解: Spring Boot的默认配置文件名与位置 properties文件的加载方式 yml文件的加载方式 示例1:properties文件和yml文件混合使用 示例2:指定profile加载不同的配置文件 接下来我将一一进行展开说明。 1. S…

    other 2023年6月25日
    00
  • MySQL5.7.20解压版安装和修改root密码的教程

    下面是MySQL5.7.20解压版安装和修改root密码的教程的完整攻略。 一、下载和解压MySQL安装包 打开MySQL官网,进入下载页面,选择MySQL Community Server 5.7.20版本的压缩包进行下载:https://dev.mysql.com/downloads/mysql/5.7.html 下载完成后,将压缩包解压到你想安装的目录…

    other 2023年6月27日
    00
  • Android移动应用开发指南之六种布局详解

    Android移动应用开发指南之六种布局详解 1. 线性布局(LinearLayout) 线性布局是Android中最常用的布局之一,它按照水平或垂直方向排列子视图。以下是一个示例: <LinearLayout android:layout_width=\"match_parent\" android:layout_height=\…

    other 2023年8月23日
    00
合作推广
合作推广
分享本页
返回顶部