1

how i can to to set background image to TListview in Delphi XE??

i want to make a application like Windows Explorer.

4

2 回答 2

9

为了在列表视图中设置水印,您需要使用LVM_SETBKIMAGE消息,并且您需要覆盖 TListView 的默认 WM_ERASEBKGND 消息。列表视图拥有位图句柄的所有权,因此您需要使用 TBitmap 的ReleaseHandle,而不仅仅是Handle.

如果您希望它与左上角对齐,而不是像资源管理器那样与右下角对齐,请使用LVBKIF_SOURCE_HBITMAP而不是LVBKIF_TYPE_WATERMARKulFlags

uses
  CommCtrl, ...;

type
  TListView = class(ComCtrls.TListView)
  protected
    procedure WndProc(var Message: TMessage);
      override;
  end;

  TForm4 = class(TForm)
    ListView1: TListView;
    procedure FormCreate(Sender: TObject);
  end;

procedure TListView.WndProc(var Message: TMessage);
begin
  if Message.Msg = WM_ERASEBKGND then
    DefaultHandler(Message)
  else
    inherited WndProc(Message);
end;

procedure TForm4.FormCreate(Sender: TObject);
var
  Img: TImage;
  BkImg: TLVBKImage;
begin
  FillChar(BkImg, SizeOf(BkImg), 0);
  BkImg.ulFlags := LVBKIF_TYPE_WATERMARK;
  // Load image and take ownership of the bitmap handle
  Img := TImage.Create(nil);
  try
    Img.Picture.LoadFromFile('C:\Watermark.bmp');
    BkImg.hbm := Img.Picture.Bitmap.ReleaseHandle;
  finally
    Img.Free;
  end;
  // Set the watermark
  SendMessage(ListView1.Handle, LVM_SETBKIMAGE, 0, LPARAM(@BkImg));
end;

拉伸水印

列表视图本身不支持在整个背景中拉伸位图。为此,您需要自己执行 StretchBlt 以响应 WM_ERASEBKGND。

type
  TMyListView = class(TListView)
  protected
    procedure CreateHandle; override;
    procedure CreateParams(var Params: TCreateParams); override;
    procedure WMEraseBkgnd(var Msg: TWMEraseBkgnd); message WM_ERASEBKGND;
  public
    Watermark: TBitmap;
  end;

procedure TMyListView.CreateHandle;
begin
  inherited;
  // Set text background color to transparent
  SendMessage(Handle, LVM_SETTEXTBKCOLOR, 0, CLR_NONE);
end;

procedure TMyListView.CreateParams(var Params: TCreateParams);
begin
  inherited;
  // Invalidate every time the listview is resized
  Params.Style := Params.Style or CS_HREDRAW or CS_VREDRAW;
end;

procedure TMyListView.WMEraseBkgnd(var Msg: TWMEraseBkgnd);
begin
  StretchBlt(Msg.DC, 0, 0, Width, Height, Watermark.Canvas.Handle,
    0, 0, Watermark.Width, Watermark.Height, SrcCopy);
  Msg.Result := 1;
end;
于 2010-10-21T13:39:37.360 回答
1

Tlistview 很好,但如果你想要更多。我建议您必须使用非常灵活的VirtualStringTree(VirtualTreeView)进行更新,您几乎可以自定义任何您想要的内容,而且最重要的是它是免费的。

于 2010-10-22T01:59:45.250 回答