简易Dota改键外挂程序制作

2019/7/10 23:19:33

本文主要是介绍简易Dota改键外挂程序制作,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!



dll部分:

复制代码 代码如下:

// FileName: add.cpp

#include <Windows.h>

/* 定义全局变量 */
HWND g_hwnd = NULL;
HHOOK g_hKeyboard = NULL;

// 设置数据段
#pragma data_seg("MySec")
static WORD g_keyNum[6]={0};        // 只有初始化过的才能用
static int g_signed = 0;
#pragma data_seg() 
#pragma comment( linker,"/section:MySec,RWS" )

LRESULT CALLBACK KeyboardProc(
    int code,       // hook code
    WPARAM wParam,  // virtual-key code
    LPARAM lParam   // keystroke-message information
    )
{
    for(int j=0; j<6; j++)
    {
        if(wParam==g_keyNum[j] && !(lParam>>31) )
        {
            switch(j)
            {
            case 0: keybd_event(VK_NUMPAD1,0,KEYEVENTF_EXTENDEDKEY,0); keybd_event(VK_NUMPAD1,0,KEYEVENTF_KEYUP,0); return 1;
            case 1: keybd_event(VK_NUMPAD2,0,KEYEVENTF_EXTENDEDKEY,0); keybd_event(VK_NUMPAD2,0,KEYEVENTF_KEYUP,0); return 1;
            case 2: keybd_event(VK_NUMPAD4,0,KEYEVENTF_EXTENDEDKEY,0); keybd_event(VK_NUMPAD4,0,KEYEVENTF_KEYUP,0); return 1;
            case 3: keybd_event(VK_NUMPAD5,0,KEYEVENTF_EXTENDEDKEY,0); keybd_event(VK_NUMPAD5,0,KEYEVENTF_KEYUP,0); return 1;
            case 4: keybd_event(VK_NUMPAD7,0,KEYEVENTF_EXTENDEDKEY,0); keybd_event(VK_NUMPAD7,0,KEYEVENTF_KEYUP,0); return 1;
            case 5: keybd_event(VK_NUMPAD8,0,KEYEVENTF_EXTENDEDKEY,0); keybd_event(VK_NUMPAD8,0,KEYEVENTF_KEYUP,0); return 1;
            }
        }
    }
    return CallNextHookEx(g_hKeyboard,code,wParam,lParam);
}
_declspec(dllexport) void SetHook (WORD KeyNum[])
{
    for(int i=0; i<6; i++)
    {
        g_keyNum[i] = KeyNum[i];
    }
    g_hKeyboard=SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,GetModuleHandle(L"DLL.dll"),0);
}

_declspec(dllexport) void DelHook ()
{
    UnhookWindowsHookEx(g_hKeyboard);
    g_hKeyboard = NULL;
}

主程序部分:

复制代码 代码如下:

// DotaDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "Dota.h"
#include "DotaDlg.h"
#include "afxdialogex.h"

#pragma comment(lib, "DLL.lib")

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

/* 全局变量 */
CDotaDlg * g_pt = NULL;

HHOOK g_hook;

/* 记录输入框是否有焦点 */
bool g_signNum[6] = {false};
/* 记录按下键的编码 */
WORD g_nkey[6] = {0};
/* 按键表 */
CString g_szkeyboard[200];


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
    CAboutDlg();

// 对话框数据
    enum { IDD = IDD_ABOUTBOX };

    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
    DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CDotaDlg 对话框

 


CDotaDlg::CDotaDlg(CWnd* pParent /*=NULL*/)
    : CDialogEx(CDotaDlg::IDD, pParent)
    , m_num7(_T(""))
    , m_num8(_T(""))
    , m_num4(_T(""))
    , m_num1(_T(""))
    , m_num2(_T(""))
    , m_num5(_T(""))
{
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDotaDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Text(pDX, IDC_EDIT_NUM7, m_num7);
    DDV_MaxChars(pDX, m_num7, 1);
    DDX_Text(pDX, IDC_EDIT_NUM8, m_num8);
    DDV_MaxChars(pDX, m_num8, 1);
    DDX_Text(pDX, IDC_EDIT_NUM4, m_num4);
    DDV_MaxChars(pDX, m_num4, 1);
    DDX_Text(pDX, IDC_EDIT_NUM1, m_num1);
    DDV_MaxChars(pDX, m_num1, 1);
    DDX_Text(pDX, IDC_EDIT_NUM2, m_num2);
    DDV_MaxChars(pDX, m_num2, 1);
    DDX_Text(pDX, IDC_EDIT_NUM5, m_num5);
    DDV_MaxChars(pDX, m_num5, 1);
}

BEGIN_MESSAGE_MAP(CDotaDlg, CDialogEx)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_BUTTON_EXIT, &CDotaDlg::OnBnClickedButtonExit)
    ON_EN_SETFOCUS(IDC_EDIT_NUM7, &CDotaDlg::OnEnSetfocusEditNum7)
    ON_WM_DESTROY()
    ON_EN_KILLFOCUS(IDC_EDIT_NUM7, &CDotaDlg::OnEnKillfocusEditNum7)
    ON_EN_SETFOCUS(IDC_EDIT_NUM8, &CDotaDlg::OnEnSetfocusEditNum8)
    ON_EN_KILLFOCUS(IDC_EDIT_NUM8, &CDotaDlg::OnEnKillfocusEditNum8)
    ON_EN_SETFOCUS(IDC_EDIT_NUM4, &CDotaDlg::OnEnSetfocusEditNum4)
    ON_EN_KILLFOCUS(IDC_EDIT_NUM4, &CDotaDlg::OnEnKillfocusEditNum4)
    ON_EN_SETFOCUS(IDC_EDIT_NUM5, &CDotaDlg::OnEnSetfocusEditNum5)
    ON_EN_KILLFOCUS(IDC_EDIT_NUM5, &CDotaDlg::OnEnKillfocusEditNum5)
    ON_EN_SETFOCUS(IDC_EDIT_NUM1, &CDotaDlg::OnEnSetfocusEditNum1)
    ON_EN_KILLFOCUS(IDC_EDIT_NUM1, &CDotaDlg::OnEnKillfocusEditNum1)
    ON_EN_SETFOCUS(IDC_EDIT_NUM2, &CDotaDlg::OnEnSetfocusEditNum2)
    ON_EN_KILLFOCUS(IDC_EDIT_NUM2, &CDotaDlg::OnEnKillfocusEditNum2)
    ON_WM_TIMER()
    ON_BN_CLICKED(IDC_BUTTON_HIDE, &CDotaDlg::OnBnClickedButtonHide)
    ON_MESSAGE(WM_SHOWTASK,OnShowTask)
END_MESSAGE_MAP()

/* 局部钩子处理程序 */
LRESULT CALLBACK KeyboardProc(
    int code,       // hook code
    WPARAM wParam,  // virtual-key code
    LPARAM lParam   // keystroke-message information
    )
{
    /* 按一个键时 */
    if(wParam && !(lParam>>31))
    {
        //CString str;
        //str.Format(L"%d", wParam);
        //AfxMessageBox(str);
        POINT point;
        GetCaretPos(&point);

        for(int i=0; i<6; i++)
        {
            if(g_signNum[i])        // 且某个输入框有输入焦点
            {
                if(wParam != VK_BACK)    //且按下的 不是 退格键
                {
                    switch(i)
                    {
                        case 0: g_pt->m_num1 = g_szkeyboard[wParam]; g_nkey[0]=wParam; g_pt->UpdateData(FALSE); break;
                        case 1: g_pt->m_num2 = g_szkeyboard[wParam]; g_nkey[1]=wParam; g_pt->UpdateData(FALSE); break;
                        case 2: g_pt->m_num4 = g_szkeyboard[wParam]; g_nkey[2]=wParam; g_pt->UpdateData(FALSE); break;
                        case 3: g_pt->m_num5 = g_szkeyboard[wParam]; g_nkey[3]=wParam; g_pt->UpdateData(FALSE); break;
                        case 4: g_pt->m_num7 = g_szkeyboard[wParam]; g_nkey[4]=wParam; g_pt->UpdateData(FALSE); break;
                        case 5: g_pt->m_num8 = g_szkeyboard[wParam]; g_nkey[5]=wParam; g_pt->UpdateData(FALSE); break;
                    }
                }
                else        // 按下退格键删除以前的改键
                {
                    switch(i)
                    {
                        case 0: g_pt->m_num1 = ""; g_nkey[0]=0; g_pt->UpdateData(FALSE); break;
                        case 1: g_pt->m_num2 = ""; g_nkey[1]=0; g_pt->UpdateData(FALSE); break;
                        case 2: g_pt->m_num4 = ""; g_nkey[2]=0; g_pt->UpdateData(FALSE); break;
                        case 3: g_pt->m_num5 = ""; g_nkey[3]=0; g_pt->UpdateData(FALSE); break;
                        case 4: g_pt->m_num7 = ""; g_nkey[4]=0; g_pt->UpdateData(FALSE); break;
                        case 5: g_pt->m_num8 = ""; g_nkey[5]=0; g_pt->UpdateData(FALSE); break;
                    }
                }
            }
        }
    }
    //return CallNextHookEx(g_hook, code, wParam, lParam);
    return 1;
}

// CDotaDlg 消息处理程序

BOOL CDotaDlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();

    // 将“关于...”菜单项添加到系统菜单中。

    // IDM_ABOUTBOX 必须在系统命令范围内。
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000);

    CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
        BOOL bNameValid;
        CString strAboutMenu;
        bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
        ASSERT(bNameValid);
        if (!strAboutMenu.IsEmpty())
        {
            pSysMenu->AppendMenu(MF_SEPARATOR);
            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
        }
    }

    // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
    //  执行此操作
    SetIcon(m_hIcon, TRUE);            // 设置大图标
    SetIcon(m_hIcon, FALSE);        // 设置小图标

    // TODO: 在此添加额外的初始化代码
    SetTimer(1, 1000, NULL);        // 设置一个定时器
    g_pt = this;
    g_szkeyboard['0']="0";g_szkeyboard['1']="1";g_szkeyboard['2']="2";g_szkeyboard['3']="3";g_szkeyboard['4']="4";
    g_szkeyboard['5']="5";g_szkeyboard['6']="6";g_szkeyboard['7']="7";g_szkeyboard['8']="8";g_szkeyboard['9']="9";
    g_szkeyboard['A']="A";g_szkeyboard['B']="B";g_szkeyboard['C']="C";g_szkeyboard['D']="D";g_szkeyboard['E']="E";
    g_szkeyboard['F']="F";g_szkeyboard['G']="G";g_szkeyboard['H']="H";g_szkeyboard['I']="I";g_szkeyboard['J']="J";
    g_szkeyboard['K']="K";g_szkeyboard['L']="L";g_szkeyboard['M']="M";g_szkeyboard['N']="N";g_szkeyboard['O']="O";
    g_szkeyboard['P']="P";g_szkeyboard['Q']="Q";g_szkeyboard['R']="R";g_szkeyboard['S']="S";g_szkeyboard['T']="T";
    g_szkeyboard['U']="U";g_szkeyboard['V']="V";g_szkeyboard['W']="W";g_szkeyboard['X']="X";g_szkeyboard['Y']="Y";
    g_szkeyboard['Z']="Z";
    g_szkeyboard[144]="NUM LOCK";
    g_szkeyboard[96]="NUM 0";g_szkeyboard[97]="NUM 1";g_szkeyboard[98]="NUM 2";g_szkeyboard[99]="NUM 3";g_szkeyboard[100]="NUM 4";
    g_szkeyboard[101]="NUM 5";g_szkeyboard[102]="NUM 6";g_szkeyboard[103]="NUM 7";g_szkeyboard[104]="NUM 8";g_szkeyboard[105]="NUM 9";
    g_szkeyboard[106]="NUM *";g_szkeyboard[107]="NUM +";g_szkeyboard[109]="NUM -";g_szkeyboard[110]="NUM DEL";g_szkeyboard[111]="NUM /";
    g_szkeyboard[VK_F1]="F1";g_szkeyboard[VK_F2]="F2";g_szkeyboard[VK_F3]="F3";g_szkeyboard[VK_F4]="F4";g_szkeyboard[VK_F5]="F5";
    g_szkeyboard[VK_F6]="F6";g_szkeyboard[VK_F7]="F7";g_szkeyboard[VK_F8]="F8";g_szkeyboard[VK_F9]="F9";g_szkeyboard[VK_F10]="F10";
    g_szkeyboard[VK_F11]="F11";g_szkeyboard[VK_F12]="F12";
    g_szkeyboard[VK_ESCAPE]="ESC";g_szkeyboard[VK_TAB]="TAB";g_szkeyboard[VK_CAPITAL]="Caps Lock";g_szkeyboard[192]="`";
    g_szkeyboard[VK_SPACE]="Space";

    /* 安装局部钩子 */
    g_hook = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, NULL, GetCurrentThreadId());

    ReadKeyNum();

    return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

void CDotaDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    {
        CAboutDlg dlgAbout;
        dlgAbout.DoModal();
    }
    else
    {
        CDialogEx::OnSysCommand(nID, lParam);
    }
}

// 如果向对话框添加最小化按钮,则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
//  这将由框架自动完成。

void CDotaDlg::OnPaint()
{
    if (IsIconic())
    {
        CPaintDC dc(this); // 用于绘制的设备上下文

        SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

        // 使图标在工作区矩形中居中
        int cxIcon = GetSystemMetrics(SM_CXICON);
        int cyIcon = GetSystemMetrics(SM_CYICON);
        CRect rect;
        GetClientRect(&rect);
        int x = (rect.Width() - cxIcon + 1) / 2;
        int y = (rect.Height() - cyIcon + 1) / 2;

        // 绘制图标
        dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
        CDialogEx::OnPaint();
    }
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CDotaDlg::OnQueryDragIcon()
{
    return static_cast<HCURSOR>(m_hIcon);
}

BOOL CDotaDlg::PreTranslateMessage (MSG * pMsg)
{

    switch(pMsg->wParam)
    {
        case VK_ESCAPE:            // 屏蔽ESC键
            return TRUE;
    }
    return CDialog::PreTranslateMessage(pMsg);
}

 

void CDotaDlg::OnBnClickedButtonExit()
{
    //OnNotifyMsg();
    SendMessage(WM_CLOSE);
}

void CDotaDlg::OnDestroy()
{
    CDialogEx::OnDestroy();

    g_pt = NULL;
    KillTimer(1);
    /* 卸载钩子 */
    UnhookWindowsHookEx(g_hook);
}

void CDotaDlg::OnEnSetfocusEditNum7()
{
    g_signNum[4] = true;
}


void CDotaDlg::OnEnKillfocusEditNum7()
{
    g_signNum[4] = false;
}


void CDotaDlg::OnEnSetfocusEditNum8()
{
    g_signNum[5] = true;
}


void CDotaDlg::OnEnKillfocusEditNum8()
{
    g_signNum[5] = false;
}


void CDotaDlg::OnEnSetfocusEditNum4()
{
    g_signNum[2] = true;
}


void CDotaDlg::OnEnKillfocusEditNum4()
{
    g_signNum[2] = false;
}


void CDotaDlg::OnEnSetfocusEditNum5()
{
    g_signNum[3] = true;
}


void CDotaDlg::OnEnKillfocusEditNum5()
{
    g_signNum[3] = false;
}


void CDotaDlg::OnEnSetfocusEditNum1()
{
    g_signNum[0] = true;
}


void CDotaDlg::OnEnKillfocusEditNum1()
{
    g_signNum[0] = false;
}


void CDotaDlg::OnEnSetfocusEditNum2()
{
    g_signNum[1] = true;
}


void CDotaDlg::OnEnKillfocusEditNum2()
{
    g_signNum[1] = false;
}

_declspec(dllimport) void SetHook (WORD KeyNum[]);
_declspec(dllimport) void DelHook ();

void CDotaDlg::OnTimer(UINT_PTR nIDEvent)
{
    static int sign = 1;
    HWND hwnd =    NULL;
    hwnd = ::FindWindow(L"Warcraft III",0);            // 获得顶层窗口句柄
    if(hwnd == ::GetForegroundWindow())                // 得到前台窗口句柄
    {
        if(sign)
        {
        //MessageBox(L"找到目标进程了");
        //DelHook(); 
            SetHook(g_nkey);    // 安装钩子。
            sign = 0;
        }
    }
    else
    {
        DelHook();
        sign = 1;
    }

    CDialogEx::OnTimer(nIDEvent);
}

_declspec(dllimport) void SetHook (WORD KeyNum[]);
void CDotaDlg::OnBnClickedButtonHide()
{
    //SetHook(g_nkey);
    CStdioFile file;
    CString str;
    CFileException mExcept;
    file.Open(L"Key.ini", CFile::modeCreate|CFile::modeReadWrite, &mExcept);
    file.SeekToEnd();
    str.Format(L"@%d-->1@\n@%d-->2@\n@%d-->4@\n@%d-->5@\n@%d-->7@\n@%d-->8@\n",
        g_nkey[0], g_nkey[1], g_nkey[2], g_nkey[3], g_nkey[4], g_nkey[5]);
    file.WriteString(str);
    file.Close();

    NOTIFYICONDATA nid;   

    nid.cbSize=(DWORD)sizeof(NOTIFYICONDATA);  

    nid.hWnd=this->m_hWnd;   

    nid.uID=IDR_MAINFRAME;   

    nid.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP ;   

    nid.uCallbackMessage=WM_SHOWTASK;//自定义的消息名称   

    nid.hIcon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));

    wcscpy(nid.szTip, L"冬雪简易改键");    //信息提示条  

    Shell_NotifyIcon(NIM_ADD,&nid);    //在托盘区添加图标   

    ShowWindow(SW_HIDE);    //隐藏主窗口
}

void CDotaDlg::ReadKeyNum(void)
{
    CStdioFile file;
    if(file.Open(L"Key.ini", CFile::modeRead))
    {
        CString buffer;
        char buf[4];
        int i;
        int j = 0;
        memset(buf,0,4);
        while(file.ReadString(buffer))
        {
            i = 0;
            for(i=0; i<4; i++)
            {
                buf[i]=char(buffer.GetAt(1+i));
                if(buf[i] == '-')
                {
                    buf[i] = '\0';
                    break;
                }
            }
            g_nkey[j] = (WORD)atol(buf);
            if(g_nkey[j])
            {
                switch(j)
                {
                case 0 : m_num1=g_szkeyboard[g_nkey[0]]; break;
                case 1 : m_num2=g_szkeyboard[g_nkey[1]]; break;
                case 2 : m_num4=g_szkeyboard[g_nkey[2]]; break;
                case 3 : m_num5=g_szkeyboard[g_nkey[3]]; break;
                case 4 : m_num7=g_szkeyboard[g_nkey[4]]; break;
                case 5 : m_num8=g_szkeyboard[g_nkey[5]]; break;
                }
            }
            j++;
        }
        UpdateData(FALSE);
        file.Close();
    }
}

void CDotaDlg::ToTray()
{  

    NOTIFYICONDATA nid;   

    nid.cbSize=(DWORD)sizeof(NOTIFYICONDATA);  

    nid.hWnd=this->m_hWnd;   

    nid.uID=IDR_MAINFRAME;   

    nid.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP ;   

    nid.uCallbackMessage=WM_SHOWTASK;//自定义的消息名称   

    nid.hIcon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));  

    wcscpy(nid.szTip,L"冬雪简易改键");    //信息提示条  

    Shell_NotifyIcon(NIM_ADD,&nid);    //在托盘区添加图标   

    ShowWindow(SW_HIDE);    //隐藏主窗口

}

LRESULT CDotaDlg::OnShowTask(WPARAM wParam,LPARAM lParam)  
{    
    if(wParam!=IDR_MAINFRAME)       
        return 1;     
    switch(lParam)    
    {           
    case WM_RBUTTONUP://右键起来时弹出快捷菜单,这里只有一个“关闭”      
        {        
            LPPOINT lpoint= new tagPOINT;        
            ::GetCursorPos(lpoint);//得到鼠标位置         
            CMenu menu;            
            menu.CreatePopupMenu();//声明一个弹出式菜单         
            //增加菜单项“关闭”,点击则发送消息WM_DESTROY给主窗口(已            
            //隐藏),将程序结束。           
            menu.AppendMenu(MF_STRING,WM_DESTROY,L"关闭"); 
            //          menu.AppendMenu(MF_STRING,WM_DESTROY,"设置..."); 
            //确定弹出式菜单的位置          
            menu.TrackPopupMenu(TPM_LEFTALIGN,lpoint->x,lpoint->y,this);       
            //资源回收           
            HMENU hmenu=menu.Detach();             
            menu.DestroyMenu();            
            delete lpoint;       
        }            
        break;         
    case WM_LBUTTONDBLCLK://双击左键的处理       
        {          

            ShowWindow(SW_SHOWNORMAL);//简单的显示主窗口完事儿   SW_SHOW则错误 

            //  this->SetForegroundWindow(); // 置顶显示         
            //~DeleteTray();       
        }            
        break;         
    default:            
        break;     
    }     
    return 0; 
}



还可参考 dota改键精灵 v4.8.7 绿色版 附使用教程及注意事项dota改键精灵(dota改建器) v4.88 免费 绿色版


这篇关于简易Dota改键外挂程序制作的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程