Domanda

Sono riuscito a trovare da altre domande alcuni dati che mi consentono di ottenere il codice successivo:

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

Il mio problema è che non riesco a trovare la finestra della console DOS.

La domanda in C # Incorporamento di una console DOS in un formato Windows

È stato utile?

Soluzione

Utilizzando porta in primo piano una finestra della console in c # come base, puoi modificare il tuo codice:

<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")

Come ha detto Jon Skeet:

È hacky, è orribile, ma per me funziona (grazie, pinvoke.net!):

E anche Cody Gray ha ragione con questo:

Probabilmente non riesci a trovarlo perché non avrà sempre questo titolo: C:\\WINDOWS\\system32\\cmd.exe.Il mio no, per esempio.

Quindi funziona, ma è instabile.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top