Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Make ShapeNode exactly as big as the text inside (Read 2191 times)
lenix
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 31
Joined: Apr 6th, 2011
Make ShapeNode exactly as big as the text inside
Jul 12th, 2011 at 7:12am
Print Post  
Hello,

I'm using transparent ShapeNode's to display labels on my diagrams.
I need the node to be as big as the text with no padding from any of the sides.

I almost got it but I can't seem be able to remove the padding on left of the text.

This is what I do:
Code
Select All
float x = 1;


float y = 1;


ShapeNode labelNode = diagram1.Factory.CreateShapeNode(x, y, 155, 6);


labelNode.TextFormat.FormatFlags = StringFormatFlags.NoWrap;


labelNode.Font = new Font("Arial", (float)0.5);


labelNode.Text = "Helllo wrolsdfasdfasdfad";


labelNode.Shape = Shapes.Rectangle;


labelNode.TextPadding = new Thickness((float)-0.071837675, (float)-0.051, (float)-0.100, (float)-0.051);



     // tesrrr.Transparent = true;


labelNode.ResizeToFitText(FitSize.KeepHeight);


labelNode.ResizeToFitText(FitSize.KeepWidth);


labelNode.Move(1, 1);
 



This is what I get:


Almost as big as the text but there is still padding on the left.

Any ideas?

Thanks in advance!

edit: Also noticed one other thing, the values im setting for TextPadding will not work (padding will be larger if text is longer) when the text inside the node changes.
So I guess I need a new way to do this, any tips are appreciated.
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Make ShapeNode exactly as big as the text insi
Reply #1 - Jul 12th, 2011 at 8:11am
Print Post  
Hi,

The Graphics.DrawString and MeasureString methods leave a couple of pixels on all sides, and they are what ShapeNode uses for measuring and drawing text:

private void Form1_Paint(object sender, PaintEventArgs e)
{
     string text = "Helllo wrolsdfasdfasdfad";
     Font font = new Font("Arial", 10);
     SizeF size = e.Graphics.MeasureString(text, font);
     e.Graphics.DrawRectangle(Pens.Red, 0, 0, size.Width, size.Height);
     e.Graphics.DrawString(text, font, Brushes.Black, 0, 0);
}

You could try measuring and custom-drawing texts using the methods of the .NET's TextRenderer instead; it is supposed to be more precise than Graphics.DrawString.
  
Back to top
 
IP Logged
 
lenix
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 31
Joined: Apr 6th, 2011
Re: Make ShapeNode exactly as big as the text insi
Reply #2 - Jul 12th, 2011 at 8:29am
Print Post  
Thanks Stoyo,
The problem is that I'm attaching the text to existing nodes, If I don't use a ShapeNode I won't be able to attach it.
Any idea what would be my best bet here?

Thanks again
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Make ShapeNode exactly as big as the text insi
Reply #3 - Jul 12th, 2011 at 8:56am
Print Post  
You can continue using ShapeNodes, but instead of setting their Transparent property, set CustomDraw=Full. Now the control will raise the DrawNode event for your labels, and you can handle it by drawing them using the TextRenderer.DrawText method. And instead of ResizeToFitText, use TextRenderer.MeasureText to find the text size and resize the node according to it.
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint