web-dev-qa-db-fra.com

Arêtes arrondies dans le bouton C # (WinForms)

It's a rounded edges button

Bonjour, grâce à des recherches effectuées ici et sur d’autres sites, j’ai créé un bouton à bords arrondis. 

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    Rectangle Rect = new Rectangle(0, 0, this.Width, this.Height);
    GraphicsPath GraphPath = new GraphicsPath();
    GraphPath.AddArc(Rect.X, Rect.Y, 50, 50, 180, 90);
    GraphPath.AddArc(Rect.X + Rect.Width - 50, Rect.Y, 50, 50, 270, 90);
    GraphPath.AddArc(Rect.X + Rect.Width - 50, Rect.Y + Rect.Height - 50, 50, 50, 0, 90);
    GraphPath.AddArc(Rect.X, Rect.Y + Rect.Height - 50, 50, 50, 90, 90);
    this.Region = new Region(GraphPath);
}

Le problème auquel je suis confronté est la "surbrillance bleue" du bouton: elle apparaît sur la plupart des boutons, mais pas sur les bords arrondis; mon bouton est donc surligné et non surligné (sur les bords). Que pourrais-je faire pour résoudre ceci? Je vous remercie.

PS: Je ne peux pas utiliser WPF. L'application est pour un très vieil ordinateur; alors, s'il vous plaît, ne le suggérez pas. En outre, le client n'a pas l'argent nécessaire pour se procurer un ordinateur plus récent. 

11
soulblazer

C’est rapide, vous voudrez peut-être peaufiner et optimiser de nombreux détails.

class RoundedButton : Button
{
   GraphicsPath GetRoundPath(RectangleF Rect, int radius)
   {
      float r2 = radius / 2f;
      GraphicsPath GraphPath = new GraphicsPath();

      GraphPath.AddArc(Rect.X, Rect.Y, radius, radius, 180, 90);
      GraphPath.AddLine(Rect.X + r2, Rect.Y, Rect.Width - r2, Rect.Y);
      GraphPath.AddArc(Rect.X + Rect.Width - radius, Rect.Y, radius, radius, 270, 90);
      GraphPath.AddLine(Rect.Width, Rect.Y + r2, Rect.Width, Rect.Height - r2);
      GraphPath.AddArc(Rect.X + Rect.Width - radius, 
                       Rect.Y + Rect.Height - radius, radius, radius, 0, 90);
      GraphPath.AddLine(Rect.Width - r2, Rect.Height, Rect.X + r2, Rect.Height);
      GraphPath.AddArc(Rect.X, Rect.Y + Rect.Height - radius, radius, radius, 90, 90);
      GraphPath.AddLine(Rect.X, Rect.Height - r2, Rect.X, Rect.Y + r2);

      GraphPath.CloseFigure();
      return GraphPath;
   }

   protected override void OnPaint(PaintEventArgs e)
   {
      base.OnPaint(e);
      RectangleF Rect = new RectangleF(0, 0, this.Width, this.Height);
      GraphicsPath GraphPath = GetRoundPath(Rect, 50);

      this.Region = new Region(GraphPath);
      using (Pen pen = new Pen(Color.CadetBlue, 1.75f))
      {
          pen.Alignment = PenAlignment.Inset;
          e.Graphics.DrawPath(pen, GraphPath);
      }
   }
}

Évidemment, puisque nous avons une classe, nous pouvons mettre en cache la GraphicsPath dans une variable de classe. Et bien sûr, vous choisissez la couleur ..

enter image description here

16
TaW

À moins de peindre vous-même, je ne pense pas que vous puissiez faire quoi que ce soit. Le bouton de base La logique de peinture n’est pas écrit sous la forme "Afficher une surbrillance bleue autour de telle partie de la région de la fenêtre". Au lieu de cela, il est écrit avec le type de région auquel il s’attend - une région rectangulaire. Donc, la peinture de base va toujours peindre une image rectangulaire en une forme recadrée. Vous aurez plus de facilité avec WPF.

0
Michael Gunter

Vous pouvez utiliser un navigateur Web, créer un bouton avec HTML et CSS, puis utiliser webbrowser.DocumentText = "your html";

0
isaque
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace button2
{
    public partial class Form1 : Form
    {
        private Button button1;
        private GroupBox box;
        public Form1()
        {
            InitializeComponent();
            show();
        }
        private void show()
        {
            box = new GroupBox();
            button1 = new Button();
            button1.Location = new Point(50, 50);
            ElipseControl nn = new ElipseControl();            
            nn.TargetControl = button1;            
            button1.Text = "First Name";
            button1.BackColor = Color.Cyan;
            button1.FlatStyle = FlatStyle.Flat;
            button1.FlatAppearance.BorderSize = 0;
            button1.FlatAppearance.BorderColor = Color.White;
            nn.CornerRadius = 10;

            button1.ForeColor = Color.Blue;
            button1.Font = new Font("Arial", 9, FontStyle.Bold);
            box.Controls.Add(button1);
            box.AutoSize = true;


            this.Controls.Add(box);

        }
    }
    class ElipseControl : Component
    {
        [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
        public static extern IntPtr CreateRoundRectRgn
            (
               int nLeftRect,
               int nTopRect,
               int nRightRect,
               int nBottomRect,
               int nWidthEllipse,
               int nHeightEllipse
            );
        private Control _cntrl;
        private int _CornerRadius = 30;

        public Control TargetControl
        {
            get { return _cntrl; }
            set
            {
                _cntrl = value;
                _cntrl.SizeChanged += (sender, eventArgs) => _cntrl.Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, _cntrl.Width, _cntrl.Height, _CornerRadius, _CornerRadius));
            }
        }

        public int CornerRadius
        {
            get { return _CornerRadius; }
            set
            {
                _CornerRadius = value;
                if (_cntrl != null)
                    _cntrl.Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, _cntrl.Width, _cntrl.Height, _CornerRadius, _CornerRadius));
            }
        }
    }
}
0
biwicks