SendInput -- 鼠标移动
// SendInputTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <Windows.h>
#include <atlstr.h>
#include <atltypes.h>
#include <iostream>
using namespace std;
class WndInof
{
public:
WndInof():m_hWnd(NULL),m_strTitle(_T("")),m_strClassName(_T("")){}
HWND m_hWnd;
CString m_strTitle;
CString m_strClassName;
CRect m_scrRect;
};
BOOL CALLBACK EnumWindowsProc(
_In_ HWND hwnd,
_In_ LPARAM lParam
)
{
if(IsWindowVisible(hwnd))
{
TCHAR szTitle[100] = {0};
TCHAR szClassName[100] = {0};
GetWindowText(hwnd,szTitle,sizeof(szTitle)/sizeof(TCHAR));
GetClassName(hwnd,szClassName,sizeof(szClassName)/sizeof(TCHAR));
if(0 == _tcscmp(_T("new 2.txt - 记事本"),szTitle) &&
0 == _tcscmp(_T("Notepad"),szClassName))
{
WndInof* pWndInfo = reinterpret_cast<WndInof*>(lParam);
pWndInfo->m_hWnd = hwnd;
pWndInfo->m_strTitle = szTitle;
pWndInfo->m_strClassName = szClassName;
return FALSE;
}
}
return TRUE;
}
int _tmain(int argc, _TCHAR* argv[])
{
wcout.imbue(locale("CHS"));
WndInof wNotePad;
EnumWindows(EnumWindowsProc,reinterpret_cast<LPARAM>(&wNotePad));
if (NULL == wNotePad.m_hWnd)
{
return 0;
}
wcout<<wNotePad.m_strClassName.GetString()<<endl;
wcout<<wNotePad.m_strTitle.GetString()<<endl;
SetForegroundWindow(wNotePad.m_hWnd);
::GetWindowRect(wNotePad.m_hWnd,&wNotePad.m_scrRect);
wcout<<"top: "<<wNotePad.m_scrRect.top<<", left: "<<wNotePad.m_scrRect.left
<<", bottom: "<<wNotePad.m_scrRect.bottom<<", right"<<wNotePad.m_scrRect.right<<endl;
//method 1
//::SetCursorPos(wNotePad.m_scrRect.TopLeft().x+20,wNotePad.m_scrRect.TopLeft().y+35);
//method 2
int cx_screen = ::GetSystemMetrics(SM_CXSCREEN); //屏幕 宽
int cy_screen = ::GetSystemMetrics(SM_CYSCREEN); // 高
INPUT input_stream[3] = {0};
input_stream[0].type = INPUT_MOUSE;
input_stream[0].mi.dx = (wNotePad.m_scrRect.TopLeft().x+20) * 65535 / cx_screen;
input_stream[0].mi.dy = (wNotePad.m_scrRect.TopLeft().y+35) * 65535 / cy_screen;
input_stream[0].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
SendInput(1,&input_stream[0],sizeof(INPUT));
return 0;
}