Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi Text Mittig auf ein bild zeichnen (https://www.delphipraxis.net/119566-text-mittig-auf-ein-bild-zeichnen.html)

seppi.tm 28. Aug 2008 12:17


Text Mittig auf ein bild zeichnen
 
Also ich zeiche so ein text auf ein bild von mir:
Delphi-Quellcode:
with Image1.Picture.Bitmap.Canvas do
  begin
    Font.Color := clBlack;
    Brush.Style := bsclear;
    Font.Size  := 10;
    Font.Name  := JvFontComboBox1.FontName;
    Font.Style := [fsBold];
    TextOut(0, 1, edit1.Text);
  end;
sooo aber jetzt habe ich das Problem das ich es gerne mittig hätte das bild ist 32px lang un d ich will das der text in der mitte steht
wenn ich die hälfte nehme fängt er in der mitte an ... das will ich nciht wie kann ich das realisieren das er in der mitte steht?

DeddyH 28. Aug 2008 12:22

Re: Text Mittig auf ein bild zeichnen
 
Schau Dir mal MSDN-Library durchsuchenDrawText an.

seppi.tm 28. Aug 2008 12:32

Re: Text Mittig auf ein bild zeichnen
 
ich habs jetzt so:


Delphi-Quellcode:
 DrawText(image1.Canvas.Handle,Pchar(edit1.text),length(edit1.Text),0,DT_CENTER);
und bekomme den Fehler
Zitat:

[Fehler] Unit1.pas(41): Die Typen der tatsächlichen und formalen Var-Parameter müssen übereinstimmen

DeddyH 28. Aug 2008 12:34

Re: Text Mittig auf ein bild zeichnen
 
Wofür soll die 0 denn stehen? Da muss ein Rect rein.

seppi.tm 28. Aug 2008 12:38

Re: Text Mittig auf ein bild zeichnen
 
Zitat:

Zitat von DeddyH
Wofür soll die 0 denn stehen? Da muss ein Rect rein.

ja hab ich grade auch gemerkt ^^
naja das wäre gelößt aber der text ist jetzt nicht ganz mittig er ist etwas nach rechts geschoben wie kann ich das beheben?

taaktaak 28. Aug 2008 12:40

Re: Text Mittig auf ein bild zeichnen
 
entweder sind leading/trailing blanks im text oder das rectangle ist falsch definiert

seppi.tm 28. Aug 2008 12:43

Re: Text Mittig auf ein bild zeichnen
 
so ist das rect definiert:
Delphi-Quellcode:
r:=Rect(0,0,image1.Picture.Width,image1.Picture.Height);
das müsste doch richtig sein oder?

taaktaak 28. Aug 2008 12:46

Re: Text Mittig auf ein bild zeichnen
 
eigentlich schon!
sind blanks vor dem string?

DeddyH 28. Aug 2008 12:47

Re: Text Mittig auf ein bild zeichnen
 
Zeig mal den ganzen Aufruf.

seppi.tm 28. Aug 2008 12:49

Re: Text Mittig auf ein bild zeichnen
 
Keine blanks

hier der anufruf:

Delphi-Quellcode:


procedure TForm1.Button1Click(Sender: TObject);
var r:trect;
begin
image1.Picture:=image2.Picture;
r:=Rect(0,0,image1.Picture.Width,image1.Picture.Height);
 with Image1.Picture.Bitmap.Canvas do
  begin
    Font.Color := clBlack;
    Brush.Style := bsclear;
    Font.Size  := 10;
    Font.Name  := JvFontComboBox1.FontName;
    Font.Style := [fsBold];
    DrawText(image1.Canvas.Handle,Pchar(edit1.text),length(edit1.Text),r,DT_CENTER );
  end;
  image1.Refresh;
end;

DeddyH 28. Aug 2008 12:55

Re: Text Mittig auf ein bild zeichnen
 
Willst Du direkt auf das Image oder auf die enthaltene Bitmap zeichnen?

seppi.tm 28. Aug 2008 12:56

Re: Text Mittig auf ein bild zeichnen
 
auf das bitmap ^^ also nicht die datei sondern das was ich sehe wieso?

DeddyH 28. Aug 2008 13:01

Re: Text Mittig auf ein bild zeichnen
 
Delphi-Quellcode:
DrawText(image1.Picture.Bitmap.Canvas.Handle,Pchar(edit1.text),length(edit1.Text),r,DT_CENTER );
So besser?

seppi.tm 28. Aug 2008 13:03

Re: Text Mittig auf ein bild zeichnen
 
Genau das selbe problem und dabei auch:

Delphi-Quellcode:
TextOut(round( (image1.picture.Width/2)-(TextWidth(edit1.Text)/2) ),1,edit1.Text);
und string habe icha cuhs chon direkt eingetragen ist das selbe...

DeddyH 28. Aug 2008 13:08

Re: Text Mittig auf ein bild zeichnen
 
Dann noch anders:
Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
var r:trect;
begin
image1.Picture:=image2.Picture;
r:=Rect(0,0,image1.Width,image1.Height);
with Image1.Picture.Bitmap.Canvas do
  begin
    Font.Color := clBlack;
    Brush.Style := bsclear;
    Font.Size  := 10;
    Font.Name  := JvFontComboBox1.FontName;
    Font.Style := [fsBold];
    DrawText(image1.Canvas.Handle,
             Pchar(trim(edit1.text)),
             -1,
             r,
             DT_CENTER or DT_VCENTER or DT_SINGLELINE);
  end;
  image1.Refresh;
end;

seppi.tm 28. Aug 2008 13:11

Re: Text Mittig auf ein bild zeichnen
 
jetzt zeichnet er nichts mehr

seppi.tm 28. Aug 2008 13:12

Re: Text Mittig auf ein bild zeichnen
 
so habe das problem gelößt mit der länge -1 klappt alles O.o...

Hador 28. Aug 2008 14:15

Re: Text Mittig auf ein bild zeichnen
 
Probiere es mal so:
Delphi-Quellcode:
var
  w: Integer;
begin
  {...}
  w := Canvas.TextWidth('Testtext');
  Canvas.TextOut((Width - w) div 2, 100 {Y-Coord}, 'Testtext');
  {...}
end;
Canvas.Textwidth gibt die die Textlänge in Pixel in Abhängigkeit von der verwendeten Schrift aus.

EDIT: Arr ... 2. Seite übersehen. Is aber dennoch einfacher die Lösung ^^


Alle Zeitangaben in WEZ +1. Es ist jetzt 16:00 Uhr.

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