Bonjour à tous,

je débute en C# et je programme de petits add-in destinés à Autocad. Souhaitant créer une combobox identique à celle d'autocad, j'ai trouvé ce code qui permet de la faire en WPF : http://adndevblog.typepad.com/autoca...-controls.html

Seul la partie "Couleur" m'intéresse mais ce n'est pas là le problème !! Je n'arrive pas à réutiliser le code avec une combobox Winform. Toute la base de mon Add-in est en Winform et je ne souhaite pas passer en WPF (pour le moment).

Voici le code de la combobox :
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
 
 
// Class that represents the collection of AcadColor
public class AcadColors : ObservableCollection<AcadColor>
{
    public AcadColors()
        : base()
    {
        // List of colors to show in the combo box
        // along with their R,G,B values
        Add(new AcadColor("ByLayer", 255, 255, 255));
        Add(new AcadColor("ByBlock", 255, 255, 255));
        Add(new AcadColor("Red", 255, 0, 0));
        Add(new AcadColor("Yellow", 255, 255, 0));
        Add(new AcadColor("Green", 0, 255, 0));
        Add(new AcadColor("Cyan", 0, 255, 255));
        Add(new AcadColor("Blue", 0, 0, 255));
        Add(new AcadColor("Magenta", 255, 0, 255));
        Add(new AcadColor("White", 255, 255, 255));
        Add(new AcadColor("Select Colors...", 255, 255, 255));
    }
 
     // To check if a color with a given name
    // exists in this collection
    public bool Contains(String name)
    {
        for(int i = 0; i < Count; i++)
        {
            if(this[i].Name.Equals(name))
                return true;
        }
        return false;
    }
}
 
 // Class that represents a single color
public class AcadColor
{
    // Color Name
    public String Name
    {
        get;
        set;
    }
 
    // Brush to bind the label that displays the color
    public System.Windows.Media.SolidColorBrush ColorBrush
    {
        get
        {
            SolidColorBrush brush
                = new SolidColorBrush(
                               Color.FromRgb(Red, Green, Blue));
            return brush; 
        }
    }
 
    // Red byte
    public byte Red
    {
        get;
        set;
    }
 
    // Green byte
    public byte Green
    {
        get;
        set;
    }
 
    // Blue byte
    public byte Blue
    {
        get;
        set;
    }
 
    // The Color combo displays a "Select color..." option
    // In that case, we do not want to show the color label
 
    public String IsVisible
    {
        get
        {
            if(Name.Contains("Select"))
                return "Collapsed";
            return "Visible";
        }
    }
 
    public AcadColor(String name, byte R, byte G, byte B)
    {
        Name = name;
        Red = R;
        Green = G;
        Blue = B;
    }
 
    public AcadColor()
    {
        Name = String.Empty;
        Red = 255;
        Green = 255;
        Blue = 255;
    }
}
 
// View Model for the Color Combo box to bind to
public class ColorViewModel : INotifyPropertyChanged
{
    // collection of colors that we will display
    AcadColors _myAcadColors = new AcadColors();
 
    // Flag to know if we are displaying the color selection dialog
    bool _showingColorDlg = false;
 
    public ColorViewModel()
    {
    }
 
    // Bound to the combo box source
    public AcadColors MyAcadColors
    {
        get
        {
            return _myAcadColors;
        }
    }
 
    // Default value to show
    private string _colorString = "Red";
 
    // Bound to the current selection in the combo
    public string ColorString
    {
        get
        {
            return _colorString;
        }
        set
        {
            if (_colorString != value)
            {
                String colorName = value;
 
                //The Color combo displays a "Select color..."
                // option. If that is the option chosen,
                // show the color pick dialog
                if (! _showingColorDlg && value.Contains("Select"))
                {// Show the color pick dialog
                    _showingColorDlg = true;
                    colorName = ShowColorDlg();
                }
                _showingColorDlg = false;
                _colorString = colorName;
                NotifyPropertyChanged("ColorString");
            }
        }
    }
 
    #region INotifyPropertyChanged Members
    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this,
                    new PropertyChangedEventArgs(propertyName));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    #endregion
 
    // Displays the AutoCAD color pick dialog
    // Method returns the name to be displayed in the combo box
    public String ShowColorDlg()
    {
        Document  doc
                = Application.DocumentManager.MdiActiveDocument;
        Editor ed = doc.Editor;
        Autodesk.AutoCAD.Windows.ColorDialog dlg
                = new Autodesk.AutoCAD.Windows.ColorDialog();
        if (dlg.ShowDialog() !=  System.Windows.Forms.DialogResult.OK)
        {
            return String.Empty;
        }
 
        Autodesk.AutoCAD.Colors.Color clr = dlg.Color;
        if (! clr.IsByAci)
        {
             if (clr.IsByLayer)
            {
                return "ByLayer";
            }
            else if (clr.IsByBlock)
            {
                return "ByBlock";
            }
            else
            {
                String colorName
                    = String.Format("{0},{1},{2}",
                                clr.Red, clr.Green, clr.Blue);
 
                if(_myAcadColors.Contains(colorName) == false)
                    _myAcadColors.Insert(
                        _myAcadColors.Count - 1,
                        new AcadColor(
                                        colorName,
                                        clr.Red,
                                        clr.Green,
                                        clr.Blue
                                     ));
                return colorName;
            }
        }
        else
        {
            // Get the AutoCAD color index
            short colIndex = clr.ColorIndex;
 
            System.Byte byt = System.Convert.ToByte(colIndex);
            int rgb
                = Autodesk.AutoCAD.Colors.EntityColor.LookUpRgb(byt);
            long b = (rgb & 0xffL);
            long g = (rgb & 0xff00L) >> 8;
            long r = rgb >> 16;
 
            String colorName = String.Format("Color {0}", colIndex);
 
            if (_myAcadColors.Contains(colorName) == false)
                _myAcadColors.Insert(
                    _myAcadColors.Count - 1,
                    new AcadColor(
                                    colorName,
                                    (byte)r, (byte)g, (byte)b));
 
            return colorName;
        }
    }
}
Code WPF :
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
 
<ComboBox   DataContext="{Binding ColorDataContext}"
            Name="MyColorCombo"
            Margin="2" Height="30" Width="250"
            ItemsSource="{Binding MyAcadColors}"
            SelectedValuePath="Name"
            SelectedValue="{Binding ColorString}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" Margin="2">
                <Label  Visibility="{Binding IsVisible}"
                        Background="{Binding ColorBrush}"
                        Width="15" Height="15"
                        BorderBrush="Black" BorderThickness="1"/>
                <TextBlock  Visibility="{Binding IsVisible}"
                            Text=" " Width="3"/>
                <TextBlock Text="{Binding Name}" Width="100"/>
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
Merci d'avance pour votre aide

Salutations !