SendInput -- 鼠标移动

6 篇文章 0 订阅

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;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值