API Samples

The Code

Here is the code for the form/controls/apis.  Very simple with comments and all.

   ''''''''''Declare API "Calls"''''''''''''''''''''''''''''''''
   Public Declare Function GetDriveType Lib "kernel32" _
   Alias "GetDriveTypeA" (ByVal nDrive As String) As Integer

   Declare Function SwapMouseButton Lib "user32.dll" _
   Alias "SwapMouseButton" (ByVal bSwap As Integer) As Integer

   Public Declare Function DeclareBeep Lib "kernel32" Alias "Beep" _
   (ByVal dwFreq As Integer, ByVal dwDuration As Integer) As Integer
   '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

   Private Sub btnUserName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUserName.Click
       'Display a message box with the name of the current user

       Dim strUserName As String = Environment.ExpandEnvironmentVariables("%username%")
       MessageBox.Show("The current user of the computer is: " & strUserName, "UserName")
   End Sub

   Private Sub btnComputerName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnComputerName.Click
       'Display a message box with the name of the computer

       Dim strComputerName As String = Environment.ExpandEnvironmentVariables("%ComputerName%")
       MessageBox.Show("The current computer name is: " & strComputerName, "ComputerName")
   End Sub

   Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
       'Exit the program

       Me.Close()
   End Sub

   Private Sub btnGetMouseX_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetMouseX.Click
       'Display the verticle position of the cursor

       lblMouseX.Text = Windows.Forms.Cursor.Position.X
   End Sub

   Private Sub btnGetMouseY_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetMouseY.Click
       'Display the horizontal position of the cursor

       lblMouseY.Text = Windows.Forms.Cursor.Position.Y
   End Sub

   Private Sub btnDriveType_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDriveType.Click
       'Display the Drive type for the Drive letter they entered

       Dim strMyDrive As String
       strMyDrive = txtDrive.Text + ":\"

       Select Case GetDriveType(strMyDrive)
           Case 2
               MessageBox.Show("Drive type: Removable")
           Case 3
               MessageBox.Show("Drive type: Fixed")
           Case Is = 4
               MessageBox.Show("Drive type: Remote")
           Case Is = 5
               MessageBox.Show("Drive type: Cd-Rom")
           Case Is = 6
               MessageBox.Show("Drive type: Ram disk")
           Case Else
               MessageBox.Show("Drive type: Unrecognized")
       End Select
   End Sub

   Private Sub btnSwapMouse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSwapMouse.Click
       'Switch the left and right mouse buttons

       SwapMouseButton(1)
   End Sub

   Private Sub btnResetMouse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResetMouse.Click
       'Restore the normal mouse button settings

       SwapMouseButton(0)
   End Sub

   Private Sub frmAPI_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
       'If they wanted to switch thier mouse back on exit, then do it

       If chkNormalMouseOnExit.Checked = True Then
           'Normalize the mouse
           SwapMouseButton(0)

           MessageBox.Show("You mouse settings are now 'normal'.", "Mouse settings", MessageBoxButtons.OK, MessageBoxIcon.Information)
       End If
   End Sub

   Private Sub trkBeepLength_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles trkBeepLength.ValueChanged
       'Show the user they select for 'Beep Length'

       lblBeepLength.Text = "Beep Length: " & trkBeepLength.Value * 100
   End Sub

   Private Sub trkBeepFreq_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles trkBeepFreq.ValueChanged
       'Show the user they select for 'Beep Frequency'

       lblBeepFreq.Text = "Beep Frequency: " & trkBeepFreq.Value * 100
   End Sub

   Private Sub btnBeep_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBeep.Click
       'Beep with the chosen settings

       DeclareBeep(trkBeepFreq.Value * 100, trkBeepLength.Value * 100)
   End Sub

You might also like...

Comments

Matthew Taylor

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” - Tom Cargill