1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| public partial class Form1 : Form
{
[DllImport("user32", SetLastError = true)]
private static extern int RegisterHotKey(IntPtr hwnd, int id, int fsModifiers, int vk);
[DllImport("user32", SetLastError = true)]
private static extern int UnregisterHotKey(IntPtr hwnd, int id);
[DllImport("kernel32", SetLastError = true)]
private static extern short GlobalAddAtom(string lpString);
[DllImport("kernel32", SetLastError = true)]
private static extern int GlobalDeleteAtom(short nAtom);
private const int MOD_WIN = 0x8;
private const int WM_HOTKEY = 0x312;
private short hotKeyId;
private NewFolderMethods n = null;
public Form1()
{
InitializeComponent();
this.Hide();
n = new NewFolderMethods();
}
public void RegisterGlobalHotKey(Keys hotkey, int modifiers)
{
try
{
String atomName = AppDomain.GetCurrentThreadId().ToString("X8");
hotKeyId = GlobalAddAtom(atomName);
if (hotKeyId == 0)
{
int err = Marshal.GetLastWin32Error();
throw new Win32Exception(err, "Unable to generate unique hotkey ID.");
}
int result = RegisterHotKey(this.Handle, hotKeyId, modifiers, (int)hotkey);
if (result == 0)
{
int err = Marshal.GetLastWin32Error();
throw new Win32Exception(err, "Unable to register hotkey");
}
}
catch (Exception e)
{
UnregisterGlobalHotKey();
}
}
public void UnregisterGlobalHotKey()
{
if (this.hotKeyId != 0)
{
UnregisterHotKey(this.Handle, hotKeyId);
GlobalDeleteAtom(hotKeyId);
hotKeyId = 0;
}
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_HOTKEY)
{
MessageBox.Show(n.GetCurrentActiveDirectory());
}
}
private void Form1_Load(object sender, EventArgs e)
{
RegisterGlobalHotKey(Keys.N, MOD_WIN);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
UnregisterGlobalHotKey();
}
} |
Partager