示例#1
0
 public static extern uint GetGUIThreadInfo(uint dwthreadid, ref GUITHREADINFO lpguithreadinfo);
        void updateIMEStatusInfo(ref IMEStatusInfo status) {
            // 【C#,WinAPI】バックグラウンドで現在のIME状態を取得 - 行き当たりばったり
            // http://sturnus.hateblo.jp/entry/2014/05/10/234603
            GUITHREADINFO gti = new GUITHREADINFO();
            gti.cbSize = Marshal.SizeOf(gti);

            if (WinApi.GetGUIThreadInfo(0, ref gti) != 0) {
                bool windowVisible = WinApi.IsWindowVisible(gti.hwndActive);

                bool windowSwitched = (status.WindowHandle != gti.hwndActive);
                bool focusSwitched = (status.ClientHandle != gti.hwndFocus);

                if (windowSwitched) {
                    status.WindowText = WinApi.GetWindowText(gti.hwndActive);
                    try {
                        status.ExecutableFileName = WinApi.GetExecutableFileNameFromWindowHandle(gti.hwndActive);
                    }
                    catch {
                        status.ExecutableFileName = string.Empty;
                    }
                }

                if (focusSwitched) {
                    status.ClientClassName = WinApi.GetClassName(gti.hwndFocus);
                }

                if (windowVisible) {
                    IntPtr hWndIME = WinApi.ImmGetDefaultIMEWnd(gti.hwndFocus);
                    status.IMEEnabled = (WinApi.SendMessage(hWndIME, 0x283, 0x5, 0) != 0);
                    status.ClientBounds = WinApi.GetWindowRect(gti.hwndFocus);
                }

                if (windowSwitched || focusSwitched) {
                    // 無視リストに当たった場合は無かったことにする
                    bool ignored = false;
                    foreach (var regex in _cachedIgnoreList) {
                        if (regex.IsMatch(status.ExecutableFileName) || 
                            regex.IsMatch(status.WindowText) || 
                            regex.IsMatch(status.ClientClassName)) {

                            ignored = true;
                            break;
                        }
                    }
                    status.Ignored = ignored;
                }

                status.WindowHandle = gti.hwndActive;
                status.ClientHandle = gti.hwndFocus;
                status.WindowVisible = windowVisible;
            }
        }