Einzelnen Beitrag anzeigen

Benutzerbild von chrisw
chrisw

Registriert seit: 11. Jan 2005
Ort: Schleswig
193 Beiträge
 
Delphi XE8 Professional
 
#4

Re: Breite eines PopUpMenüs ermitteln

  Alt 15. Dez 2005, 10:18
Versuch mal das (Allerdings nicht getestet)
Mglw müssen da noch ein paar Pixel dazu !
[EDIT] Überarbeitete Version !

Delphi-Quellcode:
function GetPopUpMenuWidth(PopUpMenu : TPopUpMenu; Canvas : TCanvas) : Integer;
var i : Integer;
    LongestCaption : Integer;
begin
  LongestCaption := -1;
  Result := 0;
  for i := 0 to PopUpMenu.Items.Count -1 do
  if LongestCaption < length(TMenuItem(PopUpMenu.Items[i]).Caption) then
  begin
    LongestCaption := length(TMenuItem(PopUpMenu.Items[i]).Caption);
    Result := Canvas.TextWidth(TMenuItem(PopUpMenu.Items[i]).Caption );
  end;
end;

function GetPopUpMenuHeight(PopUpMenu : TPopUpMenu; Canvas : TCanvas) : Integer;
begin
  Result := 0;
  if PopUpMenu.Items.Count > 0 then
  Result := Canvas.TextHeight(PopUpMenu.Items[0].Caption) + 21 * PopUpMenu.Items.Count;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  showmessage(Inttostr(GetPopUpMenuWidth(PopUpMenu1,Canvas)));
  showmessage(Inttostr(GetPopUpMenuHeight(PopUpMenu1,Canvas)));
end;
Christian Wahl
me, myself and I, die lustigen Drei !!
  Mit Zitat antworten Zitat