auto hmain = ::FindWindow(NULL, L"MFCApplication2");
if (hmain)
{
//获取窗口句柄
CRect rect;
::GetWindowRect(hmain, rect);
CString str;
CPoint user_pt;
//将user_pt点定位到用户名框内
user_pt.x = rect.left + 109+10;
user_pt.y = rect.top + 148+10;
//通过位置获取窗口句柄
auto m_hUserWnd = ::WindowFromPoint(user_pt);
//通过按钮的名字获取按钮句柄
HWND hand_button = ::FindWindowExW(hmain, NULL, TEXT("Button"), TEXT("确定"));//得到按钮的句柄
str = L"用户名:";
wchar_t buf[100];
//获取用户名
//设置Edit文本
::SendMessage(m_hUserWnd, WM_SETTEXT, 0, (LPARAM)L"ABC");
//获取Edit文本
::SendMessage(m_hUserWnd, WM_GETTEXT, (WPARAM)999, (LPARAM)buf);
//将剪贴板的内容粘贴到文本框
::SendMessage(m_hUserWnd, WM_PASTE, 0, 0);
if (hand_button)
{
::PostMessageW(hand_button, WM_LBUTTONDOWN, 0, 0);
::PostMessageW(hand_button, WM_LBUTTONUP, 0, 0);
}
str = buf;
str+=L"\r\n";
::AfxMessageBox(str);
}
vs2017 测试程序
https://download.youkuaiyun.com/download/u010261063/20042048