?
Solved

TListView -> Adding bitmaps to column header

Posted on 2006-04-10
3
Medium Priority
?
292 Views
Last Modified: 2013-12-03
Hello,

my task is:

1. Add two bitmaps to headers of columns of TListView.
One bitmap goes to the left, another to the right.

2. User clicks on the left one- I catch this event.

3. User clicks on the right one- I catch this event.

Question:

How can I do it?

Jack :-)
0
Comment
Question by:Actia
  • 2
3 Comments
 
LVL 17

Assisted Solution

by:TheRealLoki
TheRealLoki earned 1500 total points
ID: 16422242
do you mean 1 column has a "left" and 1 column has a "right"
or do you mean 1 column has both ?

if it is 1 image per column, you can easily do it by doing the following :-
Set Listview1.SmallImages to your TImageList (which has a left and right image) in positions 0 and 1
Set the Columns imageindex to 0 or 1 (e.g. listview1.columns[0].imageindex := 1; //right

make sure all items have imageindex set to -1 (so they do not show anything)
alternatively you could have a blank image in position 0, and use 1 ofr left, 2 for right

You can get the header click by using the listview1.OnColumnClick event
it says which column (column.index) was clicked.

If you wish to use 2 images in the same column, you will need to draw them in there manually. Here is some code I use to draw the "sort" icons manually (instead of the above method)
You can use this routine i wrote to draw your 2 images, change it to suit your needs exactly.
You can also use a similar routine like this to determine where exactly the user clicked (by using the mouse x and y)

        leftpos := 0;

        for i := 0 to pred(ListView1.Columns.Count) do
        begin
            if i = sortedcolumnindex then
            begin
                iSortImage.Left := ListView1.Left +
                  leftpos - GetScrollPos(ListView1.Handle, SB_HORZ) +
                  (ListView1.Columns[i].Width div 2) - (iSortImage.Picture.Bitmap.Width div 2);

                if ListView1.Tag >= st_ReverseOrder then
                begin
//                    ListView1.Columns[i].ImageIndex := 1;
                    tempbmp := TBitmap.Create;
                    try
                        ilSortImages.GetBitmap(1, tempbmp);
                        iSortImage.Picture.Assign(tempbmp);
                    finally
                        tempbmp.free;
                    end;
                end
                else
                begin
//                    ListView1.Columns[i].ImageIndex := 0;
                    tempbmp := TBitmap.Create;
                    try
                        ilSortImages.GetBitmap(0, tempbmp);
                        iSortImage.Picture.Assign(tempbmp);
                    finally
                        tempbmp.free;
                    end;
                end;
            end
            else
              ListView1.Columns[i].ImageIndex := -1;
            leftpos := leftpos + ListView1.Columns[i].Width;
        end;
0
 

Author Comment

by:Actia
ID: 16424566
So you suggest to draw bitmaps above ListView...
I tried to do it this way and that's what I found:

- TImage is always behind TListView, I solved this problem by adding TImage to TPanel

but I didn't find the way to stop user from resizing columns- it spoils the whole effort.

Jack
0
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 1500 total points
ID: 16430968
ok, i've written what I think you want, let me know if it's not quite right, sorry for teh size of the "blank" image, but i dont have time to make a smaller one :-)
To stop a user from resizing a column, set the columns min and max width properties

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ImgList, ExtCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    ListView1: TListView;
    pLeftArrow: TPanel;
    iLeftArrow: TImage;
    pRightArrow: TPanel;
    iRightArrow: TImage;
    ImageList1: TImageList;
    procedure ListView1CustomDraw(Sender: TCustomListView;
      const ARect: TRect; var DefaultDraw: Boolean);
    procedure ListView1ColumnClick(Sender: TObject; Column: TListColumn);
    procedure iLeftArrowClick(Sender: TObject);
    procedure iRightArrowClick(Sender: TObject);
  private
    { Private declarations }
    procedure RedrawArrows;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

IMPLEMENTATION

{$R *.DFM}

procedure TForm1.ListView1CustomDraw(Sender: TCustomListView;
    const ARect: TRect; var DefaultDraw: Boolean);
    begin
        RedrawArrows;
    end;

procedure TForm1.RedrawArrows;
    var
        i: integer;
        leftpos, newpos: integer;
        selectedcolumnindex: integer;
    begin
        selectedcolumnindex := ListView1.Tag;
        pLeftArrow.Top := ListView1.Top +1;
        pRightArrow.Top := ListView1.Top + 1;
// find correct column
        leftpos := 0;
       
        for i := 0 to pred(ListView1.Columns.Count) do
        begin
            if i = selectedcolumnindex then
            begin
                newpos := ListView1.Left +
                leftpos - GetScrollPos(ListView1.Handle, SB_HORZ) + 1;
               
// check for out of bounds
                if ( (selectedcolumnindex = 0) or (newpos < ListView1.Left) or (newpos > (ListView1.Left + ListView1.Width) )) then
                  pLeftArrow.Visible := False
                else
                begin
                    pLeftArrow.Left := newpos;
                    pLeftArrow.Visible := True;
                end;
// position right arrow
                newpos := ListView1.Left +
                leftpos - GetScrollPos(ListView1.Handle, SB_HORZ) +
                (ListView1.Columns[i].Width) - (pLeftArrow.Width) - 1;
// check for out of bounds
                if ( (selectedcolumnindex = pred(ListView1.Columns.Count)) or (newpos < ListView1.Left) or (newpos > (ListView1.Left + ListView1.Width) )) then
                  pRightArrow.Visible := False
                else
                begin
                    pRightArrow.Left := newpos;
                    pRightArrow.Visible := True;
                end;
               
            end
            else
              ListView1.Columns[i].ImageIndex := -1;
            leftpos := leftpos + ListView1.Columns[i].Width;
        end;
    end;

procedure TForm1.ListView1ColumnClick(Sender: TObject; Column: TListColumn);
    begin
        ListView1.Tag := Column.Index;
        RedrawArrows;
//        ListView1.CustomSort(nil, 0);
    end;

procedure TForm1.iLeftArrowClick(Sender: TObject);
    begin
        if ListView1.Tag > 0 then
        begin
            ListView1.Columns[ListView1.Tag].Index := ListView1.Tag - 1;
            ListView1.Tag := ListView1.Tag - 1;
            ListView1.Repaint;
        end;
    end;

procedure TForm1.iRightArrowClick(Sender: TObject);
    begin
        if ListView1.Tag < pred(ListView1.Columns.Count) then
        begin
            ListView1.Columns[ListView1.Tag].Index := ListView1.Tag + 1;
            ListView1.Tag := ListView1.Tag + 1;
            ListView1.Repaint;
        end;
    end;

end.


********** Form Follows ***********
********** Form Follows ***********

object Form1: TForm1
  Left = 274
  Top = 107
  Width = 585
  Height = 438
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object ListView1: TListView
    Left = 40
    Top = 96
    Width = 505
    Height = 273
    Columns = <
      item
        Caption = 'column 1'
        ImageIndex = 0
        Width = 100
      end
      item
        Caption = 'column 2'
        ImageIndex = 0
        Width = 100
      end
      item
        Caption = 'column 3'
        ImageIndex = 0
        Width = 100
      end
      item
        Caption = 'column 4'
        ImageIndex = 0
        Width = 100
      end
      item
        Caption = 'column 5'
        ImageIndex = 0
        Width = 100
      end>
    SmallImages = ImageList1
    TabOrder = 0
    ViewStyle = vsReport
    OnColumnClick = ListView1ColumnClick
    OnCustomDraw = ListView1CustomDraw
  end
  object pLeftArrow: TPanel
    Left = 40
    Top = 20
    Width = 16
    Height = 16
    BevelOuter = bvNone
    TabOrder = 1
    object iLeftArrow: TImage
      Left = 0
      Top = 0
      Width = 16
      Height = 16
      Picture.Data = {
        07544269746D617076010000424D760100000000000076000000280000002000
        000010000000010004000000000000010000120B0000120B0000100000000000
        0000000000000000800000800000008080008000000080008000808000007F7F
        7F00BFBFBF000000FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFF
        FF00333333333333333333333333333333333333333333333333333333333333
        33333333333333333333333333333FF3333333333333003333333333333F77F3
        3333333333009033333333333F7737F333333333009990333333333F773337FF
        FFFF33009999900000003F773333377777770099999999999990773FF33333FF
        FFF7330099999000000033773FF33777777733330099903333333333773FF7F3
        333333333300903333333333337737F333333333333300333333333333337733
        3333333333333333333333333333333333333333333333333333333333333333
        3333333333333333333333333333333333333333333333333333333333333333
        3333}
      OnClick = iLeftArrowClick
    end
  end
  object pRightArrow: TPanel
    Left = 60
    Top = 20
    Width = 16
    Height = 16
    TabOrder = 2
    object iRightArrow: TImage
      Left = 0
      Top = 0
      Width = 16
      Height = 16
      Picture.Data = {
        07544269746D617076010000424D760100000000000076000000280000002000
        000010000000010004000000000000010000120B0000120B0000100000000000
        0000000000000000800000800000008080008000000080008000808000007F7F
        7F00BFBFBF000000FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFF
        FF00333333333333333333333333333333333333333333333333333333333333
        33333333333333333333333333333333333333333333333333333333333FF333
        33333333330033333333333333773FF3333333333309003333333333337F773F
        F33333333309990033333FFFFF7F33773FF30000000999990033777777733333
        773F09999999999999007FFFFFFF33333F7700000009999900337777777F333F
        773333333309990033333333337F3F77333333333309003333333333337F7733
        3333333333003333333333333377333333333333333333333333333333333333
        3333333333333333333333333333333333333333333333333333333333333333
        3333}
      OnClick = iRightArrowClick
    end
  end
  object ImageList1: TImageList
    Left = 280
    Top = 40
    Bitmap = {
      494C010101000400040010001000FFFFFFFFFF10FFFFFFFFFFFFFFFF424D3600
      0000000000003600000028000000400000001000000001002000000000000010
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      000000000000000000000000000000000000424D3E000000000000003E000000
      2800000040000000100000000100010000000000800000000000000000000000
      000000000000000000000000FFFFFF00FFFF000000000000FFFF000000000000
      FFFF000000000000FFFF000000000000FFFF000000000000FFFF000000000000
      FFFF000000000000FFFF000000000000FFFF000000000000FFFF000000000000
      FFFF000000000000FFFF000000000000FFFF000000000000FFFF000000000000
      FFFF000000000000FFFF00000000000000000000000000000000000000000000
      000000000000}
  end
end
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

594 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question