473,507 Members | 2,441 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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()
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
0 1782

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5929
by: Rui Patinha | last post by:
Hello, I have a label in a report that is filled up with text from a query. The problem is that I need to justify the alignment, but not as it does by default... let me try to clear this... ...
4
12303
by: Stuart Norris | last post by:
Dear Readers, I am attempting to draw box around some text using unicode on multiline label. The label is forty characters wide and 12 lines deep. I have been trying to draw a box around text...
2
1419
by: David | last post by:
Hello. I have a Label control on my web form and I have long text in it. What I want is to disable text wrapping of this Label. Is it possible? Also I want to know how can I set text...
7
2454
by: Mike Bulava | last post by:
I have created a base form that I plan to use throughout my application let call the form form1. I have Built the project then add another form that inherits from form1, I add a few panel controls...
6
3132
by: jcrouse | last post by:
I am rotating some text is some label controls. In the one place I use it it works fine. In the other place I use it I can't figure out the syntax. I don't really understand the event. Where it...
24
1807
by: Nak | last post by:
Hi there, I have some inherited forms that I wish to make "localizable", unfortunately none of the items I wish to be "localizable" are actually being "localized". For example, I have a few of...
3
1772
by: Jeff User | last post by:
Hello I am using C#, .net1.1 Vis Studio 2003 I am using homeBase.aspx.cs page as a base for several other aspx/aspx.cs web pages. The base page handles some operations that are common to all...
2
7949
by: André Hänsel | last post by:
Hi again, I have a radio button: <p> <input id="v_yes" type="radio" name="v" value="yes" /> <label for="v_yes">Ja</label> <input id="v_no" type="radio" name="v" value="no" /> <label...
2
2310
by: swapna_munukoti | last post by:
Hi all, I am new to asp.net. So, may be my question may be simple. But I am feeling hard to achieve this. I need to create a web custom label control, for which I have to assign new property...
0
7110
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7030
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7482
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.