Call win32 API to implement a summary of global system hotkeys
Source: Internet
Author: User
This isIt is used in the C/S windows System for the customer service call center project. One module needs to call out hot keys like QQ, So I summarized the Code in this regard.
// API-assisted operationsUsing System;
Using System. Windows. Forms; // for Key namespace
Using System. Runtime. InteropServices; namespace hotkeytest
{
/// <Summary>
/// WIN32 Windows API-assisted operation class.
/// </Summary>
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
{
[Financialas (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 = 0 x0112;
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 find1_wex (IntPtr parent, IntPtr next, string sClassName, IntPtr sWindowTitle);/* ------- using HOTKEYs in a C # application ------- in form load:
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; // monitoring hotkey
Protected override void WndProc (ref Message m)
{
Const int WM_HOTKEY = 0x0312;
Switch (m. Msg)
{
Case WM_HOTKEY:
MessageBox. Show ("You clicked the hotkey! ");
Break ;}
Base. WndProc (ref m );
}
// Register the hotkey
Public void SetHotKey (Keys c, bool bCtrl, bool bShift, bool bAlt, bool bWindows)
{
// Assign the variable first
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 hotkey // default hotkey is Ctrl + Shift + J
// M_hotkey = Keys. Home;
// M_hotkey = Keys. J;
// M_ctrlhotkey = true;
// M_shifthotkey = true;
// M_althotkey = false;
// M_winhotkey = false;
// SetHotKey (m_hotkey, m_ctrlhotkey, m_shifthotkey, m_althotkey, m_winhotkey); // read the configuration Hot Key
LoadSetting (); // read the configuration file (read the Hot Key to the variable)
ShowHotkey (); // display the hotkey to the text box} // display the hotkey to the 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 );
}
// Method 2 for reading the configuration file (reading the Hot Key to the variable)
Public void LoadSetting ()
{
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; // whether a page node exists
Bool bEnableHotkeyContent = false; // whether a hotkey node exists
While (reader. Read ())
{
Switch (reader. NodeType)
{
Case System. Xml. XmlNodeType. Element: // are we parsing a <page> element
BEnablePageContent = (reader. Name = "page"); // whether a page node exists
BEnableHotkeyContent = (reader. Name = "hotkey"); // whether a hotkey node exists
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 the hotkey to the configuration file
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. Close ();
} Private void button#click (object sender, System. EventArgs e)
{
SetHotKey (m_hotkey, m_ctrlhotkey, m_shifthotkey, m_althotkey, m_winhotkey );
SaveSetting ();
// Close ();
} // Set the hotkey
Private void textBox1_KeyDown (object sender, System. Windows. Forms. KeyEventArgs e)
{
// SetHotKey (e. KeyCode,
// (ModifierKeys & Keys. Control)> 0? True: false,
// (ModifierKeys & Keys. Shift)> 0? True: false,
// (ModifierKeys & Keys. Alt)> 0? True: false,
// (ModifierKeys & Keys. 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 (); // display the hotkey to the text box
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.