Delphi-PRAXiS
Seite 1 von 3  1 23      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Listview, CustomDrawSubItem, Text wird schwarz (https://www.delphipraxis.net/163949-listview-customdrawsubitem-text-wird-schwarz.html)

FrankJ28 23. Okt 2011 11:58

Listview, CustomDrawSubItem, Text wird schwarz
 
Hallo,
ich habe mit dem folgenden Code das CustomDrwawSubItem-Event befüllt. Der Testrahmen wird auch prima gezeichnet, die normal darzustellenden Spalten jedoch erhalten einen schwarzen Kasten, wenn text enthalten ist, Das aber auch nur, wenn der Cursor/Maus nach unten bewegt wird. Die Richtung nach oben erfolgt korrekt.

Delphi-Quellcode:
procedure TInfoAuftrDlg.PlanViewCustomDrawSubItem(Sender: TCustomListView;
  Item: TListItem; SubItem: Integer; State: TCustomDrawState;
  var DefaultDraw: Boolean);
var r : TRect;
    i : Integer;
begin
 if SubItem<5
    then begin
          DefaultDraw:=true;
          exit;
         end;
  r := Item.DisplayRect(drBounds);
  for i := 0 to SubItem-1 do begin
    r.Left := r.Left + PlanView.Columns.Items[i].Width;
    r.Right := r.Left + PlanView.Columns.Items[i+1].Width;
  end;
  if SubItem=5
     then PlanView.Canvas.Pen.Color := clRed
     else PlanView.Canvas.Pen.Color := clBlue;

  PlanView.Canvas.Rectangle(r.Left, r.Top, r.Right, r.Bottom);
  DefaultDraw := False;
end;
ListView ist vom Typ vsReport
Jemand eine zündene Idee?
Danke und ciao
Frank

Bummi 23. Okt 2011 13:19

AW: Listview, CustomDrawSubItem, Text wird schwarz
 
ich verwende Listview eigentlich nie, aber so bekomme ich das IMHO gewünschte Ergebnis:


(kann sicher noch optimiert werden)

Delphi-Quellcode:
var r : TRect;
    i : Integer;
    s : String;
begin
  PlanView.Canvas.Brush.Color := clWhite;
  PlanView.Canvas.Brush.Style := bsclear;

 if SubItem<1
    then begin
          DefaultDraw:=true;
          exit;
         end;
  r := Item.DisplayRect(drbounds);


  for i := 0 to SubItem-1 do begin
    r.Left := r.Left + PlanView.Columns.Items[i].Width;
    r.Right := r.Left + PlanView.Columns.Items[i+1].Width;
  end;

  if SubItem=1
     then PlanView.Canvas.Pen.Color := clRed
     else PlanView.Canvas.Pen.Color := clBlue;


  PlanView.Canvas.FillRect(r);

  PlanView.Canvas.Rectangle(r.Left, r.Top, r.Right, r.Bottom);
  r.Left := r.Left +1;
  r.Right := r.Right - 1;
  r.Top := r.Top + 1;
  r.Bottom := r.Bottom - 1;
  if item.SubItems.Count >= SubItem then s := item.SubItems[SubItem-1];
  PlanView.Canvas.TextRect(r,s);
  DefaultDraw := false;
end;

FrankJ28 23. Okt 2011 13:42

AW: Listview, CustomDrawSubItem, Text wird schwarz
 
Liste der Anhänge anzeigen (Anzahl: 1)
Hallo Thomas,
danke für deinen Einsatz, ich habe mich nur etwas blöd ausgedrückt. Nicht der Text in dem selbstgemalten SubItem, sondern alle Anderen werden schwarz. Un das nur, wenn Cursor oder Maus auf dem Weg nach unten sind, nach oben ist alles sichtbar.
siehe Bild
Anhang 35420
Noch eine Idee?
Ciao
Frank

DCoderHH 24. Mai 2018 09:04

AW: Listview, CustomDrawSubItem, Text wird schwarz
 
Genau das selbe Problem habe ich jetzt auch mit Delphi Tokyo: Wenn Items ausgewählt werden, wird der Text schwarz, wie auf dem Bildschirmfoto im letzten Post zu sehen. Hat da jemand in den letzten Jahren schon eine Lösung gefunden?

Whookie 24. Mai 2018 09:57

AW: Listview, CustomDrawSubItem, Text wird schwarz
 
Liste der Anhänge anzeigen (Anzahl: 1)
Habe das mit 10.2.3 als Minimalbeispiel umgesetzt und es funktioniert bei mir ohne schwarze Felder...

KodeZwerg 24. Mai 2018 10:00

AW: Listview, CustomDrawSubItem, Text wird schwarz
 
Hier wurde das
Zitat:

This is a workaround for the defective behavior rather than being an answer to the question if there's a bug in the VCL, and a few thoughts.

The workaround is to set the background mode of the device context assigned by the common control for item painting cyle to transparent after carrying out custom drawing:

Delphi-Quellcode:
procedure TForm1.ListViewCustomDrawSubItem(Sender: TCustomListView;
  Item: TListItem; SubItem: Integer; State: TCustomDrawState;
  var DefaultDraw: Boolean);
var
  R: TRect;
begin
  if not [CustomDrawing] then // <- If we're not gonna do anything do not
    Exit;                     //    fiddle with the DC in any way

  DefaultDraw := False;
  ListView_GetSubItemRect(Sender.Handle, Item.Index, SubItem, LVIR_BOUNDS, @R);
  Sender.Canvas.MoveTo(R.Left, R.Top);
  Sender.Canvas.LineTo(R.Right-1, R.Bottom-1);

  SetBkMode(Sender.Canvas.Handle, TRANSPARENT); // <- will effect the next [sub]item
end;

In an [sub]item paint cycle, the painting is always done in a top-down fashion, items having a lower index are sent NM_CUSTOMDRAW notification prior to ones with higher indexes. When the mouse is moved from one row to another, two rows need to be re-drawn - the one loosing the hot state, and the one gaining it. It would seem, when custom drawing is in-effect, drawing the row that's loosing the hot-state leaves the DC in an undesirable state. This is not a problem when moving the mouse upwards, because that item gets drawn last.

Custom drawing ListView and TreeView controls are different than custom drawing other controls and somewhat complicated (see: Custom Draw With List-View and Tree-View Controls). But you have full control over the entire process. The code in the NM_CUSTOMDRAW case of TCustomListView.CNNotify in 'comctrls.pas' of the VCL is equally complicated. But despite being provided a bunch of custom drawing handlers (half of them being advanced), you have no control over what the VCL does. For instance you can't return the CDRF_xxx you'd like or you can't set the clrTextBk you want. My biased opinion is that, there's a bug/design issue in the Delphi list view control, but I have nothing more concrete than an intuition as in finding a workaround.
als Lösung gepostet.

Gerade seh ich Rot und eine Whookie Lösung... ich schick die Info trotzdem ab:)

DCoderHH 24. Mai 2018 12:23

AW: Listview, CustomDrawSubItem, Text wird schwarz
 
Danke, aber bei beiden Lösungen ist das Problem, dass der Text in der der Spalte, die rechts neben der Owner-Draw-Spalte liegt, pixelig und fett wird. Ein manuelles setzten der Font schafft keine Abhilfe. Die Schrift ist immer pixelig und fett...

KodeZwerg 24. Mai 2018 12:31

AW: Listview, CustomDrawSubItem, Text wird schwarz
 
Poste noch mal Dein Projekt damit es nachvollziehen kann.

DCoderHH 24. Mai 2018 12:50

AW: Listview, CustomDrawSubItem, Text wird schwarz
 
Liste der Anhänge anzeigen (Anzahl: 2)
Hier das Projekt zum Testen und ein Screenshot mit dem Fehler (Text wird pixelig und fett)

Luckie 24. Mai 2018 14:03

AW: Listview, CustomDrawSubItem, Text wird schwarz
 
Das ist nicht pixelig und fett, das ist eine andere Schriftart.


Alle Zeitangaben in WEZ +1. Es ist jetzt 00:01 Uhr.
Seite 1 von 3  1 23      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz