Top.Mail.Ru
Мастера DELPHI, Delphi programming community Титульная страница Поиск, карта сайта Написать письмо 
| Новости | Поиск | FAQ | Статьи | Книги | Новости VCL |
| Форумы ЧАТ | Орешник | KOL и MCK |
Форумы -» Основная («Начинающим»)/ Базы / WinAPI / Компоненты / Сети / Media / Игры / Corba и COM / KOL / FreePascal / .Net / Прочее / RSDN.ORG

« предыдущая ветвь | форум | следующая ветвь »

Problem ListBox.OnDrawItem [Delphi, Windows]


Dodfr ©   (2006-02-02 13:30:00) 

Hi,

I have problem in OnDrawItem with DrawIcon that seems not draw correctly 100% time as it should but the TextOut is drawed correcly.

Sometime the icon do not appear at all, sometime it is not fully drawed, the worst is when I move other window over the form owning the ListBox but also when I use mdvToolTip display inside the ListBox.

Any idea ?

Here is part of  code I use :

   if (odsSelected in ItemState) then
   begin
     PControl(sender).Canvas.Brush.Color:=clhighlight;
     PControl(sender).Canvas.font.color:=clwhite;
   end
   else
   begin
     PControl(sender).Canvas.Brush.Color:=clwhite;
     PControl(sender).Canvas.font.color:=0;
   end;
   PControl(sender).Canvas.FillRect(Rect);
   PControl(sender).Canvas.Pen.Color:=0;

   ImageList1.Draw(3,DC,Rect.Left+2,Rect.Top);

   PControl(sender).Canvas.TextOut(Rect.Left+22,Rect.Top+1,ListBoxProfils.Items[ItemIdx]);


Dodfr   (2006-02-05 21:18:00) [1]

Many thanks everybody for this complete explanation, I hope it will be usefull to other guys because this kind of information that take too much time to look for thru MSDN documentation.

Regards


Vladimir Kladov   (2006-02-05 10:12:00) [2]

Too long to discuss. Briefly: Canvas gets correct DC while WM_PAINT processing, on a time while it is handling. But OnDrawItem goes another way. Anyway DC passed to the handler is correct DC so your last solve is enough.


Thaddy   (2006-02-04 16:20:00) [3]

Mixed :))

Brilliant!


ECM   (2006-02-04 13:44:00) [4]


> I don't understand why it avoid the .Draw to work all the
> time and not the .TextOut.

Because here "DC" it is not equal " Canvas. Handle ".
In occasion of " odaEntire in DrawAction " I was mistaken, excuse. "odaEntire" is present practically always and "FillRect" it was never caused.

There are three variants of the decision of a problem:
1) To use exclusively DC:

 if (odsSelected in ItemState) then  begin
    hbr := GetSysColorBrush(COLOR_HIGHLIGHT);
    SetTextColor(DC,Color2Rgb(clwhite));
    SetBkColor(DC,GetSysColor(COLOR_HIGHLIGHT));
 end else begin
    hbr := GetSysColorBrush(COLOR_WINDOW);
    SetTextColor(DC,Color2Rgb(clBlack));
    SetBkColor(DC,GetSysColor(COLOR_WINDOW));
 end;
 FillRect( DC, Rect, hbr);
 ImageList1.Draw(3,DC,Rect.Left+2,Rect.Top);
 TextOut(DC,Rect.Left+22,Rect.Top+1,@ListBox1.Items[ItemIdx][1],Length(ListBox1.Items[ItemIdx]));


2) To use exclusively Canvas:

 
 if (odsSelected in ItemState) then  begin
    PControl(sender).Canvas.Brush.Color:=clhighlight;
    PControl(sender).Canvas.font.color:=clwhite;
 end else begin
    PControl(sender).Canvas.Brush.Color:=clwhite;
    PControl(sender).Canvas.font.color:=0;
 end;
 PControl(sender).Canvas.FillRect(Rect);
 ImageList1.Draw(3,PControl(sender).Canvas.Handle,Rect.Left+2,Rect.Top);
 PControl(sender).Canvas.TextOut(Rect.Left+22,Rect.Top+1,ListBox1.Items[ItemIdx]);


3) Mixed :))
But, in the beginning procedures to make the following:
  PControl(Sender).Canvas.Handle := DC;

The mechanism of carry drawn on " Canvas. Handle " on DC in case of when " Canvas. Handle <> DC " I up to the end have not understood. Probably Vladimir will help to understand a mechanism?


Dodfr   (2006-02-03 01:13:00) [5]

Thanx a lot both solutions works !

But, I would like to understand why it did not work in order to avoid future troubles so if you could answer these questions :

Why DC parameter was wrong for .Draw as it was the one passed to the Event and it is a DC that should be passed to function ?

Why FillRect had to be drawn only if not odaEntire ? I don't understand why it avoid the .Draw to work all the time and not the .TextOut.

Thank you for your answer ECM.


ECM   (2006-02-02 21:19:00) [6]

P.S.
or just:
//  PControl(sender).Canvas.FillRect(Rect);
//  PControl(sender).Canvas.Pen.Color:=0;


ECM   (2006-02-02 21:17:00) [7]

try

1)
  if not (odaEntire in DrawAction) thenPControl(sender).Canvas.FillRect(Rect);

or
2)
  ImageList1.Draw(3, PControl(sender).Canvas.Handle,Rect.Left+2,Rect.Top);


Наверх

Титульная страница Поиск, карта сайта Написать письмо