Not sure how to do this. I just want to align my Form title to the middle. That's all.
Add space's before the title name ?
(Feb 12, 2016 10:19 PM)Exodus. Wrote: Not sure how to do this. I just want to align my Form title to the middle. That's all. In windows 8/8.1 form titles are automatically aligned to the middle:
![[Image: wwMbRBS.png]](https://i.imgur.com/wwMbRBS.png)
What OS are you using?
I had this issue before, i dont think there is a way to do it through simple properties. Using google i found this for you.
Code Private Sub CenterMe()
Dim g As Graphics = Me.CreateGraphics()
Dim startingPoint As Double = (Me.Width / 2) - (g.MeasureString(Me.Text.Trim, Me.Font).Width / 2)
Dim widthOfASpace As Double = g.MeasureString(" ", Me.Font).Width
Dim tmp As String = " "
Dim tmpWidth As Double = 0
Do
tmp += " "
tmpWidth += widthOfASpace
Loop While (tmpWidth + widthOfASpace) < startingPoint
Me.Text = tmp & Me.Text.Trim & tmp
Me.Refresh()
End Sub
Just call "CenterMe()" On Form_Load event & you should be good. I think a more simpler way would to just add a couple spaces before like someone mentioned, but i guess this works correctly.
DO NOT DEAL WITH THIS MEMBER
There are no direct methods to this, you would have to draw your own form.
(Feb 13, 2016 04:27 AM)Specs Wrote: I had this issue before, i dont think there is a way to do it through simple properties. Using google i found this for you.
Code Private Sub CenterMe()
Dim g As Graphics = Me.CreateGraphics()
Dim startingPoint As Double = (Me.Width / 2) - (g.MeasureString(Me.Text.Trim, Me.Font).Width / 2)
Dim widthOfASpace As Double = g.MeasureString(" ", Me.Font).Width
Dim tmp As String = " "
Dim tmpWidth As Double = 0
Do
tmp += " "
tmpWidth += widthOfASpace
Loop While (tmpWidth + widthOfASpace) < startingPoint
Me.Text = tmp & Me.Text.Trim & tmp
Me.Refresh()
End Sub
Just call "CenterMe()" On Form_Load event & you should be good. I think a more simpler way would to just add a couple spaces before like someone mentioned, but i guess this works correctly.
This would need to be called on form resize. It's not very efficient either, cleaned it up:
Code Private Sub CenterMe()
Using g As Graphics = Me.CreateGraphics()
Dim startPoint As Double = (Me.Width / 2) - (g.MeasureString(Me.Text.Trim(), Me.Font).Width / 2)
Dim spaceWidth As Double = g.MeasureString(" ", Me.Font).Width
Me.Text = New String(" "c, CInt(startPoint / spaceWidth)) & Me.Text.Trim()
End Using
Me.Refresh()
End Sub
DO NOT DEAL WITH THIS MEMBER
This is what I do with GDI+ themes.
Code Dim G = e.Graphics
Dim F As New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center}
G.DrawString(Text, Font, New Rectangle(0, 0, Width - 1, 40), F)
Replace 40 for the height of the header. Sorry if there are any mistakes, types that all out on my phone.
DO NOT DEAL WITH THIS MEMBER
(Feb 13, 2016 05:34 AM)Earn Wrote: This is what I do with GDI+ themes.
Code Dim G = e.Graphics
Dim F As New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center}
G.DrawString(Text, Font, New Rectangle(0, 0, Width - 1, 40), F)
Replace 40 for the height of the header. Sorry if there are any mistakes, types that all out on my phone.
But you dont declare the type of G nor do you dispose of G afterwards...
DO NOT DEAL WITH THIS MEMBER
(Feb 13, 2016 05:43 AM)Ninja Wrote: But you dont declare the type of G nor do you dispose of G afterwards...
I just done it quickly as I am on my phone. I only put the "Dim G = e.Graphics" there so the OP wasn't confused by the "G.DrawString()".
DO NOT DEAL WITH THIS MEMBER
Feb 13, 2016 05:49 AM
(This post was last modified: Feb 13, 2016 05:49 AM by Ninja.)
(Feb 13, 2016 05:47 AM)Earn Wrote: I just done it quickly as I am on my phone. I only put the "Dim G = e.Graphics" there so the OP wasn't confused by the "G.DrawString()".
Code Using G as Graphics = e.Graphics
Dim F As New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center}
G.DrawString(Text, Font, New Rectangle(0, 0, Width - 1, 40), F)
End Using
Cleaned it a bit up for you.
DO NOT DEAL WITH THIS MEMBER
(Feb 13, 2016 05:49 AM)Ninja Wrote: Code Using G as Graphics = e.Graphics
Dim F As New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center}
G.DrawString(Text, Font, New Rectangle(0, 0, Width - 1, 40), F)
End Using
Cleaned it a bit up for you.
Disposing isn't needed, as the graphics object will get disposed when you exit the method.
DO NOT DEAL WITH THIS MEMBER
(Feb 13, 2016 05:54 AM)Earn Wrote: Disposing isn't needed, as the graphics object will get disposed when you exit the method.
Oh I didn't know that. Still wouldn't hurt for good practice though.
|