Beispiel #1
0
        /// <summary>
        /// Send window resize event.
        /// </summary>
        protected void ResetSize()
        {
            if (!IsActive)
            {
                return;
            }

            // 最小化状態ならば、次に表示されるときに描画が発生するものと仮定して、何もしない
            if (IsMinimized)
            {
                return;
            }

            // 今のサイズを記憶
            Vector2 size = GetSize();

            // 1px横幅を広げて、リサイズイベントを強制的に起こす
            WinApi.SetWindowPos(
                hWnd,
                IntPtr.Zero,
                0, 0, (int)size.x + 1, (int)size.y,
                WinApi.SWP_NOMOVE | WinApi.SWP_NOZORDER
                | WinApi.SWP_FRAMECHANGED | WinApi.SWP_NOOWNERZORDER
                | WinApi.SWP_NOACTIVATE | WinApi.SWP_ASYNCWINDOWPOS
                );

            // 元のサイズに戻す。この時もリサイズイベントは発生するはず
            WinApi.SetWindowPos(
                hWnd,
                IntPtr.Zero,
                0, 0, (int)size.x, (int)size.y,
                WinApi.SWP_NOMOVE | WinApi.SWP_NOZORDER
                | WinApi.SWP_FRAMECHANGED | WinApi.SWP_NOOWNERZORDER
                | WinApi.SWP_NOACTIVATE | WinApi.SWP_ASYNCWINDOWPOS
                );
        }
 /// <summary>
 /// キーコードを送ります
 /// </summary>
 public void SendKey(KeyCode code)
 {
     WinApi.PostMessage(this.hWnd, WinApi.WM_IME_CHAR, (long)code, IntPtr.Zero);
 }
 /// <summary>
 /// マウスカーソル座標を取得
 /// </summary>
 /// <returns>スクリーン上の絶対座標。(Unityのウィンドウが基準では無い)</returns>
 static public Vector2 GetCursorPosition()
 {
     WinApi.POINT point;
     WinApi.GetCursorPos(out point);
     return(new Vector2(point.x, point.y));
 }
 /// <summary>
 /// マウスカーソルを指定座標へ移動させる
 /// </summary>
 /// <param name="screenPosition">スクリーン上の絶対座標。(Unityのウィンドウが基準では無い)</param>
 static public void SetCursorPosition(Vector2 screenPosition)
 {
     WinApi.SetCursorPos((int)screenPosition.x, (int)screenPosition.y);
 }
        /// <summary>
        /// 現在のアクティブウインドウが自分自身ならばtrueを返す
        /// </summary>
        /// <returns></returns>
        public bool CheckActiveWindow()
        {
            IntPtr hwnd = WinApi.GetActiveWindow();

            return(hwnd == hWnd);
        }
        /// <summary>
        /// 自ウィンドウと思われるハンドルを取得
        /// </summary>
        /// <returns>Null if failed</returns>
        static public WindowHandle FindWindow()
        {
            // 自分自身のPIDを取得し、スレッドを取得
            System.Diagnostics.Process process = System.Diagnostics.Process.GetCurrentProcess();
            //return new WindowHandle(process.MainWindowHandle);	// ←これではダメだった。MainWindowHandle == 0 となった。

            int pid = process.Id;

            //Debug.Log("PID: " + pid);

#if UNITY_EDITOR
            // Gameビューを取得
            // 参考: https://qiita.com/MARQUE/items/292c9080a686d95af1a5
            var gameViewWin = UnityEditor.EditorWindow.GetWindow(System.Type.GetType("UnityEditor.GameView,UnityEditor"));

            // Gameビューににフォーカスを与えてから、アクティブなウィンドウを取得
            gameViewWin.Focus();
            //System.Threading.Thread.Sleep(100);
            IntPtr hwnd = WinApi.GetActiveWindow();

            var rootHwnd = hwnd;
            rootHwnd = WinApi.GetAncestor(hwnd, WinApi.GA_ROOT);

            WindowHandle gameWindow = new WindowHandle(rootHwnd);

            // 一応PIDをチェックし、自分一致したらそのウィンドウを使うことにして終了
            if (gameWindow.ProcessId == pid)
            {
                return(gameWindow);
            }
#else
            IntPtr hwnd;
#endif

            // アクティブウィンドウを取得する方法
            hwnd = WinApi.GetActiveWindow();
            if (hwnd == IntPtr.Zero)
            {
                return(null);
            }
            //Debug.Log("Active HWND:" + hwnd);

            WindowHandle window = new WindowHandle(hwnd);
            if (window.ProcessId != pid)
            {
                return(null);
            }
            return(window);


            //// 現存するウィンドウ一式を取得してPIDが一致するものを利用する方法
            //WindowHandle[] handles = FindWindows();
            //foreach (WindowHandle window in handles)
            //{
            //    Debug.Log(window);

            //    // PIDが一致するものを検索
            //    if (window.ProcessId == pid)
            //    {
            //        return window;
            //    }
            //}
            //return null;
        }
 /// <summary>
 /// ウィンドウスタイルを最初のものに戻す
 /// </summary>
 private void RestoreWindowState()
 {
     WinApi.SetWindowLong(hWnd, WinApi.GWL_STYLE, this.OriginalWindowStyle);
     WinApi.SetWindowLong(hWnd, WinApi.GWL_EXSTYLE, this.OriginalWindowExStyle);
     WinApi.ShowWindow(hWnd, WinApi.SW_SHOW);
 }
Beispiel #8
0
        /// <summary>
        /// ウィンドウ透過をON/OFF
        /// </summary>
        public void EnableTransparent(bool enable)
        {
            if (!IsActive)
            {
                return;
            }

            bool maximized = IsMaximized;

            // 最大化状態ならば、一度通常ウィンドウに戻してから透過・枠有無を変更する
            if (maximized)
            {
                Restore();
            }

            if (enable)
            {
                // 現在のウィンドウ情報を記憶
                StoreWindowSize();

                // 枠無しウィンドウにする
                EnableBorderless(true);

                switch (TransparentType)
                {
                case TransparentTypes.Alpha:
                    EnableTransparentByDWM();
                    break;

                case TransparentTypes.ColorKey:
                    EnableTransparentBySetLayered();
                    break;
                }
            }
            else
            {
                // 現在の指定ではなく、透過にした時点の指定に基づいて無効化
                switch (_currentTransparentType)
                {
                case TransparentTypes.Alpha:
                    DisableTransparentByDWM();
                    break;

                case TransparentTypes.ColorKey:
                    DisableTransparentBySetLayered();
                    break;
                }

                // 枠ありウィンドウにする
                EnableBorderless(false);

                // 操作の透過をやめる
                EnableClickThrough(false);
            }

            // 戻す方法を決めるため、透明化が変更された時のタイプを記録しておく
            _currentTransparentType = TransparentType;

            // 呼ぶ前に最大化状態だったならば、再度最大化
            if (maximized)
            {
                // ウィンドウ再描画は最大化時に行われる
                Maximize();
            }
            else
            {
                // ウィンドウ枠の分サイズが変わった際、Unityにリサイズイベントを発生させないとサイズがずれる
                ResetSize();

                // ウィンドウ再描画
                WinApi.ShowWindow(hWnd, WinApi.SW_SHOW);
            }
        }