Bonjour,

J'ai un petit soucis et je souhaite un peu d'aide. Je cherche à réaliser un navigateur d'image très simple dans lequel je n'aurais qu'une fenêtre "scrollable" avec des thumbnails et leurs label.

J'ai éviemment essayé d'utiliser listview mais je craint qu'il soit impossible de modifier l'apparance des items comme je le voudrai. De plus, ajouter 10'000 items prend un temps fou même avec le virtual mode activé.

J'ai alors songé à créer quelque chose d'alternatif avec la création dynamique de panels dans un panel mère. Le problème est que je rencontre deux plantage. L'un quand j'ajoute plus de 1'500 panels et l'autre quand je redimensionne trop rapidement ma fenêtre.

J'ai également un petit souci quand je scroll mon panel mère. La fluidité n'est pas au rendez-vous (elle ne l'est pas non plus d'ailleurs avec listview).

Je vous joins mon programme de test:

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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
 
namespace WindowsFormsApplication1
{
    public class Form1 : Form
    {
        /// <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 Windows Form 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.OneNewPanel = new System.Windows.Forms.Button();
            this.HundredNewPanels = new System.Windows.Forms.Button();
            this.button1 = new System.Windows.Forms.Button();
            this.panel1 = new System.Windows.Forms.Panel();
            this.SuspendLayout();
            // 
            // OneNewPanel
            // 
            this.OneNewPanel.Location = new System.Drawing.Point(12, 12);
            this.OneNewPanel.Name = "OneNewPanel";
            this.OneNewPanel.Size = new System.Drawing.Size(75, 23);
            this.OneNewPanel.TabIndex = 1;
            this.OneNewPanel.Text = "New Panel";
            this.OneNewPanel.UseVisualStyleBackColor = true;
            this.OneNewPanel.Click += new System.EventHandler(this.one_new_panel_Click);
            // 
            // HundredNewPanels
            // 
            this.HundredNewPanels.Location = new System.Drawing.Point(12, 41);
            this.HundredNewPanels.Name = "HundredNewPanels";
            this.HundredNewPanels.Size = new System.Drawing.Size(75, 23);
            this.HundredNewPanels.TabIndex = 2;
            this.HundredNewPanels.Text = "+ 100";
            this.HundredNewPanels.UseVisualStyleBackColor = true;
            this.HundredNewPanels.Click += new System.EventHandler(this.one_hundred_new_panels_Click);
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(12, 70);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 4;
            this.button1.Text = "+ 1000";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.one_thousand_new_panels_Click);
            // 
            // panel1
            // 
            this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.panel1.BackColor = System.Drawing.Color.White;
            this.panel1.Location = new System.Drawing.Point(93, 12);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(627, 481);
            this.panel1.TabIndex = 5;
            this.panel1.Resize += new System.EventHandler(this.panel1_Resize);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(732, 508);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.HundredNewPanels);
            this.Controls.Add(this.OneNewPanel);
            this.DoubleBuffered = true;
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
 
        }
 
        #endregion
 
        private Button OneNewPanel;
        private Button HundredNewPanels;
        private Panel panel1;
        private Button button1;
        public Form1()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }
 
        #region Blabla
        private void one_new_panel_Click(object sender, EventArgs e)
        {
            NewPanel();
        }
 
        private void one_hundred_new_panels_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 100; i++) NewPanel();
        }
 
 
        private void one_thousand_new_panels_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 1000; i++) NewPanel();
        }
        #endregion
 
        int i = 0;
        int new_x = 0;
        int new_y = 0;
 
        int offsetx = 8;
        int offsety = 8;
        int panel_width = 120;
        int panel_height = 120;
        int panel_padding = 10;
 
        int columns = 3;
 
        public void NewPanel()
        {
            int x = (panel_width  + panel_padding) * new_x + offsetx;
            int y = (panel_height + panel_padding) * new_y + offsety;
 
            Panel panel = new Panel();
            panel.BorderStyle = BorderStyle.FixedSingle;
            panel.Location = new Point(x,y);
            panel.Name = "Panel" + i.ToString();
            panel.Size = new Size(120, 120);
            panel.TabIndex = 0;
            panel.BackColor = System.Drawing.Color.Ivory;
            panel1.Controls.Add(panel);
 
            Label label = new Label();
            label.AutoSize = true;
            label.Location = new System.Drawing.Point(10, 10);
            label.Name = "label1"+ i.ToString();
            label.Size = new System.Drawing.Size(35, 13);
            label.TabIndex = 0;
            label.Text = i.ToString();
            panel.Controls.Add(label);
 
            if (new_x < columns)
                new_x++;
            else
            {
                new_x = 0;
                new_y++;
            }
            i++;
        }        
 
        int old_columns = 0;
        private void panel1_Resize(object sender, EventArgs e)
        {
            columns = panel1.Width / (panel_width + panel_padding) -1;
            if (old_columns != columns)
            {
                Redispose();
                old_columns = columns;
            }            
        }
 
        BackgroundWorker job = new BackgroundWorker();
 
        private void Redispose()
        {
            if (job.IsBusy)
            {
                job.CancelAsync();
                while (job.IsBusy);
            }
            changeLocationHandler = new ChangeLocationHandler(ChangeLocation);
            job.WorkerSupportsCancellation = true;
            job.RunWorkerAsync();                        
            job.DoWork += new DoWorkEventHandler(backgroundJob_DoWork);            
        }
 
        private delegate void ChangeLocationHandler(int i, int x, int y);
        ChangeLocationHandler changeLocationHandler;
        private void ChangeLocation(int i, int x, int y)
        {
            panel1.Controls[i].Location = new Point(x, y);
        }
 
        private void backgroundJob_DoWork(object sender, DoWorkEventArgs e)
        {
            new_x = 0;
            new_y = 0;
 
            int i = 0;
            foreach (Panel panel in panel1.Controls)
            {
                int x = (panel_width + panel_padding) * new_x + offsetx;
                int y = (panel_height + panel_padding) * new_y + offsety;
                object[] args = {i, x, y };
                this.Invoke(changeLocationHandler, args);
 
                if (new_x < columns)
                    new_x++;
                else
                {
                    new_x = 0;
                    new_y++;
                }
                i++;
                if ((sender as BackgroundWorker).CancellationPending)
                    return;
            }
        }
    }
}