canvas - Get the coordinates of the limits of a text with Delphi -


hi use follow code draw rotate text in delphi application. user can choose if use or not use gdi+ draw text:

procedure tform1.button1click(sender: tobject); var   mylogfont: tlogfont;   myfont: hfont;   t: string;   ff: igpfontfamily;   ft: igpfont;   br: igpsolidbrush;   gr: igpgraphics;   pp: tgppointf;   pen: igppen; begin   t := 'hello';    if not drawusinggdip.checked     begin       // draw using gdi       fillchar(mylogfont, sizeof(mylogfont), 0);       mylogfont       begin         lfheight:=0;         lfwidth:=0;         lfescapement:=-strtoint(edit1.text)*10;         lforientation:=-strtoint(edit1.text)*10;         lfweight:=fw_normal;         lfitalic:=0;         lfunderline:=0;         lfstrikeout:=0;         lfcharset:=default_charset;         lfoutprecision:=out_default_precis;         lfclipprecision:=clip_default_precis;         lfquality:=default_quality;         lfpitchandfamily:=1;       end;       myfont:=createfontindirect(mylogfont);       form1.canvas.font.handle:=myfont;       form1.canvas.font.name := 'arial';       form1.canvas.font.size := 13;       form1.canvas.textout(103, 100, t);     end   else     begin       // draw using gdi+       pen := tgppen.create($ff000000);        ff := tgpfontfamily.create('arial');       ft := tgpfont.create(ff, 16, fontstyleregular, unitpixel);       br := tgpsolidbrush.create(tgpcolor.red);        gr := tgpgraphics.create(form1.canvas.handle);        gr.settextrenderinghint(textrenderinghintantialias);       gr.translatetransform(100.0, 100.0);       gr.rotatetransform(strtoint(edit1.text));        pp := tgppointf.create(0, 0);       gr.drawstring(t, ft, pp, br);        gr.resettransform;     end; end; 

now need know (if possible without draw text) coordinates of vertices of rectangle bounds text (see image):

vertices

is there simple way these coordinates both , without use gdi+ library?

for gdi implementation can use

  tsiz := form1.canvas.textextent(t);        // tsiz : tagsize    ang := (2.0*pi*strtoint(edit1.text))/360;  // ang : double    tpts[0].x := 100;   // tpts : array[0..4] of tpoint   tpts[0].y := 100;   tpts[1].x := 100 + round(tsiz.cx * cos(ang));   tpts[1].y := 100 + round(tsiz.cx * sin(ang));   tpts[2].x := tpts[1].x - round(tsiz.cy*sin(ang));   tpts[2].y := tpts[1].y + round(tsiz.cy*cos(ang));   tpts[3].x := tpts[0].x - round(tsiz.cy*sin(ang));   tpts[3].y := tpts[0].y + round(tsiz.cy*cos(ang));   tpts[4] := tpts[0];    //tpts contains corner points of bounding rect    form1.canvas.textout(100, 100, t);  // draw text   form1.canvas.polyline(tpts);        // draw bounding rect 

for gdi+ it's lot easier

  sft := tgpstringformat.genericdefault;      // sft : igpstringformat   mrect := gr.measurestring(t, ft, pp, sft);  // mrect : tgprectf   // after transforms   // mrect bounding rect   gr.drawrectangle(pen,mrect);   // mrect transformed drawrectangle - coordinates can   // calculated in same way gdi case    // mrect.width -> tsiz.cx , mrect.height -> tsiz.cy  

enter image description here


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

php - MySQLi multi_query results for later use -