uWpLabel.pas
上传用户:yjb1804
上传日期:2021-01-30
资源大小:3105k
文件大小:3k
- unit uWpLabel;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,CommCtrl,
- StdCtrls, ImgList,ExtCtrls;
- type
- TGyphyAlign=(gaLeft,gaRigth);
- TwpLable=class(TLabel)
- private
- FShowShortName:Boolean;
- FFullName:string;
- FGyphyAlign:TGyphyAlign;
- procedure LoadImage(Img:TImage);
- protected
- procedure Paint; override;
- procedure DoDrawImage(align:TGyphyAlign) ;
- published
- property ShowShortName:Boolean read FShowShortName write FShowShortName default True;
- property GyphyAlign:TGyphyAlign read FGyphyAlign write FGyphyAlign default gaLeft;
- end;
- procedure Register;
- implementation
- uses ShellAPI;
- procedure Register;
- begin
- classes.RegisterComponents('HtmlEdit',[TwpLable]);
- end;
- { TwpLable }
- procedure TwpLable.DoDrawImage(align: TGyphyAlign);
- var
- TextWidth:integer;
- R,ImageRect:TRect;
- Image:TImage;
- begin
- Image:=TImage.Create(nil);
- try
- LoadImage(Image);
- image.AutoSize:=true;
- self.AutoSize:=false;
- case Align of
- gaLeft:
- begin
- R:=GetClientRect;
- ImageRect.Left:=R.Left;
- ImageRect.Top:=R.Top;
- ImageRect.Right:=Image.ClientWidth;
- ImageRect.Bottom:=R.Bottom;
- end;
- gaRigth:
- begin
- R:=GetClientRect;
- ImageRect.Left:=R.Right;
- ImageRect.Top:=R.Top;
- ImageRect.Right:=R.Right+Image.ClientWidth;
- ImageRect.Bottom:=R.Bottom;
- SetBounds(R.Left,r.Top,r.Right+Image.ClientWidth,r.Bottom);
-
- //self.AutoSize:=true;
- end;
- else;
- end;
- Canvas.StretchDraw(ImageRect,Image.Picture.Graphic);
- finally
- Image.free;
- end;
- end;
- procedure TwpLable.LoadImage(Img: TImage);
- var
- ImageList:TImageList;
- FileInfo: TSHFileInfo;
- ImageListHandle:THandle;
- begin
- // 小图标
- ImageListHandle := SHGetFileInfo(PAnsiChar(FFullName), 0, FileInfo, SizeOf(FileInfo), SHGFI_SYSICONINDEX or SHGFI_SMALLICON);
- ImageList:=TImageList.Create(nil);
- try
- ImageList_AddIcon(ImageList.Handle,ImageListHandle);
- ImageList.GetIcon(0,Img.Picture.Icon);
- finally
- ImageList.free;
- end;
- end;
- procedure TwpLable.Paint;
- const
- Alignments: array[TAlignment] of Word = (DT_LEFT, DT_RIGHT, DT_CENTER);
- WordWraps: array[Boolean] of Word = (0, DT_WORDBREAK);
- var
- Rect, CalcRect: TRect;
- DrawStyle: Longint;
- begin
-
- FFullName:=Caption;
- if ShowShortName then Caption:=ExtractFileName(Caption);
- AutoSize:=true;
- with Canvas do
- begin
- if not Transparent then
- begin
- Brush.Color := Self.Color;
- Brush.Style := bsSolid;
- FillRect(ClientRect);
- end;
- Brush.Style := bsClear;
- Rect := ClientRect;
- { DoDrawText takes care of BiDi alignments }
- DrawStyle := DT_EXPANDTABS or WordWraps[WordWrap] or Alignments[Alignment];
- { Calculate vertical layout }
- if Layout <> tlTop then
- begin
- CalcRect := Rect;
- DoDrawText(CalcRect, DrawStyle or DT_CALCRECT);
- if Layout = tlBottom then OffsetRect(Rect, 0, Height - CalcRect.Bottom)
- else OffsetRect(Rect, 0, (Height - CalcRect.Bottom) div 2);
- end;
- DoDrawText(Rect, DrawStyle);
- end;
- DoDrawImage(GyphyAlign);
- end;
- end.