Einzelnen Beitrag anzeigen

Benutzerbild von eddy
eddy

Registriert seit: 3. Jan 2003
Ort: Sachsen
573 Beiträge
 
Delphi 5 Professional
 
#16
  Alt 12. Mär 2003, 13:37
Hallo Paul Jr.,

weil mit Style=fsBold der Text nicht mehr in den Button gepaßt hat, habe ich noch ein bischen weiter gemacht. Die Parameterübergabe ist noch die erste, es sollte aber kein Problem sein, diese anzupassen.

Mit der Ermittlung der längsten String aus Captions und unter Berücksichtigung von Style habe ich die Länge der Buttons geändert.
Und weil lange Button nicht mehr nebeneinander passen, habe ich auch noch untereinander probiert.

Verbesserungen sind ausdrücklich erwünscht!

Vielleicht kriegen wir ja eine Neufassung mit vernünftiger Parameterübergabe von Paul Jr.


Delphi-Quellcode:
{ hier steckt jetzt noch eine ganze Menge drin, das über die ursprüngliche Fassung
  hinausgeht, z.B. nebeneinander --> Funktion dient als Grundlage für die
  Realisierung einer sinnvollen Komponente
}

//...bei einem Message Dialog die Beschriftungen der Buttons ändern
function xMessageDlg(const Msg: string; DlgType : TMsgDlgType;
                     Buttons : TMsgDlgButtons; Captions: array of string) : Integer;
const
  nebeneinander = false;
var
  aMsgDlg : TForm;
  aLbl : TLabel;
  CaptionIndex,
  CapLen, // Länge der Zeichenkette
  j, i : integer;
  dlgButton : TButton; // uses stdctrls
begin
  // Dlg erzeugen
  aMsgDlg := CreateMessageDialog(Msg, DlgType, buttons);
  aLbl := TLabel.Create(aMsgDlg);
  aLbl.AutoSize := true;
  aLbl.Font.Style := [fsBold]; // damit die ermittelte Länge paßt

  // Längsten Eintrag ermitteln für Berechnung TButton.Width
  CapLen := 0;
  for i := 0 to High(Captions) do begin
    aLbl.Caption := Captions[i];
    if aLbl.Width > CapLen then CapLen := aLbl.Width;
  end;
  aLbl.Free; // wird nicht mehr benötigt

  CaptionIndex := 0;
  // alle Objekte des Dialoges testen
  for i := 0 to aMsgDlg.ComponentCount - 1 do begin
    // Schrift-Art Message ändern
    if (aMsgDlg.Components[i] is TLabel) then begin
      TLabel(aMsgDlg.Components[i]).Font.Style := [fsBold];
      TLabel(aMsgDlg.Components[i]).Font.Color := clRed;
    end;
    // wenn es ein Button ist, dann...
    if (aMsgDlg.Components[i] is TButton) then begin
      dlgButton := TButton(aMsgDlg.Components[i]);
      if CaptionIndex > High(Captions) then Break;
      // Beschriftung entsprechend Captions-array ändern
      dlgButton.Caption := Captions[CaptionIndex];
      dlgButton.Font.Style := [fsBold];
      dlgButton.Width := CapLen + 2 * 12;
      // li. Rand = 32, Zwischenraum = 16
      // Button nebeneinander
      if nebeneinander then begin
        dlgButton.Left := 32 + (dlgButton.Width + 16)*CaptionIndex;
      end
      // Button untereinander
      else begin
        dlgButton.Left := 32;
        if CaptionIndex = 0
          then j := dlgButton.Top
          else dlgButton.Top := j + (dlgButton.Height + 8)*CaptionIndex;
      end;
      Inc(CaptionIndex);
    end;
  end;
  // s.o.; rechter Rand = 32
  if nebeneinander then begin
    aMsgDlg.Width := 32 + (dlgButton.Width + 16)*CaptionIndex + 16;
  end
  // Button untereinander
  else begin
    if aMsgDlg.Width < 2 * 32 + dlgButton.Width
      then aMsgDlg.Width := 2 * 32 + dlgButton.Width;
    aMsgDlg.Height := j + (dlgButton.Height + 8)*CaptionIndex + 32;

    // mittige Anordnung der Buttons
    CaptionIndex := 0;
    for i := 0 to aMsgDlg.ComponentCount - 1 do begin
      if (aMsgDlg.Components[i] is TButton) then begin
        dlgButton := TButton(aMsgDlg.Components[i]);
        if CaptionIndex > High(Captions) then Break;
        dlgButton.Left := (aMsgDlg.Width - dlgButton.Width) div 2;
        Inc(CaptionIndex);
      end;
    end;
  end;

  // nachdem Dialog-Width ermittelt ist, kann Msg.Width ermittelt werden
  // keine Überprüfung auf Übergröße vorgenommen
  for i := 0 to aMsgDlg.ComponentCount - 1 do begin
    if (aMsgDlg.Components[i] is TLabel) then begin
      TLabel(aMsgDlg.Components[i]).Width := aMsgDlg.Width -
                                     TLabel(aMsgDlg.Components[i]).Left - 32;
      TLabel(aMsgDlg.Components[i]).Caption := Msg;
    end;
  end;

  Result := aMsgDlg.ShowModal;
end;

mfg
eddy

[edit=Admin]Delphi-Tags für den automatischen Highlighter eingefügt. Mfg. Daniel[/edit]
[edit=SirThornberry]Edit zwecks fehlendem Highlighting - Mfg, SirThornberry[/edit]
  Mit Zitat antworten Zitat