Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   GUI-Design mit VCL / FireMonkey / Common Controls (https://www.delphipraxis.net/18-gui-design-mit-vcl-firemonkey-common-controls/)
-   -   Delphi Sort Arrow in Listview (https://www.delphipraxis.net/116002-sort-arrow-listview.html)

cookie22 21. Jun 2008 14:52


Sort Arrow in Listview
 
hallo,

ich hab folgendes problem, ich habe noch ein altes delphi 5 project und möchte dem listview gerne sort arrows verpassen (in den column headern).

bisher hab ich das so gemacht:

Delphi-Quellcode:
procedure TfrmMain.SetColumnImage(List: TdfsExtListView; Column: Integer; Image: Integer; ShowImage: Boolean);
var
  Align,hHeader: integer;
  HD: HD_ITEM;
begin
  hHeader := SendMessage(List.Handle, LVM_GETHEADER, 0, 0);
  with HD do
  begin
      case List.Columns[Column].Alignment of
        taLeftJustify: Align := HDF_LEFT;
        taCenter:      Align := HDF_CENTER;
        taRightJustify: Align := HDF_RIGHT;
      else
        Align := HDF_LEFT;
      end;


  //hd.hbm := imgUp.Picture.Bitmap.Handle;

      mask := HDI_IMAGE or HDI_FORMAT;

      pszText := PChar(List.Columns[Column].Caption);

      if ShowImage then
        fmt := HDF_STRING or Align or HDF_IMAGE or HDF_BITMAP_ON_RIGHT
      else
        fmt := HDF_STRING or Align;

      iImage := Image;
  end;

  SendMessage(hHeader, HDM_SETITEM, Column, Integer(@HD));

end;
Das funktioniert auch soweit ganz gut, bis auf das der listview die bitmaps aus der imagelist nimmt, die mit dem listview verbunden ist. ich möchte hier selbst irgendein bitmap einfügen, bringe es aber nicht hin das es angezeit wird, jemand eine idee? habe es mit der auskommentierten zeile versucht aber das klappt nicht.

danke im voraus. :)

toms 22. Jun 2008 08:28

Re: Sort Arrow in Listview
 
Hallo,

Ersetze HDI_IMAGE einfach durch HDF_BITMAP.
Ich habe den Code - vielleicht auch interessant für andere - noch etwas erweitert.
Die SortDirection habe ich jedoch nicht vollständig implementiert.
Auch sollte der Code in eine eigene Komponente gepackt werden.


Delphi-Quellcode:
  private
    { Private declarations }
    FSortUpGlyph: TBitmap;
    FSortDownGlyph: TBitmap;
    procedure SetColumnImage(List: TListView; Column, Image: Integer; ShowImage: Boolean);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses
  CommCtrl;

type
  TSortDirection = (sdAscending, sdDescending);

procedure TForm1.SetColumnImage(List: TListView; Column, Image: Integer;
                                 ShowImage: Boolean);
var
  Align,hHeader: integer;
  HD: HD_ITEM;
begin
  hHeader := SendMessage(List.Handle, LVM_GETHEADER, 0, 0);
  with HD do
  begin
    FillChar(HD, SizeOf(HD), 0);

    Mask := (HDI_BITMAP) or (HDI_FORMAT);

    pszText := PChar(List.Columns[Column].Caption);

    case List.Columns[Column].Alignment of
      taLeftJustify: Align := HDF_LEFT;
      taCenter:      Align := HDF_CENTER;
      taRightJustify: Align := HDF_RIGHT;
    else
      Align := HDF_LEFT;
    end;

    if SortDirection = sdAscending then
        Hbm := SortUpGlyph.Handle
      else
        Hbm := SortDownGlyph.Handle;

    if ShowImage then
      fmt := HDF_STRING or HDF_BITMAP or HDF_BITMAP_ON_RIGHT
    else
      fmt := HDF_STRING or Align;

    iImage := Image;
  end;
  SendMessage(hHeader, HDM_SETITEM, Column, Integer(@HD));
end;

function CustomSortProc(Item1, Item2: TListItem; ParamSort: integer): integer; stdcall;
begin
  Result := -lstrcmp(PChar(TListItem(Item1).Caption),
                     PChar(TListItem(Item2).Caption));
end;

procedure TForm1.ListView1ColumnClick(Sender: TObject;
  Column: TListColumn);
var
  i : integer;
begin
  ListView1.CustomSort( @CustomSortProc, Column.Index );
  // This loop displays the icon in the selected column.
  for i := 0 to ListView1.Columns.Count - 1 do
    SetColumnImage(ListView1, i, 0, i = Column.Index);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FSortUpGlyph := TBitmap.Create;
  FSortDownGlyph := TBitmap.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FSortUpGlyph.Free;
  FSortDownGlyph.Free;
end;

cookie22 22. Jun 2008 13:24

Re: Sort Arrow in Listview
 
Jo, klappt! Vielen Dank. :-D

edit:

Leider werden die Bitmaps nicht transparent dargestellt. Jemand ne Idee?

littleDave 22. Jun 2008 13:51

Re: Sort Arrow in Listview
 
@Toms
Bei deinem Quelltext ist Zeile 35 (Hbm := FSortUpGlyph.Handle;) unnötig, da Hbm in Zeile 46-49 nochmal neu zugewiesen wird, oder?

toms 22. Jun 2008 19:11

Re: Sort Arrow in Listview
 
Zitat:

Zitat von littleDave
@Toms
Bei deinem Quelltext ist Zeile 35 (Hbm := FSortUpGlyph.Handle;) unnötig, da Hbm in Zeile 46-49 nochmal neu zugewiesen wird, oder?

Stimmt, habe den Code in Zeile 35 im Quelltext gelöscht.


Alle Zeitangaben in WEZ +1. Es ist jetzt 02:31 Uhr.

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