Intégration d'une console DOS sous une forme Windows avec Visual Basic
-
27-10-2019 - |
Question
J'ai réussi à trouver de questions des données qui me permet d'obtenir le code suivant:
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll")> _
Public Shared Function MoveWindow(ByVal hWnd As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal bRepaint As Boolean) As Boolean
End Function
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Public Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
End Function
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindow( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As IntPtr
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim hwnd As IntPtr
hwnd = FindWindow(vbNullChar, "C:\\WINDOWS\\system32\\cmd.exe")
If hwnd.Equals(IntPtr.Zero) Then
MessageBox.Show("Got null handle")
Else
SetParent(hwnd, Me.Handle)
MoveWindow(hwnd, 0, 0, Me.Width, Me.Height, False)
End If
End Sub
End Class
Mon problème est que je ne parviens pas à trouver la fenêtre de console DOS.
La question en C # Embedding une console DOS dans une fenêtre forme
La solution
En utilisant afficher une fenêtre de la console à l'avant en c # comme base, vous pouvez modifier votre code:
<DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True)> _
Private Shared Function FindWindowByCaption(ByVal zeroOnly As IntPtr, ByVal lpWindowName As String) As IntPtr
End Function
''in frmLoad:
hwnd = FindWindowByCaption(IntPtr.Zero, "c:\WINDOWS\system32\cmd.exe")
Jon Skeet dit:
Il est aki, il est horrible, mais ça marche pour moi (merci, pinvoke.net!):
Et Cody Gray est aussi correct avec ceci:
Vous ne pouvez probablement pas réussi à le trouver car il n'a pas toujours ce titre:
C:\\WINDOWS\\system32\\cmd.exe
. Le mien ne pas, par exemple.
Il fonctionne, mais est squameuse.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow