利用pngimage实现PNG图形化按钮

关键字:皮肤 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;

之前上传的版本如果安装了DEV组件,则加载PNG图片时将会出错。感谢下载的网友指出的错误,因为本人上传之后一直未关注过评论,今天偶然发现,在此向各位致歉。 我在本机的XE版本下安装时好像不需要obj文件,但既然有网友指出,我就将d7目录下的obj拷贝了一份放在xe源码目录内。 该修改版本仅在D7和XE中测试。如果是其他版本,请自行修改。 安装说明参见压缩包内的reademe文件。 ---------------------------------------------------------- 针对XE版本增加的属性 如下: Anchors: TAnchors; //按钮相对位置 Action: TActionList; //与 actionlist 连接 CaptionAlign: TAlignment; //按钮标题对齐方式 //是否填充渐变色,取决于后两个变量 IsDrawGradientColorFace: Boolean; //是否以渐变色填充按钮表面 IsDrawFaceOnMouseOver: boolean; //鼠标悬停时是否填充按钮表面 IsDrawFaceOnPush: Boolean; //鼠标按下时是否填充按钮表面 //边框 IsDrawBorderOnMouseOver: Boolean; IsDrawBorderOnPush: Boolean; //悬停及按下时的渐变色设置 DrawOverFaceStartColor: TColor; //鼠标悬停时渐变填充起始色 DrawOverFaceEndColor: TColor; //鼠标悬停时渐变填充结束色 DrawPushFaceStartColor: TColor; DrawPushFaceEndColor: TColor; //按钮表面单色 DrawUniqueColorFaceOnMouseOver: TColor; //鼠标悬停时单色填充时的颜色 DrawUniqueFaceColorOnPush: TColor; //边框 DrawBorderOnMouseOver: TColor; //鼠标悬停时边框颜色 DrawBorderOnPush: TColor; IsShowCaption: boolean; //是否显示按钮文字 注:与颜色相关的属性仅在 buttonStyle 设为 pbsFlat 或 pbsNoFrame 时有效
procedure BitmapFileToPNG(const Source, Dest: String); var Bitmap: TBitmap; PNG: TPNGObject; begin Bitmap := TBitmap.Create; PNG := TPNGObject.Create; {In case something goes wrong, free booth Bitmap and PNG} try Bitmap.LoadFromFile(Source); PNG.Assign(Bitmap); //Convert data into png PNG.SaveToFile(Dest); finally Bitmap.Free; PNG.Free; end end; Converting from PNG file to Windows bitmap file The above inverse. Loads a png and saves into a bitmap procedure PNGFileToBitmap(const Source, Dest: String); var Bitmap: TBitmap; PNG: TPNGObject; begin PNG := TPNGObject.Create; Bitmap := TBitmap.Create; {In case something goes wrong, free booth PNG and Bitmap} try PNG.LoadFromFile(Source); Bitmap.Assign(PNG); //Convert data into bitmap Bitmap.SaveToFile(Dest); finally PNG.Free; Bitmap.Free; end end; Converting from TImage to PNG file This method converts from TImage to PNG. It has full exception handling and allows converting from file formats other than TBitmap (since they allow assigning to a TBitmap) procedure TImageToPNG(Source: TImage; const Dest: String); var PNG: TPNGObject; BMP: TBitmap; begin PNG := TPNGObject.Create; {In case something goes wrong, free PNG} try //If the TImage contains a TBitmap, just assign from it if Source.Picture.Graphic is TBitmap then PNG.Assign(TBitmap(Source.Picture.Graphic)) //Convert bitmap data into png else begin //Otherwise try to assign first to a TBimap BMP := TBitmap.Create; try BMP.Assign(Source.Picture.Graphic); PNG.Assign(BMP); finally BMP.Free; end; end; //Save to PNG format PNG.SaveToFile(Dest); finally PNG.Free; end end;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值