Win32Api Class

This is a sample of Win32Api Classes

Class found on Developer Fusion

''''''''''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



The Microsoft Class


' Copyright (c) Microsoft Corporation. All rights reserved.
Imports System.Runtime.InteropServices
Imports System.Text
' Class to wrap up Windows 32 API constants and functions.
Public Class Win32API
_
Public Structure OSVersionInfo
Public OSVersionInfoSize As Integer
Public majorVersion As Integer
Public minorVersion As Integer
Public buildNumber As Integer
Public platformId As Integer
_
Public versionString As String
End Structure

_
Public Structure SECURITY_ATTRIBUTES
Public nLength As Integer 'dword
Public lpSecurityDescriptor As Integer 'lpvoid
Public bInheritHandle As Integer 'bool
End Structure

Public Const GWL_EXSTYLE As Integer = (-20)
Public Const GW_OWNER As Integer = 4
Public Const SW_RESTORE As Integer = 9
Public Const SW_SHOW As Integer = 5
Public Const WS_EX_TOOLWINDOW As Integer = &H80
Public Const WS_EX_APPWINDOW As Integer = &H40000

Public Declare Function CreateDirectory Lib "kernel32" _
Alias "CreateDirectoryA" (ByVal lpPathName As String, _
ByRef lpSecurityAttributes _
As SECURITY_ATTRIBUTES) As Boolean


Public Delegate Function EnumWindowsCallback(ByVal hWnd As Integer, _
ByVal lParam As Integer) As Boolean

Public Declare Function EnumWindows Lib "user32.dll" _
Alias "EnumWindows" (ByVal callback As EnumWindowsCallback, _
ByVal lParam As Integer) As Integer

CharSet:=CharSet.Ansi, ExactSpelling:=True, _
CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function EnumWindowsDllImport(ByVal callback As EnumWindowsCallback, _
ByVal lParam As Integer) As Integer
End Function

Public Declare Auto Function FindWindow Lib "user32.dll" _
Alias "FindWindow" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Integer

Public Declare Auto Function FindWindowAny Lib "user32.dll" _
Alias "FindWindow" (ByVal lpClassName As Integer, _
ByVal lpWindowName As Integer) As Integer

Public Declare Auto Function FindWindowNullClassName Lib "user32.dll" _
Alias "FindWindow" (ByVal lpClassName As Integer, _
ByVal lpWindowName As String) As Integer

Public Declare Auto Function FindWindowNullWindowCaption Lib "user32.dll" _
Alias "FindWindow" (ByVal lpClassName As String, _
ByVal lpWindowName As Integer) As Integer


Public Declare Function GetClassName Lib "user32.dll" _
Alias "GetClassNameA" (ByVal hwnd As Integer, _
ByVal lpClassName As StringBuilder, _
ByVal cch As Integer) As Integer

Public Declare Function GetDiskFreeSpace Lib "kernel32" _
Alias "GetDiskFreeSpaceA" (ByVal lpRootPathName As String, _
ByRef lpSectorsPerCluster As Integer, _
ByRef lpBytesPerSector As Integer, _
ByRef lpNumberOfFreeClusters As Integer, _
ByRef lpTotalNumberOfClusters As Integer) As Integer


Public Declare Function GetDiskFreeSpaceEx Lib "kernel32" _
Alias "GetDiskFreeSpaceExA" (ByVal lpRootPathName As String, _
ByRef lpFreeBytesAvailableToCaller As Integer, _
ByRef lpTotalNumberOfBytes As Integer, _
ByRef lpTotalNumberOfFreeBytes As UInt32) As Integer

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

Public Declare Function GetParent Lib "user32.dll" _
Alias "GetParent" (ByVal hwnd As Integer) As Integer


Declare Ansi Function GetVersionEx Lib "kernel32.dll" _
Alias "GetVersionExA" (ByRef osvi As OSVersionInfo) As Boolean

Public Declare Function GetWindow Lib "user32.dll" _
Alias "GetWindow" (ByVal hwnd As Integer, _
ByVal wCmd As Integer) As Integer

Public Declare Function GetWindowLong Lib "user32.dll" _
Alias "GetWindowLongA" (ByVal hwnd As Integer, _
ByVal nIndex As Integer) As Integer

Public Declare Sub GetWindowText Lib "user32.dll" _
Alias "GetWindowTextA" (ByVal hWnd As Integer, _
ByVal lpString As StringBuilder, _
ByVal nMaxCount As Integer)

Public Declare Function IsIconic Lib "user32.dll" _
Alias "IsIconic" (ByVal hwnd As Integer) As Boolean

Public Declare Function IsPwrHibernateAllowed Lib "Powrprof.dll" _
Alias "IsPwrHibernateAllowed" () As Integer

Public Declare Function IsWindowVisible Lib "user32.dll" _
Alias "IsWindowVisible" (ByVal hwnd As Integer) As Boolean

Public Declare Function SetForegroundWindow Lib "user32.dll" _
Alias "SetForegroundWindow" (ByVal hwnd As Integer) As Integer

Public Declare Function SetSuspendState Lib "Powrprof.dll" _
Alias "SetSuspendState" (ByVal Hibernate As Integer, _
ByVal ForceCritical As Integer, _
ByVal DisableWakeEvent As Integer) As Integer

Public Declare Function ShowWindow Lib "user32.dll" _
Alias "ShowWindow" (ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer

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

End Class

0 comments:

Post a Comment