csecules

Is there any way I can rotate a button on a form, or create a custom shaped button I need to make a diamond shaped button, which is really just a 60x60 square button rotated 45 degrees. I've tried creating a custom control, but I just can't seem to get it right. I've done some googling too, but haven't come up with anything helpful. Thanks for any help!



Re: Visual Basic General Rotate a button or create a custom shaped button?

Tall Dude

See my last post at

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1239373&SiteID=1

Start up time of the program is a little slow

because some of the buttons regions are a

little time comsuming to make.

I have some more code that shows how to 'pre-make' the

custom region, save it as a file, and load the region from

a file. (Which is much faster.)

If I get time tommorrow, I will try to find the other code.






Re: Visual Basic General Rotate a button or create a custom shaped button?

Tall Dude

Found that other thing.

See my last post at

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=335218&SiteID=1

(Regions can be used to shape a form or a button.)






Re: Visual Basic General Rotate a button or create a custom shaped button?

csecules

Regions work great, thanks. I think I just had to understand them. Problem fixed.





Re: Visual Basic General Rotate a button or create a custom shaped button?

csecules

Sorry. Another slight problem cropped up. I specified the region, and its bounds look ok. I set the mousedown and mouseup event handlers to color the button border when the button is pressed. Now, I've noticed that the border, which is what gets highlighted, is not 4 straight lines as it should be (the modified button is a square rotated 45 degrees, so that it is a diamond now), but the 4 corners only. It is almost like some other invisible object is sitting on top of the button. I'd really like the button to illuminate all four of its sides when clicked, so that I can show the user that the click worked. Any ideas why only the corners light up, or conversely, why none of the sides get highlighted

Thanks.





Re: Visual Basic General Rotate a button or create a custom shaped button?

JohnWein

I didn't post this code because you said you had moved on.

Add a class to your project and replace the code in the class with this code:

Code Snippet

Public Class DiamondButton

Inherits PictureBox

Private MD As Boolean

Public Shadows Event Click(ByVal sender As Object, ByVal e As System.EventArgs)

Private Sub DrawDiamond(ByVal Raised As Boolean)

Dim W As Int32 = Me.Width

Dim H As Int32 = Me.Height

Dim W2 As Int32 = Me.Width \ 2

Dim H2 As Int32 = Me.Height \ 2

Dim Points1 As Point() = {New Point(0, H2), _

New Point(W2, 0), _

New Point(W, H2)}

Dim Points2 As Point() = {Points1(2), _

New Point(W2, H), _

Points1(0)}

Dim G As Graphics = Graphics.FromImage(Me.Image)

If Raised Then

G.DrawLines(Pens.White, Points1)

G.DrawLines(Pens.Black, Points2)

Else

G.DrawLines(Pens.Black, Points1)

G.DrawLines(Pens.White, Points2)

End If

G.Dispose()

Me.Refresh()

End Sub

Private Sub DiamondButton_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown

Dim X As Double = e.X - Me.Width / 2

Dim Y As Double = e.Y - Me.Height / 2

If Math.Sqrt(X * X + Y * Y) < (Me.Width / 2) Then

DrawDiamond(False)

MD = True

End If

End Sub

Private Sub DiamondButton_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp

If MD Then

DrawDiamond(True)

Dim ec As System.EventArgs = Nothing

RaiseEvent Click(Me, ec)

End If

MD = False

End Sub

Private Sub DiamondButton_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

Dim F As New Font("Arial", Me.Height \ 2, FontStyle.Regular)

e.Graphics.DrawString(Me.Text, F, Brushes.Black, 12, (Me.Height \ 2 - 21))

End Sub

Private Sub DiamondButton_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged

Me.Image = New Bitmap(Me.Width, Me.Height)

DrawDiamond(True)

End Sub

Private T As String

Public Shadows Property Text() As String

Get

Return T

End Get

Set(ByVal value As String)

T = value

End Set

End Property

End Class

Build the project and drag the button onto your form. This may give you some ideas.





Re: Visual Basic General Rotate a button or create a custom shaped button?

csecules

I don't know if this will work for me. My button is not your typical button. I actually have an image on the main form of the piece of hardware I am emulating. I've superimposed button over the device's buttons on the image and set certain properties of each button:

BackColor: Transparent

FlatStyle: Flat

FlatAppearance.BorderColor: toggles between <Nothing> and Yellow for highlighting.

This highlighting method works perfect on all of my regular, rectangular buttons, but the diamond-shaped buttons (which are cursor buttons - up, down, left, right) only highlight the corners, like I said. I need to keep the buttons transparent so that the picture of the actual hardware button can show through.

Any other suggestions