Delphi-PRAXiS
Seite 2 von 3     12 3      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Caption der Buttons Yes/No im Dialog MessageDlg ändern (https://www.delphipraxis.net/3307-caption-der-buttons-yes-no-im-dialog-messagedlg-aendern.html)

sakura 11. Mär 2003 15:36

Hallo, das war ein Copy&Paste Fehler :oops: Es geht um die Message, von daher sollte es wohl (aMsgDlg.Components[i] is TLabel) sein.

Sorry,
...:cat:...

eddy 11. Mär 2003 16:49

Hallo sakura,
Hallo Paul Jr.,

habe noch ein wenig herumgebastet, jetzt kann man auch noch die Farbe und die Schriftart der Msg ändern:

Delphi-Quellcode:
//...bei einem Message Dialog die Beschriftungen der Buttons ändern
function xMessageDlg(const Msg: string; DlgType : TMsgDlgType;
                     Buttons : TMsgDlgButtons; Captions: array of string) : Integer;
var
  aMsgDlg : TForm;
  CaptionIndex,
  i : integer;
  dlgButton : TButton; // uses stdctrls
begin
  // Dlg erzeugen
  aMsgDlg := CreateMessageDialog(Msg, DlgType, buttons);
  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];
      Inc(CaptionIndex);
    end;
  end;
  Result := aMsgDlg.ShowModal;
end;

mfg
eddy

[edit=SirThornberry]Code-Tags durch Delphi-Tags ersetzt - Mfg, SirThornberry[/edit]

Gast 12. Mär 2003 09:49

Hallo Eddy :D

Toll wie Du es gemacht hast... :coder: !!!

Leider war ich gestern nicht mehr im Forum... aber wie ich sehe hat Sakura die geistliche Führung übernommen :angle: ... dafür auch dank an Dich Sakura... 8)

Gruß an Euch beiden...

Paul Jr.

Luckie 12. Mär 2003 10:11

Jetzt müßt ihr den Funktionskopf nur noch so umbauen, dass man auch alle Parameter mit übergeben kann und nicht immer im Code rumwurschteln muß.

Gast 12. Mär 2003 10:47

Tja... Luckie 8) , dass kriege sogar ich schon hin... :mrgreen: das Wichtigste habe ich schon auf einem goldenem Teller bekommen...

Gruß

Paul Jr.

eddy 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]

Gast 12. Mär 2003 14:09

Hallo Eddy 8) ,

hervorragend... :idea: !!!

Das werde ich jetzt als feste Bestandteil meiner so umfangreicher Schatztruhe machen... Du muss wissen, das dieses Problem schon mal in Forum behandelt wurde (jetzt weiß ich nicht mehr ob hier oder irgendwo anders...) und keiner wusste die Lösung...

Weiter so und viel Erfolg :D

Danke

Paul Jr.

elundril 28. Jan 2008 16:50

Re: Caption der Buttons Yes/No im Dialog MessageDlg ändern
 
lustiges Ding dieser xMessageDlg. is der Code Free4Use?

lg elundril

RavenIV 29. Jan 2008 08:08

Re: Caption der Buttons Yes/No im Dialog MessageDlg ändern
 
Zitat:

Zitat von elundril
lustiges Ding dieser xMessageDlg. is der Code Free4Use?

Sonst wäre der Code wohl nicht hier gepostet worden.

Wenn Du dem Author einen Gefallen machen willst, dann verweist Du auf die Delphi-Praxis und auf den Benutzername.

elundril 29. Jan 2008 15:10

Re: Caption der Buttons Yes/No im Dialog MessageDlg ändern
 
Zitat:

Zitat von RavenIV
Wenn Du dem Author einen Gefallen machen willst, dann verweist Du auf die Delphi-Praxis und auf den Benutzername.

genau das wollt ich wissen, ob ich den Namen dazuschreiben MUSS an einer bestimmten Stelle. Im ReadMe werd ich sowieso den dann erwähnen.

lg elundril


Alle Zeitangaben in WEZ +1. Es ist jetzt 23:39 Uhr.
Seite 2 von 3     12 3      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz