没见过优化大师,下面的代码应该可以吧CMD窗体嵌套到你自己的窗体内:
Declare Function SetParent Lib "user32" Alias "SetParent" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
System.Diagnostics.Process.Start("c:\windows\system32\cmd.exe")
System.Threading.Thread.Sleep(50) '我发现,不休息的话,下面的FindWindow有可能找不到窗体
Dim h As IntPtr
h = FindWindow(vbNullString, "c:\windows\system32\cmd.exe")
Public Shared Sub Main()
If System.Environment.OSVersion.ToString.IndexOf("NT") = -1 Then
MsgBox("暂时不支持非NT系统,程序退出!")
Application.Exit()
Else
Application.Run(New Form1)
End If
End Sub
#Region "private viable"
Dim sw As IO.StreamWriter
Dim sr As New MyStreamReader
Dim err As New MyStreamReader
Dim p As System.Diagnostics.Process = New System.Diagnostics.Process
Dim psI As New System.Diagnostics.ProcessStartInfo(System.Environment.GetEnvironmentVariable("ComSpec"))
#End Region
Friend Class MyStreamReader
Public stream As IO.StreamReader
Public bytes(1024) As Byte
Public Sub New()
End Sub
End Class
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
p = New System.Diagnostics.Process
'"cmd.exe"为nt的命令行程序
psI = New System.Diagnostics.ProcessStartInfo("cmd.exe") '指定启动进程时用的一组数值
psI.UseShellExecute = False '是否使用外壳程序启动进程
psI.RedirectStandardInput = True '从文件读入数据
psI.RedirectStandardOutput = True '向文件写入数据
psI.RedirectStandardError = True '用于向文件或日志写入数据
psI.CreateNoWindow = True '是否在新窗口中启动该进程值
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Delegate Sub TextAddHandler(ByVal strPara As String)
Private Sub AppText(ByVal strAdd As String)
Dim sums As Object '用于传递
Dim flag As Boolean = False
Dim i As Object
Me.TextBox1.Text &= strAdd
End Sub
'回调函数
'为了保证执行invoke方法的线程顺序,使用了MethodImpl
<MethodImpl(MethodImplOptions.Synchronized)> _
Sub CBstream(ByVal s As IAsyncResult)
Try
Dim t As MyStreamReader = CType(s.AsyncState, MyStreamReader)
If t.stream.BaseStream Is Nothing Then
Exit Sub
End If
Dim i As Integer = t.stream.BaseStream.EndRead(s)
Dim strReceive As String = System.Text.Encoding.Default.GetString(t.bytes, 0, i)
Me.Invoke(New TextAddHandler(AddressOf AppText), New Object() {strReceive})
t.stream.BaseStream.BeginRead(t.bytes, 0, 1024, New AsyncCallback(AddressOf CBstream), t)
Catch ex As Exception
MsgBox("CBstream--" & ex.ToString)
End Try
End Sub
Private Sub Bt执行_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bt执行.Click
Me.CmdExcute()
End Sub
Private Sub CmdExcute()
Try
Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
If Me.ComboBox1.Text <> "" Then
sw.WriteLine(Me.ComboBox1.Text)
Else
sw.WriteLine("Dir")
End If
Me.Cursor = System.Windows.Forms.Cursors.Default
'向combobox中添加元素
' Me.ComboBox1.Items.Add(Me.ComboBox1.Text)
' Me.ComboBox1.SelectionStart = 0
' Me.ComboBox1.SelectionLength = Me.ComboBox1.Text.Length
Me.ComboBox1.Focus()
Catch ex As Exception
MsgBox("CmdExcute--" & ex.ToString)
End Try
End Sub
Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
Try
p.Close()
If Not sw Is Nothing Then
sw.Close()
End If
If Not sr Is Nothing Then
sr.stream.Close()
End If
If Not err Is Nothing Then
err.stream.Close()
End If
Catch ex As Exception
End
End Try
End
End Sub
Private Sub Bt清空_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bt清空.Click
Me.TextBox1.Text = String.Empty
End Sub
Private Sub ComboBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown
If e.KeyCode = Keys.Enter Then '是否按下回车键
Me.CmdExcute()
End If
End Sub
End Class
Imports System.Threading
Imports System.Runtime.CompilerServices
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
Application.EnableVisualStyles()
Application.DoEvents()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Panel1 As System.Windows.Forms.Panel
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Bt执行 As System.Windows.Forms.Button
Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
Friend WithEvents Bt清空 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Panel1 = New System.Windows.Forms.Panel
Me.ComboBox1 = New System.Windows.Forms.ComboBox
Me.Label1 = New System.Windows.Forms.Label
Me.Bt执行 = New System.Windows.Forms.Button
Me.Bt清空 = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Panel1.SuspendLayout()
Me.SuspendLayout()
'
'Panel1
'
Me.Panel1.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.Panel1.Controls.Add(Me.ComboBox1)
Me.Panel1.Controls.Add(Me.Label1)
Me.Panel1.Controls.Add(Me.Bt执行)
Me.Panel1.Controls.Add(Me.Bt清空)
Me.Panel1.Location = New System.Drawing.Point(8, 352)
Me.Panel1.Name = "Panel1"
Me.Panel1.Size = New System.Drawing.Size(560, 48)
Me.Panel1.TabIndex = 1
'
'ComboBox1
'
Me.ComboBox1.Location = New System.Drawing.Point(216, 16)
Me.ComboBox1.Name = "ComboBox1"
Me.ComboBox1.Size = New System.Drawing.Size(176, 20)
Me.ComboBox1.TabIndex = 3
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(152, 16)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(48, 24)
Me.Label1.TabIndex = 2
Me.Label1.Text = "命令"
'
'Bt执行
'
Me.Bt执行.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.Bt执行.Location = New System.Drawing.Point(440, 16)
Me.Bt执行.Name = "Bt执行"
Me.Bt执行.Size = New System.Drawing.Size(64, 24)
Me.Bt执行.TabIndex = 0
Me.Bt执行.Text = "执行(&E)"
'
'Bt清空
'
Me.Bt清空.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.Bt清空.Location = New System.Drawing.Point(32, 16)
Me.Bt清空.Name = "Bt清空"
Me.Bt清空.Size = New System.Drawing.Size(72, 24)
Me.Bt清空.TabIndex = 0
Me.Bt清空.Text = "清空"
'
'TextBox1
'
Me.TextBox1.BackColor = System.Drawing.Color.Black
Me.TextBox1.ForeColor = System.Drawing.Color.White
Me.TextBox1.Location = New System.Drawing.Point(16, 16)
Me.TextBox1.Multiline = True
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.ReadOnly = True
Me.TextBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
Me.TextBox1.Size = New System.Drawing.Size(552, 320)
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = ""
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(576, 406)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Panel1)
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "实现CMD功能"
Me.Panel1.ResumeLayout(False)
Me.ResumeLayout(False)