CODES VB.NET

*** AUDIO ***

* Pour lire/arrêter un fichier .wav :
My.Computer.Audio.Play("mon_fichier.wav")
My.Computer.Audio.Stop()


* Signaler la fin d'un long processus :
beep()


* Modifier volume carte son par défaut :
Imports System.Runtime.InteropServices

Declare Function SendMessageW Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr

Const APPCOMMAND_VOLUME_MUTE As Integer = &H80000
Const APPCOMMAND_VOLUME_UP As Integer = &HA0000
Const APPCOMMAND_VOLUME_DOWN As Integer = &H90000
Const WM_APPCOMMAND As Integer = &H319
SendMessageW(Me.Handle, WM_APPCOMMAND, Me.Handle, New IntPtr(APPCOMMAND_VOLUME_DOWN))
SendMessageW(Me.Handle, WM_APPCOMMAND, Me.Handle, New IntPtr(APPCOMMAND_VOLUME_UP))
SendMessageW(Me.Handle, WM_APPCOMMAND, Me.Handle, New IntPtr(APPCOMMAND_VOLUME_MUTE))


*** BDD ***

* Créer une base de données .mdb sans Microsoft Access :
Dim catADOX As New ADOX.Catalog

catADOX.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=ma_base.mdb")
catADOX = Nothing


* Ouvrir/Fermer une base de données :
Dim maConnexion = New ADODB.Connection

maConnexion.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=ma_base.mdb;")
maConnexion.Close()
maConnexion = Nothing


*** CLAVIER ***

* Pour savoir si une touche spéciale est enfoncée :
My.Computer.Keyboard.CtrlKeyDown
My.Computer.Keyboard.AltKeyDown
My.Computer.Keyboard.ShiftKeyDown


* Pour simuler l'appui des touches au clavier :
My.Computer.Keyboard.SendKeys("mes_touches")


*** DESSIN ***

* Obtenir un dégradé bi-ton :
Dim largeur As Integer, hauteur As Integer
Dim point1 As New Point(), point2 As New Point()
Dim couleur1 As New Color(), couleur2 As New Color()

largeur = PictureBox1.Width
hauteur = PictureBox1.Height
couleur1 = Color.Blue
couleur2 = Color.Yellow
point1.X = 0
point1.Y = 0
point2.X = largeur
point2.Y = hauteur

Dim lgbTest As New LinearGradientBrush(point1, point2, couleur1, couleur2)

e.Graphics.FillRectangle(lgbTest, 0, 0, largeur, hauteur)


*** DISQUE DUR ***

* Pour obtenir le numéro de série du disque dur :
Declare Function GetVolumeInformationA Lib "kernel32" (ByVal lpRootPathName As String, ByVal lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Integer, ByRef lpVolumeSerialNumber As Integer, ByVal lpMaximumComponentLength As Integer, ByVal lpFileSystemFlags As Integer, ByVal lpFileSystemNameBuffer As String, ByVal nFileSystemNameSize As Integer) As Integer

Dim mon_numero As Integer

GetVolumeInformationA("c:\", "", 9, mon_numero, 0, 0, "", 3)
MsgBox(Hex(mon_numero))


*** DOSSIER ***

* Obtenir le chemin de l'application :
My.Application.ExecutablePath
My.Application.Info.DirectoryPath
Environment.CommandLine


* Obtenir le chemin des dossiers spéciaux :
My.Computer.FileSystem.SpecialDirectories.MyDocuments
Environment.SpecialFolder.MyComputer


* Créer un dossier :
IO.Directory.CreateDirectory("mon_dossier")


* Savoir si un dossier existe :
IO.Directory.Exists("mon_dossier")


*** FICHIER ***

* Extraire le nom du fichier à partir de son chemin :
Dim fichier As FileInfo = New FileInfo(chemin)
MsgBox(fichier.Name)


* Obtenir/modifier les attributs d'un fichier :
Dim attributsTemp As IO.FileAttributes

attributsTemp = IO.File.GetAttributes(chemin)
IO.File.SetAttributes(chemin, attributsTemp)


* Enlever la lecture seule sur un fichier :
IO.File.SetAttributes(chemin, IO.FileAttributes.Normal)


* Savoir si un fichier existe :
IO.File.Exists("mon_fichier.txt")
My.Computer.FileSystem.FileExists("mon_fichier.txt")


* Pour obtenir la taille d'un fichier :
FileLen("mon_fichier.txt")


* Pour compter le nombre de fichiers d'un dossier :
My.Computer.FileSystem.GetFiles("c:\temp", FileIO.SearchOption.SearchTopLevelOnly, "*.*").Count


*** IMAGE ***

* Importer image dans un controle :
PictureBox1.Image = System.Drawing.Image.FromFile("mon_image.jpg")


* Capture/enregistrement d'écran:
Dim bureau = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)

Graphics.FromImage(bureau).CopyFromScreen(New Point(0, 0), New Point(0, 0), Screen.PrimaryScreen.Bounds.Size)
Graphics.FromImage(bureau).Flush()
bureau.Save("c:\bureau.png", System.Drawing.Imaging.ImageFormat.Png)


* Convertir une image en icone :
Dim image As New Bitmap("image.jpg"), icone as Icon

image.MakeTransparent(Color.White)
icone = Icon.FromHandle(image.GetHicon())
Dim flux As System.IO.StreamWriter = System.IO.File.CreateText("icone.ico")
icone.Save(flux.BaseStream)
flux.Close()


*** MEMOIRE ***

* Obtenir des informations sur la mémoire et le système :
My.Computer.Info.AvailablePhysicalMemory()


* Pour connaitre la mémoire allouée à l'application :
My.Application.Info.WorkingSet


*** MULTITACHE ***

* Pour rendre la main au système (utile pendant les traitements lours, les boucles, etc.) :
Application.DoEvents()


* Pour lancer un thread :
Imports System.Threading

Control.CheckForIllegalCrossThreadCalls = False

Dim monThread0 As New Thread(AddressOf ma_fonction)
monThread0.Start()


* Pour lancer un programme :
Process.Start("C:\Windows\System32\shutdown.exe", "-s -t 0")
Shell("calc.exe", AppWinStyle.NormalFocus, True)


* Pour compter le nombre de processus actifs :
Process.GetProcesses().Count


* Obtenir le nombre de coeurs du processeur :
Imports System.Management

Dim collection As New ManagementObjectSearcher("select * from Win32_Processor"), i As Integer
i = 0
For Each element As ManagementObject In collection.Get
i = i + element.Properties("NumberOfLogicalProcessors").Value
Next
msgbox i & " taches"


*** RACCOURCIS ***

* Pour réduire les appels en cascade :
Imports VB = Microsoft.VisualBasic


* Pour créer un raccourci :
Dim WSHShell = CreateObject("WScript.Shell")
Dim raccourcis = WSHShell.CreateShortcut("calculatrice.lnk")

raccourcis.TargetPath = "c:\windows\system32\calc.exe"
raccourcis.arguments = ""
raccourcis.WorkingDirectory = My.Computer.FileSystem.GetParentPath(raccourcis.TargetPath)
raccourcis.WindowStyle = 4
raccourcis.IconLocation = WSHShell.ExpandEnvironmentStrings(raccourcis.TargetPath & ", 0")
raccourcis.Save()


*** REGISTRE ***

* Pour exporter le registre :
Shell("regedit /e ""c:\registre.txt""", , True)


*** SAUVEGARDE ***

* Pour créer un point de restauration système :
Dim nouveauPoint = GetObject("winmgmts:\\.\root\default:Systemrestore")
Dim retour = nouveauPoint.createrestorepoint("sauvegarde manuelle", 0, 100)


*** SOURIS ***

* Maintenir bouton milieu enfoncé :
Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As Integer)

mouse_event(&H20, 0, 0, 0, 0)


* Relacher bouton milieu :
Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As Integer)

mouse_event(&H40, 0, 0, 0, 0)


* Afficher icone sablier :
Me.Cursor = System.Windows.Forms.Cursors.WaitCursor


*** TABLEAU ***

* Pour effacer une dimension d'un tableau :
Array.Clear(mon_tableau, 0, mon_tableau.Length)


* Pour trier/inverser un tableau :
Array.Sort(tableau)
Array.Reverse(tableau)


* Pour concaténer 2 tableaux :
Dim tab3 = tab1.Union(tab2).ToArray()


*** TEXTE ***

* Répétition de caractères :
StrDup(10, "#")
Space(10)


* Convertir un caractère en une suite binaire :
Dim ascii As Integer, chaine as String

chaine = ""
ascii = Asc("A")
Do
chaine = Format(ascii Mod 2) & chaine
ascii = Fix(ascii / 2)
Loop While (ascii / 2 > 0)
chaine = CDbl(chaine).ToString("00000000")


*** TEMPS ***

* Pour mesurer un écart de temps au millième :
Dim ecart As New StopWatch

ecart.start
MsgBox("écart = " & format(ecart.ElapsedMilliseconds) & " ms")


*** VARIABLES ***

* Pour lister toutes les variables du formulaire actuel :
For Each variable In Me.GetType().GetFields(60)
...
Next