程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> VC >> vc教程 >> vc模擬鼠標鍵盤操作實用類

vc模擬鼠標鍵盤操作實用類

編輯:vc教程

//****************** 類mk頭文件mk.h **************************
#if !defined(AFX_MK_H__B024D48F_090A_4F6F_A199_32996DF699B3__INCLUDED_)
#define AFX_MK_H__B024D48F_090A_4F6F_A199_32996DF699B3__INCLUDED_
 

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "winable.h"

class mk
{
public:
mk();

static void Key(BYTE k, int mSeconds=0);//按一個鍵,mSeconds表按鍵後到
                                                                         //下一次操作延遲的時間:ms
static void Key2(BYTE k1, BYTE k2,int mSeconds=0);//按2個鍵
static void Key3(BYTE k1,BYTE k2,BYTE k3,int mSeconds=0);//按3個鍵

static void Click(int x,int y,int mSeconds=0);//單擊
static void RClick(int x,int y,int mSeconds=0);//右擊
static void DClick(int x,int y,int mSeconds=0);//雙擊
static void CtrlClick(int x, int y, int mSeconds=0);//Ctrl+單擊
                static void Drag(int x1,int y1,int x2,int y2,int mSeconds=0);//鼠標拖動

virtual ~mk();

};

#endif // !defined(AFX_MK_H__B024D48F_090A_4F6F_A199_32996DF699B3__INCLUDED_)
//****************** 類mk頭文件mk.h end**************************
//****************** 類mk實現文件mk.cpp **************************
#include "stdafx.h"
#include "mk.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

mk::mk()
{

}

mk::~mk()
{

}
void mk::Click(int x, int y, int mSeconds)
{
SetCursorPos(x,y);
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
Sleep(mSeconds);
}
void mk::DClick(int x, int y, int mSeconds)
{
SetCursorPos(x,y);
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
Sleep(mSeconds);
}

void mk::Drag(int x1, int y1, int x2, int y2, int mSeconds)
{
SetCursorPos(x1,y1);
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
SetCursorPos(x2,y2);
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
Sleep(mSeconds);
}

void mk::RClick(int x, int y, int mSeconds)
{
SetCursorPos(x,y);
mouse_event(MOUSEEVENTF_RIGHTDOWN,0,0,0,0);
mouse_event(MOUSEEVENTF_RIGHTUP,0,0,0,0);
Sleep(mSeconds);
}
void mk::Key(BYTE k, int mSeconds)
{
keybd_event(k,0,0,0);
keybd_event(k,0,KEYEVENTF_KEYUP,0);
Sleep(mSeconds);
}
void mk::Key2(BYTE k1, BYTE k2, int mSeconds)
{
//法1
//keybd_event(k1, 0, 0 ,0);
    //keybd_event(k2, 0, 0 ,0);
    //keybd_event(k2, 0, KEYEVENTF_KEYUP,0);
    //keybd_event(k1, 0, KEYEVENTF_KEYUP,0);
//法2,更通用,但須先#include "winable.h"
INPUT input[4];
    memset(input,0,sizeof(input));
    input[0].type=input[1].type=input[2].type=input[3].type=INPUT_KEYBOARD;
    input[0].ki.wVk=input[3].ki.wVk=k1;
    input[1].ki.wVk=input[2].ki.wVk=k2;
    input[2].ki.dwFlags = input[3].ki.dwFlags = KEYEVENTF_KEYUP;

    SendInput(4, input, sizeof(INPUT));

Sleep(mSeconds);
}

void mk::CtrlClick(int x, int y, int mSeconds)
{
SetCursorPos(x,y);
keybd_event(VK_CONTROL ,0,0,0);
mouse_event(MOUSEEVENTF_LE

[1] [2] [3] 下一頁

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved