没那么复杂,我就搞过 mfc 嵌入 putty.exe ,代码如下:
HANDLE hProcess = CreateProcess(...); // 用 cmd.exe /K python.exe 启动
if (NULL == hProcess)
{
CMessageBox::Show(this, _T("FAILED TO START PUTTY"),
CTextTraits::EMPTY_STRING, IDOK, MB_OK);
return FALSE;
}
if (0 == ::WaitForInputIdle(hProcess, 1000))
{
m_wndTelnet = GetProcessMainWnd((DWORD)iProcessID);
}
CWnd *pWndPos = GetDlgItem(IDC_STATIC_POSITION); //嵌入的位置标识 ID
if (NULL == pWndPos)
{
return FALSE;
}
CRect rcPos;
pWndPos->GetWindowRect(&rcPos);
ScreenToClient(&rcPos);
rcPos.DeflateRect(1, 12, 1, 1);
::SetParent(m_wndTelnet, m_hWnd); // 窗口句柄 m_wndTelnet.
::SetWindowPos(m_wndTelnet, NULL, rcPos.left,
rcPos.top , rcPos.Width(), rcPos.Height(), SWP_SHOWWINDOW);
LONG lRet = ::SetWindowLong(m_wndTelnet, GWL_STYLE, 0x156B0000); // 这个魔术数字我也忘记啥意思了。
直接就能在 cmd 里面输入参数了。。。。
我还在外面搞了参数表格,拦截按钮事件把参数组好了往 m_wndTelnet 塞,
::SetForegroundWindow(hwndTelnet);
for (int i = 0; i < strMsg.GetLength(); ++i)
{
TCHAR chKey = strMsg.GetAt(i);
SHORT shTmp = VkKeyScan(chKey);
BYTE byVKey = shTmp & 0xFF; // key
bool bShift = (shTmp & 0x0100) == 0x0100 ? true : false;
if (bShift)
{
keybd_event(VK_SHIFT, MapVirtualKey(VK_SHIFT, MAPVK_VK_TO_VSC), 0, 0);
}
keybd_event(byVKey, MapVirtualKey(byVKey, MAPVK_VK_TO_VSC), 0, 0);
keybd_event(byVKey, MapVirtualKey(byVKey, MAPVK_VK_TO_VSC), KEYEVENTF_KEYUP, 0);
if (bShift)
{
keybd_event(VK_SHIFT, MapVirtualKey(VK_SHIFT, MAPVK_VK_TO_VSC), KEYEVENTF_KEYUP, 0);
}
}
Sleep(100);
keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN, MAPVK_VK_TO_VSC), 0, 0);
keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN, MAPVK_VK_TO_VSC), KEYEVENTF_KEYUP, 0);