2015-12-16 1 views
1
public partial class Form1 : Form 
{ 
    CheckBoxExt checkBox; 
    public Form1() 
    { 
     InitializeComponent(); 
     checkBox = new CheckBoxExt(); 
     this.Controls.Add(checkBox); 
    } 
} 
public class CheckBoxExt : CheckBox 
{ 
    public CheckBoxExt() 
    { 
     this.Size = new Size(20, 20); 
     this.Location = new Point(200, 200); 
     this.Appearance = Appearance.Button; 
    } 
    protected override void OnPaint(PaintEventArgs e) 
    { 
     base.OnPaint(e); 
     var point = this.PointToScreen(new Point(0, 0)); 
     e.Graphics.DrawLine(new Pen(ColorTranslator.FromHtml("#666666"), 2), new Point(point.X + 5, point.Y + 10), new Point(point.X + 15, point.Y + 10)); 
     if (this.Checked) 
      e.Graphics.DrawLine(new Pen(ColorTranslator.FromHtml("#666666"), 2), new Point(point.X + 10, point.Y + 5), new Point(point.X + 10, point.Y + 15)); 
    } 
    /// <summary> 
    /// to remove the focus dotted border over the control 
    /// </summary> 
    protected override bool ShowFocusCues 
    { 
     get 
     { 
      return false; 
     } 
    } 
} 

Voici mon code, j'ai personnalisé CheckBox et le définit Apparence en tant que bouton afin qu'il agisse en tant que bouton bascule.Comment dessiner un plus/moins sur un bouton bascule basé sur Toggle Etat dans WinForms

J'ai besoin de dessiner une petite ligne horizontale qui s'affichera comme un signe moins sur le bouton, et dessiner une petite ligne horizontale et verticale qui s'affichera comme bouton Plus. Par conséquent, il faut basculée, si elle est dans un état contrôlé, plus besoin d'être démontré et si elle est décochée besoin moins à afficher sur le bouton à bascule

Merci à l'avance

+0

Quelle est votre question? –

+0

PointToScreen() est faux, il suffit de l'enlever. –

Répondre

0

j'utiliser un contrôle imageList et peuplé avec plus et moins images respectivement avec la résolution désirée.

La propriété BackgroundImage de la commande peut être utilisée pour définir les images "+" et "-".

Et, n'oubliez pas d'enregistrer CheckedChanged événement du contrôle pour basculer le BackgroundImage du contrôle.

Quelque chose comme ceci par exemple -

public Form1() 
    { 
     InitializeComponent(); 

     var checkBox = new CheckBoxExt(); 

     //My imageList contain two images, for "+" and "-" 

     //Register chacked changed event for the control 
     checkBox.CheckedChanged += checkBox_CheckedChanged; 

     //Set the initial image as "-" 
     checkBox.BackgroundImage = this.imageList1.Images[1]; 

     this.Controls.Add(checkBox); 
    } 

    void checkBox_CheckedChanged(object sender, EventArgs e) 
    { 
     if (((CheckBox)sender).Checked) 
     { 
      ((CheckBox)sender).BackgroundImage = this.imageList1.Images[0]; 
     } 
     else 
     { 
      ((CheckBox)sender).BackgroundImage = this.imageList1.Images[1]; 
     } 
    }