1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
pngimage,
gifimage,
Dialogs, StdCtrls, CheckLst, jpeg, ExtCtrls;
type
TForm1 = class(TForm)
CheckListBox1: TCheckListBox;
procedure FormCreate(Sender: TObject);
procedure CheckListBox1Enter(Sender: TObject);
procedure CheckListBox1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
private
{ Déclarations privées }
public
PROCEDURE AppShowHint(VAR HintStr: STRING; VAR CanShow: Boolean; VAR HintInfo: THintInfo); // gestion de l'aide (dans listtitre)
{ Déclarations publiques }
end;
type
TMyHintWindow = class(THintWindow)
CONSTRUCTOR Create(AOwner: TComponent); override;
private
Image: string;
protected
procedure Paint; override;
public
procedure ActivateHint(aRect: TRect; const aHint: string); override;
end;
var
Form1: TForm1;
itemindexprec:Integer;
MyHintinfo: Thintinfo;
lzwidth,lzheight:Integer;
Letext:String;
JPG_HEADER: array[0..2] of byte = ($FF, $D8, $FF);
GIF_HEADER: array[0..2] of byte = ($47, $49, $46);
BMP_HEADER: array[0..1] of byte = ($42, $4D);
PNG_HEADER: array[0..3] of byte = ($89, $50, $4E, $47);
TIF_HEADER: array[0..2] of byte = ($49, $49, $2A);
implementation
{$R *.dfm}
{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
FUNCTION Getimagetype(Filename: STRING): STRING;
VAR
STREAM: Tfilestream;
Memstr: Tmemorystream;
BEGIN
Result := '';
STREAM := Tfilestream.Create(Filename, Fmopenread);
Memstr := Tmemorystream.Create;
TRY
Memstr.COPYFROM(STREAM, 5);
IF Memstr.SIZE > 4 THEN
BEGIN
IF Comparemem(Memstr.MEMORY, @Jpg_Header, SIZEOF(Jpg_Header)) THEN Result := 'jpg' ELSE
IF Comparemem(Memstr.MEMORY, @Gif_Header, SIZEOF(Gif_Header)) THEN Result := 'gif' ELSE
IF Comparemem(Memstr.MEMORY, @Png_Header, SIZEOF(Png_Header)) THEN Result := 'png' ELSE
IF Comparemem(Memstr.MEMORY, @Bmp_Header, SIZEOF(Bmp_Header)) THEN Result := 'bmp' ELSE
IF Comparemem(Memstr.MEMORY, @Tif_Header, SIZEOF(Tif_Header)) THEN Result := 'tif' ELSE
END;
FINALLY
STREAM.FREE;
Memstr.FREE;
END; {Try}
END; {Getimagetype}
CONSTRUCTOR TMyHintWindow.Create(AOwner: TComponent);
BEGIN
INHERITED Create(AOwner);
END;
procedure TMyHintWindow.ActivateHint(aRect: TRect; const aHint: string);
var
i: integer;
begin
IF (MyHintinfo.Hintcontrol Is TCheckListBox) THEN WITH TCheckListBox(MyHintinfo.Hintcontrol) DO
begin
if lzwidth<>0 then
aRect:=rect(Mouse.CursorPos.X+10,Mouse.CursorPos.Y+10,Mouse.CursorPos.X+11+lzwidth,Mouse.CursorPos.Y+7+lzheight)
else aRect:=rect(0,0,1,1);
i := ItemAtPos(ScreenToClient(Mouse.CursorPos), TRUE);
if (i > -1) and (itemindexprec<>i) then
begin
Image := Items[i];
inherited;
Exit;
end
else itemindexprec:=-1;
end;
Application.CancelHint;{}
end;
procedure TMyHintWindow.Paint;
var Cpy:String;
Bmp: TBitmap;
Jpg: Tjpegimage;
Png: Tpngobject;
Gif: TGifImage;
Tmp: TPicture;
begin
Cpy:='';
lzwidth:=0;
lzheight:=0;
if fileexists(Image) then
begin
Cpy:=Getimagetype(Image);
Bmp := TBitmap.Create;
try
if cpy='bmp' THEN Bmp.LoadFromFile(Image);
if cpy='jpg' THEN
BEGIN
Jpg := Tjpegimage.Create;
try
Jpg.LoadFromFile(Image);
Bmp.ASSIGN(Jpg);
finally
Jpg.Free;
end;
end;
if cpy='png' THEN
BEGIN
Png:= Tpngobject.Create;
try
Png.Loadfromfile (Image);
Bmp.ASSIGN(png);
Bmp.Width := Png.Width;
Bmp.Height := Png.Height;
Png.DRAW(Bmp.Canvas, Bmp.Canvas.Cliprect);
finally
png.Free;
end;
end;
if cpy='gif' THEN
BEGIN
Tmp:=TPicture.Create;
Gif:=TGifImage.create;
try
Gif.LoadFromFile(Image);
Tmp.assign(Gif.Bitmap);
bmp.Assign(Tmp.Bitmap);
finally
Gif.Free;
tmp.Free;
end;
end;{}
if cpy<>'' then
begin
lzwidth:=Bmp.Width;
lzheight:=Bmp.Height;
Canvas.Draw(-1, 0, Bmp);
end;
finally
Bmp.Free;
end;
end;
end;
procedure TForm1.CheckListBox1Enter(Sender: TObject);
begin
HintWindowClass := TMyHintWindow;
end;
procedure TForm1.CheckListBox1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
Application.ActivateHint(Mouse.CursorPos);
end;
PROCEDURE Tform1.Appshowhint(VAR Hintstr: STRING; VAR Canshow: BOOLEAN; VAR Hintinfo: Thintinfo);
BEGIN
MyHintinfo:=Hintinfo;
hintstr:=' ';
END;
procedure TForm1.FormCreate(Sender: TObject);
var i:integer;
begin
TMyHintWindow.Create(self);
Application.OnShowHint := AppShowHint;
CheckListBox1.Hint := '';
CheckListBox1.ShowHint := TRUE;
itemindexprec:=-1;
for i:=0 to pred(CheckListBox1.count) do CheckListBox1.Checked[i]:=true;
end;
end. |
Partager