Bonjour,

Je me suis récemment mis au développement sous windows mobile (j'ai déjà de l'expérience en développement vb .net sur pc), et je me retrouve un peu embêté par la gestion de la mémoire windows mobile :s

Voila mon code :

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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Threading
Imports System.Xml

Namespace DodgerCodeSnippets
    ''' <summary>
    ''' Description résumée de FormMain.
    ''' </summary>
    Public Class FormMain
        Inherits System.Windows.Forms.Form

        Private lst As New List(Of String)
        Private offScreenBitmap As Bitmap
        Private offScreenGraphics As Graphics
        ' Représenta la zone dans laquelle on dessine les items
        Private onScreenGraphics As Graphics
        ' Représenta la zone réellement affichée
        Private m_bSelectionDone As Boolean = False

        Private m_iScreenWidth As Integer = 480
        ' Taille de l'écran
        Private m_iScreenHeight As Integer = 588

        Private m_iItemsCount As Integer = Nothing
        ' Nombre d'Item dans la liste
        Private m_iItemheight As Integer = 80
        ' auteur d'un item
        Private m_iStripWidth As Integer = 480
        Private m_iStripHeight As Integer = 588
        ' Hauteur totale de la liste
        Private m_iStripPosY As Integer = 1
        Private m_iOldStripPosY As Integer = -10

        Private m_iMoveStep As Integer = 1
        ' Déplacement en nombre de pixels à chaque boucle
        Private m_iSens As Integer = 0
        ' 0,+1,-1
        Private m_iTouchStartY As Integer = 0
        ' Position en pixel de l'endroit où l'on a posé le doigt
        Private m_iTouchEndY As Integer = 0
        ' Position en pixel de l'endroit où l'on a relevé le doigt
        Private m_iSelectedIndexItem As Integer = -1
        ' Numéro de l'Item sélectionné
        Private m_iPixelPerScroll As Integer = 0
        ' De combien de pixel doit on scrollé après un mouvement de doigt sur l'écran
        Private m_iSleep As Integer = 3
        ' nombre de milliseconde d'attente dans la boucle
        Public Shared OddCellBackGroundColor As System.Drawing.Color = System.Drawing.Color.Transparent
        Public Shared EvenCellBackGroundColor As System.Drawing.Color = System.Drawing.Color.Transparent
        Public Shared SelectedCellBackGroundColor As System.Drawing.Color = System.Drawing.Color.Transparent
        Public Shared CellBorderColor As System.Drawing.Color = System.Drawing.Color.LightGray

        Public Sub New()
            parsexml()
            m_iItemsCount = lst.Count
            InitScreens()
            DoLoop()
        End Sub

        Private Sub InitScreens()
            Me.Visible = True
            Me.WindowState = FormWindowState.Normal
            m_iStripHeight = m_iItemsCount * m_iItemheight
            ' Creation d'un ecran virtuel plus grand que l'écran du PDA qui contient toute la liste
            offScreenBitmap = New Bitmap(m_iStripWidth, m_iStripHeight)
            offScreenGraphics = Graphics.FromImage(offScreenBitmap)
            offScreenGraphics.Clear(Color.Black)
            onScreenGraphics = Me.CreateGraphics()
        End Sub


        Private Sub DoLoop()
            ' C'est ici que l'on dessine les Items
            For iItem As Integer = 0 To m_iItemsCount - 1
                DrawItem(iItem)
            Next iItem

            onScreenGraphics.Clear(Color.Black)
            onScreenGraphics.DrawImage(offScreenBitmap, 0, 0, New Rectangle(0, 0, 480, 588), GraphicsUnit.Pixel)
            Application.DoEvents()

            Render()

            onScreenGraphics.Dispose()
            Application.DoEvents()
            MessageBox.Show("Vous avez sélectionné l'item : " + m_iSelectedIndexItem, "Info")
            Application.[Exit]()
        End Sub

        ''' <summary>
        ''' Nettoyage des ressources utilisées.
        ''' </summary>
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            MyBase.Dispose(disposing)
        End Sub

        ''' <summary>
        ''' Point d'entrée principal de l'application.
        ''' </summary>

        Public Shared Sub Main()
            Application.Run(New FormMain())
        End Sub

        ''' <summary>
        ''' Boucle principale de rendu de la liste
        ''' </summary>
        Protected Overridable Sub Render()
            ' distance parcourrue en pixel depuis le dernier mouvement de doigt
            Dim iPixelScrollCount As Integer = 0
            While Not m_bSelectionDone
                Try
                    ' On bloque le scroll quand on arrive en bas
                    If (m_iSens = 1) AndAlso (m_iStripPosY + m_iScreenHeight >= m_iStripHeight) Then
                        m_iSens = 0
                        iPixelScrollCount = 0
                    End If
                    ' On bloque le scroll quand on arrive en haut
                    If (m_iSens = -1) AndAlso (m_iStripPosY <= 0) Then
                        m_iSens = 0
                        iPixelScrollCount = 0
                    End If

                    If m_iSens <> 0 Then
                        iPixelScrollCount += 1
                        m_iStripPosY += m_iMoveStep * m_iSens
                    End If

                    ' Si on a scrollé du nombre de pixel que l'on souhaitait
                    ' On s'arrete
                    If iPixelScrollCount > m_iPixelPerScroll Then
                        m_iSens = 0
                        iPixelScrollCount = 0
                    End If

                    ' Si on a pas bougé, inutile de re-dessiner
                    If m_iOldStripPosY <> m_iStripPosY Then
                        onScreenGraphics.DrawImage(offScreenBitmap, 0, 0, New Rectangle(0, m_iStripPosY, 480, 588), GraphicsUnit.Pixel)
                        m_iOldStripPosY = m_iStripPosY
                    End If
                    Application.DoEvents()
                    ' On laisse le temps au PDA de souffler
                    ' On ralentit quelque peu le scrolling
                    Thread.Sleep(m_iSleep)
                Catch exp As Exception
                    MessageBox.Show("Oups il y a un problème dans Render :" + exp.ToString())
                End Try
            End While
        End Sub

        ''' <summary>
        ''' Dessine un Item ( élément ) de la liste à la position indiquée
        ''' </summary>
        ''' <param name="iIndex"></param>
        Private Sub DrawItem(ByVal iIndex As Integer)
            Dim font As Font
            Dim text As String

            Dim blackBrush As Brush = New SolidBrush(Color.Black)
            Dim brushOdd As Brush = New SolidBrush(OddCellBackGroundColor)
            Dim brushEven As Brush = New SolidBrush(EvenCellBackGroundColor)
            Dim brush As Brush = Nothing
            font = New Font(FontFamily.GenericSansSerif, 5, FontStyle.Bold)
            Dim pen As New Pen(CellBorderColor)
           ' On intervertit les couleurs pour donner un coté sympa
            If (iIndex Mod 2) = 0 Then
                brush = brushOdd
            Else
                brush = brushEven
            End If
            offScreenGraphics.FillRectangle(brush, 0, iIndex * m_iItemheight, m_iScreenWidth - 1, m_iItemheight)
            offScreenGraphics.DrawRectangle(pen, 0, iIndex * m_iItemheight, m_iScreenWidth, m_iItemheight)
            ' C'est ici que vous insérez le texte que vous souhaitez
            text = lst.Item(iIndex)
            offScreenGraphics.DrawString(text, font, blackBrush, 10, (iIndex * m_iItemheight) + 10)
        End Sub

        ''' <summary>
        ''' Dessine un Item sélectionné à la position indiquée
        ''' </summary>
        ''' <param name="iIndex"></param>
        Private Sub DrawSelectedItem(ByVal iIndex As Integer)

            Dim text As String
            Dim font As Font
            Dim pen As New Pen(CellBorderColor)
            Dim blackBrush As Brush = New SolidBrush(Color.Black)
            font = New Font(FontFamily.GenericSansSerif, 5, FontStyle.Bold)
            ' On dessine le rectangle plein
            offScreenGraphics.FillRectangle(New SolidBrush(SelectedCellBackGroundColor), 0, iIndex * m_iItemheight, m_iScreenWidth - 1, m_iItemheight)
            ' avec un petit contour gris
            offScreenGraphics.DrawRectangle(pen, 0, iIndex * m_iItemheight, m_iScreenWidth, m_iItemheight)
            text = lst.Item(iIndex)
            ' On affiche le texte
            offScreenGraphics.DrawString(text, font, blackBrush, 10, (iIndex * m_iItemheight) + 10)
            m_iOldStripPosY = -1
            ' Force à redessiner ( voir méthode Render() )
        End Sub

        ' Gestion du scroll au doigt et à l'oeuil ... heu non au stylet !
        Private Sub FormMain_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
            m_iTouchEndY = e.Y
            If m_iSens = 0 Then
                If m_iTouchStartY - m_iTouchEndY > 0 Then
                    m_iSens = 1
                Else
                    m_iSens = -1
                End If
                Dim iDelta As Integer = Math.Abs(m_iTouchStartY - m_iTouchEndY)
                ' Plus le doigt a glissé sur une grande distance,
                ' plus on scroll loin
                If iDelta > 160 Then
                    m_iPixelPerScroll = 588
                    m_iSleep = 0
                ElseIf (iDelta > 100) AndAlso (iDelta < 160) Then
                    m_iPixelPerScroll = 294
                    m_iSleep = 2
                ElseIf (iDelta < 100) AndAlso (iDelta > 20) Then
                    m_iPixelPerScroll = 147
                    m_iSleep = 4
                Else
                    ' On commence par effacer le précédent item sélectionné si il existe
                    Dim iPreviousSelectionIndex As Integer = m_iSelectedIndexItem
                    If m_iSelectedIndexItem > -1 Then
                        DrawItem(iPreviousSelectionIndex)
                    End If
                    m_iSens = 0
                    m_iSelectedIndexItem = (m_iStripPosY + m_iTouchEndY) / m_iItemheight
                    DrawSelectedItem(m_iSelectedIndexItem)
                    If iPreviousSelectionIndex = m_iSelectedIndexItem Then
                        m_bSelectionDone = True
                    End If
                End If

                m_iTouchStartY = 0
                m_iTouchEndY = 0
            Else
                ' si on touche l'écran pendant le scroll, ça l'arrete
                m_iSens = 0
            End If
        End Sub

        Private Sub FormMain_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
            m_iTouchStartY = e.Y
        End Sub

        Private Sub InitializeComponent()
            Me.SuspendLayout()
            '
            'FormMain
            '
            Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit
            Me.ClientSize = New System.Drawing.Size(480, 588)
            Me.Location = New System.Drawing.Point(0, 52)
            Me.Name = "FormMain"
            Me.Text = "iListBox"
            Me.ResumeLayout(False)

        End Sub
        Public Function parsexml() As String

            Dim m_xmlr As XmlTextReader
            'Create the XML Reader

            m_xmlr = New XmlTextReader("http://yp.shoutcast.com/sbin/newxml.phtml")
            'Disable whitespace so that you don't have to read over whitespaces

            m_xmlr.WhitespaceHandling = WhitespaceHandling.None
            'read the xml declaration and advance to family tag

            m_xmlr.Read()
            'read the family tag

            m_xmlr.Read()
            'Load the Loop

            While Not m_xmlr.EOF
                'Go to the name tag

                m_xmlr.Read()
                'if not start element exit while loop

                If Not m_xmlr.IsStartElement() Then
                    Exit While
                End If
                'Get the Gender Attribute Value

                Dim nameAttribute As String = m_xmlr.GetAttribute("name")
                'Read elements firstname and lastname

                m_xmlr.Read()

                lst.Add(nameAttribute)

            End While
            'close the reader

            m_xmlr.Close()

        End Function
    End Class
End Namespace
Ici la taille de l'item dans ma listbox est de 80px, le nombre d'item était, au moment du test, de 386, sois un écran virtuel de 30880 * 480 pixels (voir ligne en rouge dans le code). Ça semble être un peu lourd à gérer pour wm6 qui, si j'ai bien compris, ne supporte que des process de 32mo.

Ma listbox s'affiche parfaitement si je prends 10 comme taille d'item, le problème est que c'est beaucoup trop petit pour être utilisé au doigt, les items étant plus petit que ceux de la listbox d'origine :p

Comment réduire cette consommation de mémoire ou partager en plusieurs processus ce bout de code ?

Merci d'avance