1 利用Spy++获取窗口和某一控件的Class Name, Title Name;
2 利用Class Name, Title Name获取handle然后发送消息;
代码如下:
2 利用Class Name, Title Name获取handle然后发送消息;
代码如下:
void CFindWindowTestDlg::OnBnClickedFindSkypeBtn()
{
// TODO: Add your control notification handler code here
HWND m_pHwnd = ::FindWindow(_T("TConversationForm"), _T("Jacky"));
ASSERT(m_pHwnd != NULL);
HWND m_pHwndTChatEntryControl = ::FindWindowEx(m_pHwnd, NULL, _T("TChatEntryControl"), NULL);
ASSERT(m_pHwndTChatEntryControl != NULL);
HWND m_pHwndTTChatRichEdit = ::FindWindowEx(m_pHwndTChatEntryControl, NULL, _T("TChatRichEdit"), NULL);
ASSERT(m_pHwndTTChatRichEdit != NULL);
if (m_pHwndTTChatRichEdit != NULL)
{
for (int i =0; i<20; i++)
{
::PostMessage(m_pHwndTTChatRichEdit, WM_CHAR, 'T', NULL);
::PostMessage(m_pHwndTTChatRichEdit, WM_CHAR, 'e', NULL);
::PostMessage(m_pHwndTTChatRichEdit, WM_CHAR, 's', NULL);
::PostMessage(m_pHwndTTChatRichEdit, WM_CHAR, 't', NULL);
::PostMessage(m_pHwndTTChatRichEdit, WM_CHAR, ' ', NULL);
::PostMessage(m_pHwndTTChatRichEdit, WM_CHAR, 'M', NULL);
::PostMessage(m_pHwndTTChatRichEdit, WM_CHAR, 'e', NULL);
::PostMessage(m_pHwndTTChatRichEdit, WM_CHAR, 's', NULL);
::PostMessage(m_pHwndTTChatRichEdit, WM_CHAR, 's', NULL);
::PostMessage(m_pHwndTTChatRichEdit, WM_CHAR, 'a', NULL);
::PostMessage(m_pHwndTTChatRichEdit, WM_CHAR, 'g', NULL);
::PostMessage(m_pHwndTTChatRichEdit, WM_CHAR, 'e', NULL);
::PostMessage(m_pHwndTTChatRichEdit, WM_CHAR, ' ', NULL);
::PostMessage(m_pHwndTTChatRichEdit, WM_KEYDOWN, VK_RETURN, NULL);
::PostMessage(m_pHwndTTChatRichEdit, WM_KEYUP, VK_RETURN, NULL);
Sleep(200);
}
}
if(m_pHwnd)
{
//::PostMessage(m_pHwnd, WM_CLOSE, 0, 0);//Exit
}
}
BOOL CLeakDiagAutoLogDlg::DoAutoLogging()
{
CString strData;
m_pMsgEdit->GetWindowText(strData);
HWND m_pHwnd = ::FindWindow(NULL, _T("LeakDiag"));
if (m_pHwnd != NULL)
{
HWND m_pHwnLogBtnCtrl = ::FindWindowEx(m_pHwnd, NULL, _T("Button"), _T("Log"));
if (m_pHwnLogBtnCtrl != NULL)
{
long id = ::GetDlgCtrlID(m_pHwnLogBtnCtrl);
::PostMessage(m_pHwnd, WM_COMMAND, MAKELONG(id, BN_CLICKED), NULL);
}
else
{
strData += _T("m_pHwnLogBtnCtrl is NULL\r\n");
m_pMsgEdit->SetWindowText(strData);
return FALSE;
}
}
else
{
strData += _T("m_pHwnd is NULL!\r\n");
m_pMsgEdit->SetWindowText(strData);
return FALSE;
}
return TRUE;
// HWND m_pHwnStartControl = ::FindWindowEx(m_pHwnd, NULL, _T("Button"), _T("Start"));
// ASSERT(m_pHwnStartControl != NULL);
// long id = ::GetDlgCtrlID(m_pHwnStartControl);
// ::PostMessage(m_pHwnd, WM_COMMAND, MAKELONG(id,BN_CLICKED), NULL);
}
void CLeakDiagAutoLogDlg::OnBnClickedRunBtn()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
if (0 == m_dwTimeSpan)
{
m_dwTimeSpan =1;
}
CEdit* pEdit = static_cast<CEdit*>(GetDlgItem(IDC_RUN_BTN));
if (!m_bTimer)
{
SetTimer(1, UINT(m_dwTimeSpan*1000), NULL);
m_bTimer = TRUE;
pEdit->SetWindowText(_T("Stop"));
}
else
{
KillTimer(1);
m_bTimer = FALSE;
pEdit->SetWindowText(_T("Start"));
}
UpdateData(FALSE);
}
void CLeakDiagAutoLogDlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CTime cTime = CTime::GetCurrentTime();
CString strTime = cTime.Format("[%Y-%m-%d %H:%M:%S] ");//display YYYY:MM:DD HH:MM:SS
switch (nIDEvent)
{
case 1:
{
if (DoAutoLogging())
{
CString strData;
CString strTemp;
strTemp.Format("[RUN = %ld] DoAutoLogging", ++m_dwTotalRun);
m_pMsgEdit->GetWindowText(strData);
strData += strTime;
strData += strTemp;
strData += "\r\n";
m_pMsgEdit->SetWindowText(strData);
}
break;
}
case 2:
default:
{
break;
}
}
CDialog::OnTimer(nIDEvent);
}