areparag.pas

来自「delphi编程控件」· PAS 代码 · 共 790 行 · 第 1/2 页

PAS
790
字号
    else
      if NewValue > FMaxValue then
        Result := FMaxValue;
  end;
end;

function TAutoSpinEdit.GetMinHeight: Integer;
var
  DC: HDC;
  SaveFont: HFont;
  I: Integer;
  SysMetrics, Metrics: TTextMetric;
begin
  DC := GetDC(0);
  GetTextMetrics(DC, SysMetrics);
  SaveFont := SelectObject(DC, Font.Handle);
  GetTextMetrics(DC, Metrics);
  SelectObject(DC, SaveFont);
  ReleaseDC(0, DC);
  I := SysMetrics.tmHeight;
  if I > Metrics.tmHeight then I := Metrics.tmHeight;
  Result := Metrics.tmHeight + I div 4 +
    GetSystemMetrics(SM_CYBORDER) * 4 + 2;
end;

procedure TAutoSpinEdit.SetEditRect;
var
  Loc: TRect;
begin
  SendMessage(Handle, EM_GETRECT, 0, LongInt(@Loc));
  Loc.Bottom := ClientHeight + 1;  { +1 is workaround for windows paint bug }
  Loc.Right := ClientWidth - FButton.Width - 2;
  Loc.Top := 0;
  Loc.Left := 0;
  SendMessage(Handle, EM_SETRECTNP, 0, LongInt(@Loc));
end;

function TAutoSpinEdit.GetValue: TFloat;
var
  S: string;
  Code, P: Integer;
begin
  try
    S := Text;
    while (Length(S) > 0) and (S[1] = ' ') do Delete(S, 1, 1);
    repeat
      P := Pos(DecimalSeparator, S);
      if P > 0 then S[P] := '.';
    until P = 0;
    repeat
      Val(S, Result, Code);
      if Code > 0 then S := Copy(S, 1, Code - 1);
    until (Code = 0) or (Length(S) = 0);
    if S = '' then Result := 0
    else
      repeat
        P := Pos('.', S);
        if P > 0 then S[P] := DecimalSeparator;
      until P = 0;
  except
    Result := FMinValue;
  end;
end;

procedure TAutoSpinEdit.SetValue(NewValue: TFloat);
var
  S: string;
begin
  S := FloatToStr(Round(CheckValue(NewValue) * 100) / 100);
  if Prefix = '' then Text := S
  else Text := S + ' ' + Prefix;
end;

procedure TAutoSpinEdit.WMSize(var Message: TWMSize);
var
  MinHeight: Integer;
begin
  inherited;
  MinHeight := GetMinHeight;
  { text edit bug: if size to less than minheight, then edit ctrl does
    not display the text }
  if Height < MinHeight then Height := MinHeight
  else
    if FButton <> nil then
    begin
      FButton.SetBounds(Width - FButton.Width - 5, 0,
        FButton.Width, Height - 5);
      SetEditRect;
    end;
end;

procedure TAutoSpinEdit.CMEnter(var Message: TCMGotFocus);
begin
  if AutoSelect and not (csLButtonDown in ControlState) then
    SelectAll;
  inherited;
end;

procedure TAutoSpinEdit.CMExit(var Message: TCMExit);
begin
  inherited;
  if CheckValue(Value) <> Value then SetValue(Value);
end;

procedure TAutoSpinEdit.UpClick(Sender: TObject);
begin
  Value := Value + FIncrement;
end;

procedure TAutoSpinEdit.DownClick(Sender: TObject);
begin
  Value := Value - FIncrement;
end;

procedure TAutoSpinEdit.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);
  Params.Style := Params.Style or ES_MULTILINE or WS_CLIPCHILDREN;
end;

procedure TAutoSpinEdit.CreateWnd;
begin
  inherited CreateWnd;
  SetEditRect;
end;

procedure TAutoSpinEdit.KeyDown(var Key: Word; Shift: TShiftState);
begin
  if Key = VK_UP then UpClick (Self)
  else
    if Key = VK_DOWN then DownClick (Self);
  inherited KeyDown(Key, Shift);
end;

procedure TAutoSpinEdit.KeyPress(var Key: Char);
begin
  if Key = #13 then Key := #0
  else inherited KeyPress(Key);
end;

{ TAutoTimerSpeedButton }

destructor TAutoTimerSpeedButton.Destroy;
begin
  if FRepeatTimer <> nil then
    FRepeatTimer.Free;
  inherited Destroy;
end;

procedure TAutoTimerSpeedButton.MouseDown(Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  inherited MouseDown (Button, Shift, X, Y);
  if tbAllowTimer in FTimeBtnState then
  begin
    if FRepeatTimer = nil then
      FRepeatTimer := TTimer.Create(Self);
    FRepeatTimer.OnTimer := TimerExpired;
    FRepeatTimer.Interval := InitRepeatPause;
    FRepeatTimer.Enabled  := True;
  end;
end;

procedure TAutoTimerSpeedButton.MouseUp(Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  inherited MouseUp (Button, Shift, X, Y);
  if FRepeatTimer <> nil then
    FRepeatTimer.Enabled  := False;
end;

procedure TAutoTimerSpeedButton.TimerExpired(Sender: TObject);
begin
  FRepeatTimer.Interval := RepeatPause;
  if (FState = bsDown) and MouseCapture then
  begin
    try
      Click;
    except
      FRepeatTimer.Enabled := False;
      raise;
    end;
  end;
end;

procedure TAutoTimerSpeedButton.Paint;
var
  R: TRect;
begin
  inherited Paint;
  if tbFocusRect in FTimeBtnState then
  begin
    R := Bounds(0, 0, Width, Height);
    InflateRect(R, -3, -3);
    if FState = bsDown then
      OffsetRect(R, 1, 1);
    DrawFocusRect(Canvas.Handle, R);
  end;
end;

{ procedures }

function TwipsToCm(Value: Integer): TFloat;
begin
  Result := Value / 1440 * 2.538;
end;

function CmToTwips(Value: TFloat): Integer;
begin
  Result := Round(Value / 2.538 * 1440);
end;

procedure AREParagraphEditor(const Paragraph: TAutoParaAttributes);
var
  AREParagraphForm: TAREParagraphForm;
begin
  AREParagraphForm := TAREParagraphForm.Create(nil);
  with AREParagraphForm, Paragraph do
  begin
    AlignmentComboBox.ItemIndex := Ord(Alignment);

    if LeftIndentInTwips <= 0 then
    begin
      LeftIndentSpinEdit.Value :=
        TwipsToCm(FirstIndentInTwips + LeftIndentInTwips);
      if LeftIndentInTwips = 0 then SpecialComboBox.ItemIndex := 0
      else SpecialComboBox.ItemIndex := 1;
      SpecialComboBoxChange(nil);
      if LeftIndentInTwips = 0 then SpecialSpinEdit.Text := ''
      else SpecialSpinEdit.Value := TwipsToCm(-LeftIndentInTwips);
    end
    else
    begin
      LeftIndentSpinEdit.Value := TwipsToCm(FirstIndentInTwips);
      SpecialComboBox.ItemIndex := 2;
      SpecialComboBoxChange(nil);
      SpecialSpinEdit.Value := TwipsToCm(LeftIndentInTwips);
    end;
    RightIndentSpinEdit.Value := TwipsToCm(RightIndentInTwips);

    SpaceBeforeSpinEdit.Value := SpaceBeforeInTwips / 20;
    SpaceAfterSpinEdit.Value := SpaceAfterInTwips / 20;
    LineSpacingRuleComboBox.ItemIndex := Byte(LineSpacingRule);
    LineSpacingRuleComboBoxChange(nil);
    if LineSpacingRuleComboBox.ItemIndex > 2 then
      LineSpacingSpinEdit.Value := LineSpacingInTwips / 20;

    if ShowModal = mrOk then
    begin
      Alignment := TParagraphAlignment(AlignmentComboBox.ItemIndex);

      case SpecialComboBox.ItemIndex of
        0: begin
             FirstIndentInTwips := CmToTwips(LeftIndentSpinEdit.Value);
             LeftIndentInTwips := 0;
           end;
        1: begin
             FirstIndentInTwips :=
               CmToTwips(LeftIndentSpinEdit.Value + SpecialSpinEdit.Value);
             LeftIndentInTwips := CmToTwips(-SpecialSpinEdit.Value);
           end;
        2: begin
             FirstIndentInTwips := CmToTwips(LeftIndentSpinEdit.Value);
             LeftIndentInTwips := CmToTwips(SpecialSpinEdit.Value);
           end;
      end;
      RightIndentInTwips := CmToTwips(RightIndentSpinEdit.Value);

      SpaceBeforeInTwips := Round(SpaceBeforeSpinEdit.Value * 20);
      SpaceAfterInTwips := Round(SpaceAfterSpinEdit.Value * 20);
      LineSpacingRule :=
        TLineSpacingRule(LineSpacingRuleComboBox.ItemIndex);
      LineSpacingInTwips := Round(LineSpacingSpinEdit.Value * 20);
    end;
  end;
  AREParagraphForm.Free;
end;

{ TAREParagraphForm }

procedure TAREParagraphForm.SpecialComboBoxChange(Sender: TObject);
begin
  with SpecialSpinEdit do
  begin
    Enabled := SpecialComboBox.ItemIndex > 0;
    if Enabled then
      if Text = '' then Value := 0
      else
    else Text := '';
  end;
end;

procedure TAREParagraphForm.LineSpacingRuleComboBoxChange(Sender: TObject);
begin
  LineSpacingSpinEdit.Enabled := LineSpacingRuleComboBox.ItemIndex > 2;
  case LineSpacingRuleComboBox.ItemIndex of
    3, 4:
       with LineSpacingSpinEdit do
       begin
         Increment := 1;
         MinValue := 0;
         MaxValue := 1584;
         Prefix := 'pt';
         Value := 12;
       end;
    5: with LineSpacingSpinEdit do
       begin
         Increment := 0.5;
         MinValue := 0.5;
         MaxValue := 132;
         Prefix := '';
         Value := 3;
       end;
  else
    LineSpacingSpinEdit.Text := '';
  end;
end;

procedure TAREParagraphForm.FormCreate(Sender: TObject);
begin
  LeftIndentSpinEdit := TAutoSpinEdit.Create(Self);
  with LeftIndentSpinEdit do
  begin
    SetBounds(96, 68, 73, 22);
    Increment := 0.1;
    MinValue := -55.87;
    MaxValue := 55.87;
    Prefix := 'cm';
    Parent := Self;
  end;
  RightIndentSpinEdit := TAutoSpinEdit.Create(Self);
  with RightIndentSpinEdit do
  begin
    SetBounds(96, 96, 73, 22);
    Increment := 0.1;
    MinValue := -55.87;
    MaxValue := 55.87;
    Prefix := 'cm';
    Parent := Self;
  end;
  SpecialSpinEdit := TAutoSpinEdit.Create(Self);
  with SpecialSpinEdit do
  begin
    SetBounds(292, 96, 73, 22);
    Increment := 0.1;
    MinValue := 0;
    MaxValue := 55.87;
    Prefix := 'cm';
    Parent := Self;
  end;
  SpaceBeforeSpinEdit := TAutoSpinEdit.Create(Self);
  with SpaceBeforeSpinEdit do
  begin
    SetBounds(96, 152, 73, 22);
    Increment := 6;
    MinValue := 0;
    MaxValue := 1584;
    Prefix := 'pt';
    Parent := Self;
  end;
  SpaceAfterSpinEdit := TAutoSpinEdit.Create(Self);
  with SpaceAfterSpinEdit do
  begin
    SetBounds(96, 180, 73, 22);
    Increment := 6;
    MinValue := 0;
    MaxValue := 1584;
    Prefix := 'pt';
    Parent := Self;
  end;
  LineSpacingSpinEdit := TAutoSpinEdit.Create(Self);
  with LineSpacingSpinEdit do
  begin
    SetBounds(292, 180, 73, 22);
    Parent := Self;
  end;
end;

procedure TAREParagraphForm.FormShow(Sender: TObject);
begin
  AlignmentComboBox.TabOrder := 0;
  LeftIndentSpinEdit.TabOrder := 1;
  RightIndentSpinEdit.TabOrder := 2;
  SpecialComboBox.TabOrder := 3;
  SpecialSpinEdit.TabOrder := 4;
  SpaceBeforeSpinEdit.TabOrder := 5;
  SpaceAfterSpinEdit.TabOrder := 6;
  LineSpacingRuleComboBox.TabOrder := 7;
  LineSpacingSpinEdit.TabOrder := 8;
  OkButton.TabOrder := 9;
  CancelButton.TabOrder := 10;
end;

end.

⌨️ 快捷键说明

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