Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long Private Const WM_CLOSE = &H10 Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Private Declare Function SetWindowPlacement Lib "user32" (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long Private Const SW_NORMAL = 1 Private Type POINTAPI x As Long y As Long End Type Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Private Type WINDOWPLACEMENT Length As Long flags As Long showCmd As Long ptMinPosition As POINTAPI ptMaxPosition As POINTAPI rcNormalPosition As RECT End TypePrivate Sub Command2_Click() Dim myHwnd As Long myHwnd = FindWindowEx(Me.hwnd, 0, "ConsoleWindowClass", "C:\WINDOWS\system32\telnet.exe") SendMessage myHwnd, WM_CLOSE, 0, 0 End SubPrivate Sub Command1_Click() Shell "C:\WINDOWS\system32\telnet.exe", vbHide '打开telnet myHwnd = FindWindow("ConsoleWindowClass", "C:\WINDOWS\system32\telnet.exe") '得到telnet的句柄 Dim cc As RECT GetWindowRect myHwnd, cc '得到telnet外矩形 SetParent myHwnd, Me.hwnd '设置telnet为本窗体的子对象 Dim bb As POINTAPI bb.x = 100 bb.y = 100 Dim aa As WINDOWPLACEMENT aa.Length = Len(aa) aa.showCmd = SW_NORMAL aa.ptMinPosition = bb aa.ptMaxPosition = bb aa.rcNormalPosition.Left = 100 'telnet的左边界位置为100 aa.rcNormalPosition.Top = 100 'telnet的上边界位置为100 aa.rcNormalPosition.Right = cc.Right - cc.Left + aa.rcNormalPosition.Left aa.rcNormalPosition.Bottom = cc.Bottom - cc.Top + aa.rcNormalPosition.Top SetWindowPlacement myHwnd, aa '设置telnet的位置 End SubPrivate Sub Form_Load() Command1_Click End SubPrivate Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) Command2_Click End Sub
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Const WM_CLOSE = &H10
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function SetWindowPlacement Lib "user32" (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long
Private Const SW_NORMAL = 1
Private Type POINTAPI
x As Long
y As Long
End Type
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type WINDOWPLACEMENT
Length As Long
flags As Long
showCmd As Long
ptMinPosition As POINTAPI
ptMaxPosition As POINTAPI
rcNormalPosition As RECT
End TypePrivate Sub Command2_Click()
Dim myHwnd As Long
myHwnd = FindWindowEx(Me.hwnd, 0, "ConsoleWindowClass", "C:\WINDOWS\system32\telnet.exe")
SendMessage myHwnd, WM_CLOSE, 0, 0
End SubPrivate Sub Command1_Click()
Shell "C:\WINDOWS\system32\telnet.exe", vbHide '打开telnet
myHwnd = FindWindow("ConsoleWindowClass", "C:\WINDOWS\system32\telnet.exe") '得到telnet的句柄
Dim cc As RECT
GetWindowRect myHwnd, cc '得到telnet外矩形
SetParent myHwnd, Me.hwnd '设置telnet为本窗体的子对象
Dim bb As POINTAPI
bb.x = 100
bb.y = 100
Dim aa As WINDOWPLACEMENT
aa.Length = Len(aa)
aa.showCmd = SW_NORMAL
aa.ptMinPosition = bb
aa.ptMaxPosition = bb
aa.rcNormalPosition.Left = 100 'telnet的左边界位置为100
aa.rcNormalPosition.Top = 100 'telnet的上边界位置为100
aa.rcNormalPosition.Right = cc.Right - cc.Left + aa.rcNormalPosition.Left
aa.rcNormalPosition.Bottom = cc.Bottom - cc.Top + aa.rcNormalPosition.Top
SetWindowPlacement myHwnd, aa '设置telnet的位置
End SubPrivate Sub Form_Load()
Command1_Click
End SubPrivate Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Command2_Click
End Sub
如果我单击command2 ,在相同的telnet窗体中执行命令ping 192.168.1.2 然后将结果保存至temp.txt请问可以实现这样的功能吗