C # implement the blood display and key modification functions in Warcraft (wariii) (with source code) (without affecting chat and typing)

Source: Internet
Author: User

When someone mentioned this feature in the forum, I think it should be able to implement it. I wrote it over the weekend. Here I will share it with you:

Train of Thought: hook + sendmessage,

 

First, because the key war3 we want to change is not a self-written program, we can only use hooks to monitor the keys on the keyboard:

Keyboard HOOK:

Using system; </P> <p> using system. runtime. interopservices; <br/> using system. windows. forms; </P> <p> namespace quickey <br/>{< br/> public class keyboardhook <br/>{< br/> private const int wm_keydown = 0x100; // press the message <br/> private const int wm_keyup = 0x101; // release the message <br/> private const int wm_syskeydown = 0x104; <br/> private const int wm_syskeyup = 0x105; </P> <p> // global event <br/> public event keyeventhandler Onkeydownevent; <br/> public event keyeventhandler onkeyupevent; <br/> public event keypresseventhandler onkeypressevent; </P> <p> static int hkeyboardhook = 0; </P> <p> // mouse constant <br/> Public const int wh_keyboard_ll = 13; </P> <p> Public Delegate int hookproc (INT ncode, int32 wparam, intptr lparam); </P> <p> // declare the keyboard hook event type <br/> hookproc keyboardhookprocedure; </P> <p> /// <summary> <br/> // declares the mail sending structure type of the keyboard hook. <br />/// </Summary> <br/> [structlayout (layoutkind. sequential)] <br/> public class keyboardhookstruct <br/>{< br/> Public int vkcode; // indicates a virtual keyboard code ranging from 1 to 254 <br/> Public int scancode; // indicates the hardware scan Code <br/> Public int flags; <br/> Public int time; <br/> Public int dwextrainfo; <br/>}</P> <p> // install hook <br/> [dllimport ("user32.dll", charset = charset. auto, callingconvention = callingconvention. stdcall)] <br/> publi C static extern int setwindowshookex (INT idhook, hookproc lpfn, intptr hinstance, int threadid); <br/> // next hook <br/> [dllimport ("user32.dll ", charset = charset. auto, callingconvention = callingconvention. stdcall)] <br/> Public static extern int callnexthookex (INT idhook, int ncode, int32 wparam, intptr lparam ); <br/> // uninstall hook <br/> [dllimport ("user32.dll", charset = charset. auto, callingconvention = Callingconvention. stdcall)] <br/> Public static extern bool unhookwindowshookex (INT idhook); </P> <p> private int keyboardhookproc (INT ncode, int32 wparam, intptr lparam) <br/>{< br/> If (ncode> = 0) & (onkeydownevent! = NULL | onkeyupevent! = NULL | onkeypressevent! = NULL) <br/>{< br/> keyboardhookstruct mykbhookstruct = (keyboardhookstruct) Marshal. ptrtostructure (lparam, typeof (keyboardhookstruct); </P> <p> // triggers onkeydownevent <br/> If (onkeydownevent! = NULL & (wparam = wm_keydown | wparam = wm_syskeydown) <br/>{< br/> keys keydata = (KEYS) mykbhookstruct. vkcode; <br/> keyeventargs E = new keyeventargs (keydata); <br/> onkeydownevent (this, e ); <br/>}< br/> return callnexthookex (hkeyboardhook, ncode, wparam, lparam ); <br/>}</P> <p> Public void start () <br/>{< br/> If (hkeyboardhook = 0) <br/>{< br/> keyboardhookprocedure = new hoo Kproc (keyboardhookproc); <br/> // hkeyboardhook = setwindowshookex (wh_keyboard_ll, keyboardhookprocedure, Marshal. gethinstance (system. reflection. assembly. getexecutingassembly (). getmodules () [0]), 0); <br/> using (system. diagnostics. process curprocess = system. diagnostics. process. getcurrentprocess () <br/> using (system. diagnostics. processmodule curmodule = curprocess. mainmodule) <br/> hkeyboardhook = Setwindowshookex (wh_keyboard_ll, keyboardhookprocedure, getmodulehandle (curmodule. modulename), 0); </P> <p> If (hkeyboardhook = 0) <br/>{< br/> stop (); <br/> throw new exception ("set globalkeyboardhook failed! "); <Br/>}< br/> Public void stop () <br/>{< br/> bool retkeyboard = true; <br/> If (hkeyboardhook! = 0) <br/>{< br/> retkeyboard = unhookwindowshookex (hkeyboardhook); <br/> hkeyboardhook = 0; <br/>}< br/> If (! Retkeyboard) <br/> throw new exception ("Unload globalkeyboardhook failed! "); <Br/>}</P> <p> // install the hook in the constructor <br/> Public keyboardhook () <br/>{< br/> Start (); <br/>}< br/> // uninstall hook in destructor <br/> ~ Keyboardhook () <br/>{< br/> stop (); <br/>}< br/>

 

Create a global HOOK:

 

Keyboardhook hook = new keyboardhook (); <br/> private void form1_load (Object sender, eventargs e) <br/>{< br/> hook. onkeydownevent + = new keyeventhandler (hook_onkeydownevent); </P> <p >}</P> <p> void hook_onkeydownevent (Object sender, keyeventargs E) {<br/> // All keyboard buttons can be intercepted here <br/> MessageBox. show (E. keyValue. tostring (); <br/>}

Open war3, press a few keyboards in it, and several dialog boxes are displayed, indicating that the buttons in war3 can be monitored,

My idea is to send messages to the window. You must find the handle to get the window:

[Dllimport ("user32.dll")] <br/> Public static extern intptr findwindow (string lpclassname, string lpwindowname); </P> <p> bool ishookenable = true; // global variable indicating whether the hook works <br/> private const int wm_keydown = 0x100; <br/> private const int wm_keyup = 0x101; <br/> void hook_onkeydownevent (Object sender, keyeventargs e) <br/>{< br/> If (E. keycode = keys. scroll) <br/>{< br/> ishookenable =! Ishookenable; <br/> This. Text = ishookenable? "Quickey-enabled": "quickey-Disabled"; <br/> policyicon1.text = This. text; <br/>}< br/> If (ishookenable) <br/>{< br/> intptr war3 = findwindow (null, "Warcraft III "); <br/> If (war3! = Intptr. Zero) <br/>{< br/> MessageBox. Show ("war3 found"); <br/>}< br/>}

Run, press it, and the "war3 found" dialog box pops up to get the handle of the war3 window.

And the rest is to send the button message to the window:

Declare first: <br/> [dllimport ("user32.dll")] <br/> Public static extern int sendmessage (intptr hwnd, uint MSG, int wparam, int lparam ); </P> <p> If (ishookenable) <br/> {<br/> intptr war3 = findwindow (null, "Warcraft III "); <br/> If (war3! = Intptr. zero) <br/> {<br/> If (E. keycode = keys. d) <br/> sendmessage (war3, wm_keydown, (INT) keys. c, 0); <br/>}< br/>}

Open war3 to the game creation interface and press d, huh? The game is created, indicating that the message is successfully sent.

In this way, the intercepted buttons can be sent to the specified message to "change" the key.

Private const int key_quotleft = 219; // [key code on the keyboard <br/> private const int key_quotright = 221; // code on the keyboard] key <br/> If (ishookenable) <br/>{< br/> intptr war3 = findwindow (null, "Warcraft III "); <br/> If (war3! = Intptr. zero) <br/>{< br/> setforegroundwindow (war3); // set the war3 window to the front. This sentence is not required during my own test, however, after removing this sentence, the friend said no <br/> sendmessage (war3, wm_keydown, key_quotleft, 0 ); // press the [key to display the blood of Youjun <br/> sendmessage (war3, wm_keydown, key_quotright, 0); </P> <p> sendmessage (war3, wm_keydown, int. parse (hash [E. keyValue. tostring ()]. tostring (), 0); // press <br/> sendmessage (war3, wm_keyup, Int. parse (hash [E. keyValue. tostring ()]. tostring (), 0); // release <br/>}< br/>}

 

OK. This is the general idea. The specific code can be downloaded below,

Over

 

The interface is as follows:

 

Program source code (write relatively messy, will be): quickey.rar: http://download.csdn.net/source/1755782

You are welcome to reprint it. Please indicate the source!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.