drawItem event

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.
 
F

Fergus Cooney

Hi e,

One solution, which I haven't used but have heard of, is to use a timer.
This will add a delay to your painting, but hopefully not one noticeable to
the User.

Having not done it, I'm not sure whether you reset the Timer each time
your DrawItem event occurs or whether you set it off once and then draw when
it goes off. In the latter case, you may need a variable to keep track of
whether it's been started or not. There doesn't seem to be a way to ask the
Timer how long it's got to go.

Regards,
Fergus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top