.net - 使用 Visual Basic 在 Windows 窗体中嵌入 DOS 控制台

标签 .net windows vb.net winapi windows-console

我设法从其他问题中找到了一些数据,使我能够实现下一个代码:

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

我的问题是我找不到 DOS 控制台窗口。

C#中的问题 Embedding a DOS console in a windows form

最佳答案

使用 bring a console window to front in c#作为基础,您可以修改您的代码:

<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 所说:

It's hacky, it's horrible, but it works for me (thanks, pinvoke.net!):

Cody Gray 对此也是正确的:

You probably can't manage to find it because it won't always have this title: C:\\WINDOWS\\system32\\cmd.exe. Mine doesn't, for example.

所以它有效,但不稳定。

关于.net - 使用 Visual Basic 在 Windows 窗体中嵌入 DOS 控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8831761/

相关文章:

.net - 简单线程速成类?

windows - 是否有任何关于 Windows 远程协作工具的建议?

android - 如何通过wifi设置PC和android设备之间的连接

c++ - 导入库如何工作以及为什么 MinGW 不需要它们?

mysql - 如果重复记录则改变颜色

c# - 具有 protected setter 的最小起订量属性

c# - 无法将 DBNull.Value 转换为类型 'System.Double' 。请使用可空类型

mysql - 单击按钮即可运行两个或多个查询

c# - 比较两个日期与范围

c# - WebApi 文件读取访问被拒绝