unit MirosImageButton;
interface
uses
Windows,SysUtils,Classes,Graphics,Controls,Messages;
const
VNormal = 0;
VMouseOn = 1;
VMouseDown= 2;
type
bStyle = (bFFStyle, bGlyph);
TRectModle = (rRect, rRoundRect);
TJustStyle = (jsNormal, jsMouseOn, jsMouseDown);
TCustomMirosImageButton = class(TCustomControl)
private
MNormalGlyph, MMouseOnGlyph, MMouseDownGlyph : TBitmap;
MCaption : string;
MStyle : bStyle;
MJustStyle: byte;
MRectModle: TRectModle;
MRoundColor, MInnerColor, MOutColor, MThirdColor,
MFourColor, MFaceColor, MMouseOnColor, MOldColor : TColor;
MMouseDowning, MShowARound, MShowCaption, MouseOnSeted : Boolean;
FTransParent: Boolean;
FTransparentColor: TColor;
procedure CMFocusChanged(var Msg: TCMFocusChanged); message CM_FOCUSCHANGED;
public
constructor Create(AOwner : TComponent);override;
destructor Destroy;override;
procedure DrawAround();
procedure DrawSecond();
procedure DrawThird();
procedure DrawCaption;
procedure SetCaption(Cap : String);
procedure SetInnerColor(Value : TColor);
procedure SetOutColor(Value : TColor);
procedure SetThirdColor(Value : TColor);
procedure SetFourColor(Value : TColor);
procedure SetFaceColor(Value : TColor);
procedure SetRoundColor(Value : TColor);
procedure SetAround(B : Boolean);
procedure SetShowCaption(B : Boolean);
procedure SetNormalGlyph(Bmp : TBitmap);
procedure SetMouseOnGlyph(Bmp : TBitmap);
procedure SetMouseDownGlyph(Bmp : TBitmap);
procedure SetStyle(aStyle : bStyle);
procedure SetMouseOnColor(Value : TColor);
procedure CopyCanvas(Source : TBitmap);
procedure SetGlyphModle(Value : Byte);
protected
procedure Paint;override; //WM_KILLFOCUS //WM_SEFOCUS
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);override;
procedure WMKeyDown (var Message: TWMKeyDown); message WM_KEYDOWN;
procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
published
property OnClick;
property Caption : string read MCaption write SetCaption;
property InnerColor : TColor read MInnerColor write SetInnerColor default $00E7EBED;
property InnerOutColor : TColor read MOutColor write SetOutColor default $00959899;
property SInnerColor : TColor read MThirdColor write SetThirdColor default $00DBE0E3;
property SInnerOutColor : TColor read MFourColor write SetFourColor default $00ADB2B4;
property FaceColor : TColor read MFaceColor write SetFaceColor default $00C9D1D5;
property ARoundColor : TColor read MRoundColor write SetRoundColor default $00000000;
property ShowAround : Boolean read MShowAround write SetARound default true;
property ShowCaption : Boolean read MShowCaption write SetShowCaption default true;
property NormalGlyph : TBitmap read MNormalGlyph write SetNormalGlyph;
property MouseOnGlyph : TBitmap read MMouseOnGlyph write SetMouseOnGlyph;
property MouseDownGlyph : TBitmap read MMouseDownGlyph write SetMouseDownGlyph;
property Style : bStyle read MStyle write SetStyle default bFFStyle;
property MouseOnColor : TColor read MMouseOnColor write SetMouseOnColor default $00000000;
property TransParent: Boolean read FTransparent write FTransparent default False;
property TransParentColor: TColor read FTransParentColor Write FTransParentColor default clFuchsia;
property Font;
property ShowHint;
property Align;
property Cursor;
property PopupMenu;
property Visible;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDrag;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnResize;
property OnStartDrag;
property OnMouseLeave;//如果编译不过 请把这个注解了 D7好象不支持
property OnMouseEnter;//如果编译不过 请把这个注解了 D7好象不支持
property OnEnter; ;//如果编译不过 请把这个注解了 D7好象不支持
end;
TMirosButton = class(TCustomMirosImageButton)
public
constructor Create(AOwner : TComponent);override;
destructor Destroy;override;
published
property OnClick;
property Caption;
property InnerColor;
property InnerOutColor;
property SInnerColor;
property SInnerOutColor;
property FaceColor;
property ARoundColor;
property ShowAround;
property ShowCaption;
property NormalGlyph;
property MouseOnGlyph;
property MouseDownGlyph;
property Style;
property MouseOnColor;
property Font;
property ShowHint;
property Align;
property Cursor;
property PopupMenu;
property Visible;
property Transparent;
property TransparentColor;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDrag;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnResize;
property OnStartDrag;
property OnMouseLeave;
property OnMouseEnter;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('MirosButton', [TMirosButton]);
end;
constructor TCustomMirosImageButton.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
MNormalGlyph := TBitmap.Create;
MMouseOnGlyph := TBitmap.Create;
MMouseDownGlyph := TBitmap.Create;
MStyle := bFFStyle;
MJustStyle := 0;
MCaption := 'MirosButton';
MRectModle := rRect;
MShowARound := true;
MShowCaption := true;
MMouseDowning := false;
MMouseOnColor := $00000000;
MRoundColor := $00000000;
MInnerColor := $00E7EBED;
MOutColor := $00959899;
MThirdColor := $00DBE0E3;
MFourColor := $00ADB2B4;
MFaceColor := $00C9D1D5;
DoubleBuffered := true;
FTransParent:= False;
FTransParentColor := Graphics.clFuchsia;
end;
destructor TCustomMirosImageButton.Destroy;
begin
MNormalGlyph.Destroy;
MMouseOnGlyph.Destroy;
MMouseDownGlyph.Destroy;
inherited Destroy;
end;
constructor TMirosButton.Create(AOwner: TComponent);
begin
Inherited Create(AOwner);
end;
destructor TMirosButton.Destroy;
begin
inherited Destroy;
end;
procedure TCustomMirosImageButton.DrawAround();
begin
with Canvas do
begin
Brush.Style := bsClear;
Pen.Style := psClear;
Brush.Color := MFaceColor;
Rectangle(0,0,Width,Height);
Pen.Style := psSolid;
Brush.Style := bsSolid;
Brush.Color := MFaceColor;
Pen.Color := MRoundColor;
end;
if MShowARound then
case MRectModle of
rRect:
begin
Canvas.Rectangle(Rect(0,0 ,Width ,Height))
end;
rRoundRect:
begin
Canvas.Brush.Color := MFaceColor;
Canvas.Pen.Color := MRoundColor;
Canvas.RoundRect(0,0,Width,Height,5,5);
end;
end;
end;
procedure TCustomMirosImageButton.DrawSecond();
begin
if MMouseDowning then
Canvas.Pen.Color := MOutColor
else
Canvas.Pen.Color := MInnerColor;
case MRectModle of
rRect:
begin
Canvas.Rectangle(Rect(1,1,Width - 1,Height - 1));
if MMouseDowning then
Canvas.Pen.Color := MInnerColor
else
Canvas.Pen.Color := MOutColor;
Canvas.MoveTo(2,Height - 2);
Canvas.LineTo(Width - 2,Height - 2);
Canvas.LineTo(Width - 2, 1);
end;
rRoundRect:
begin
Canvas.Brush.Color := MFaceColor;
Canvas.Pen.Color := MInnerColor;
Canvas.RoundRect(1,1,Width - 1,Height - 1,5,5);
end;
end;
end;
procedure TCustomMirosImageButton.DrawThird();
begin
if MMouseDowning then
Canvas.Pen.Color := MFourColor
else
Canvas.Pen.Color := MThirdColor;
case MRectModle of
rRect:
begin
Canvas.Rectangle(Rect(2,2,Width - 2,Height - 2));
if MMouseDowning then
Canvas.Pen.Color := MThirdColor
else
Canvas.Pen.Color := MFourColor;
Canvas.MoveTo(3,Height - 3);
Canvas.LineTo(Width - 3,Height - 3);
Canvas.LineTo(Width - 3,2);
end;
rRoundRect:
begin
Canvas.Brush.Color := MFaceColor;
Canvas.Pen.Color := MThirdColor;
Canvas.RoundRect(2,2,Width - 2,Height - 2,5,5);
end;
end;
end;
procedure TCustomMirosImageButton.DrawCaption;
var
x, y : Integer;
begin
x := Canvas.TextWidth(MCaption);
y := Canvas.TextHeight(MCaption);
Canvas.Font.Color := Font.Color;
Canvas.Brush.Style := bsClear;
if MMouseDowning then
Canvas.TextOut((Width - x) div 2 + 1,(Height - y) div 2 + 1,MCaption)
else
Canvas.TextOut((Width - x) div 2,(Height - y) div 2,MCaption);
Canvas.Brush.Style := bsSolid;
end;
procedure TCustomMirosImageButton.Paint;
begin
DrawAround;
case MStyle of
bFFStyle:
begin
DrawSecond;
DrawThird;
end;
bGlyph:SetGlyphModle(MJustStyle);
end;
if MShowCaption then
DrawCaption;
end;
procedure TCustomMirosImageButton.MouseDown(Button: TMouseButton; Shift: TShiftState; X: Integer; Y: Integer);
begin
case MStyle of
bFFStyle:
begin
MMouseDowning := true;
invalidate;
end;
bGlyph:
if MJustStyle <> 2 then
begin
MJustStyle := 2;
invalidate;
end;
end;
end;
procedure TCustomMirosImageButton.MouseUp(Button: TMouseButton; Shift: TShiftState; X: Integer; Y: Integer);
begin
case MStyle of
bFFStyle:
begin
MMouseDowning := false;
invalidate;
end;
bGlyph:
if MJustStyle <> 0 then
begin
MJustStyle := 0;
invalidate;
end;
end;
end;
procedure TCustomMirosImageButton.CMFocusChanged(var Msg: TCMFocusChanged);
begin
end;
procedure TCustomMirosImageButton.WMKeyDown(var Message: TWMKey);
begin
if csDesigning in ComponentState then Exit;
if Message.CharCode = VK_SPACE then
MouseDown(mbLeft,[],0,0);
end;
procedure TCustomMirosImageButton.SetCaption(Cap: string);
begin
MCaption := Cap;
invalidate;
end;
procedure TCustomMirosImageButton.SetInnerColor(Value: TColor);
begin
MInnerColor := Value;
invalidate;
end;
procedure TCustomMirosImageButton.SetOutColor(Value: TColor);
begin
MOutColor := Value;
invalidate;
end;
procedure TCustomMirosImageButton.SetThirdColor(Value: TColor);
begin
MThirdColor := Value;
invalidate;
end;
procedure TCustomMirosImageButton.SetFourColor(Value: TColor);
begin
MFourColor := Value;
invalidate;
end;
procedure TCustomMirosImageButton.SetFaceColor(Value: TColor);
begin
MFaceColor := Value;
invalidate;
end;
procedure TCustomMirosImageButton.SetRoundColor(Value: TColor);
begin
MRoundColor := Value;
invalidate;
end;
procedure TCustomMirosImageButton.SetAround(B: Boolean);
begin
MShowAround := B;
invalidate;
end;
procedure TCustomMirosImageButton.SetShowCaption(B: Boolean);
begin
MShowCaption := B;
invalidate;
end;
procedure TCustomMirosImageButton.SetNormalGlyph(Bmp: TBitmap);
begin
MStyle := bGlyph;
MJustStyle := 0;
MNormalGlyph.Assign(Bmp);
Width := Bmp.Width;
Height := Bmp.Height;
invalidate;
end;
procedure TCustomMirosImageButton.SetMouseOnGlyph(Bmp: TBitmap);
begin
MStyle := bGlyph;
MJustStyle := 0;
MMouseOnGlyph.Assign(Bmp);
Width := Bmp.Width;
Height := Bmp.Height;
invalidate;
end;
procedure TCustomMirosImageButton.SetMouseDownGlyph(Bmp: TBitmap);
begin
MStyle := bGlyph;
MJustStyle := 0;
MMouseDownGlyph.Assign(Bmp);
Width := Bmp.Width;
Height := Bmp.Height;
invalidate;
end;
procedure TCustomMirosImageButton.SetStyle(aStyle: bStyle);
begin
MStyle := aStyle;
invalidate;
end;
procedure TCustomMirosImageButton.CMMouseEnter(var Message: TMessage);
begin
if csDesigning in ComponentState then Exit;
case MStyle of
bFFStyle:
begin
if not MouseOnSeted then
begin
MOldColor := Font.Color;
Font.Color := MMouseOnColor;
MouseOnSeted := true;
invalidate;
end;
end;
bGlyph:
if MJustStyle <> 1 then
begin
MJustStyle := 1;
invalidate;
end;
end;
end;
procedure TCustomMirosImageButton.CMMouseLeave(var Message: TMessage);
begin
if csDesigning in ComponentState then Exit;
case MStyle of
bFFStyle:
begin
if MouseOnSeted then
begin
Font.Color := MOldColor;
MouseOnSeted := false;
invalidate;
end;
end;
bGlyph:
if MJustStyle <> 0 then
begin
MJustStyle := 0;
invalidate;
end;
end;
end;
procedure TCustomMirosImageButton.SetMouseOnColor(Value: TColor);
begin
MMouseOnColor := Value;
invalidate;
end;
procedure TCustomMirosImageButton.CopyCanvas(Source: TBitmap);
var
Rec: TRect;
begin
if MShowAround then
Rec := Rect(1,1,Width - 1, Height - 1)
else Rec := ClientRect;
if FTransparent then
Canvas.BrushCopy(Rec, Source, rect(0,0,Source.Width,Source.Height), FTransparentColor)
else Canvas.CopyRect(Rec,
Source.Canvas,
rect(0,0,Source.Width,Source.Height));
end;
procedure TCustomMirosImageButton.SetGlyphModle(Value: Byte);
begin
case Value of
0:CopyCanvas(MNormalGlyph);
1:CopyCanvas(MMouseOnGlyph);
2:CopyCanvas(MMouseDownGlyph);
end;
// invalidate;
end;
end.
很久以前写了 那时还是Delphi Newer 代码有点烂 参考一下吧!