Bonjour,
J'ai développé un simple UserControl contenant une checkbox. Le but étant d'avoir une checkbox à 3 états avec l'image d'une led qui change de couleur (bleu, rouge et vert).
J'ai donc mes trois images (led_bleue, led_rouge et led_vert) que j'ai affecté à la propriété BackgroundImage de ma checkbox et qui changent en fonction de CheckState.
Ces trois images sont des leds rondes au format png dont la partie inutile est transparente.
J'ai mis toutes les propriétés de couleurs que je connais à :et dans mon constructeur j'ai inséré la ligne :
Code : Sélectionner tout - Visualiser dans une fenêtre à part Color.TransparentEt pourtant, quand j'utilise ce control dans une WindowsForm dont les styles visuels ne sont pas activés,
Code : Sélectionner tout - Visualiser dans une fenêtre à part this.SetStyle(ControleStyles.SupportsTransparentBackColor, true);un carré blanc apparaît autour de mon contrôle lorsque celui-ci est à l'état
Code : Sélectionner tout - Visualiser dans une fenêtre à part //Application.EnableVisualStyles()
et que le pointeur de ma souris n'est pas sur le control, le reste du temps la transparence fonctionne correctement.
Code : Sélectionner tout - Visualiser dans une fenêtre à part CheckState = CheckState.Indeterminate
Est-ce que quelqu'un aurait une idée pour corriger ce problème ? Et, est-ce qu'au moins c'est faisable ?
Merci d'avance,
Benji
PS : voilà le code du designer
et de la class associée
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 namespace Controles_generiques { partial class LedTroisEtats { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Component Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.checkBox = new System.Windows.Forms.CheckBox(); this.SuspendLayout(); // // checkBox // this.checkBox.Appearance = System.Windows.Forms.Appearance.Button; this.checkBox.AutoCheck = false; this.checkBox.BackgroundImage = global::COMAVE.Properties.Resources.led_bleue; this.checkBox.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; this.checkBox.Checked = true; this.checkBox.CheckState = System.Windows.Forms.CheckState.Indeterminate; this.checkBox.Dock = System.Windows.Forms.DockStyle.Fill; this.checkBox.FlatAppearance.BorderColor = System.Drawing.SystemColors.ButtonHighlight; this.checkBox.FlatAppearance.BorderSize = 0; this.checkBox.FlatAppearance.CheckedBackColor = System.Drawing.Color.Transparent; this.checkBox.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Transparent; this.checkBox.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent; this.checkBox.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.checkBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 50F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.checkBox.ForeColor = System.Drawing.Color.Black; this.checkBox.Location = new System.Drawing.Point(0, 0); this.checkBox.Name = "checkBox"; this.checkBox.Size = new System.Drawing.Size(200, 200); this.checkBox.TabIndex = 0; this.checkBox.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; this.checkBox.ThreeState = true; this.checkBox.UseMnemonic = false; this.checkBox.UseVisualStyleBackColor = false; this.checkBox.CheckStateChanged += new System.EventHandler(this.checkBox_CheckStateChanged); this.checkBox.SizeChanged += new System.EventHandler(this.checkBox_SizeChanged); // // LedTroisEtats // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackColor = System.Drawing.Color.Transparent; this.Controls.Add(this.checkBox); this.DoubleBuffered = true; this.Name = "LedTroisEtats"; this.Size = new System.Drawing.Size(200, 200); this.ResumeLayout(false); } #endregion private System.Windows.Forms.CheckBox checkBox; } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102 using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; namespace Controles_generiques { public partial class LedTroisEtats : UserControl { #region Propriétés [Browsable(true), DefaultValue(CheckState.Indeterminate), Description("Indique l'état du contrôle"), Category("Appearance")] public CheckState CheckState { get { return checkBox.CheckState; } set { checkBox.CheckState = value; } } [Browsable(true), DefaultValue(""), Description("Texte affiché"), Category("Appearance")] public string Title { get { return checkBox.Text; } set { checkBox.Text = value; } } [Browsable(true), DefaultValue(true), Description("Autorise le changement d'état par clic"), Category("Behavior")] public bool Clickable { get { return checkBox.AutoCheck; } set { checkBox.AutoCheck = value; } } [Browsable(true), Description("Levé lorsque le contrôle change d'état"), Category("UserControlEvents")] public event EventHandler LedStateChanged { add { checkBox.CheckStateChanged += value; } remove { checkBox.CheckStateChanged -= value; } } [Browsable(true), Description("Levé lors d'un clic sur le contrôle"), Category("UserControlEvents")] public event EventHandler LedTroisEtatsClick { add { checkBox.Click += value; } remove { checkBox.Click -= value; } } #endregion public LedTroisEtats() { InitializeComponent(); this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); } protected override void OnClick(EventArgs e) { base.OnClick(e); } private void checkBox_CheckStateChanged(object sender, EventArgs e) { if (checkBox.CheckState == CheckState.Unchecked) { checkBox.BackgroundImage = Properties.Resources.led_rouge; checkBox.ForeColor = Color.DarkRed; } else if (checkBox.CheckState == CheckState.Checked) { checkBox.BackgroundImage = Properties.Resources.led_verte; checkBox.ForeColor = Color.DarkGreen; } else { checkBox.BackgroundImage = Properties.Resources.led_bleue; checkBox.ForeColor = Color.Black; } } private void checkBox_SizeChanged(object sender, EventArgs e) { int sizeFont; sizeFont = (checkBox.Height > checkBox.Width) ? checkBox.Width / 4 : checkBox.Height / 4; if(sizeFont != 0) checkBox.Font = new Font(checkBox.Font.Name, sizeFont, checkBox.Font.Style); } } }
Partager