Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » Draw, Display, Images, Bitmaps, Icons » Centering text in a rectange
Centering text in a rectange [message #35340] Fri, 03 February 2012 16:57 Go to next message
Alboni is currently offline  Alboni
Messages: 182
Registered: January 2012
Location: Deventer, Netherlands
Experienced Member
I need to display a number or objectname on top of a bitmap. I can't figure out how to get the text centered over the bitmap.

index.php?t=getfile&id=3640&private=0

// Relevant portion of the code I wrote
void Furniture::Paint(Draw& d)
{
switch (orientation)
{
case 1 : d.DrawImage(x,y,w,h,RotateClockwise(Img::Get(image))); break;
case 2 : d.DrawImage(x,y,w,h,Rotate180(Img::Get(image))); break;
case 3 : d.DrawImage(x,y,w,h,RotateAntiClockwise(Img::Get(image))); break;
default: d.DrawImage(x,y,w,h,Img::Get(image));
}
d.DrawText(x+w/3,y+h/3,name,Arial(30), Magenta);
}
  • Attachment: table.png
    (Size: 10.59KB, Downloaded 361 times)

[Updated on: Fri, 03 February 2012 17:01]

Report message to a moderator

Re: Centering text in a rectange [message #35341 is a reply to message #35340] Fri, 03 February 2012 17:21 Go to previous messageGo to next message
jerson is currently offline  jerson
Messages: 200
Registered: June 2010
Location: Bombay, India
Experienced Member

This is how I would do it. The gurus out here might have simpler ways to achieve the same

1- Get size of text to be displayed. You will use the GetTextSize() function to achieve this

2- Get the size of the rectangle using GetSize()

3- X position centered text will start at
PosX = (Rect_X_extent-Text_x_extent)/2
PosY = (Rect_Y_extent-Text_Y_extent)/2

4- put the text there.

You might need to consider rotation of text differently.

Maybe there are utility functions that achieve the same, but, I do not know as I'm a relative newbie
Re: Centering text in a rectange [message #35342 is a reply to message #35340] Fri, 03 February 2012 17:48 Go to previous messageGo to next message
Alboni is currently offline  Alboni
Messages: 182
Registered: January 2012
Location: Deventer, Netherlands
Experienced Member
Good enough for me Smile
Thanks man
Re: Centering text in a rectange [message #35351 is a reply to message #35341] Sat, 04 February 2012 15:33 Go to previous message
mirek is currently offline  mirek
Messages: 12098
Registered: November 2005
Ultimate Member
jerson wrote on Fri, 03 February 2012 11:21

This is how I would do it. The gurus out here might have simpler ways to achieve the same



Point p = rect.CenterPos(GetTextSize(...));

[Updated on: Sat, 04 February 2012 15:34]

Report message to a moderator

Previous Topic: How to use the same .iml from multiple source files.
Next Topic: Bug: Missing width in DrawArc on X11
Goto Forum:
  


Current Time: Fri Nov 15 13:41:30 CET 2019

Total time taken to generate the page: 0.01605 seconds