-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplicationContext1.cs
99 lines (82 loc) · 2.65 KB
/
ApplicationContext1.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
using Microsoft.Win32;
using System;
using System.Windows.Forms;
using System.Reflection;
public class ApplicationContext1 : ApplicationContext
{
private readonly NotifyIcon notifyIcon;
private readonly ContextMenuStrip contextMenu;
private readonly System.Threading.Timer timer;
private readonly uint WM_IME_CONTROL = 0x283;
private readonly IntPtr IMC_SETCONVERSIONMODE = new IntPtr(0x002);
private readonly IntPtr IME_CHINESE = new IntPtr(0x401);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[System.Runtime.InteropServices.DllImport("imm32.dll")]
public static extern IntPtr ImmGetDefaultIMEWnd(IntPtr hWnd);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern Int16 GetKeyboardLayout(Int32 hWnd);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, uint wMsg, IntPtr wParam, IntPtr lParam);
public ApplicationContext1()
{
contextMenu = new ContextMenuStrip();
contextMenu.ShowImageMargin = false;
contextMenu.ShowCheckMargin = true;
var enable = new ToolStripMenuItem("Enable", null, OnCheck);
enable.Checked = true;
enable.CheckOnClick = true;
contextMenu.Items.Add(enable);
contextMenu.Items.Add(new ToolStripMenuItem("Exit", null, OnExit));
notifyIcon = new NotifyIcon();
notifyIcon.Icon = SystemIcons.Application;
notifyIcon.ContextMenuStrip = contextMenu;
notifyIcon.Visible = true;
var tsInterval = new TimeSpan(0, 0, 1);
timer = new System.Threading.Timer(
new System.Threading.TimerCallback(LockIME), null, tsInterval, tsInterval);
}
private void LockIME(object state)
{
try
{
var hWnd = GetForegroundWindow();
if (hWnd == IntPtr.Zero) return;
var id = ImmGetDefaultIMEWnd(hWnd);
if (id == IntPtr.Zero) return;
if (InChsStatus())
{
SetChsStatus(id);
}
}
catch { }
}
private bool InChsStatus()
{
var langId = GetKeyboardLayout(0);
var lang = (langId & 0xffff);
return lang == 0x804;
}
private void SetChsStatus(IntPtr hWnd)
{
SendMessage(hWnd, WM_IME_CONTROL, IMC_SETCONVERSIONMODE, IME_CHINESE);
}
private void OnExit(object sender, EventArgs e)
{
timer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
timer.Dispose();
notifyIcon.Dispose();
Application.Exit();
}
private void OnCheck(object sender, EventArgs e)
{
if ((sender as ToolStripMenuItem).Checked)
{
timer.Change(1, 1); ;
}
else
{
timer.Change(Timeout.Infinite, Timeout.Infinite); ;
}
}
}