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

📄 jvfindreplace.pas

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

function TJvFindReplace.ReplaceOne(Sender: TObject): Boolean;
var
  Equal: Integer;
  S, R: string;
begin
  Result := False;

  if FShowDialogs then
  begin
    S := TReplaceDialog(Sender).FindText;
    R := TReplaceDialog(Sender).ReplaceText;
  end
  else
  begin
    S := FFindText;
    R := FReplaceText;
  end;

  if frMatchCase in TFindDialog(Sender).Options then
    Equal := AnsiCompareStr(FEditControl.SelText, S)
  else
    Equal := AnsiCompareText(FEditControl.SelText, S);

  if Equal = 0 then
  begin
    Result := True;
    FEditControl.SelText := R;
    FEditControl.SetFocus;
  end;
end;

procedure TJvFindReplace.Notification(AComponent: TComponent; Operation: TOperation);
begin
  inherited Notification(AComponent, Operation);
  if (Operation = opRemove) and (AComponent = FEditControl) then
    FEditControl := nil;
end;

procedure TJvFindReplace.NeedDialogs;
begin
  if not Assigned(FFindDialog) then
  begin
    FFindDialog := TFindDialog.Create(Self);
    FFindDialog.FindText := FFindText;
    FFindDialog.OnFind := DoOnFind;
  end;
  if not Assigned(FReplaceDialog) then
  begin
    FReplaceDialog := TReplaceDialog.Create(Self);
    FReplaceDialog.FindText := FFindText;
    FReplaceDialog.ReplaceText := FReplaceText;
    FReplaceDialog.OnFind := DoOnFind;
    FReplaceDialog.OnReplace := DoOnReplace;
  end;
end;

procedure TJvFindReplace.UpdateDialogs;
begin
  NeedDialogs;

  FFindDialog.Position := GetPosition;
  {$IFDEF VCL}
  FFindDialog.Top := GetTop;
  FFindDialog.Left := GetLeft;
  {$ENDIF VCL}
  {$IFDEF VisualCLX}
  FFindDialog.Position := Point(GetTop, GetLeft);
  {$ENDIF VisualCLX}
  FFindDialog.Options := FOptions;
  FFindDialog.HelpContext := GetHelpContext;
  FFindDialog.FindText := GetFindText;
  FReplaceDialog.Position := GetPosition;

  {$IFDEF VCL}
  FReplaceDialog.Top := GetTop;
  FReplaceDialog.Left := GetLeft;
  {$ENDIF VCL}
  {$IFDEF VisualCLX}
  FReplaceDialog.Position := Point(GetTop, GetLeft);
  {$ENDIF VisualCLX}
  FReplaceDialog.Options := FOptions;
  FReplaceDialog.HelpContext := GetHelpContext;
  FReplaceDialog.FindText := GetFindText;
  FReplaceDialog.ReplaceText := GetReplaceText;
end;

procedure TJvFindReplace.DoOnFind(Sender: TObject);
var
  FoundPos: TFoundText;
  S: string;
  Offset: Integer;
begin
  if FShowDialogs then
    S := TFindDialog(Sender).FindText
  else
    S := FFindText;

  if FKeepText then
  begin
    FFindText := TFindDialog(Sender).FindText;
    FReplaceText := TReplaceDialog(Sender).ReplaceText;
  end;
  if (FEditControl.SelStart = 0) and (FEditControl.SelLength < 1) then
    Offset := 0
  else
    Offset := 1;
  if not (frDown in TFindDialog(Sender).Options) then
    FoundPos := FindInTextRev(FEditControl.Text, S, Length(FEditControl.Text) - FEditControl.SelStart + Offset,
      Length(FEditControl.Text), FFast)
  else
    FoundPos := FindInText(FEditControl.Text, S, FEditControl.SelStart + Offset, Length(FEditControl.Text), FFast);

  if FoundPos.StartAt > -1 then
  begin
    if (frWholeWord in TFindDialog(Sender).Options) and not FoundPos.isWhole then
      DoFailed(Sender)
    else
    if (frMatchCase in TFindDialog(Sender).Options) and not FoundPos.isSameCase then
      DoFailed(Sender)
    else
    begin
      FEditControl.SetFocus;
      FEditControl.SelStart := FoundPos.StartAt;
      FEditControl.SelLength := FoundPos.EndAt;
      {$IFDEF VCL}
      SendMessage(FEditControl.Handle, EM_SCROLLCARET, 0, 0);
      {$ENDIF VCL}
      if Assigned(FOnFind) then
        FOnFind(Self);
    end
  end
  else
    DoFailed(Sender);
end;

procedure TJvFindReplace.DoOnReplace(Sender: TObject);
begin
  if FEditControl.SelLength < 1 then
    DoOnFind(Sender);
  if FEditControl.SelLength < 1 then
    Exit;

  if frReplaceAll in TFindDialog(Sender).Options then
  begin
    SetFindText(FReplaceDialog.FindText);
    SetReplaceText(FReplaceDialog.ReplaceText);
    ReplaceAll(FFindText, FReplaceText);
    if Assigned(FOnReplace) then
      FOnReplace(Self);
  end
  else
  begin
    ReplaceOne(Sender);
    if Assigned(FOnReplace) then
      FOnReplace(Self);
    DoOnFind(Sender);
  end;
end;

procedure TJvFindReplace.DoOnShow(Sender: TObject);
begin
  if not Assigned(FEditControl) then
    Error;
  UpdateDialogs;
  if Assigned(FOnShow) then
    FOnShow(Self);
end;

procedure TJvFindReplace.DoOnClose(Sender: TObject);
begin
  if not Assigned(FEditControl) then
    Error;
  UpdateDialogs;
  if Assigned(FOnClose) then
    FOnClose(Self);
end;

procedure TJvFindReplace.DoFailed(Sender: TObject);
var
  FCaption: string;
begin
  if not Assigned(FEditControl) then
    Error;
  if Assigned(FOnNotFound) then
    FOnNotFound(Self);
  if not FShowDialogs then
    Exit;

  if Sender = FReplaceDialog then
    FCaption := RsReplaceCaption
  else
    FCaption := RsFindCaption;

  MessageBox(
    {$IFDEF VCL}
    TFindDialog(Sender).Handle,
    {$ENDIF VCL}
    {$IFDEF VisualCLX}
    TFindDialog(Sender).Form.Handle,
    {$ENDIF VisualCLX}
    PChar(Format(RsNotFound, [TFindDialog(Sender).FindText])),
    PChar(FCaption), MB_OK or MB_ICONINFORMATION);
end;

procedure TJvFindReplace.DoReplacingAll;
begin
  if Assigned(FOnReplacingAll) then
    FOnReplacingAll(Self);
end;

procedure TJvFindReplace.DoReplacedAll(Sender: TObject);
begin
  if FShowDialogs then
  begin
    MessageBox(
      {$IFDEF VCL}
      TFindDialog(Sender).Handle,
      {$ENDIF VCL}
      {$IFDEF VisualCLX}
      TFindDialog(Sender).Form.Handle,
      {$ENDIF VisualCLX}
      PChar(Format(RsXOccurencesReplaced, [FNumberReplaced, TFindDialog(Sender).FindText])),
      PChar(RsReplaceCaption), MB_OK or MB_ICONINFORMATION);
  end;

  if Assigned(FOnReplacedAll) then
    FOnReplacedAll(Self, FNumberReplaced);
end;

procedure TJvFindReplace.DoProgress(Position: Integer; var Terminate: Boolean);
begin
  if Assigned(FOnProgress) then
    FOnProgress(Self, Position, Terminate);
end;

procedure TJvFindReplace.SetPosition(Value: TPoint);
begin
  FPosition := Value;
  UpdateDialogs;
end;

procedure TJvFindReplace.SetDialogTop(Value: Integer);
begin
  FTop := Value;
  UpdateDialogs;
end;

procedure TJvFindReplace.SetDialogLeft(Value: Integer);
begin
  FLeft := Value;
  UpdateDialogs;
end;

procedure TJvFindReplace.SetOptions(Value: TFindOptions);
begin
  FOptions := Value;
  UpdateDialogs;
end;

procedure TJvFindReplace.SetEditControl(Value: TCustomEdit);
begin
  FEditControl := Value;
  if Value <> nil then
    Value.FreeNotification(Self);
end;

procedure TJvFindReplace.SetFindText(const Value: string);
begin
  FFindText := Value;
  if Assigned(FFindDialog) then
    FFindDialog.FindText := Value;
  if Assigned(FReplaceDialog) then
    FReplaceDialog.FindText := Value;
end;

procedure TJvFindReplace.SetShowDialogs(Value: Boolean);
begin
  if FShowDialogs <> Value then
    FShowDialogs := Value;
  if not Value then
  begin
    NeedDialogs;
    {$IFDEF VCL}
    FFindDialog.CloseDialog;
    FReplaceDialog.CloseDialog;
    {$ENDIF VCL}
    {$IFDEF VisualCLX}
    FFindDialog.Form.Close;
    FReplaceDialog.Form.Close;
    {$ENDIF VisualCLX}
  end;
end;

procedure TJvFindReplace.SetReplaceText(const Value: string);
begin
  FReplaceText := Value;
  if Assigned(FReplaceDialog) then
    FReplaceDialog.ReplaceText := Value;
end;

procedure TJvFindReplace.SetHelpContext(Value: THelpContext);
begin
  FHelpContext := Value;
  UpdateDialogs;
end;

function TJvFindReplace.GetPosition: TPoint;
begin
  if (csDesigning in ComponentState) or not Assigned(FFindDialog) then
    Result := FPosition
  else
    Result := FFindDialog.Position;
end;

function TJvFindReplace.GetTop: Integer;
begin
  if (csDesigning in ComponentState) or not Assigned(FFindDialog) then
    Result := FPosition.Y
  else
    {$IFDEF VCL}
    Result := FFindDialog.Top;
    {$ENDIF VCL}
    {$IFDEF VisualCLX}
    Result := FFindDialog.Position.Y;
    {$ENDIF VisualCLX}
end;

function TJvFindReplace.GetLeft: Integer;
begin
  if (csDesigning in ComponentState) or not Assigned(FFindDialog) then
    Result := FPosition.X
  else
    {$IFDEF VCL}
    Result := FFindDialog.Left;
    {$ENDIF VCL}
    {$IFDEF VisualCLX}
    Result := FFindDialog.Position.X;
    {$ENDIF VisualCLX}
end;

function TJvFindReplace.GetOptions: TFindOptions;
begin
  if (csDesigning in ComponentState) or not Assigned(FFindDialog) then
    Result := FOptions
  else
    Result := FFindDialog.Options;
end;

function TJvFindReplace.GetHelpContext: THelpContext;
begin
  if (csDesigning in ComponentState) or not Assigned(FFindDialog) then
    Result := FHelpContext
  else
    Result := FFindDialog.HelpContext;
end;

function TJvFindReplace.GetFindText: string;
begin
  if not FShowDialogs or (csDesigning in ComponentState) or not Assigned(FFindDialog) then
    Result := FFindText
  else
    Result := FFindDialog.FindText;
end;

function TJvFindReplace.GetReplaceText: string;
begin
  if not FShowDialogs or (csDesigning in ComponentState) or not Assigned(FReplaceDialog) then
    Result := FReplaceText
  else
    Result := FReplaceDialog.ReplaceText;
end;

{$IFDEF UNITVERSIONING}
initialization
  RegisterUnitVersion(HInstance, UnitVersioning);

finalization
  UnregisterUnitVersion(HInstance);
{$ENDIF UNITVERSIONING}

end.

⌨️ 快捷键说明

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