-
Notifications
You must be signed in to change notification settings - Fork 0
/
WindowsAPI.cs
145 lines (105 loc) · 4.16 KB
/
WindowsAPI.cs
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace HDF.DeskBand
{
public enum KeyModifiers
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}
public struct HotKey
{
public Keys Key { get; set; }
public KeyModifiers Modifier { get; set; }
public HotKey(Keys key, KeyModifiers m)
{
Key = key;
Modifier = m;
}
}
public class WindowsAPI
{
[DllImport("kernel32.dll")]
private static extern UInt32 GlobalAddAtom(String lpString);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool RegisterHotKey(IntPtr handle, int id, uint modifiers, uint virtualCode);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool UnregisterHotKey(IntPtr handle, int id);
private static Dictionary<HotKey, uint> HotKeyDict = new Dictionary<HotKey, uint>();
public static bool RegisterHotKey(IntPtr handle, HotKey hotkey)
{
if (HotKeyDict.ContainsKey(hotkey))
return false;
var hotkeyid = GlobalAddAtom(Guid.NewGuid().ToString());
HotKeyDict.Add(hotkey, hotkeyid);
return RegisterHotKey(handle, (int)hotkeyid, (uint)hotkey.Modifier, (uint)hotkey.Key);
}
public static bool UnregisterHotKey(IntPtr handle, HotKey hotkey)
{
if (!HotKeyDict.ContainsKey(hotkey))
return false;
var hotkeyid = HotKeyDict[hotkey];
HotKeyDict.Remove(hotkey);
return UnregisterHotKey(handle, (int)hotkeyid);
}
public static bool RegisterHotKey(IntPtr handle, Keys key, KeyModifiers flag)
{
if (HotKeyDict.ContainsKey(new HotKey(key, flag)))
return false;
var hotkeyid = GlobalAddAtom(Guid.NewGuid().ToString());
HotKeyDict.Add(new HotKey(key, flag), hotkeyid);
return RegisterHotKey(handle, (int)hotkeyid, (uint)flag, (uint)key);
}
public static bool UnregisterHotKey(IntPtr handle, Keys key, KeyModifiers flag)
{
if (!HotKeyDict.ContainsKey(new HotKey(key, flag)))
return false;
var hotkeyid = HotKeyDict[new HotKey(key, flag)];
return UnregisterHotKey(handle, (int)hotkeyid);
}
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool AddClipboardFormatListener(IntPtr hwnd);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool RemoveClipboardFormatListener(IntPtr hwnd);
//private static IntPtr WindowsHandle;
//public static bool AddClipboardFormatListener(IWin32Window windows)
//{
// WindowsHandle = windows.Handle;
// if (WindowsHandle == IntPtr.Zero)
// return false;
// return AddClipboardFormatListener(WindowsHandle);
//}
//public static bool RemoveClipboardFormatListener(IWin32Window windows)
//{
// WindowsHandle = windows.Handle;
// if (WindowsHandle == IntPtr.Zero)
// return false;
// return AddClipboardFormatListener(WindowsHandle);
//}
public static bool SetForeground(string windowName)
{
IntPtr Handle = FindWindow(windowName, null); //The className & WindowName I got using Spy++
if (Handle != null)
{
return SetForegroundWindow(Handle);
}
return false;
}
//// 函数讲解: https://blog.csdn.net/qq_34126663/article/details/70255257
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
}
}