📄 qiswitchpanel.pas
字号:
procedure TiSwitchPanel.iPaintTo(Canvas: TCanvas);
var
DrawRect : TRect;
MaxHeight : Integer;
PanelHeight : Integer;
MarginTopPixels : Integer;
MarginRowPixels : Integer;
MarginBottomPixels : Integer;
Row1Top : Integer;
Row2Top : Integer;
ARect : TRect;
AText : String;
ATextWidth : Integer;
ATextHeight : Integer;
CenterX1 : Integer;
CenterX2 : Integer;
CenterX3 : Integer;
TopY1 : Integer;
TopY2 : Integer;
TopY3 : Integer;
PanelRect : TRect;
KnobInnerRadius : Integer;
HandlePoint1 : TPoint;
HandlePoint2 : TPoint;
HandlePoint3 : TPoint;
HandlePoint4 : TPoint;
KnobAngle : Double;
TempCenterPoint : TPoint;
begin
with Canvas, DrawRect do
begin
DrawBackGround(Canvas, BackGroundColor);
DrawRect := Rect(0, 0, Width-1, Height-1);
Font.Assign(FPanelFontInactive);
MaxHeight := TextHeight('A');
Font.Assign(FPanelFontActive);
if TextHeight('A') > MaxHeight then
begin
MaxHeight := TextHeight('A');
end;
MarginTopPixels := Round(MaxHeight * FPanelMarginTop);
MarginRowPixels := Round(MaxHeight * FPanelMarginRow);
MarginBottomPixels := Round(MaxHeight * FPanelMarginBottom);
Row1Top := Top + MarginTopPixels;
Row2Top := Row1Top + MaxHeight + MarginRowPixels;
case PositionCount of
2 : begin
CenterX1 := Width div 4;
CenterX2 := (Width *3) div 4;
CenterX3 := 0;
TopY1 := Row1Top;
TopY2 := Row1Top;
TopY3 := 0;
PanelHeight := Row1Top + MaxHeight + MarginBottomPixels;
case Position of
0 : KnobAngle := 120.0;
else KnobAngle := 60.0;
end;
end;
else begin
CenterX1 := Width div 4;
CenterX2 := Width div 2;
CenterX3 := (Width *3) div 4;
TopY1 := Row2Top;
TopY2 := Row1Top;
TopY3 := Row2Top;
PanelHeight := Row2Top + MaxHeight + MarginBottomPixels;
case Position of
0 : KnobAngle := 120.0;
1 : KnobAngle := 90.0;
else KnobAngle := 60.0;
end;
end;
end;
PanelRect := Rect(DrawRect.Left, DrawRect.Top, DrawRect.Right, DrawRect.Top + PanelHeight);
FKnobCenterPoint.X := Width div 2;
FKnobCenterPoint.Y := (Bottom + PanelRect.Bottom) div 2;
if KnobRadiusAutoSize then
begin
FKnobRadius := Width div 2 - FKnobSideMargin;
if (Height - PanelRect.Bottom) div 2 - FKnobSideMargin < FKnobRadius then
FKnobRadius := (Height - PanelRect.Bottom) div 2 - FKnobSideMargin;
end;
KnobInnerRadius := FKnobRadius - KnobHandleWidth;
TempCenterPoint := GetXYRadPoint(KnobAngle, KnobInnerRadius, FKnobCenterPoint);
HandlePoint1 := GetXYRadPoint(KnobAngle + 90, KnobHandleWidth div 2 + 1, TempCenterPoint );
HandlePoint2 := GetXYRadPoint(KnobAngle - 90, KnobHandleWidth div 2 + 1, TempCenterPoint );
TempCenterPoint := GetXYRadPoint(KnobAngle + 180, KnobInnerRadius, FKnobCenterPoint);
HandlePoint4 := GetXYRadPoint(KnobAngle + 90, KnobHandleWidth div 2 + 1, TempCenterPoint );
HandlePoint3 := GetXYRadPoint(KnobAngle - 90, KnobHandleWidth div 2 + 1, TempCenterPoint );
Brush.Style := bsSolid;
Pen.Style := psSolid;
Brush.Color := FPanelBackGroundColor;
Pen.Color := FPanelBackGroundColor;
Rectangle(PanelRect.Left, PanelRect.Top, PanelRect.Right+1, PanelRect.Bottom+1);
case FBorderStyle of
ispbsRaised : DrawBorder(Canvas);
ispbsLowered : DrawBorder(Canvas);
ispbsLine : begin
Brush.Style := bsClear;
Pen.Color := clBlack;
Pen.Style := psSolid;
Rectangle(Left, Top, Right+1, Bottom+1);
end;
ispbsTop : if (FKnobSideMargin = 0) then
begin
Brush.Color := FBackGroundColorTop;
Pen.Color := FBackGroundColorTop;
Rectangle(PanelRect.Left, PanelRect.Bottom, PanelRect.Right, FKnobCenterPoint.Y);
Pen.Color := clBlack;
PolyLine([Point(PanelRect.Left, PanelRect.Bottom), Point(PanelRect.Left, FKnobCenterPoint.Y)]);
PolyLine([Point(PanelRect.Right, PanelRect.Bottom), Point(PanelRect.Right, FKnobCenterPoint.Y)]);
end;
end;
Brush.Style := bsClear;
if PositionCount > 0 then
begin
if Position = 0 then Font.Assign(FPanelFontActive) else Font.Assign(FPanelFontInactive);
AText := FPositionCaption1;
ARect := Rect(CenterX1, TopY1, CenterX1, TopY1);
iDrawText(Canvas, AText, ARect, [itfVTop, itfHCenter, itfNoClip]);
ATextHeight := TextHeight(AText);
ATextWidth := TextWidth(AText);
FCaptionRect1 := Rect(ARect.Left - ATextWidth div 2, ARect.Top,
ARect.Left + ATextWidth div 2, ARect.Top + ATextHeight);
end;
if PositionCount > 1 then
begin
if Position = 1 then Font.Assign(FPanelFontActive) else Font.Assign(FPanelFontInactive);
AText := FPositionCaption2;
ARect := Rect(CenterX2, TopY2, CenterX2, TopY2);
iDrawText(Canvas, AText, ARect, [itfVTop, itfHCenter, itfNoClip]);
ATextHeight := TextHeight(AText);
ATextWidth := TextWidth(AText);
FCaptionRect2 := Rect(ARect.Left - ATextWidth div 2, ARect.Top,
ARect.Left + ATextWidth div 2, ARect.Top + ATextHeight);
end;
if PositionCount > 2 then
begin
if Position = 2 then Font.Assign(FPanelFontActive) else Font.Assign(FPanelFontInactive);
AText := FPositionCaption3;
ARect := Rect(CenterX3, TopY3, CenterX3, TopY3);
iDrawText(Canvas, AText, ARect, [itfVTop, itfHCenter, itfNoClip]);
ATextHeight := TextHeight(AText);
ATextWidth := TextWidth(AText);
FCaptionRect3 := Rect(ARect.Left - ATextWidth div 2, ARect.Top,
ARect.Left + ATextWidth div 2, ARect.Top + ATextHeight);
end;
Brush.Style := bssolid;
Pen.Style := psSolid;
Pen.Color := clBlack;
Brush.Color := KnobOuterColor;
FKnobOuterRect := Rect(FKnobCenterPoint.X - KnobRadius, FKnobCenterPoint.Y - KnobRadius,
FKnobCenterPoint.X + KnobRadius, FKnobCenterPoint.Y + KnobRadius);
Ellipse(FKnobOuterRect.Left, FKnobOuterRect.Top, FKnobOuterRect.Right, FKnobOuterRect.Bottom);
Brush.Color := KnobInnerColor;
FKnobInnerRect := Rect(FKnobCenterPoint.X - KnobInnerRadius, FKnobCenterPoint.Y - KnobInnerRadius,
FKnobCenterPoint.X + KnobInnerRadius, FKnobCenterPoint.Y + KnobInnerRadius);
Ellipse(FKnobInnerRect.Left, FKnobInnerRect.Top, FKnobInnerRect.Right, FKnobInnerRect.Bottom);
Brush.Color := KnobHandleColor;
Polygon([HandlePoint1, HandlePoint2, HandlePoint3, HandlePoint4]);
end;
end;
//****************************************************************************************************************************************************
procedure TiSwitchPanel.iDoKillFocus;
begin
FMouseDown := False;
FKeyDown := False;
FMouseDownKnob := False;
FMouseDownCaption1 := False;
FMouseDownCaption2 := False;
FMouseDownCaption3 := False;
inherited;
end;
//****************************************************************************************************************************************************
procedure TiSwitchPanel.iMouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
SetFocus;
if PositionCount > 0 then if PtInRect(FCaptionRect1, Point(X, Y)) then FMouseDownCaption1 := True;
if PositionCount > 1 then if PtInRect(FCaptionRect2, Point(X, Y)) then FMouseDownCaption2 := True;
if PositionCount > 2 then if PtInRect(FCaptionRect3, Point(X, Y)) then FMouseDownCaption3 := True;
if PtInRect(FKnobOuterRect, Point(X, Y)) then
begin
FMouseDownKnob := True;
if FMouseControlStyle = ispmcsRotate then FMouseDownDegrees := RadToDeg(ArcTan2(Y-FKnobCenterPoint.y,X-FKnobCenterPoint.x));
end;
end;
//****************************************************************************************************************************************************
procedure TiSwitchPanel.iMouseMove(Shift: TShiftState; X, Y: Integer);
var
CurrentDegrees : Double;
DeltaAngle : Double;
begin
FUserGenerated := True;
try
if FMouseDownKnob then if FMouseControlStyle = ispmcsRotate then
begin
CurrentDegrees := RadToDeg(ArcTan2(Y-FKnobCenterPoint.y,X-FKnobCenterPoint.x));
DeltaAngle := FMouseDownDegrees - CurrentDegrees;
if DeltaAngle > 200 then DeltaAngle := DeltaAngle - 360;
if DeltaAngle < -200 then DeltaAngle := DeltaAngle + 360;
if DeltaAngle > 22.5 then
begin
Position := Position - 1;
FMouseDownDegrees := CurrentDegrees - 22.5;
end
else if DeltaAngle < -22.5 then
begin
Position := Position + 1;
FMouseDownDegrees := CurrentDegrees + 22.5;
end;
end;
finally
FUserGenerated := False;
end;
end;
//****************************************************************************************************************************************************
procedure TiSwitchPanel.iMouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
NewPosisition : Integer;
begin
FUserGenerated := True;
try
if FMouseDownCaption1 then if PtInRect(FCaptionRect1, Point(X, Y)) then Position := 0;
if FMouseDownCaption2 then if PtInRect(FCaptionRect2, Point(X, Y)) then Position := 1;
if FMouseDownCaption3 then if PtInRect(FCaptionRect3, Point(X, Y)) then Position := 2;
if FMouseDownKnob then if FMouseControlStyle = ispmcsIncrement then if PtInRect(FKnobOuterRect, Point(X, Y)) then
begin
NewPosisition := FPosition + 1;
if NewPosisition > PositionCount -1 then NewPosisition := 0;
Position := NewPosisition;
end;
FMouseDown := False;
FKeyDown := False;
FMouseDownKnob := False;
FMouseDownCaption1 := False;
FMouseDownCaption2 := False;
FMouseDownCaption3 := False;
finally
FUserGenerated := False;
end;
end;
//****************************************************************************************************************************************************
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -