Bah voilà t'as réussi à faire ce prog.
Mes critiques constructives :
-------------------------------------------------------Côté interface :
- Le filtre "Tous les fichiers(*.*)" dans ton "openfiledialog" est inutile
- Le titre "sélectionnez un fichier Jpeg" de fenêtre de ton "openfiledialog" contient des guillemets qui n'ont pas leur place.
- Le filtre "Fichiers jpeg(*.jpg) contient un guillemet qui n'a pas sa place non plus.
- Les "ToolStripMenuItem" de ton "MenuStrip1" pourraient être agrémentés d'icônes.
---------------------------------------------------------Côté code :
- Ton code pour ajouter ton soft au clic droit est chargé et redondant et de ce fait peut largement être diminuer de moitié (voir plus).
Q : Comment se rendre compte que le code est redondant (à répétitions) ?
R : Quand une certaine partie (des lignes) sont les mêmes que d'autres dans différents endroits d'une même méthode : procédure (Sub) ou fonction (Function).
Q : Que faire pour diminuer ce code redondant ?
R : On doit créer une méthode générique qui permettrait avec la partie redondante de ce code d'être appliquée pour les différents scénarios.
Q : Comment faire ?
R : En identifiant les variables : les valeurs qui sont visibles dans le code redondant. Ce sont ces valeurs qui font que le code n'est pas entièrement identique pour les différents scénarios.
Dans ton cas :
Code : Tout sélectionner
Dim objArray As Object()
Dim box As TextBox
Dim objArray2 As Object()
Dim flagArray As Boolean()
If Operators.ConditionalCompareObjectEqual(Me.ComboBox1.SelectedItem, "Haut", False) Then
Me.AppPath = Application.ExecutablePath
Me.TextBox1.Text = Me.AppPath
Me.WshShell = RuntimeHelpers.GetObjectValue(Interaction.CreateObject("WScript.Shell", ""))
NewLateBinding.LateCall(Me.WshShell, Nothing, "RegWrite", New Object() { "HKEY_CLASSES_ROOT\DesktopBackground\Shell\NCLS\", "NCLS", "REG_SZ" }, Nothing, Nothing, Nothing, True)
objArray = New Object(3 - 1) {}
objArray(0) = "HKEY_CLASSES_ROOT\DesktopBackground\Shell\NCLS\icon"
box = Me.TextBox1
objArray(1) = box.Text
objArray(2) = "REG_SZ"
objArray2 = objArray
flagArray = New Boolean() { False, True, False }
NewLateBinding.LateCall(Me.WshShell, Nothing, "RegWrite", objArray2, Nothing, Nothing, flagArray, True)
If flagArray(1) Then
box.Text = CStr(Conversions.ChangeType(RuntimeHelpers.GetObjectValue(objArray2(1)), GetType(String)))
End If
NewLateBinding.LateCall(Me.WshShell, Nothing, "RegWrite", New Object() { "HKEY_CLASSES_ROOT\DesktopBackground\Shell\NCLS\Position", "Top", "REG_SZ" }, Nothing, Nothing, Nothing, True)
objArray2 = New Object(3 - 1) {}
objArray2(0) = "HKEY_CLASSES_ROOT\DesktopBackground\Shell\NCLS\command\"
box = Me.TextBox1
objArray2(1) = box.Text
objArray2(2) = "REG_SZ"
objArray = objArray2
flagArray = New Boolean() { False, True, False }
NewLateBinding.LateCall(Me.WshShell, Nothing, "RegWrite", objArray, Nothing, Nothing, flagArray, True)
If flagArray(1) Then
box.Text = CStr(Conversions.ChangeType(RuntimeHelpers.GetObjectValue(objArray(1)), GetType(String)))
End If
ElseIf Operators.ConditionalCompareObjectEqual(Me.ComboBox1.SelectedItem, "Milieu", False) Then
Me.AppPath = Application.ExecutablePath
Me.TextBox1.Text = Me.AppPath
Me.WshShell = RuntimeHelpers.GetObjectValue(Interaction.CreateObject("WScript.Shell", ""))
NewLateBinding.LateCall(Me.WshShell, Nothing, "RegWrite", New Object() { "HKEY_CLASSES_ROOT\DesktopBackground\Shell\NCLS\", "NCLS", "REG_SZ" }, Nothing, Nothing, Nothing, True)
objArray2 = New Object(3 - 1) {}
objArray2(0) = "HKEY_CLASSES_ROOT\DesktopBackground\Shell\NCLS\icon"
box = Me.TextBox1
objArray2(1) = box.Text
objArray2(2) = "REG_SZ"
objArray = objArray2
flagArray = New Boolean() { False, True, False }
NewLateBinding.LateCall(Me.WshShell, Nothing, "RegWrite", objArray, Nothing, Nothing, flagArray, True)
If flagArray(1) Then
box.Text = CStr(Conversions.ChangeType(RuntimeHelpers.GetObjectValue(objArray(1)), GetType(String)))
End If
NewLateBinding.LateCall(Me.WshShell, Nothing, "RegWrite", New Object() { "HKEY_CLASSES_ROOT\DesktopBackground\Shell\NCLS\Position", "Center", "REG_SZ" }, Nothing, Nothing, Nothing, True)
objArray2 = New Object(3 - 1) {}
objArray2(0) = "HKEY_CLASSES_ROOT\DesktopBackground\Shell\NCLS\command\"
box = Me.TextBox1
objArray2(1) = box.Text
objArray2(2) = "REG_SZ"
objArray = objArray2
flagArray = New Boolean() { False, True, False }
NewLateBinding.LateCall(Me.WshShell, Nothing, "RegWrite", objArray, Nothing, Nothing, flagArray, True)
If flagArray(1) Then
box.Text = CStr(Conversions.ChangeType(RuntimeHelpers.GetObjectValue(objArray(1)), GetType(String)))
End If
ElseIf Operators.ConditionalCompareObjectEqual(Me.ComboBox1.SelectedItem, "Bas", False) Then
Me.AppPath = Application.ExecutablePath
Me.TextBox1.Text = Me.AppPath
Me.WshShell = RuntimeHelpers.GetObjectValue(Interaction.CreateObject("WScript.Shell", ""))
NewLateBinding.LateCall(Me.WshShell, Nothing, "RegWrite", New Object() { "HKEY_CLASSES_ROOT\DesktopBackground\Shell\NCLS\", "NCLS", "REG_SZ" }, Nothing, Nothing, Nothing, True)
objArray2 = New Object(3 - 1) {}
objArray2(0) = "HKEY_CLASSES_ROOT\DesktopBackground\Shell\NCLS\icon"
box = Me.TextBox1
objArray2(1) = box.Text
objArray2(2) = "REG_SZ"
objArray = objArray2
flagArray = New Boolean() { False, True, False }
NewLateBinding.LateCall(Me.WshShell, Nothing, "RegWrite", objArray, Nothing, Nothing, flagArray, True)
If flagArray(1) Then
box.Text = CStr(Conversions.ChangeType(RuntimeHelpers.GetObjectValue(objArray(1)), GetType(String)))
End If
NewLateBinding.LateCall(Me.WshShell, Nothing, "RegWrite", New Object() { "HKEY_CLASSES_ROOT\DesktopBackground\Shell\NCLS\Position", "Bottom", "REG_SZ" }, Nothing, Nothing, Nothing, True)
objArray2 = New Object(3 - 1) {}
objArray2(0) = "HKEY_CLASSES_ROOT\DesktopBackground\Shell\NCLS\command\"
box = Me.TextBox1
objArray2(1) = box.Text
objArray2(2) = "REG_SZ"
objArray = objArray2
flagArray = New Boolean() { False, True, False }
NewLateBinding.LateCall(Me.WshShell, Nothing, "RegWrite", objArray, Nothing, Nothing, flagArray, True)
If flagArray(1) Then
box.Text = CStr(Conversions.ChangeType(RuntimeHelpers.GetObjectValue(objArray(1)), GetType(String)))
End If
End If
On remarque vite la redondance de cette partie de code :
Code : Tout sélectionner
Me.AppPath = Application.ExecutablePath
Me.TextBox1.Text = Me.AppPath
Me.WshShell = RuntimeHelpers.GetObjectValue(Interaction.CreateObject("WScript.Shell", ""))
NewLateBinding.LateCall(Me.WshShell, Nothing, "RegWrite", New Object() { "HKEY_CLASSES_ROOT\DesktopBackground\Shell\NCLS\", "NCLS", "REG_SZ" }, Nothing, Nothing, Nothing, True)
objArray = New Object(3 - 1) {}
objArray(0) = "HKEY_CLASSES_ROOT\DesktopBackground\Shell\NCLS\icon"
box = Me.TextBox1
objArray(1) = box.Text
objArray(2) = "REG_SZ"
objArray2 = objArray
flagArray = New Boolean() { False, True, False }
NewLateBinding.LateCall(Me.WshShell, Nothing, "RegWrite", objArray2, Nothing, Nothing, flagArray, True)
If flagArray(1) Then
box.Text = CStr(Conversions.ChangeType(RuntimeHelpers.GetObjectValue(objArray2(1)), GetType(String)))
End If
NewLateBinding.LateCall(Me.WshShell, Nothing, "RegWrite", New Object() { "HKEY_CLASSES_ROOT\DesktopBackground\Shell\NCLS\Position", "Top", "REG_SZ" }, Nothing, Nothing, Nothing, True)
objArray2 = New Object(3 - 1) {}
objArray2(0) = "HKEY_CLASSES_ROOT\DesktopBackground\Shell\NCLS\command\"
box = Me.TextBox1
objArray2(1) = box.Text
objArray2(2) = "REG_SZ"
objArray = objArray2
flagArray = New Boolean() { False, True, False }
NewLateBinding.LateCall(Me.WshShell, Nothing, "RegWrite", objArray, Nothing, Nothing, flagArray, True)
If flagArray(1) Then
box.Text = CStr(Conversions.ChangeType(RuntimeHelpers.GetObjectValue(objArray(1)), GetType(String)))
End If
Donc pour diminuer en grande partie tout ca il suffit d'identifier les variables (Haut, milieu, bas (Top, Center, Bottom).
) qui font que chaque partie redondantes sont différentes afin de les passer en argument à une procédure.
-->Le nom de ta procédure pourrait s'appeler : CreateRightClic()
-->L'argument à passer à ta procédure serait : La variable de la position de l'item qu'on pourrait nommer "Posit".
-->La procédure s'écrit donc ainsi :
Code : Tout sélectionner
Private sub CreateRightClic (Byval Posit as string)
'Tu places ici ton code redondant et ta variable "Posit" là ou il est nécessaire de l'utiliser
End sub
-->Et voilà la procédure complète :
Code : Tout sélectionner
Private sub CreateRightClic (Byval Posit as string)
Dim objArray As Object()
Dim box As TextBox
Dim objArray2 As Object()
Dim flagArray As Boolean()
Me.AppPath = Application.ExecutablePath
Me.TextBox1.Text = Me.AppPath
Me.WshShell = RuntimeHelpers.GetObjectValue(Interaction.CreateObject("WScript.Shell", ""))
NewLateBinding.LateCall(Me.WshShell, Nothing, "RegWrite", New Object() { "HKEY_CLASSES_ROOT\DesktopBackground\Shell\NCLS\", "NCLS", "REG_SZ" }, Nothing, Nothing, Nothing, True)
objArray = New Object(3 - 1) {}
objArray(0) = "HKEY_CLASSES_ROOT\DesktopBackground\Shell\NCLS\icon"
box = Me.TextBox1
objArray(1) = box.Text
objArray(2) = "REG_SZ"
objArray2 = objArray
flagArray = New Boolean() { False, True, False }
NewLateBinding.LateCall(Me.WshShell, Nothing, "RegWrite", objArray2, Nothing, Nothing, flagArray, True)
If flagArray(1) Then
box.Text = CStr(Conversions.ChangeType(RuntimeHelpers.GetObjectValue(objArray2(1)), GetType(String)))
End If
NewLateBinding.LateCall(Me.WshShell, Nothing, "RegWrite", New Object() { "HKEY_CLASSES_ROOT\DesktopBackground\Shell\NCLS\Position", Posit, "REG_SZ" }, Nothing, Nothing, Nothing, True)
objArray2 = New Object(3 - 1) {}
objArray2(0) = "HKEY_CLASSES_ROOT\DesktopBackground\Shell\NCLS\command\"
box = Me.TextBox1
objArray2(1) = box.Text
objArray2(2) = "REG_SZ"
objArray = objArray2
flagArray = New Boolean() { False, True, False }
NewLateBinding.LateCall(Me.WshShell, Nothing, "RegWrite", objArray, Nothing, Nothing, flagArray, True)
If flagArray(1) Then
box.Text = CStr(Conversions.ChangeType(RuntimeHelpers.GetObjectValue(objArray(1)), GetType(String)))
End If
End sub
-->Il ne faut pas oublier que lorsque l'utilisateur sélectionne une valeur dans la Combobox qu'il choisit entre celleslà :
- Haut
- Milieu
- Bas
Il faut donc établir une correspondance pour que la valeur qui sera envoyé en tant qu'argument dans ta procédure "CreateRightClic()" c'est à dire l'argument "Posit" arrive dans le corps de ta procédure en tant que valeur qui sera inscrite comme cela dans le registre :
- Top
- Center
- Bottom
Pour se faire tu peux utiliser une conditionnel (If ...Elseif...Elseif... End if) ou un connecteur de sélection (Select ....Case....Case...Case... End Select) :
---> Voilà enfin la procédure finale à placer dans ton "form1" :
Code : Tout sélectionner
Private sub CreateRightClic (Byval Posit as string)
Dim Posit as string = Posit
Select Case Posit
Case "Haut"
Posit = "Top"
Case "Milieu"
Posit = "Center"
Case "Bas"
Posit = "Bottom"
End Select
Dim objArray As Object()
Dim box As TextBox
Dim objArray2 As Object()
Dim flagArray As Boolean()
Me.AppPath = Application.ExecutablePath
Me.TextBox1.Text = Me.AppPath
Me.WshShell = RuntimeHelpers.GetObjectValue(Interaction.CreateObject("WScript.Shell", ""))
NewLateBinding.LateCall(Me.WshShell, Nothing, "RegWrite", New Object() { "HKEY_CLASSES_ROOT\DesktopBackground\Shell\NCLS\", "NCLS", "REG_SZ" }, Nothing, Nothing, Nothing, True)
objArray = New Object(3 - 1) {}
objArray(0) = "HKEY_CLASSES_ROOT\DesktopBackground\Shell\NCLS\icon"
box = Me.TextBox1
objArray(1) = box.Text
objArray(2) = "REG_SZ"
objArray2 = objArray
flagArray = New Boolean() { False, True, False }
NewLateBinding.LateCall(Me.WshShell, Nothing, "RegWrite", objArray2, Nothing, Nothing, flagArray, True)
If flagArray(1) Then
box.Text = CStr(Conversions.ChangeType(RuntimeHelpers.GetObjectValue(objArray2(1)), GetType(String)))
End If
NewLateBinding.LateCall(Me.WshShell, Nothing, "RegWrite", New Object() { "HKEY_CLASSES_ROOT\DesktopBackground\Shell\NCLS\Position", Posit, "REG_SZ" }, Nothing, Nothing, Nothing, True)
objArray2 = New Object(3 - 1) {}
objArray2(0) = "HKEY_CLASSES_ROOT\DesktopBackground\Shell\NCLS\command\"
box = Me.TextBox1
objArray2(1) = box.Text
objArray2(2) = "REG_SZ"
objArray = objArray2
flagArray = New Boolean() { False, True, False }
NewLateBinding.LateCall(Me.WshShell, Nothing, "RegWrite", objArray, Nothing, Nothing, flagArray, True)
If flagArray(1) Then
box.Text = CStr(Conversions.ChangeType(RuntimeHelpers.GetObjectValue(objArray(1)), GetType(String)))
End If
End sub
---> Et voilà enfin ton code diminué de 2/3 :
Code : Tout sélectionner
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
If Operators.ConditionalCompareObjectEqual(Me.ComboBox1.SelectedItem, "Haut", False) Then
CreateRightClic(Me.ComboBox1.SelectedItem)
ElseIf Operators.ConditionalCompareObjectEqual(Me.ComboBox1.SelectedItem, "Milieu", False) Then
CreateRightClic(Me.ComboBox1.SelectedItem)
ElseIf Operators.ConditionalCompareObjectEqual(Me.ComboBox1.SelectedItem, "Bas", False) Then
CreateRightClic(Me.ComboBox1.SelectedItem)
End If
End sub
Voilà le raisonnement. On otpimise le code en supprimant le superflux.
Maintenant ton code pour ajouter un menu contextuel est très lourd, il y a plus simple mais c'est ton choix...Tu préfères toujours utiliser le shell de vbs tu déclares une multitude de variables alors qu'elles inutiles et consommatrices de mémoire...OO
J'espère que tu tireras un trait sur cette méthode car le framework offre bien plus de possibilités et qui sont bien plus simples à mettre en place.

*
Bonne continuation.