⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jvqofficecolorpanel.pas

📁 East make Tray Icon in delphi
💻 PAS
📖 第 1 页 / 共 2 页
字号:
end;

//=== { TJvSubColorButton } ==================================================

constructor TJvSubColorButton.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FEdgeWidth := 4;
end;

function TJvSubColorButton.GetEdgeWidth: Integer;
begin
  Result := Height div 5;
end;

procedure TJvSubColorButton.Paint;
var
  B, X, Y: Integer;
  FColor: TColor;
begin
  if not Visible then
    Exit;
  inherited Paint;

  if Enabled then
    FColor := ButtonColor
  else
    FColor := clGray;
  if EdgeWidth >= 0 then
    B := EdgeWidth
  else
    B := Height div 5;
  with Canvas do
  begin
    if not Glyph.Empty then
    begin
      Glyph.Transparent := True;
      X := (Width div 2) - 9 + Integer(FState in [TJvButtonState(bsDown)]);
      Y := (Height div 2) + 4 + Integer(FState in [TJvButtonState(bsDown)]);
      Pen.Color := FColor;
      Brush.Color := FColor;
      Brush.Style := bsSolid;
      Rectangle(X, Y, X + 17, Y + 4);
    end
    else
    begin
      if Caption = '' then
      begin
        Pen.Color := clGray;
        Brush.Color := FColor;
        Brush.Style := bsSolid;
        Rectangle(B, B, Width - B, Height - B);
      end
      else
      begin
        Pen.Color := clGray;
        Brush.Style := bsClear;
        Polygon([Point(B - 1, B - 1), Point(Width - (B - 1), B - 1),
          Point(Width - (B - 1), Height - (B - 1)), Point(B - 1, Height - (B - 1))]);
        Pen.Color := clGray;
        Brush.Color := FColor;
        Brush.Style := bsSolid;
        Rectangle(B + 1, B + 1, Height, Height - B);
      end;
    end;
  end;
end;

procedure TJvSubColorButton.SetEdgeWidth(const Value: Integer);
begin
  if FEdgeWidth <> Value then
  begin
    FEdgeWidth := Value;
    Repaint;
  end;
end;

//=== { TJvCustomOfficeColorPanel } ==========================================

constructor TJvCustomOfficeColorPanel.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  ControlStyle := ControlStyle - [csAcceptsControls];
  FInited := False;
  FSelectedColor := clBlack; 
  FClickColorButton := cbctNone;

  FProperties := TJvOfficeColorPanelProperties.Create;
  FProperties.OnPropertiesChanged := PropertiesChanged;

  FAutoButton := TJvSubColorButton.Create(Self);

  with FAutoButton do
  begin
    Parent := Self;
    GroupIndex := 1;
    Tag := MaxColorButtonNumber + 1;
    Down := True;
    AllowAllUp := True;
    ButtonColor := FProperties.AutoColor;
    Hint := ColorToString(ButtonColor);
    Visible := False;
    OnClick := ColorButtonClick;
    OnMouseUp := RedirectToColorButtonClick;
  end;

  FOtherButton := TJvSubColorButton.Create(Self);
  with FOtherButton do
  begin
    Parent := Self;
    GroupIndex := 1;
    Tag := MaxColorButtonNumber + 2;
    ButtonColor := clDefault;
    Hint := ColorToString(ButtonColor);
    AllowAllUp := True;
    Visible := False;
    OnClick := ColorButtonClick;
    OnMouseUp := RedirectToColorButtonClick;
  end;

  FColorDialog := TJvOfficeColorDialog.Create(Self); 

//  Font.Name := 'MS Shell Dlg 2';
  FAutoButton.Flat := True;
  FOtherButton.Flat := True;
  Flat := True;
  SetWordStyle(True);

  MakeColorButtons;

  FInited := True;
end;

destructor TJvCustomOfficeColorPanel.Destroy;
begin
  FProperties.Free;
  inherited Destroy;
end;

procedure TJvCustomOfficeColorPanel.SetButton(Button: TControl);
begin
  FOwner := Button;
end;

procedure TJvCustomOfficeColorPanel.MakeColorButtons;
var
  I: Integer;
begin
  for I := 0 to MaxColorButtonNumber - 1 do
  begin
    FColorButtons[I].Free;
    FColorButtons[I] := TJvSubColorButton.Create(Self);
    with FColorButtons[I] do
    begin
      Parent := Self;
      GroupIndex := 1;
      AllowAllUp := True;
      ButtonColor := SubColorButtonColors[I];
      Tag := I;
      Flat := True;
      Hint := ColorToString(ButtonColor);
      OnClick := ColorButtonClick;
      OnMouseUp := RedirectToColorButtonClick;
    end;
  end;
  Invalidate;
end;

procedure TJvCustomOfficeColorPanel.AdjustColorButtons;
var
  I: Integer;
  ButtonLine: Integer;
  TempHeight: Integer;
  LButtonCount, LColorsButtonTop, LColorsButtonLeft: Integer;
begin
  if (not FInited) or (Parent = nil) then
    Exit;
  DisableAlign;
  TempHeight := 0;
  LButtonCount := 0;
  ButtonLine := (MaxColorButtonNumber + LineColorButtonCount - 1) div LineColorButtonCount;

  if Properties.ShowAutoButton then
    Inc(LButtonCount);
  if Properties.ShowOtherButton then
    Inc(LButtonCount);

  with Properties do
  begin
    Width := HorizontalMargin * 2 + ColorSize * LineColorButtonCount +
      ColorSpace * (LineColorButtonCount - 1);
    Height := TopMargin + BottomMargin + ColorSpaceTop + ColorSpaceBottom + ColorSize * ButtonLine +
      ColorSpace * (ButtonLine - 1) + ButtonHeight * LButtonCount + TempHeight;
  end;

  with Properties do
    if not Properties.ShowAutoButton then
    begin
      FAutoButton.Visible := False;
      LColorsButtonTop := 0;
      LColorsButtonLeft := HorizontalMargin;
    end
    else
    begin
      FAutoButton.Visible := True;
      FAutoButton.Caption := AutoCaption;
      LColorsButtonTop := FAutoButton.Top + FAutoButton.Height;
      FAutoButton.SetBounds(HorizontalMargin, TopMargin + TempHeight,
        ClientWidth - HorizontalMargin * 2, ButtonHeight);
      LColorsButtonLeft := FAutoButton.Left;
    end;

  with Properties do
    for I := 0 to MaxColorButtonNumber - 1 do
      FColorButtons[I].SetBounds(LColorsButtonLeft + (I mod LineColorButtonCount) * (ColorSpace + ColorSize),
        LColorsButtonTop + ColorSpaceTop + (I div LineColorButtonCount) * (ColorSpace + ColorSize),
        ColorSize, ColorSize);

  with Properties do
    if not Properties.ShowOtherButton then
      FOtherButton.Visible := False
    else
    begin
      FOtherButton.Visible := True;
      FOtherButton.Caption := OtherCaption;
      FOtherButton.SetBounds(FAutoButton.Left,
        FColorButtons[MaxColorButtonNumber - 1].Top + ColorSize + ColorSpaceBottom,
        FAutoButton.Width, ButtonHeight);
    end;
end;

procedure TJvCustomOfficeColorPanel.ColorButtonClick(Sender: TObject);

var
  I: Integer;

begin
  if Sender is TJvColorSpeedButton then
  begin
    if TComponent(Sender).Tag = FAutoButton.Tag then
       FClickColorButton := cbctAutoButton
    else
    if TComponent(Sender).Tag = FOtherButton.Tag then
       FClickColorButton := cbctOtherButton
    else
      FClickColorButton := cbctColorsButton;
  end
  else
    FClickColorButton := cbctNone;

  if Assigned(FOnColorButtonClick) then
     FOnColorButtonClick(Sender);

  if TComponent(Sender).Tag = FOtherButton.Tag then
  begin 
    FColorDialog.Color := FSelectedColor;
    if FColorDialog.Execute then
    begin
      SetSelectedColor(FColorDialog.Color);
      FOtherButton.ButtonColor := FSelectedColor;
      FOtherButton.Hint := ColorToString(FOtherButton.ButtonColor);
    end
    else
      Exit;
  end
  else
  begin
    TJvSubColorButton(Sender).Down := True; 
    //in clx have bug
    FAutoButton.Down := FAutoButton = Sender;
    FOtherButton.Down := FOtherButton = Sender;
    for I := 0 to MaxColorButtonNumber - 1 do
      FColorButtons[I].Down := FColorButtons[I] = Sender; 
    SetSelectedColor(TJvSubColorButton(Sender).ButtonColor);
  end;
end;

procedure TJvCustomOfficeColorPanel.SetWordStyle(const Value: Boolean);
begin
  if FWordStyle <> Value then
  begin
    FWordStyle := Value;
    with Properties do
      if FWordStyle then
      begin
        SetFlat(True);

        ButtonHeight := MinButtonHeight;
        ColorSize := MinColorSize;
        ColorSpace := MinColorSpace;
        ColorSpaceTop := MinColorSpaceTop;
        ColorSpaceBottom := MinColorSpaceBottom;
        TopMargin := MinTopMargin;
        BottomMargin := MinBottomMargin;
        HorizontalMargin := MinHorizontalMargin;
      end;
  end;
end;

procedure TJvCustomOfficeColorPanel.SetFlat(const Value: Boolean);
begin
  if FFlat <> Value then
  begin
    FFlat := Value;
    Invalidate;
  end;
end;

procedure TJvCustomOfficeColorPanel.SetSelectedColor(const Value: TColor);
var
  I: Integer;
begin
  if FSelectedColor <> Value then
  begin
    FSelectedColor := Value;
    Color := Value;
    if FAutoButton.ButtonColor = Value then
      FAutoButton.Down := True
    else
    begin
      FAutoButton.Down := False;
      for I := 0 to MaxColorButtonNumber - 1 do
        if FColorButtons[I].ButtonColor = Value then
        begin
          FColorButtons[I].Down := True;
          Break;
        end
        else
          FColorButtons[I].Down := False;
    end;

    if Assigned(FOnColorChange) then
      FOnColorChange(Self);
  end;
end;

function TJvCustomOfficeColorPanel.GetCustomColors: TStrings;
begin
  Result := FColorDialog.CustomColors;
end;

procedure TJvCustomOfficeColorPanel.SetCustomColors(const Value: TStrings);
begin
  FColorDialog.CustomColors.Assign(Value);
end;

procedure TJvCustomOfficeColorPanel.Resize;
begin
  inherited Resize;
  if FInited then
    AdjustColorButtons;
end;

procedure TJvCustomOfficeColorPanel.Paint;
begin
  inherited Paint;
  if FFlat then
  begin
    Canvas.Brush.Color := clBtnFace;  
    FrameRect(Canvas, ClientRect); 
    Canvas.Brush.Color := Color;
  end;
  if FInited then
    AdjustColorButtons;
end;

procedure TJvCustomOfficeColorPanel.SetEnabled( const  Value: Boolean);
var
  I: Integer;
begin
  inherited SetEnabled(Value);
  FAutoButton.Enabled := Value;
  FOtherButton.Enabled := Value;
  for I := 0 to MaxColorButtonNumber - 1 do
    FColorButtons[I].Enabled := Value;
end;

procedure TJvCustomOfficeColorPanel.ShowHintChanged;
var
  I: Integer;
begin
  inherited ShowHintChanged;
  FAutoButton.ShowHint := ShowHint;
  FOtherButton.ShowHint := ShowHint;
  for I := 0 to MaxColorButtonNumber - 1 do
    FColorButtons[I].ShowHint := ShowHint;
end;




procedure TJvCustomOfficeColorPanel.InitWidget;
begin
  inherited InitWidget;
  AdjustColorButtons;
end;


function TJvCustomOfficeColorPanel.GetProperties: TJvOfficeColorPanelProperties;
begin
  Result := FProperties;
end;

procedure TJvCustomOfficeColorPanel.SetProperties(const Value: TJvOfficeColorPanelProperties);
begin
  if FProperties <> Value then
    FProperties.Assign(Value);
end;

procedure TJvCustomOfficeColorPanel.PropertiesChanged(Sender: TObject;
  PropName: string);
var
  LFlag: Boolean;
  I: Integer;
begin
  LFlag := False;
  if Cmp(PropName, cShowAutoButton) or Cmp(PropName, cShowOtherButton) then
    LFlag := True
  else
  if Cmp(PropName, cAutoCaption) then
  begin
    if Properties.AutoCaption = '' then
      Properties.ShowAutoButton := False;
  end
  else
  if Cmp(PropName, cOtherCaption) then
  begin
    if Properties.OtherCaption = '' then
      Properties.ShowOtherButton := False;
  end
  else
  if Cmp(PropName, cAutoHint) then
    FAutoButton.Hint := Properties.AutoHint
  else
  if Cmp(PropName, cOtherHint) then
    FOtherButton.Hint := Properties.OtherHint
  else
  if Cmp(PropName, cAutoColor) then
    FAutoButton.ButtonColor := Properties.AutoColor
  else
  if Cmp(PropName, cShowColorHint) then
  begin
    FAutoButton.ShowHint :=  Properties.ShowColorHint;
    FOtherButton.ShowHint :=  Properties.ShowColorHint;
    for I := 0 to MaxColorButtonNumber - 1 do
      FColorButtons[I].ShowHint := Properties.ShowColorHint;
  end
  else
    LFlag := True;
  if LFlag then
    AdjustColorButtons;
end;

type
  TControlAccessProtected = class(TControl);

procedure TJvCustomOfficeColorPanel.RedirectToColorButtonClick(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
   LParent: TWinControl;
begin
  if Assigned(OnMouseUp) then
    OnMouseUp(Sender, Button, Shift, X, Y);

  // If any of the possible parents has a popup menu, we let it
  // run, and do not select the button (hence the exit), unless
  // the properties tell us to select anyway
  LParent := Self;
  while Assigned(LParent) do
    if Assigned(TControlAccessProtected(LParent).PopupMenu) then
    begin
      if not Properties.SelectIfPopup then
        Exit;
    end
    else
      LParent := LParent.Parent;

  // if the user asked not to right click select, we stop here
  if not Properties.RightClickSelect then
    Exit;

  if Button = mbRight then
    ColorButtonClick(Sender);
end;

//=== { TJvColorSpeedButton } ================================================

constructor TJvColorSpeedButton.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FButtonColor := clDefault;
end;

procedure TJvColorSpeedButton.SetButtonColor(const Value: TColor);
begin
  if FButtonColor <> Value then
  begin
    FButtonColor := Value;
    Invalidate;
  end;
end;

{$IFDEF UNITVERSIONING}
const
  UnitVersioning: TUnitVersionInfo = (
    RCSfile: '$RCSfile: JvQOfficeColorPanel.pas,v $';
    Revision: '$Revision: 1.26 $';
    Date: '$Date: 2004/09/11 21:07:03 $';
    LogPath: 'JVCL\run'
  );

initialization
  RegisterUnitVersion(HInstance, UnitVersioning);

finalization
  UnregisterUnitVersion(HInstance);
{$ENDIF UNITVERSIONING}

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -