☆ListViewの背景に画像を描画する。
注意すべき点は次の通りです。
1.ComObjを忘れない。
忘れてもコンパイルできますが、実行時にはエラーになります。
こういうエラーは原因がなかなかわからないですしね。
って、実は私自身がはまっていました(笑)
2.ListView1.DoubleBuffered を設定する。
ちらつきを抑えることができます。
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, SubClassUnit, ComCtrls; type TForm1 = class(TForm) ListView1: TListView; procedure FormCreate(Sender: TObject); private SubClass1: TSubClass; procedure ImageDraw; public procedure SubClass1MessageAfter(Sender: TObject; var Message: TMessage); end; var Form1: TForm1; implementation {$R *.dfm} // ComObjがなくてもコンパイルはできますが、 // 実行時にエラーになります。 uses ComObj, CommCtrl; procedure TForm1.FormCreate(Sender: TObject); begin // これを設定しないとちらつきます。 ListView1.DoubleBuffered := True; // サブクラスの設定 SubClass1:= TSubClass.Create(Self); SubClass1.TargetControl := ListView1; SubClass1.OnMessageAfter := SubClass1MessageAfter; // 背景画像の描画 ImageDraw; end; procedure TForm1.SubClass1MessageAfter(Sender: TObject; var Message: TMessage); begin if Message.Msg = WM_ERASEBKGND then ListView1.DefaultHandler(Message); end; procedure TForm1.ImageDraw; var LVBKImage: TLVBKImage; S: String; begin S := 'c:\windows\サポテック織り.bmp'; LVBKImage.ulFlags := LVBKIF_SOURCE_URL or LVBKIF_STYLE_TILE; LVBKImage.hbm := 0; LVBKImage.pszImage := PChar(S); LVBKImage.cchImageMax := 0; LVBKImage.xOffsetPercent := 0; LVBKImage.yOffsetPercent := 0; // マクロを使ってみます。 ListView_SetTextBkColor(ListView1.Handle, CLR_NONE); ListView_SetBkImage(ListView1.Handle, @LVBKImage); end; end.
DelphiFAQ.com
Putting a background image on a ListView (Delphi 7)
Delphi Library [Mr.XRAY]
サブクラス化コンポーネント
MSDN
ListView_SetBkImage Macro
LVBKIMAGE Structure
ListView_SetTextBkColor Macro
| 固定リンク