GUI开发,屏蔽输入法影响 {#gui开发屏蔽输入法影响}
public class WindowsImManager {
<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-type">boolean</span> <span class="hljs-variable">status</span> <span class="hljs-operator">=</span> <span class="hljs-literal">true</span>;
<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">native</span> WinNT.HANDLE <span class="hljs-title function_">ImmGetContext</span><span class="hljs-params">(WinDef.HWND hwnd)</span>;
<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">native</span> WinNT.HANDLE <span class="hljs-title function_">ImmAssociateContext</span><span class="hljs-params">(WinDef.HWND hwnd, WinNT.HANDLE himc)</span>;
<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">native</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">ImmReleaseContext</span><span class="hljs-params">(WinDef.HWND hwnd, WinNT.HANDLE himc)</span>;
<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">native</span> WinNT.HANDLE <span class="hljs-title function_">ImmCreateContext</span><span class="hljs-params">()</span>;
<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">native</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">ImmDestroyContext</span><span class="hljs-params">(WinNT.HANDLE himc)</span>;
<span class="hljs-keyword">static</span> {
Native.register(<span class="hljs-string">"imm32"</span>);
}
<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-type">User32</span> <span class="hljs-variable">u</span> <span class="hljs-operator">=</span> User32.INSTANCE;
<span class="hljs-comment">/**
* 打开输入法
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">makeOn</span><span class="hljs-params">()</span> {
<span class="hljs-keyword">if</span> (status) {
<span class="hljs-keyword">return</span>;
}
status = <span class="hljs-literal">true</span>;
WinDef.<span class="hljs-type">HWND</span> <span class="hljs-variable">hwnd</span> <span class="hljs-operator">=</span> u.GetForegroundWindow();
WinNT.<span class="hljs-type">HANDLE</span> <span class="hljs-variable">himc</span> <span class="hljs-operator">=</span> ImmGetContext(hwnd);
<span class="hljs-keyword">if</span> (himc == <span class="hljs-literal">null</span>) {
himc = ImmCreateContext();
ImmAssociateContext(hwnd, himc);
}
ImmReleaseContext(hwnd, himc);
}
<span class="hljs-comment">/**
* 关闭输入法
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">makeOff</span><span class="hljs-params">()</span> {
<span class="hljs-keyword">if</span> (!status) {
<span class="hljs-keyword">return</span>;
}
status = <span class="hljs-literal">false</span>;
WinDef.<span class="hljs-type">HWND</span> <span class="hljs-variable">hwnd</span> <span class="hljs-operator">=</span> u.GetForegroundWindow();
WinNT.<span class="hljs-type">HANDLE</span> <span class="hljs-variable">himc</span> <span class="hljs-operator">=</span> ImmAssociateContext(hwnd, <span class="hljs-literal">null</span>);
<span class="hljs-keyword">if</span> (himc != <span class="hljs-literal">null</span>) {
ImmDestroyContext(himc);
}
ImmReleaseContext(hwnd, himc);
}
`}
`