[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

John Doe

10/11/2004 5:20:00 PM

Can someone tell me why the text in the follow code is drawn two different
sizes? I'm trying to create a GraphicsPath with an Ellipse and some text
centered in it. I want the Ellipse (but not the inside of the text) to be
part of the control's region. This works, but the String added to the
GraphicsPath is not the expected size (and thus not centered). I added a
call to Graphics.DrawString(...) to draw the text and it draws the expected
size and properly centered. Why doesn't the text added to the GraphicsPath
draw the correct size, and how can I correct this so the text is the proper
size and centered? I assume it has something to do with the fact the the
String is measured in the context of a Graphics object, but drawn in the
GraphicsPath outside of the context of any Graphics object.

Public Class uctTest

Inherits System.Windows.Forms.Button



Protected Overrides Sub OnPaint(ByVal pe As PaintEventArgs)

Dim ButtonPath As Drawing2D.GraphicsPath

Dim ButtonBrush As Brush

Dim ButtonPen As Pen



ButtonPath = New
Drawing2D.GraphicsPath(Drawing2D.FillMode.Alternate)

ButtonPath.AddEllipse(New Rectangle(New Point(0, 0), Me.Size))



Dim TextSize As SizeF = pe.Graphics.MeasureString(Me.Text, Me.Font)

Dim TextLeft As Single = Me.Width / 2.0! - TextSize.Width / 2.0!

Dim TextTop As Single = Me.Height / 2.0! - TextSize.Height / 2.0!

Dim TextPoint As New Point(TextLeft, TextTop)

ButtonPath.AddString(Me.Text, Me.Font.FontFamily, Me.Font.Style,
Me.Font.Size, TextPoint, StringFormat.GenericDefault)

Dim myRegion As New Region(ButtonPath)

Me.Region = myRegion



ButtonBrush = New SolidBrush(Color.Red)

pe.Graphics.FillPath(ButtonBrush, ButtonPath)

ButtonBrush.Dispose()



' Debug: Draw the Text to see what size it is.

' Debug: This draws the text to correct size and properly centered.

' Debug: The Text in the GraphicsPath does not draw the expected

' Debug: size, and thus not centered.

pe.Graphics.DrawString(Me.Text, Me.Font, Brushes.Blue, TextLeft,
TextTop)



End Sub

End Class



-----

Nobody


2 Answers

Justin Rogers

10/11/2004 10:13:00 PM

0

I hope you realize that in one call you are using a specific Font and in the
other
you are using a FontFamily. When you use a FontFamily, you are selecting a
different Font (most likely) from the one you used when calling MeasureString
because with AddString the font selector is coming into play and choosing
a best fit match to the FontFamily.

Don''t do your rendering of the text within the GraphicsPath context and you
should be OK. Instead just render the path, then render your string using a
DrawString call.

--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/jus...

"Nobody" <nobody@nowhere.com> wrote in message
news:Oz5UWa7rEHA.2948@TK2MSFTNGP11.phx.gbl...
> Can someone tell me why the text in the follow code is drawn two different
> sizes? I''m trying to create a GraphicsPath with an Ellipse and some text
> centered in it. I want the Ellipse (but not the inside of the text) to be
> part of the control''s region. This works, but the String added to the
> GraphicsPath is not the expected size (and thus not centered). I added a call
> to Graphics.DrawString(...) to draw the text and it draws the expected size
> and properly centered. Why doesn''t the text added to the GraphicsPath draw
> the correct size, and how can I correct this so the text is the proper size
> and centered? I assume it has something to do with the fact the the String is
> measured in the context of a Graphics object, but drawn in the GraphicsPath
> outside of the context of any Graphics object.
>
> Public Class uctTest
>
> Inherits System.Windows.Forms.Button
>
>
>
> Protected Overrides Sub OnPaint(ByVal pe As PaintEventArgs)
>
> Dim ButtonPath As Drawing2D.GraphicsPath
>
> Dim ButtonBrush As Brush
>
> Dim ButtonPen As Pen
>
>
>
> ButtonPath = New Drawing2D.GraphicsPath(Drawing2D.FillMode.Alternate)
>
> ButtonPath.AddEllipse(New Rectangle(New Point(0, 0), Me.Size))
>
>
>
> Dim TextSize As SizeF = pe.Graphics.MeasureString(Me.Text, Me.Font)
>
> Dim TextLeft As Single = Me.Width / 2.0! - TextSize.Width / 2.0!
>
> Dim TextTop As Single = Me.Height / 2.0! - TextSize.Height / 2.0!
>
> Dim TextPoint As New Point(TextLeft, TextTop)
>
> ButtonPath.AddString(Me.Text, Me.Font.FontFamily, Me.Font.Style,
> Me.Font.Size, TextPoint, StringFormat.GenericDefault)
>
> Dim myRegion As New Region(ButtonPath)
>
> Me.Region = myRegion
>
>
>
> ButtonBrush = New SolidBrush(Color.Red)
>
> pe.Graphics.FillPath(ButtonBrush, ButtonPath)
>
> ButtonBrush.Dispose()
>
>
>
> '' Debug: Draw the Text to see what size it is.
>
> '' Debug: This draws the text to correct size and properly centered.
>
> '' Debug: The Text in the GraphicsPath does not draw the expected
>
> '' Debug: size, and thus not centered.
>
> pe.Graphics.DrawString(Me.Text, Me.Font, Brushes.Blue, TextLeft,
> TextTop)
>
>
>
> End Sub
>
> End Class
>
>
>
> -----
>
> Nobody
>
>


John Doe

10/12/2004 7:50:00 PM

0

The problem is that I have to render the text in the context of the
GraphicsPath because the text has to part of the GraphicsPath I use to
create the Region for the control. The region of the control should consist
of an Ellipse with the text "cut out" of the middle so you can see (and
click) through the text to controls behind it.

Unfortunately, Graphics.MeasureString(...) requires a Font, and
GraphicsPath.AddString(...) requires a FontFamily, Style, and Size. Any
suggestions on how I could get the text in the GraphicsPath centered?

-----
Nobody

"Justin Rogers" <Justin@games4dotnet.com> wrote in message
news:%23SsvE99rEHA.2196@TK2MSFTNGP15.phx.gbl...
>I hope you realize that in one call you are using a specific Font and in
>the other
> you are using a FontFamily. When you use a FontFamily, you are selecting a
> different Font (most likely) from the one you used when calling
> MeasureString
> because with AddString the font selector is coming into play and choosing
> a best fit match to the FontFamily.
>
> Don''t do your rendering of the text within the GraphicsPath context and
> you
> should be OK. Instead just render the path, then render your string using
> a
> DrawString call.
>
> --
> Justin Rogers
> DigiTec Web Consultants, LLC.
> Blog: http://weblogs.asp.net/jus...
>
> "Nobody" <nobody@nowhere.com> wrote in message
> news:Oz5UWa7rEHA.2948@TK2MSFTNGP11.phx.gbl...
>> Can someone tell me why the text in the follow code is drawn two
>> different sizes? I''m trying to create a GraphicsPath with an Ellipse and
>> some text centered in it. I want the Ellipse (but not the inside of the
>> text) to be part of the control''s region. This works, but the String
>> added to the GraphicsPath is not the expected size (and thus not
>> centered). I added a call to Graphics.DrawString(...) to draw the text
>> and it draws the expected size and properly centered. Why doesn''t the
>> text added to the GraphicsPath draw the correct size, and how can I
>> correct this so the text is the proper size and centered? I assume it
>> has something to do with the fact the the String is measured in the
>> context of a Graphics object, but drawn in the GraphicsPath outside of
>> the context of any Graphics object.
>>
>> Public Class uctTest
>>
>> Inherits System.Windows.Forms.Button
>>
>>
>>
>> Protected Overrides Sub OnPaint(ByVal pe As PaintEventArgs)
>>
>> Dim ButtonPath As Drawing2D.GraphicsPath
>>
>> Dim ButtonBrush As Brush
>>
>> Dim ButtonPen As Pen
>>
>>
>>
>> ButtonPath = New
>> Drawing2D.GraphicsPath(Drawing2D.FillMode.Alternate)
>>
>> ButtonPath.AddEllipse(New Rectangle(New Point(0, 0), Me.Size))
>>
>>
>>
>> Dim TextSize As SizeF = pe.Graphics.MeasureString(Me.Text,
>> Me.Font)
>>
>> Dim TextLeft As Single = Me.Width / 2.0! - TextSize.Width / 2.0!
>>
>> Dim TextTop As Single = Me.Height / 2.0! - TextSize.Height / 2.0!
>>
>> Dim TextPoint As New Point(TextLeft, TextTop)
>>
>> ButtonPath.AddString(Me.Text, Me.Font.FontFamily, Me.Font.Style,
>> Me.Font.Size, TextPoint, StringFormat.GenericDefault)
>>
>> Dim myRegion As New Region(ButtonPath)
>>
>> Me.Region = myRegion
>>
>>
>>
>> ButtonBrush = New SolidBrush(Color.Red)
>>
>> pe.Graphics.FillPath(ButtonBrush, ButtonPath)
>>
>> ButtonBrush.Dispose()
>>
>>
>>
>> '' Debug: Draw the Text to see what size it is.
>>
>> '' Debug: This draws the text to correct size and properly
>> centered.
>>
>> '' Debug: The Text in the GraphicsPath does not draw the expected
>>
>> '' Debug: size, and thus not centered.
>>
>> pe.Graphics.DrawString(Me.Text, Me.Font, Brushes.Blue, TextLeft,
>> TextTop)
>>
>>
>>
>> End Sub
>>
>> End Class
>>
>>
>>
>> -----
>>
>> Nobody
>>
>>
>
>