Call the Win32 API to implement the global system hot key

xiaoxiao2021-03-05  21

Recently, the project in the Customer Service Call Center is the Windows system of C / S. There is a module to implement the hotkey calls like QQ, so I summarize the code in this regard.

// API auxiliary operation

Using system;

Using system.windows.forms; // for key namespace

Using system.Runtime.InteropServices;

Namespace HotKeytest

{

///

/// Win32 Windows API auxiliary operation class.

///

Public class nativewin32

{

Public nativewin32 ()

{}

/ * ------- USING WIN32 Windows API IN A C # Application ------- * /

[DLLIMPORT ("User32.dll", Charset = Charset.Auto)]

Static Public Extern INTPTR getForegroundWindow (); //

[Structlayout (layoutkind.sequential, charset = charset.auto)]

Public struct stringbuffer

{

[Marshalas (unmanagedtype.byvaltstr, sizeconst = 256)]]]]

Public String Sztext;

}

[DLLIMPORT ("User32.dll", Charset = Charset.Auto)]

Public Static Extern Int GetWindowText (INTPTR HWND, OUT STRINGBUFFER ClassName, INT NMAXCOUNT);

[DLLIMPORT ("User32.dll", Charset = Charset.Auto)]

Public Static Extern Int SendMessage (INTPTR HWND, INT MSG, INT WPARAM, INT LPARAM);

[DLLIMPORT ("User32.dll", Charset = Charset.Auto)]

Public Static Extern INTPTR SendMessage (INTPTR HWND, INT MSG, INT WPARAM, INTPTR LPARAM);

Public const INT wm_syscommand = 0x0112;

Public const int sc_close = 0xf060;

Public Delegate Bool EnumthreadProc (INTPTR HWND, INTPTR LPARAM);

[DLLIMPORT ("User32.dll", Charset = Charset.Auto)]

Public Static Extern Bool ENUMTHREADWINDOWS (INT THREADID, ENUMTHREADPROC PFNENUM, INTPTR LPARAM);

[DLLIMPORT ("User32.dll", Charset = Charset.Auto)]

Public Static Extern INTPTR FINDWINDOWEX (INTPTR PARENT, INTPTR Next, STRING SclassName, INTPTR SWINDOWTILE);

/ * ------- USING HotKeys in A C # Application -------

In form loading:

Bool Success = RegisterhotKey (Handle, 100, Keymodifiers.Control | Keymodifiers.shift, Keys.j); In form closing:

UnregisterhotKey (Handle, 100);

Protected Override Void WndProc (Ref Message M)

{

Const int WM_HOTKEY = 0x0312;

Switch (m.msg)

{

Case WM_HOTKEY:

MessageBox.show ("Hotkey Pressed");

Break;

}

Base.WndProc (Ref M);

}

------- USING HotKeys in a c # Application ------- * /

[DLLIMPORT ("User32.dll", setLastError = true)]

Public Static Extern Bool RegisterhotKey (INTPTR HWND, // Handle to Window

INT ID, // Hot Key Identifier

KeyModifiers fsmodifiers, // Key-modifier options

Keys vk // virtual-key code

);

[DLLIMPORT ("User32.dll", setLastError = true)]

Public Static Extern Bool Unregisterhotkey (INTPTR HWND, // Handle To Window

Int ID // Hot Key Identifier

);

[Flags ()]

Public Enum Keymodifiers

{

None = 0,

Alt = 1,

CONTROL = 2,

SHIFT = 4,

WINDOWS = 8

}

}

}

_______________________________________

Form code:

protected const string m_sfilename = "config.xml";

Protected Keys M_hotKey;

Protected Bool M_ctrlhotKey, M_shifthotkey, M_AlthotKey, M_WinhotKey;

// Monitor hotkey

Protected Override Void WndProc (Ref Message M)

{

Const int WM_HOTKEY = 0x0312;

Switch (m.msg)

{

Case WM_HOTKEY:

Messagebox.show ("You have a hotkey!");

Break;

}

Base.WndProc (Ref M);

}

// Register hotkey

Public Void SetHotKey (Keys C, Bool Boctrl, Bool Bshift, Bool Balt, Bool Bwindows)

{

// prior to the variable

m_hotKey = C;

m_ctrlhotkey = bctrl;

m_shifthotkey = bshift;

m_AlthotKey = BALT;

M_WinhotKey = bwindows;

// Register the system hotkey

Nativewin32.keymodifiers modifiers = nativewin32.keymodifiers.none;

IF (m_ctrlhotkey)

Modifiers | = nativewin32.keymodifiers.control; if (m_shifthotkey)

Modifiers | = nativewin32.keymodifiers.shift;

IF (m_ALTHOTKEY)

Modifiers | = nativewin32.keymodifiers.alt;

IF (M_WinhotKey)

Modifiers | = nativewin32.keymodifiers.windows;

NativeWin32.Registerhotkey (Handle, 100, Modifiers, C);

}

Private Void Form1_Load (Object Sender, System.EventArgs E)

{

// Set the initial hot button // default hotkey is ctrl shift j

// m_hotkey = keys.home;

// m_hotkey = keys.j;

// m_ctrlhotkey = true;

// m_shifthKey = True;

// m_ALTHOTKEY = FALSE;

// m_winhotkey = false;

// setHotKey (M_HOTKEY, M_CTRLHOTKEY, M_SHIFTHOTKEY, M_ALTHOTKEY, M_WINHOTKEY);

// Read Configuration Hotkey

LoadSetting (); read configuration file (read hot button to variable)

ShowhotKey (); // Show hotkey to text box

}

/ / Show hotkey to text box

Public void showhotKey ()

{

String shotKey = "";

IF (m_ctrlhotkey)

SHOTKEY = "Ctrl";

IF (m_shifthotkey)

{

IF (ShotKey.Length! = 0)

SHOTKEY = " ";

SHOTKEY = "Shift";

}

IF (m_ALTHOTKEY)

{

IF (ShotKey.Length! = 0)

SHOTKEY = " ";

SHOTKEY = "alt";

}

IF (M_WinhotKey)

{

IF (ShotKey.Length! = 0)

SHOTKEY = " ";

SHOTKEY = "WIN";

}

IF (ShotKey.Length! = 0)

SHOTKEY = " ";

SHOTKEY = (CHAR) (int) m_hotkey;

THIS.TEXTBOX1.TEXT = ShotKey;

THIS.TEXTBOX1.SELECT (0, 0);

}

// Read configuration file method 2 (read hot button to variable)

Public void loading setting ()

{

System.xml.xmlTextReader Reader = NULL;

IF (! system.io.file.exists (m_sfilename)) Return;

Try

{

Reader = new system.xml.xmlTextReader (m_sfilename);

Reader.WhitespaceHandling = system.xml.WhitespaceHandling.none; Bool BenablePageContent = false; // Is there a Page node

Bool BenablehotKeyContent = false; // Is there a hotkey node

While (Reader.Read ())

{

Switch (Reader.NodeType)

{

Case system.xml.xmlnodetype.element: // area We Parsing a Element

BenablePageContent = (Reader.Name == "Page"); // Is there a Page node

BenablehotKeyContent = (Reader.Name == "Hotkey"); // Is there a hotkey node

IF (BenablehotKeyContent)

{

IF (Reader ["Ctrl"]! = null)

M_ctrlhotkey = (Reader ["Ctrl"] == "TRUE")? True: false;

IF (Reader ["Shift"]! = NULL)

M_shifthotKey = (Reader ["Shift"] == "TRUE")? True: false;

IF (reader ["alt"]! = null)

M_ALTHOTKEY = (Reader ["alt"] == "TRUE")? True: False;

IF (Reader ["Windows"]! = NULL)

M_WinhotKey = (Reader ["Windows"] == "TRUE")? TRUE: FALSE;

}

Break;

Case system.xml.xmlNodetype.Text:

IF (BenablehotKeyContent)

{

m_hotkey = (keys) System.int32.parse (Reader.Value);

SetHotKey (M_HOTKEY, M_CTRLHOTKEY, M_SHIFTHOTKEY, M_ALTHOTKEY, M_WINHOTKEY); // Register HotKey

}

BenablePageContent = FALSE;

BenablehotKeyContent = FALSE;

Break;

Case system.xml.xmlnodetype.attribute:

IF (BenablehotKeyContent)

{

IF (reader.name == "ctrl")

M_ctrlhotKey = (Reader.Value == "True")? True: false;

IF (reader.name == "shift")

m_shifthotkey = (Reader.Value == "True")? True: False;

IF (reader.name == "alt")

M_ALTHOTKEY = (Reader.Value == "TRUE")? True: false;

IF (reader.name == "windows")

M_WinhotKey = (Reader.Value == "True")? True: false;}

Break;

}

} // End while

}

Catch (System.xml.xmlexception E)

{

e.tostring ();

}

Finally

{

IF (reader! = null) reader.close ();

}

}

// Store hotkey to profile

Public void savesetting ()

{

System.xml.xmlTextWriter W = new system.xml.xmlTextWriter (m_sfilename, new system.text.utf8encoding ());

w.formatting = system.xml.formatting.indented;

w.writestartDocument ();

w.writestartElement ("setting");

W.WriteStartElement ("Hotkey"); //

W.WriteAttributeString ("Ctrl", M_CTRLHOTKEY? "true": "false");

W.WriteAttributeString ("Shift", M_shifthotkey? "true": "false");

W.WriteAttributeString ("alt", m_althotkey? "true": "false");

W.WriteAttributeString ("Windows", M_WinhotKey? "true": "false");

INT n = (int) m_hotkey;

w.writeString (n.tostring ());

w.writeEndelement (); //

w.writeEndelement (); //

W. close ();

}

Private void Button1_Click (Object Sender, System.Eventargs E)

{

SetHotKey (M_HOTKEY, M_CTRLHOTKEY, M_SHIFTHOTKEY, M_ALTHOTKEY, M_WINHOTKEY);

SAVESETTING ();

// close ();

}

// Set hotkey

Private void textbox1_keydown (Object sender, system.windows.keyeventargs e)

{

// sethotkey (E.KEYCODE,

// (ModifierKeys & Keys.Control)> 0? True: false,

// (ModifierKeys & Keys.shift)> 0? True: false,

// (ModifierKeys & Keys.alt> 0? True: False,

// ((ModifierKeys.Lwin> 0 || (ModifierKeys & Keys.rwin> 0)? True: false

M_hotKey = E.KEYCODE;

M_ctrlhotkey = (ModifierKeys & Keys.Control)> 0? true: false;

M_shifthotKey = (ModifierKeys & Keys.shift)> 0? true: false; m_althotkey = (modifierkeys & keys.alt)> 0? true: false;

M_WinhotKey = (ModifierKeys & Keys.Lwin> 0 || (MODIFIERKEYS & Keys.Rwin)> 0)? TRUE: FALSE;

ShowhotKey (); // Show hotkey to text box

}

转载请注明原文地址:https://www.9cbs.com/read-36927.html

New Post(0)