By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,617 Members | 1,605 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,617 IT Pros & Developers. It's quick & easy.

Inherited label text alignment and rotation problem

P: n/a
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()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'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
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
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
InitializeComponent()
'
'TransparentLabel
'
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
Get
RotationAngle = _RotationAngle
End Get
Set(ByVal value As Integer)
_RotationAngle = value
Me.Invalidate()
End Set
End Property

Private Sub TransparentLabel_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Try
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
Else
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
Else
sFormat.Alignment = StringAlignment.Center
End If

'variables to capture the size of the text area
Dim width As Double = e.Graphics.MeasureString(Text,
Me.Font).Width
Dim height As Double = e.Graphics.MeasureString(Text,
Me.Font).Height
'convert the rotation angle into radians for trig functions
Dim angleRadian As Double = ((_rotationAngle Mod 360) / 180) *
Math.PI
'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,
Font)
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)
Else
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.RotateTransform(CInt(_RotationAngle))
G.DrawString(Me.Text, Font, myBrush, TextArea, sFormat)
G.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
G.ResetTransform()

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
Next
Next

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
Rgn.Exclude(Pth)
End If
Me.Region = Rgn

B.Dispose()
G.Dispose()
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?

Stuart
Oct 19 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.