E
e
I have an ownerdrawn listbox that paints items in various colors dependant
upon whether or not it has focus, selected, etc. The code that physically
paints the items is in the drawItem event, and it works... the items change
colors accordingly and the end result is what I expected to get. But it
feels sluggish, like it's painting redundant info or too many times, or
something.
So I carefully debugged the drawitem event and discovered that it's being
fired for *every single state change* of any item in the listbox. As an
example, when the listbox did not have focus and item 5 was already selected
in it, I clicked directly onto item 2 and the following 5 calls to drawItem
were all fired in succession (before the mouseDown event could even be
captured):
1. paint item 5, state is selected and focused
2. paint item 5, state is selected but not focused
3. paint item 5, state is not selected and not focused
4. paint item 2, state is selected but not focused
5. paint item 2, state is selected and focused
If the listbox is already focused, the results are similar (sans step 1
which is triggered by the enter event in the listbox)
The sequence makes sense, but I obviously don't need to display every step
of it and the more items I have. I'm essentially painting twice as much as I
need to and I know that's where my performance issue must be.
What's the best practice here? I'm guessing begin & endUpdate, but I'm not
sure how to implement them. These draw events seem to happen so fast
they're outside of the scope of my accessible events to place a beginUpdate
into, and I wouldn't know when to endUpdate either.
upon whether or not it has focus, selected, etc. The code that physically
paints the items is in the drawItem event, and it works... the items change
colors accordingly and the end result is what I expected to get. But it
feels sluggish, like it's painting redundant info or too many times, or
something.
So I carefully debugged the drawitem event and discovered that it's being
fired for *every single state change* of any item in the listbox. As an
example, when the listbox did not have focus and item 5 was already selected
in it, I clicked directly onto item 2 and the following 5 calls to drawItem
were all fired in succession (before the mouseDown event could even be
captured):
1. paint item 5, state is selected and focused
2. paint item 5, state is selected but not focused
3. paint item 5, state is not selected and not focused
4. paint item 2, state is selected but not focused
5. paint item 2, state is selected and focused
If the listbox is already focused, the results are similar (sans step 1
which is triggered by the enter event in the listbox)
The sequence makes sense, but I obviously don't need to display every step
of it and the more items I have. I'm essentially painting twice as much as I
need to and I know that's where my performance issue must be.
What's the best practice here? I'm guessing begin & endUpdate, but I'm not
sure how to implement them. These draw events seem to happen so fast
they're outside of the scope of my accessible events to place a beginUpdate
into, and I wouldn't know when to endUpdate either.