469,600 Members | 2,444 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,600 developers. It's quick & easy.

Inherited label text alignment and rotation problem

I'm having a problem with an inherited label, applying text rotation to
aligned text.
If text rotation is applied to the aligned text, the alignment goes 'nuts'.
I can find no logic to what is happening.

I've built the following code from several examples on the web, if you
remove the rotation then alignment works fine:

Imports System.ComponentModel

Public Class TransparentLabel
Inherits Label

Private _RotationAngle As Integer

#Region " Windows Form Designer generated code "

Public Sub New()

'This call is required by the Windows Form Designer.

'Add any initialization after the InitializeComponent() call

End Sub

'UserControl1 overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
End If
End If
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()Private Sub
Me.BackColor = System.Drawing.Color.DeepPink
Me.Name = "TransparentLabel"
Me.Size = New System.Drawing.Size(256, 46)

End Sub

#End Region
Public Property RotationAngle() As Integer
RotationAngle = _RotationAngle
End Get
Set(ByVal value As Integer)
_RotationAngle = value
End Set
End Property

Private Sub TransparentLabel_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim sFormat As StringFormat = Nothing

sFormat = New StringFormat

If Me.TextAlign <= ContentAlignment.TopRight Then
sFormat.LineAlignment = StringAlignment.Near
ElseIf Me.TextAlign >= ContentAlignment.BottomRight Then
sFormat.LineAlignment = StringAlignment.Far
sFormat.LineAlignment = StringAlignment.Center
End If

If Me.TextAlign = ContentAlignment.BottomLeft Or Me.TextAlign =
ContentAlignment.MiddleLeft Or Me.TextAlign = ContentAlignment.TopLeft Then
sFormat.Alignment = StringAlignment.Near
ElseIf Me.TextAlign = ContentAlignment.BottomRight Or
Me.TextAlign = ContentAlignment.MiddleRight Or Me.TextAlign =
ContentAlignment.TopRight Then
sFormat.Alignment = StringAlignment.Far
sFormat.Alignment = StringAlignment.Center
End If

'variables to capture the size of the text area
Dim width As Double = e.Graphics.MeasureString(Text,
Dim height As Double = e.Graphics.MeasureString(Text,
'convert the rotation angle into radians for trig functions
Dim angleRadian As Double = ((_rotationAngle Mod 360) / 180) *
'capture the forground color as a brush
Dim myBrush As Brush = New SolidBrush(Me.ForeColor)

'If Me.BackColor = Color.Transparent Then
If Me.AutoSize Then
Dim Siz As Drawing.SizeF = e.Graphics.MeasureString(Me.Text,
Me.Width = Siz.Width + 1
Me.Height = Siz.Height + 1
End If
Dim B As New Bitmap(Me.Width, Me.Height)
Dim G As Graphics = Graphics.FromImage(B)
Dim TextArea As New Rectangle(0, 0, Me.Width, Me.Height)

If Me.BackColor = Color.Transparent Then
G.FillRectangle(New SolidBrush(Color.DeepPink), TextArea)
G.FillRectangle(New SolidBrush(Me.BackColor), TextArea)
End If

G.TranslateTransform(CInt((ClientRectangle.Width + (height *
Math.Sin(angleRadian)) - (width * Math.Cos(angleRadian))) / 2),
CInt((ClientRectangle.Height - (height * Math.Cos(angleRadian)) - (width *
Math.Sin(angleRadian))) / 2))
G.DrawString(Me.Text, Font, myBrush, TextArea, sFormat)
G.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias

Dim Pth As New Drawing2D.GraphicsPath()
Dim X, Y As Short
For X = 0 To Me.Width - 1
For Y = 0 To Me.Height - 1
If B.GetPixel(X, Y).ToArgb.ToString =
Color.DeepPink.ToArgb.ToString Then
Pth.AddRectangle(New Rectangle(X, Y, 1, 1))
End If

Dim Rgn As Region

If Me.BackColor = Color.Transparent Then
G.FillRectangle(New SolidBrush(Me.ForeColor), New
Rectangle(0, 0, Me.Width, Me.Height))
End If

e.Graphics.DrawImage(B, 0, 0)
Rgn = New Region(New Rectangle(0, 0, Me.Width, Me.Height))
If Me.BackColor = Color.Transparent Then
End If
Me.Region = Rgn

Catch ex As Exception
End Try
End Sub

Private Sub TransparentLabel_Load(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
SetStyle(ControlStyles.AllPaintingInWmPaint, True)
SetStyle(ControlStyles.DoubleBuffer, True)
SetStyle(ControlStyles.UserPaint, True)
End Sub
End Class

Can anyone advise me what I'm doing wrong to throw the alignment out?

Oct 19 '06 #1
0 1540

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Rui Patinha | last post: by
4 posts views Thread by Stuart Norris | last post: by
2 posts views Thread by David | last post: by
24 posts views Thread by Nak | last post: by
3 posts views Thread by Jeff User | last post: by
2 posts views Thread by André Hänsel | last post: by
2 posts views Thread by swapna_munukoti | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.