关键字:皮肤 pngimage png
imgbtnfunc.pas-----------------------------------------------------------------------------
unit imgbtnfunc;
interface
uses
Classes, Forms, ExtCtrls, Controls, Graphics, SysUtils, Windows, pngimage;
type
TImgBtnStatus = (ibsNormal,ibsHover,ibsSelected,ibsDisabled);
PImgBtnParam = ^TImgBtnParam;
TImgBtnParam = record
// index:integer;
bselected,bdisabled:boolean;
// rect:TRect;
normal,hover,selected,disabled:string;
end;
TImgBtnFunc = class(TComponent)
private
FParentForm:TWinControl;
public
skinfolder:string;
constructor Create(AOwner: TComponent);override;
// constructor Create(ParentForm:TForm);
destructor Destroy;override;
function NewImageButton(rect:TRect;
normal:string;hover:string='';selected:string='';disabled:string=''):timage;overload;
function NewImageButton(left,top,width,height:integer;
normal:string;hover:string='';selected:string='';disabled:string=''):timage;overload;
procedure FreeImageButton(img:timage);
procedure AssignToImage(img:timage;pngfile:string);
procedure imgmousedown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure imgmouseenter(sender:tobject);
procedure imgmouseleave(sender:tobject);
function getImgBtnParam(img:TImage):TImgBtnParam;
procedure DrawImgBtn(img:timage;status:TImgBtnStatus);
procedure DrawRndRectRegion(w,h:integer);
function test:integer;
end;
implementation
constructor TImgBtnFunc.Create(AOwner: TComponent);
begin
inherited Create(nil);
FParentForm := TWinControl(AOwner);
skinfolder := 'skin/default/';
end;
{
constructor TImgBtnFunc.Create(ParentForm:TForm);
begin
Create(nil);
FParentForm := ParentForm;
skinfolder := 'skin/default/';
end;
}
destructor TImgBtnFunc.Destroy;
var
i:integer;
begin
for i:=componentcount-1 downto 0 do
FreeImageButton(timage(components[i]));
inherited;
end;
procedure TImgBtnFunc.DrawRndRectRegion(w,h:integer);
var
Rgn:HRgn;
Rect:TRect;
begin
Rect := fparentform.ClientRect;
Rgn := CreateRoundRectRgn(Rect.left,Rect.top,Rect.right,Rect.bottom,w,h);
SetWindowRgn(fparentform.Handle,Rgn,TRUE);
end;
function TImgBtnFunc.getImgBtnParam(img:TImage):TImgBtnParam;
begin
if not Assigned(img) then
exit;
result := pimgbtnparam(img.Tag)^;
end;
procedure TImgBtnFunc.DrawImgBtn(img:timage;status:TImgBtnStatus);
var
imgp:timgbtnparam;
pngfile:string;
begin
imgp := pimgbtnparam(img.tag)^;
case status of
ibsNormal:pngfile := extractfilepath(application.exename)+skinfolder+imgp.normal;
ibsHover:pngfile := extractfilepath(application.exename)+skinfolder+imgp.hover;
ibsSelected:pngfile := extractfilepath(application.exename)+skinfolder+imgp.selected;
ibsDisabled:pngfile := extractfilepath(application.exename)+skinfolder+imgp.disabled;
end;
if (pngfile<>'') then
assigntoimage(img,pngfile);
end;
function TImgBtnFunc.NewImageButton(rect:TRect;
normal:string;hover:string='';selected:string='';disabled:string=''):timage;
begin
result := NewImageButton(rect.left,rect.top,rect.Right-rect.left,rect.bottom-rect.top,normal,hover,selected,disabled);
end;
function TImgBtnFunc.NewImageButton(left,top,width,height:integer;
normal:string;hover:string='';selected:string='';disabled:string=''):timage;
var
img: timage;
ImgBtnParam:PImgBtnParam;
begin
new(ImgBtnParam);
ImgBtnParam^.bselected := false;
ImgBtnParam^.bdisabled := false;
// ImgBtnParam^.rect := rect;
ImgBtnParam^.normal := normal;
ImgBtnParam^.hover := hover;
ImgBtnParam^.selected := selected;
ImgBtnParam^.disabled := disabled;
img := timage.create(self);
img.parent := fparentform;
img.left := left;
img.top := top;
img.width := width;
img.height := height;
img.tag := integer(ImgBtnParam);
img.OnMouseDown := imgmousedown;
img.onmouseenter := imgmouseenter;
img.onmouseleave := imgmouseleave;
DrawImgBtn(img,ibsnormal);
result := img;
end;
procedure TImgBtnFunc.FreeImageButton(img:timage);
begin
dispose(pimgbtnparam(img.Tag));
img.free;
end;
function TImgBtnFunc.test:integer;
begin
result := componentcount;
end;
procedure TImgBtnFunc.AssignToImage(img:timage;pngfile:string);
var
png:tpngobject;
begin
if not Assigned(img) then
exit;
png := tpngobject.create;
try
png.LoadFromFile(pngfile);
img.Picture.Assign(png);
finally
png.free;
end;
end;
procedure TImgBtnFunc.imgmousedown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
img:timage;
imgparam:PImgBtnParam;
begin
if (mbLeft=button) then
exit;
img := timage(sender);
imgparam := PImgBtnParam(img.tag);
if imgparam.bdisabled then
exit;
imgparam.bselected := not imgparam.bselected;
if (not imgparam.bselected) and (imgparam.normal<>'') then
DrawImgBtn(img,ibsNormal)
else if imgparam.bselected and (imgparam.selected<>'') then
DrawImgBtn(img,ibsSelected)
end;
procedure TImgBtnFunc.imgmouseenter(sender:tobject);
var
img:timage;
imgparam:PImgBtnParam;
begin
img := timage(sender);
imgparam := PImgBtnParam(img.tag);
if (imgparam.bselected or imgparam.bdisabled) then
exit;
if imgparam.hover<>'' then
DrawImgBtn(img,ibsHover)
end;
procedure TImgBtnFunc.imgmouseleave(sender:tobject);
var
img:timage;
imgparam:PImgBtnParam;
begin
img := timage(sender);
imgparam := PImgBtnParam(img.tag);
if imgparam.bselected or imgparam.bdisabled then
exit;
if imgparam.normal<>''then
DrawImgBtn(img,ibsNormal)
end;
end.
应用示例
imgbtns:timgbtnfunc;
procedure TForm1.ActiveTab(tab:timage);
var
imgp:pimgbtnparam;
begin
tabactive := tab;
tabactive.bringtofront;
imgp := pimgbtnparam(tab.tag);
imgp^.bselected := not imgp^.bselected;
if (imgp^.bselected) then
imgbtns.drawimgbtn(tab,ibsSelected)
else
imgbtns.drawimgbtn(tab,ibsNormal);
end;
procedure TForm1.DrawSkin;
begin
imgbtns.DrawRndRectRegion(5,5);
tabbg := imgbtns.NewImageButton(4,53,width-8,29,'tab_bg.png');
win_min := imgbtns.NewImageButton(width-69,0,26,18,'min.png','min_hover.png');
win_close := imgbtns.NewImageButton(width-43,0,43,18,'close.png','close_hover.png');
find := imgbtns.NewImageButton(165,23,226,30,'find.png');
tab1 := imgbtns.NewImageButton(width-322,56,172,26,'tab_inactive.png','tab_hover.png','tab_active.png');
tab2 := imgbtns.NewImageButton(width-176,56,172,26,'tab_inactive.png','tab_hover.png','tab_active.png');
end;
procedure TForm1.Create(AOwner:TComponent);
begin
tabbg.Stretch := true;
tabbg.OnMouseDown := formmousedown; //无标题拖动
tabbg.OnMouseMove := formmousemove;
win_min.OnMouseDown := sysbtndown; //系统按钮自定义事件
win_close.OnMouseDown := sysbtndown;
tab1.OnMouseDown := imgdown; //自定义tab分组按钮事件
tab2.OnMouseDown := imgdown;
activetab(tab1);
end;
procedure TForm1.sysbtndown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
imgbtns.imgmousedown(Sender,Button,Shift,X, Y);
if (timage(sender)=win_min) then
begin
visible := false;
trayicon1.Visible := true
end
else if (timage(sender)=win_close) then
close;
end;
procedure Tform1.imgdown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
ActiveTab(tabactive);
tabactive := timage(sender);
ActiveTab(tabactive);
end;
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Button=mbLeft then
Begin
ReleaseCapture;
SendMessage(handle, wm_SysCommand, sc_DragMove, 0);
end;
end;
procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if ssleft in shift then
releasecapture;
perform(WM_SYSCOMMAND,sc_DragMove,0);
end;