📄 jvspeedbar.pas
字号:
Result := -1;
for I := 0 to FSections.Count - 1 do
if Sections[I].Caption = ACaption then
begin
Result := I;
Exit;
end;
end;
function TJvSpeedBar.AppendSection(Value: TJvSpeedBarSection): Integer;
var
UniqueName: string;
I: Integer;
begin
I := 0;
UniqueName := Value.Caption;
while SearchSection(UniqueName) >= 0 do
begin
Inc(I);
UniqueName := Value.Caption + Format(' (%d)', [I]);
end;
Value.Caption := UniqueName;
Result := FSections.Add(Value);
if Result >= 0 then
begin
Value.FParent := Self;
for I := 0 to Value.Count - 1 do
begin
Value[I].FSection := Result;
SetItemParams(Value[I], not (csLoading in ComponentState));
end;
end;
end;
function TJvSpeedBar.AddSection(const ACaption: string): Integer;
var
Section: TJvSpeedBarSection;
begin
if Owner <> nil then
Section := TJvSpeedBarSection.Create(Owner)
else
Section := TJvSpeedBarSection.Create(Self);
Section.Caption := ACaption;
Result := AppendSection(Section);
end;
procedure TJvSpeedBar.SetItemParams(Item: TJvSpeedItem; InitBounds: Boolean);
begin
with Item do
begin
FParent := Self;
with FButton do
begin
if InitBounds then
SetBounds(0, 0, BtnWidth, BtnHeight);
Style := FButtonStyle;
Flat := (sbFlatBtns in Options);
Transparent := (sbTransparentBtns in Options);
GrayedInactive := (sbGrayedBtns in Options);
end;
SetEditing(FEditWin <> NullHandle);
end;
end;
function TJvSpeedBar.NewItem(AOwner: TComponent; Section: Integer;
const AName: string): TJvSpeedItem;
begin
Result := nil;
if (Section >= 0) and (Section < FSections.Count) then
begin
Result := TJvSpeedItem.Create(AOwner);
try
Sections[Section].FList.Add(Result);
Result.FSection := Section;
SetItemParams(Result, True);
if AName <> '' then
with Result do
begin
Name := AName;
Caption := AName;
FButton.Visible := False;
FButton.Parent := Self;
end;
except
Result.Free;
raise;
end;
end;
end;
procedure TJvSpeedBar.AddItem(Section: Integer; Item: TJvSpeedItem);
var
I, Index: Integer;
begin
if FindItem(Item, I, Index) then
begin
Sections[I].FList.Delete(Index);
if Section >= FSections.Count then
Section := FSections.Count - 1;
Sections[Section].FList.Add(Item);
Item.FSection := Section;
Exit;
end;
if (Section >= 0) and (Item <> nil) then
begin
if Assigned(FOnAddItem) then
begin
FOnAddItem(Item);
Section := Item.FSection;
end;
if FSections.Count = 0 then
Section := AddSection('')
else
if Section >= FSections.Count then
Section := FSections.Count - 1;
Sections[Section].FList.Add(Item);
Item.FSection := Section;
SetItemParams(Item, not (csLoading in ComponentState));
Item.FButton.Visible := False;
Item.FButton.Parent := Self;
end;
end;
function TJvSpeedBar.FindItem(Item: TJvSpeedItem; var Section,
Index: Integer): Boolean;
var
I: Integer;
begin
Result := False;
Section := -1;
for I := 0 to FSections.Count - 1 do
if FSections[I] <> nil then
begin
Index := Sections[I].FList.IndexOf(Item);
if Index >= 0 then
begin
Section := I;
Result := True;
Exit;
end;
end;
end;
procedure TJvSpeedBar.AlignItemsToGrid;
begin
ForEachItem(AlignItemToGrid, 0);
end;
procedure TJvSpeedBar.AlignItemToGrid(Item: TJvSpeedItem; Data: Longint);
begin
if Item.Visible then
begin
if GetOrientation = boVertical then
begin
Item.Left := Trunc((Item.Left - FOffset.X) / FGridSize.X) * FGridSize.X + FOffset.X;
Item.Top := Round((Item.Top - FOffset.Y) / FGridSize.Y) * FGridSize.Y + FOffset.Y;
end
else
begin
Item.Left := Round((Item.Left - FOffset.X) / FGridSize.X) * FGridSize.X + FOffset.X;
Item.Top := Trunc((Item.Top - FOffset.Y) / FGridSize.Y) * FGridSize.Y + FOffset.Y;
end;
end;
end;
function TJvSpeedBar.AcceptDropItem(Item: TJvSpeedItem; X, Y: Integer): Boolean;
var
I, Sect: Integer;
begin
Result := False;
if FindItem(Item, Sect, I) then
begin
if GetOrientation = boVertical then
begin
X := Trunc((X - FOffset.X) / FGridSize.X) * FGridSize.X + FOffset.X;
Y := Round((Y - FOffset.Y) / FGridSize.Y) * FGridSize.Y + FOffset.Y;
end
else
begin
X := Round((X - FOffset.X) / FGridSize.X) * FGridSize.X + FOffset.X;
Y := Trunc((Y - FOffset.Y) / FGridSize.Y) * FGridSize.Y + FOffset.Y;
end;
Item.Left := X;
Item.Top := Y;
Result := PtInRect(ClientRect, Point(X, Y));
if Result then
Item.FButton.BringToFront
else
Item.FButton.SendToBack;
Item.Visible := Result;
end;
end;
procedure TJvSpeedBar.SetItemEditing(Item: TJvSpeedItem; Data: Longint);
begin
Item.SetEditing(FEditWin <> NullHandle);
end;
function TJvSpeedBar.GetEditing: Boolean;
begin
Result := (FEditWin <> NullHandle);
end;
procedure TJvSpeedBar.SetEditing(Win: HWND);
begin
FEditWin := Win;
ForEachItem(SetItemEditing, 0);
if (FEditWin = NullHandle) and not (csDesigning in ComponentState) then
AfterCustomize;
end;
procedure TJvSpeedBar.Paint;
var
XCnt, YCnt, X, Y: Integer;
BevelSize, SaveIndex: Integer;
Rect: TRect;
C1, C2: TColor;
procedure BevelLine(C: TColor; X1, Y1, X2, Y2: Integer);
begin
with Canvas do
begin
Pen.Color := C;
MoveTo(X1, Y1);
LineTo(X2, Y2);
end;
end;
begin
if not FLocked then
begin
Rect := ClientRect;
BevelSize := BorderWidth;
if BevelOuter <> bvNone then
Inc(BevelSize, BevelWidth);
if BevelInner <> bvNone then
Inc(BevelSize, BevelWidth);
InflateRect(Rect, -BevelSize, -BevelSize);
inherited Paint;
Canvas.Brush.Color := Color;
{$IFDEF JVCLThemesEnabled}
if ThemeServices.ThemesEnabled and ParentBackground then
begin
Canvas.Brush.Color := Parent.Brush.Color;
DrawThemedBackground(Self, Canvas, Rect);
end
else
{$ENDIF JVCLThemesEnabled}
Canvas.FillRect(Rect);
if (FWallpaper.Graphic <> nil) and (FWallpaper.Width > 0) and
(FWallpaper.Height > 0) then
begin
SaveIndex := SaveDC(Canvas.Handle);
try
with Rect do
IntersectClipRect(Canvas.Handle, Left, Top, Right - Left +
BevelSize, Bottom - Top + BevelSize);
if sbStretchBitmap in Options then
Canvas.StretchDraw(Rect, FWallpaper.Graphic)
else
begin
XCnt := (ClientWidth - 2 * BevelSize) div FWallpaper.Width;
YCnt := (ClientHeight - 2 * BevelSize) div FWallpaper.Height;
for X := 0 to XCnt do
for Y := 0 to YCnt do
Canvas.Draw(Rect.Left + X * FWallpaper.Width,
Rect.Top + Y * FWallpaper.Height, FWallpaper.Graphic);
end;
finally
RestoreDC(Canvas.Handle, SaveIndex);
end;
end;
if FBoundLines <> [] then
begin
C1 := clBtnShadow;
C2 := clBtnHighlight;
if blTop in FBoundLines then
begin
BevelLine(C1, Rect.Left, Rect.Top, Rect.Right, Rect.Top);
BevelLine(C2, Rect.Left, Rect.Top + 1, Rect.Right, Rect.Top + 1);
end;
if blLeft in FBoundLines then
begin
BevelLine(C1, Rect.Left, Rect.Top, Rect.Left, Rect.Bottom);
BevelLine(C2, Rect.Left + 1, Rect.Top + Ord(blTop in FBoundLines), Rect.Left + 1, Rect.Bottom);
end;
if blBottom in FBoundLines then
begin
BevelLine(C1, Rect.Left, Rect.Bottom - 2, Rect.Right, Rect.Bottom - 2);
BevelLine(C2, Rect.Left, Rect.Bottom - 1, Rect.Right, Rect.Bottom - 1);
end;
if blRight in FBoundLines then
begin
BevelLine(C1, Rect.Right - 2, Rect.Top, Rect.Right - 2, Rect.Bottom - Ord(blBottom in FBoundLines));
BevelLine(C2, Rect.Right - 1, Rect.Top, Rect.Right - 1, Rect.Bottom);
end;
end;
end;
end;
procedure TJvSpeedBar.ApplyOrientation(Value: TBarOrientation);
begin
if (GetOrientation <> Value) and not (csReading in ComponentState) then
begin
FLocked := True;
try
FOrientation := Value;
SwapInt(Integer(FButtonSize.X), Integer(FButtonSize.Y));
SwapInt(Integer(FGridSize.X), Integer(FGridSize.Y));
SwapInt(Integer(FOffset.X), Integer(FOffset.Y));
ForEachItem(SwapItemBounds, 0);
finally
FLocked := False;
Invalidate;
end;
if FEditWin <> NullHandle then
SendMessage(FEditWin, CM_SPEEDBARCHANGED, SBR_BTNSIZECHANGED, Longint(Self));
end;
end;
procedure TJvSpeedBar.SetOrientation(Value: TBarOrientation);
begin
if GetOrientation <> Value then
begin
if FPosition = bpAuto then
raise EJvSpeedbarError.CreateRes(@RsEAutoSpeedbarMode);
ApplyOrientation(Value);
end;
end;
function TJvSpeedBar.GetOrientation: TBarOrientation;
begin
if FPosition = bpCustom then
Result := FOrientation
else
case Align of
alLeft, alRight:
Result := boVertical;
alTop, alBottom:
Result := boHorizontal;
else
Result := FOrientation;
end;
end;
function TJvSpeedBar.GetAlign: TAlign;
begin
Result := FAlign;
end;
procedure TJvSpeedBar.SetAlign(Value: TAlign);
var
X, Y: Integer;
begin
{ fix previous version error }
if (csLoading in ComponentState) and (Value = alNone) and
(Position = bpAuto) then
FFix := True;
if Align <> Value then
begin
X := Width;
Y := Height;
if (FPosition = bpAuto) and (Value in [alClient, alNone]) then
raise EJvSpeedbarError.CreateRes(@RsEAutoSpeedbarMode);
inherited Align := Value;
if csLoading in ComponentState then
begin
Width := X;
Height := Y;
end;
if FPosition = bpAuto then
case Value of
alLeft, alRight:
ApplyOrientation(boVertical);
alTop, alBottom:
ApplyOrientation(boHorizontal);
else
if not (csLoading in ComponentState) then
raise EJvSpeedbarError.CreateRes(@RsEAutoSpeedbarMode);
end;
FAlign := inherited Align;
end;
end;
{$IFDEF VisualCLX}
procedure TJvSpeedBar.ChangeScale(M, D, MH, DH: Integer);
{$ENDIF VisualCLX}
{$IFDEF VCL}
procedure TJvSpeedBar.ChangeScale(M, D: Integer);
{$ENDIF VCL}
var
Flags: TSbScaleFlags;
begin
DisableAlign;
try
if csLoading in ComponentState then
Flags := ScaleFlags
else
Flags := [sfOffsetX, sfOffsetY, sfBtnSizeX, sfBtnSizeY];
{$IFDEF VCL}
if (sfBtnSizeX in Flags) and not (csFixedWidth in ControlStyle) then
FButtonSize.X := MulDiv(FButtonSize.X, M, D);
if sfOffsetX in Flags then
FOffset.X := MulDiv(FOffset.X, M, D);
{$ENDIF VCL}
{$IFDEF VisualCLX}
if (sfBtnSizeX in Flags) and not (csFixedWidth in ControlStyle) then
FButtonSize.X := MulDiv(FButtonSize.X, MH, DH);
if sfOffsetX in Flags then
FOffset.X := MulDiv(FOffset.X, MH, DH);
{$ENDIF VisualCLX}
if (sfBtnSizeY in Flags) and not (csFixedHeight in ControlStyle) then
FButtonSize.Y := MulDiv(FButtonSize.Y, M, D);
if sfOffsetY in Flags then
FOffset.Y := MulDiv(FOffset.Y, M, D);
UpdateGridSize;
inherited ChangeScale(M, D {$IFDEF VisualCLX}, MH, DH {$ENDIF});
ApplyButtonSize;
AlignItemsToGrid;
FScaleFlags := [];
finally
EnableAlign;
end;
end;
procedure TJvSpeedBar.AlignControls(AControl: TControl; var Rect: TRect);
var
P: TPoint;
Min: Integer;
begin
if FBoundLines <> [] then
begin
if blTop in FBoundLines then
Inc(Rect.Top, 2);
if blBottom in FBoundLines then
Dec(Rect.Bottom, 2);
if blLeft in FBoundLines then
Inc(Rect.Left, 2);
if blRight in FBoundLines then
Dec(Rect.Right, 2);
end;
inherited AlignControls(AControl, Rect);
Min := MinButtonsOffset;
if FOffset.X < Min then
begin
P.X := Min - FOffset.X;
FOffset.X := Min;
end
else
P.X := 0;
if FOffset.Y < Min then
begin
P.Y := Min - FOffset.Y;
FOffset.Y := Min;
end
else
P.Y := 0;
if not (csLoading in Comp
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -