



Basics
Data Types Windows Forms Buttons Check Box Text Box Combo Box Radio Buttons Progress Bar Color & Font Dialogs Open & Save Dialogs Process Control
More Complex Examples
Audio Player Rotating Rectangle

Popular Articles
Moving a borderless form in vb.net
ComboBox1.Items.Add
Link Progress Bar with
Timer in vb.net
How to
open a folder using the process control
Start Vb.net programming. No get rich quick
scam
How to Convert the Date Format |
Moving a borderless form in vb .net
To only see the code on how to Drag a Form on the Mouse Down
event Click here
Constructing Alternative Shaped
Forms - Wav Player
In this part of the tutorial I am
going to show you how to make a form in a rather different shape from
the normal square windows form. I will also demonstrate how you can use
code to move your borderless form on the Mouse Down event in vb.net. We will be
making use of the On Paint event to draw the graphics for our
form. The sample application that we will build is but a simple little
audio player that only can play wave sounds (*.wav). Should you want to
write a proper media player you should use the mci send string commands,
which we will discuss briefly later on in this tutorial. In this
tutorial you will also learn how to create controls (with event
handling) in the forms loading procedure.

Figure: Wav Player
You can copy
and paste the following code into your form. Note that
'''All commentary looking like this is
not part of the code. It explains
what the code is doing. After you pasted the code into your application
test it, change it and test it again. that is the best way to get the
hang of it. Open a new project. From the toolbox menu click on the icon
to create an Open File Dialog. Open the forms Code view and paste the
following:
Public Class
Form1
Public WithEvents lblClose As Label
''' Declaring the controls me want to
display on the form.
Public WithEvents lblPlay As Label
''' Declaring that we want to use event
arguments with our
Public WithEvents lblOpen As Label
''' controls
Public WithEvents lblStop As Label
Public WithEvents lblLoop As Label
Public WithEvents lblDisplay As Label
Dim isMouseDown As Integer ''' To
check if mouse is down
Dim MyX, MyY As Integer ''' To
calculate in which direction form should move
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.TransparencyKey = Me.BackColor
''' To Make Form transparent
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
''' Removing the square border around our
form
Me.Width = 360 : Me.Height = 180
''' Setting the form to the correct size
Me.Text = "WavPlay" ''' Changing
our forms text to display "WavPlay" in the taskbar
CreatePlayButton() ''' Calling the
subs(we will create further down)to create our controls on the form
CreateCloseButton()
CreateOpenButton()
CreateDisplay()
CreateStopButton()
CreateLoopButton()
End Sub
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
isMouseDown = 1 '''to check if
mouse is down
MyX = e.X : MyY = e.Y ''' to
calculate form/mouse position increase or decrease
Me.Cursor = Cursors.NoMove2D
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
'''Code to move the form when the
mouse is down
If isMouseDown = 1 Then ''' Move
form only when mouse button is pressed down on form
If e.X > MyX Then Me.Left = Me.Left + 1
If e.X < MyX Then Me.Left = Me.Left - 1
If e.Y > MyY Then Me.Top = Me.Top + 1
If e.Y < MyY Then Me.Top = Me.Top - 1
End If
End Sub
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
isMouseDown = 0 ''' to check if
mouse is down
Me.Cursor = Cursors.Default
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles Me.Paint
''' Drawing the forms shape using graphics method
e.Graphics.FillEllipse(Brushes.Gold, 1, 1, 160, 160)
e.Graphics.FillRectangle(Brushes.Gold, 80, 20, 200, 140)
e.Graphics.FillEllipse(Brushes.Gold, 200, 20, 160, 160)
e.Graphics.FillRectangle(Brushes.DarkSlateGray, 100, 40, 130, 80)
End Sub
Sub CreatePlayButton() ''' Our sub
to create Play button. As u can see we used labels instead of buttons
lblPlay = New Label
lblPlay.Left = 120 : lblPlay.Top = 140
lblPlay.BackColor = Color.Gold
lblPlay.Cursor = Cursors.Hand
lblPlay.AutoSize = True
lblPlay.Text = "Play"
Me.Controls.Add(lblPlay)
End Sub
Sub CreateCloseButton() ''' Our
sub to create Close (X) button.
lblClose = New Label
lblClose.Left = 260 : lblClose.Top = 20
lblClose.BackColor = Color.Gold
lblClose.Cursor = Cursors.Hand
lblClose.AutoSize = True
lblClose.Text = "X"
Me.Controls.Add(lblClose)
End Sub
Sub CreateOpenButton() ''' Our sub
to create Open button.
lblOpen = New Label
lblOpen.Left = 80 : lblOpen.Top = 140
lblOpen.BackColor = Color.Gold
lblOpen.Cursor = Cursors.Hand
lblOpen.AutoSize = True
lblOpen.Text = "Open"
Me.Controls.Add(lblOpen)
End Sub
Sub CreateStopButton() ''' Our sub
to create Stop button.
lblStop = New Label
lblStop.Left = 160 : lblStop.Top = 140
lblStop.BackColor = Color.Gold
lblStop.Cursor = Cursors.Hand
lblStop.AutoSize = True
lblStop.Text = "Stop"
Me.Controls.Add(lblStop)
End Sub
Sub CreateLoopButton() ''' Our sub
to create Loop button.
lblLoop = New Label
lblLoop.Left = 200 : lblLoop.Top = 140
lblLoop.BackColor = Color.Gold
lblLoop.Cursor = Cursors.Hand
lblLoop.AutoSize = True
lblLoop.Text = "Loop"
Me.Controls.Add(lblLoop)
End Sub
Sub CreateDisplay() ''' Our sub to
create our Display window for text
lblDisplay = New Label
lblDisplay.Left = 105 : lblDisplay.Top = 45
lblDisplay.Width = 120 : lblDisplay.Height = 70
lblDisplay.BackColor = Color.Black
lblDisplay.ForeColor = Color.GreenYellow
'lblDisplay.AutoSize = True
lblDisplay.Text = ("..........Audio Player..........")
Me.Controls.Add(lblDisplay)
End Sub
Private Sub lblClose_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles lblClose.Click
Me.Close() ''' Code for Clicking
on Close Button
End Sub
Private Sub lblOpen_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles lblOpen.Click
OpenFileDialog1.Filter = "Audio Files|*.wav"
''' Open file dialog can only display
*.wav extentions
OpenFileDialog1.ShowDialog() '''
command to show dialog
lblDisplay.Text = OpenFileDialog1.SafeFileName ''' Display window to
show the filename the user chose
End Sub
Private Sub lblPlay_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles lblPlay.Click
On Error GoTo MyError ''' error
handling code if user click on play before filename was chosen
''' Play the chosen file
My.Computer.Audio.Play(OpenFileDialog1.FileName, _
AudioPlayMode.Background) : lblDisplay.Text = "Playing: " &
OpenFileDialog1.SafeFileName
Exit Sub ''' as you see above we
use the safefilename to cut out the path
MyError:
My.Computer.Audio.PlaySystemSound( _
Media.SystemSounds.Asterisk)
End Sub
Private Sub lblOpen_MouseHover(ByVal sender As Object, ByVal e As
System.EventArgs) Handles lblOpen.MouseHover
lblDisplay.Text = "Select Audio File *.wav"
''' for visual effects we use the mouse
hover and
End Sub ''' mouse leave events to
change the text in our
''' Display window.
Private Sub lblOpen_MouseLeave(ByVal sender As Object, ByVal e As
System.EventArgs) Handles lblOpen.MouseLeave
lblDisplay.Text = "..........Audio Player.........."
End Sub
Private Sub lblPlay_MouseHover(ByVal sender As Object, ByVal e As
System.EventArgs) Handles lblPlay.MouseHover
lblDisplay.Text = ".......Play Audio File......."
End Sub
Private Sub lblPlay_MouseLeave(ByVal sender As Object, ByVal e As
System.EventArgs) Handles lblPlay.MouseLeave
lblDisplay.Text = "..........Audio Player.........."
End Sub
Private Sub lblClose_MouseHover(ByVal sender As Object, ByVal e As
System.EventArgs) Handles lblClose.MouseHover
lblDisplay.Text = " Exit"
End Sub
Private Sub lblClose_MouseLeave(ByVal sender As Object, ByVal e As
System.EventArgs) Handles lblClose.MouseLeave
lblDisplay.Text = "..........Audio Player.........."
End Sub
Private Sub lblStop_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles lblStop.Click
My.Computer.Audio.Stop() ''' Stop
Playing
End Sub
Private Sub lblLoop_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles lblLoop.Click
My.Computer.Audio.Play(OpenFileDialog1.FileName, _
AudioPlayMode.BackgroundLoop) '''
Looping playmode
End Sub
Private Sub lblStop_MouseHover(ByVal sender As Object, ByVal e As
System.EventArgs) Handles lblStop.MouseHover
lblDisplay.Text = "...............Stop..............."
End Sub
Private Sub lblStop_MouseLeave(ByVal sender As Object, ByVal e As
System.EventArgs) Handles lblStop.MouseLeave
lblDisplay.Text = "..........Audio Player.........."
End Sub
Private Sub lblLoop_MouseHover(ByVal sender As Object, ByVal e As
System.EventArgs) Handles lblLoop.MouseHover
lblDisplay.Text = ".........Play Looped........."
End Sub
Private Sub lblLoop_MouseLeave(ByVal sender As Object, ByVal e As
System.EventArgs) Handles lblLoop.MouseLeave
lblDisplay.Text = "..........Audio Player.........."
End Sub
End Class
If you liked this article then
Please Thumb This Up with a short review -

Back to Top
Email us at: info@pro2visual.com
|