unit MyLabel;
interface
uses
Winapi.Windows,
Winapi.Messages, System.SysUtils, System.Classes,
Vcl.StdCtrls,
Vcl.Graphics,
Vcl.Controls, Dialogs;
const
CColorDefault: TColor = clRed;
CColorHover: TColor = clGreen;
// CColorClick: TColor = $000000AA;
type
TMyLabel =
class(TLabel)
private
{Private-Deklarationen}
FLastLabelColor: TColor;
FLastLabelStyle: TFontStyles;
FHoverColor: TColor;
FHoverColorEnabled: Boolean;
FHoverStylesEnabled: Boolean;
// FColorActive: TColor;
// FColorClick: TColor;
FOnMouseEnter: TNotifyEvent;
FOnMouseLeave: TNotifyEvent;
FUnderlinedOnEnter: Boolean;
FItalicOnEnter: Boolean;
function GetLastLabelColor: TColor;
procedure SetLastLabelColor;
function GetLastLabelStyle: TFontStyles;
procedure SetLastLabelStyle;
procedure SetLabelMouseProperties(Sender: TLabel; Color: TColor; Styles: TFontStyles = []; Cursor: TCursor = crDefault);
protected
{Protected-Deklarationen}
public
{Public-Deklarationen}
constructor Create(AOwner: TComponent);
override;
procedure CMMouseEnter(
var Message: TMessage);
message CM_MOUSEENTER;
procedure CMMouseLeave(
var Message: TMessage);
message CM_MOUSELEAVE;
published
{Published-Deklarationen}
property HoverColor: TColor
read FHoverColor
write FHoverColor;
property HoverColorEnabled: Boolean
read FHoverColorEnabled
write FHoverColorEnabled;
property HoverStylesEnabled: Boolean
read FHoverStylesEnabled
write FHoverStylesEnabled;
// property ColorActive: TColor read FColorActive write FColorActive;
// property ColorClick: TColor read FColorClick write FColorClick;
property UnderlinedOnEnter: Boolean
read FUnderlinedOnEnter
write FUnderlinedOnEnter;
property ItalicOnEnter: Boolean
read FItalicOnEnter
write FItalicOnEnter;
property OnMouseEnter: TNotifyEvent
read FOnMouseEnter
write FOnMouseEnter;
property OnMouseLeave: TNotifyEvent
read FOnMouseLeave
write FOnMouseLeave;
end;
procedure register;
implementation
function TMyLabel.GetLastLabelColor: TColor;
begin
// Wenn die Maus auf einem Label ist und man per Klick die Farbe ändert, dann hier darauf reagieren und nicht die alte Farbe zurückschreiben
if FLastLabelColor = Self.Font.Color
then
Result := FLastLabelColor
else
Result := Self.Font.Color;
FLastLabelColor := clNone;
end;
procedure TMyLabel.SetLastLabelColor;
begin
if FLastLabelColor = clNone
then
FLastLabelColor := Self.Font.Color;
end;
function TMyLabel.GetLastLabelStyle: TFontStyles;
begin
Result := FLastLabelStyle;
FLastLabelStyle := [];
end;
procedure TMyLabel.SetLastLabelStyle;
begin
if FLastLabelStyle = []
then
FLastLabelStyle := Self.Font.Style;
end;
procedure TMyLabel.SetLabelMouseProperties(Sender: TLabel; Color: TColor; Styles: TFontStyles = []; Cursor: TCursor = crDefault);
begin
TLabel(Sender).Font.Color := Color;
TLabel(Sender).Cursor := Cursor;
TLabel(Sender).Font.Style := Styles;
end;
//
constructor TMyLabel.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FLastLabelColor := clNone;
FLastLabelStyle := [];
FUnderlinedOnEnter := True;
// Color := CColorDefault;
Font.Color := CColorDefault;
HoverColor := CColorHover;
FHoverColorEnabled := True;
FHoverStylesEnabled := True;
// ColorActive := CColorHover;
// ColorClick := CColorClick;
end;
procedure TMyLabel.CMMouseEnter(
var Message: TMessage);
var
HoverColor: TColor;
HoverStyles: TFontStyles;
begin
inherited;
SetLastLabelStyle;
SetLastLabelColor;
if FHoverColorEnabled
then
HoverColor := FHoverColor
else
HoverColor := Self.Font.Color;
if FHoverStylesEnabled
then
begin
HoverStyles := [];
if FUnderlinedOnEnter
then
HoverStyles := HoverStyles + [fsUnderline];
if FItalicOnEnter
then
HoverStyles := HoverStyles + [fsItalic];
end
else
HoverStyles := Self.Font.Style;
SetLabelMouseProperties(Self, HoverColor, HoverStyles, crHandPoint);
if Assigned(FOnMouseEnter)
then
FOnMouseEnter(Self);
end;
procedure TMyLabel.CMMouseLeave(
var Message: TMessage);
begin
inherited;
SetLabelMouseProperties(Self, GetLastLabelColor, GetLastLabelStyle);
if Assigned(FOnMouseLeave)
then
FOnMouseLeave(Self);
end;
procedure register;
begin
RegisterComponents('
MyLabel', [TMyLabel]);
end;
end.