许明会的计算机技术主页

Language:C,C++,.NET Framework(C#)
Thinking:Design Pattern,Algorithm,WPF,Windows Internals
Database:SQLServer,Oracle,MySQL,PostSQL
IT:MCITP,Exchange,Lync,Virtualization,CCNP

导航

游戏外挂最基本的按键模拟

  玩魔兽世界的时候,很多小号找大号带副本升级。台服在客户端不活动的情况下会强迫用户下线,导致尽管小号无事可做,还要在屏幕下等待。于是自己开发一个程序,让游戏角色保持活动,不至于下线。

 

代码
///本程序僅僅監控台服WOW程序,挂機刷經驗(如破碎)保持角色與服務器互動。
///程序模擬鍵盤操作,每隔指定的時間模擬按空格,讓角色跳躍以防被踢。


//---------------------------------------------------------------------------
#include <windows.h>
#define ID_TIMER 1
#define TIMER_ELAPSE 5000    //角色跳跳時間間隔
//---------------------------------------------------------------------------
HWND hWnd;
LPCTSTR ClsName 
= L"WOW_JUMPER";
LPCTSTR WindowCaption 
= L"魔獸世界台服破碎挂機跳跳虎";
LPCTSTR WOW_CLASSNAME 
= L"GxWindowClassD3d";
LPCTSTR WOW_CAPTION 
= L"魔獸世界";
//HWND hwndWoW = FindWindow(TEXT("GxWindowClassD3d"),TEXT("魔獸世界"));

LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);

HWND hwndWoW, hwndJumper;
DWORD dwThreadIDWoW, dwThreadIDJumper;
BOOL bOK;
int lResult;

//---------------------------------------------------------------------------
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, 
int nCmdShow)
{
    MSG         Msg;
    WNDCLASSEX  WndClsEx;

    WndClsEx.cbSize        
= sizeof(WNDCLASSEX);
    WndClsEx.style         
= CS_HREDRAW | CS_VREDRAW;
    WndClsEx.lpfnWndProc   
= WndProc;
    WndClsEx.cbClsExtra    
= NULL;
    WndClsEx.cbWndExtra    
= NULL;
    WndClsEx.hInstance     
= hInstance;
    WndClsEx.hIcon         
= LoadIcon(NULL, IDI_APPLICATION);
    WndClsEx.hCursor       
= LoadCursor(NULL, IDC_ARROW);
    WndClsEx.hbrBackground 
= (HBRUSH)GetStockObject(WHITE_BRUSH);
    WndClsEx.lpszMenuName  
= NULL;
    WndClsEx.lpszClassName 
= ClsName;
    WndClsEx.hIconSm       
= LoadIcon(NULL, IDI_APPLICATION);

    RegisterClassEx(
&WndClsEx);

    hWnd 
= CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, ClsName, WindowCaption,WS_OVERLAPPEDWINDOW,
        
100,120,320,240,
        NULL, NULL, hInstance, NULL);

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    
while( GetMessage(&Msg, NULL, 00) )
    {
        TranslateMessage(
&Msg);
        DispatchMessage(
&Msg);
    }

    
return Msg.wParam;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
    
switch(Msg)
    {
    
case WM_CREATE:
        
//HWND hwndWoW = FindWindow(TEXT("GxWindowClassD3d"),TEXT("魔獸世界"));
        hwndWoW = FindWindow(WOW_CLASSNAME , WOW_CAPTION );
        dwThreadIDWoW 
= GetWindowThreadProcessId(hwndWoW,NULL);

        hwndJumper 
= GetForegroundWindow();
        dwThreadIDJumper 
= GetWindowThreadProcessId(hwndJumper,NULL);

        bOK 
= AttachThreadInput(dwThreadIDJumper, dwThreadIDWoW,TRUE);

        
if(bOK)//在獲得魔獸程序句柄級線程,並與本程序線程成功綁定後,方可啟動定時器。
        {
            SetTimer(hWnd,ID_TIMER,TIMER_ELAPSE,NULL);
        }
        
else
        {
            MessageBox(NULL,TEXT(
"魔獸世界主程序未找到,請先運行WOW後再運行本程序。"),TEXT("監控程序已關閉"),0x40);        
            SendMessage(hWnd,WM_CLOSE,
0,0);
            
return 0;
        }
        
break;

    
case WM_ACTIVATE:
        bOK 
= SetWindowPos(hWnd,HWND_TOPMOST ,100,120,320,240,SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE);
        
break;
    
case WM_TIMER:
        hwndWoW 
= FindWindow(WOW_CLASSNAME , WOW_CAPTION );
        
if(hwndWoW == NULL)//監控程序已經終止,則退出本程序。
        {
            SendMessage(hWnd,WM_CLOSE,
0,0);        
            
return 0;
        }
        bOK 
= SetForegroundWindow(hwndWoW);//保證激活WOW程序,100毫秒後主窗體應該能顯示;然後模擬空格鍵按下抬起
        Sleep(100);
        keybd_event(VK_SPACE,
0,0,0);
        Sleep(
10); 
        keybd_event(VK_SPACE,
0, KEYEVENTF_KEYUP,0);  
        
break;
    
case WM_DESTROY:
        KillTimer(hWnd,ID_TIMER);
        bOK 
= AttachThreadInput(dwThreadIDJumper, dwThreadIDWoW,FALSE);        
        PostQuitMessage(WM_QUIT);
        
break;
    
default:
        
return DefWindowProc(hWnd, Msg, wParam, lParam);
    }
    
return 0;
}
//---------------------------------------------------------------------------


posted on 2010-12-03 09:02  许明会  阅读(1324)  评论(0编辑  收藏  举报