procedure Register;
begin
RegisterComponents('Standard', [TXpSkin]);
end;
{ TXpSkin }
procedure TXpSkin.InitItem(AComp: TComponent; AEnable, AUpdate: boolean);
var
i: Integer;
procedure ActiveMenu(AMenuItem: TMenuItem);
var
j: Integer;
begin
for j := 0 to AMenuItem.Count - 1 do
ActiveMenu(AMenuItem.Items[j]);
if AEnable then
begin
AMenuItem.OnAdvancedDrawItem := AdvancedDrawItem;
AMenuItem.OnMeasureItem := MeasureItem;
end
else
begin
AMenuItem.OnAdvancedDrawItem := nil;
AMenuItem.OnMeasureItem := nil;
end;
end;
begin
if AComp.InheritsFrom(TMenu) then
begin
TMenu(AComp).OwnerDraw := AEnable;
for i := 0 to TMenu(AComp).Items.Count - 1 do
ActiveMenu(TMenu(AComp).Items[i]);
end;
if AComp.InheritsFrom(TMenuItem) then
ActiveMenu(TMenuItem(AComp));
end;
procedure TXpSkin.InitItems(AForm: TWinControl; AEnable, AUpdate: boolean);
var
i: integer;
Comp: TComponent;
begin
for i := 0 to AForm.ComponentCount - 1 do
begin
Comp := AForm.Components[i];
InitItem(Comp, AEnable, AUpdate);
end;
end;
procedure TXpSkin.MeasureItem(Sender: TObject; ACanvas: TCanvas;
var Width, Height: Integer);
var
MenuItem: TMenuItem;
ImageList: TCustomImageList;
begin
Inc(Width, I_Indent);
{ MenuItem := TMenuItem(Sender);
ImageList := MenuItem.GetImageList;}
if not IsTopMenu(Sender) then
Inc(Width, I_ImageWidth + I_Indent);
if TMenuItem(Sender).IsLine then
begin
Height := I_SplitHeight;
end
else
Height := IfThen(ACanvas.TextHeight('WWW') > I_ImageHeight,
ACanvas.TextHeight('WWW'), I_ImageHeight);
end;
procedure DrawMenuBackGround;
begin
TopMenu := IsTopMenu(MenuItem);
Selected := odSelected in State;
if Selected or (odHotLight in State) then
begin
ACanvas.Brush.Color := $00EFD3C6;
ACanvas.Pen.Color := clBlue;
ACanvas.Rectangle(ARect);
end
else
begin
ACanvas.Brush.Color := clMenu;
ACanvas.FillRect(ARect);
if not TopMenu then
begin
ACanvas.Brush.Color := clWindow;
FRect := ARect;
Inc(FRect.Left, I_ImageWidth);
ACanvas.FillRect(FRect);
end;
end;
end;
procedure DrawMenuText;
begin
FRect := ARect;
Inc(FRect.Left, I_Indent);
if not TopMenu then
Inc(FRect.Left, I_ImageWidth);
sText := MenuItem.Caption;
if MenuItem.IsLine then
begin
DrawMenuSplitLine;
Exit;
end;
if not MenuItem.Enabled then
ACanvas.Font.Color := clGray
else
ACanvas.Font.Color := clMenuText;
DrawText(ACanvas.Handle, PChar(sText), Length(sText),
FRect, DT_LEFT or DT_SINGLELINE or DT_VCENTER);
if MenuItem.ShortCut <> 0 then
begin
Dec(FRect.Right, I_Indent * 3);
sText := ShortCutToText(MenuItem.ShortCut);
DrawText(ACanvas.Handle, PChar(sText),
Length(sText), FRect, DT_RIGHT or DT_SINGLELINE or DT_VCENTER);
end;
end;
procedure DrawImageIcon;
begin
if DrawImage then
begin
FRect := ARect;
if Assigned(ImageList) and (MenuItem.ImageIndex > -1) and
(MenuItem.ImageIndex < ImageList.Count) and not MenuItem.Checked then
begin
Inc(FRect.Left, (I_ImageWidth - ImageList.Width) div 2);
Inc(FRect.Top, (I_ImageHeight - ImageList.Height) div 2);
FRect.Right := FRect.Left + I_ImageWidth;
ImageList.Draw(ACanvas, FRect.Left, FRect.Top, MenuItem.ImageIndex,
MenuItem.Enabled);
end
else
begin
{ Draw a menu check }
FRect.Right := FRect.Left + I_ImageWidth;
Glyph := TBitmap.Create;
try
Glyph.Transparent := True;
Glyph.Handle := LoadBitmap(0, PChar(OBM_CHECK));
OldBrushColor := ACanvas.Font.Color;
ACanvas.Font.Color := clBtnText;
ACanvas.Draw(FRect.Left + (FRect.Right - FRect.Left - Glyph.Width) div 2 + 1,
FRect.Top + (FRect.Bottom - FRect.Top - Glyph.Height) div 2 + 1, Glyph);
ACanvas.Font.Color := OldBrushColor;
finally
Glyph.Free;
end;
end;
end;
end;
begin
MenuItem := TMenuItem(Sender);
ImageList := MenuItem.GetImageList;
DrawImage := (ImageList <> nil) and (MenuItem.ImageIndex in [0..ImageList.Count - 1])
or MenuItem.Checked;
DrawMenuBackGround;
DrawMenuText;
DrawImageIcon;
end;
destructor TXpSkin.Destroy;
begin
InitItems(FForm, False, False);
inherited;
end;
constructor TXpSkin.Create(AOwner: TComponent);
begin
inherited;
SetDesigning(True);
FForm := AOwner as TForm;
end;