Reduire 2 methodes en 1

Résolu
hypothetix Messages postés 191 Date d'inscription dimanche 19 janvier 2020 Statut Membre Dernière intervention 24 avril 2024 - 29 août 2022 à 17:55
Whismeril Messages postés 19030 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 27 avril 2024 - 29 août 2022 à 20:58

Bonjour,

J'ai 2 methodes(surcharge) qui font le même traitement, une sur MainForm et une sur un bouton.
Ca fonctionne, le rendu n'est pas parfait mais ce n'est pas le propos.
Ma question: Es-ce possible de faire plus compact ?
C'est à dire une seule methode qui accepterait les 2 paramètres.

      RoundBorder(this,10);//for MainForm
      RoundBorder(buttonEnter,5);

     public static void RoundBorder(Form frm, int CornerRadius)
    {
        Rectangle Bounds = new Rectangle(0, 0, frm.Width, frm.Height);
        System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
        path.AddArc(Bounds.X, Bounds.Y, CornerRadius, CornerRadius, 180, 90);
        path.AddArc(Bounds.X + Bounds.Width - CornerRadius, Bounds.Y, CornerRadius, CornerRadius, 270, 90);
        path.AddArc(Bounds.X + Bounds.Width - CornerRadius, Bounds.Y + Bounds.Height - CornerRadius, CornerRadius-1, CornerRadius-1, 0, 90);
        path.AddArc(Bounds.X, Bounds.Y + Bounds.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90);
        path.CloseAllFigures();
        frm.Region = new Region(path);
        frm.Show();
    }

    public static void RoundBorder(System.Windows.Forms.Button frm, int CornerRadius)
    {
        Rectangle Bounds = new Rectangle(0, 0, frm.Width, frm.Height);
        System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
        path.AddArc(Bounds.X, Bounds.Y, CornerRadius, CornerRadius, 180, 90);
        path.AddArc(Bounds.X + Bounds.Width - CornerRadius, Bounds.Y, CornerRadius, CornerRadius, 270, 90);
        path.AddArc(Bounds.X + Bounds.Width - CornerRadius, Bounds.Y + Bounds.Height - CornerRadius, CornerRadius-1, CornerRadius-1, 0, 90);
        path.AddArc(Bounds.X, Bounds.Y + Bounds.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90);
        path.CloseAllFigures();
        frm.Region = new Region(path);
        frm.Show();
    }

Merci.

6 réponses

Whismeril Messages postés 19030 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 27 avril 2024 656
29 août 2022 à 19:59

Bonsoir

en regardant la chaine d'héritage

On constate, qu'ils dérivent tous les 2 de Control

Chaine d'héritage de Form
Chaine d'héritage de Button

Comme tes 2 codes sont strictement identiques, on peut supposer que les propriétés et méthodes communes que tu utilises dérivent de Control, on vérifie donc.

https://docs.microsoft.com/fr-fr/dotnet/api/system.windows.forms.control?view=windowsdesktop-6.0

  • Height et Width OK (et ça je n'en doutais pas, quasiment tous les contrôles ayant une interface graphique
  • Region OK
  • Show OK

Donc en première approche, on peut tenter ça

     public static void RoundBorder(Control frm, int CornerRadius)
    {
        Rectangle Bounds = new Rectangle(0, 0, frm.Width, frm.Height);
        System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
        path.AddArc(Bounds.X, Bounds.Y, CornerRadius, CornerRadius, 180, 90);
        path.AddArc(Bounds.X + Bounds.Width - CornerRadius, Bounds.Y, CornerRadius, CornerRadius, 270, 90);
        path.AddArc(Bounds.X + Bounds.Width - CornerRadius, Bounds.Y + Bounds.Height - CornerRadius, CornerRadius-1, CornerRadius-1, 0, 90);
        path.AddArc(Bounds.X, Bounds.Y + Bounds.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90);
        path.CloseAllFigures();
        frm.Region = new Region(path);
        frm.Show();
    }

Dis-moi si ça marche.


2
NHenry Messages postés 15113 Date d'inscription vendredi 14 mars 2003 Statut Modérateur Dernière intervention 22 avril 2024 159
29 août 2022 à 19:54

Voir peut être en prenant la première classe commune de leur héritage.
(System.Windows.Forms.Control me semble pas forcément le plus adapté).


0
vb95 Messages postés 3472 Date d'inscription samedi 11 janvier 2014 Statut Contributeur Dernière intervention 13 avril 2024 169
Modifié le 29 août 2022 à 20:09

Bonjour

A tester en utilisant la classe de base Object vu qu'une Form et un Button héritent tous les deux de la classe Object .

public static void RoundBorder(Object objet, int CornerRadius)
    {
        Rectangle Bounds = new Rectangle(0, 0, objet.Width, objet.Height);
        System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
        path.AddArc(Bounds.X, Bounds.Y, CornerRadius, CornerRadius, 180, 90);
        path.AddArc(Bounds.X + Bounds.Width - CornerRadius, Bounds.Y, CornerRadius, CornerRadius, 270, 90);
        path.AddArc(Bounds.X + Bounds.Width - CornerRadius, Bounds.Y + Bounds.Height - CornerRadius, CornerRadius-1, CornerRadius-1, 0, 90);
        path.AddArc(Bounds.X, Bounds.Y + Bounds.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90);
        path.CloseAllFigures();
        objet.Region = new Region(path);
        objet.Show();
    }

Suite à l'intervention de Whismeril remplacer Object par Control dans la procédure .

Un salut et un merci à lui 


0
Whismeril Messages postés 19030 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 27 avril 2024 656
29 août 2022 à 20:01

Hello,

non object ne contient pas Height, Width, Region et Show().

Il faudrait caster par la suite.

Control est la meilleure option à priori (sauf si recodage particulier dans la chaine d'héritage)

0
hypothetix Messages postés 191 Date d'inscription dimanche 19 janvier 2020 Statut Membre Dernière intervention 24 avril 2024 6
29 août 2022 à 20:22

Ça fonctionne parfaitement.
Merci.

0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
hypothetix Messages postés 191 Date d'inscription dimanche 19 janvier 2020 Statut Membre Dernière intervention 24 avril 2024 6
29 août 2022 à 20:27

Merci aux forces vives qui font vivre ce site.

0
Whismeril Messages postés 19030 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 27 avril 2024 656
29 août 2022 à 20:58

De rien


0
Rejoignez-nous