Ejemplo n.º 1
0
 internal static extern IntPtr GetWindow(IntPtr hWnd, GETWINDOW_CMD uCmd);//本当のuCmdはuint型
Ejemplo n.º 2
0
        //右Ctrlキー+右Shiftキーが押されたら
        //全体画面取得
        //各RECT取得
        //キャプチャ画像更新
        private void MyTimer_Tick(object sender, EventArgs e)
        {
            //キー入力取得用
            //Keyを仮想キーコードに変換
            int vKey1 = KeyInterop.VirtualKeyFromKey(Key.RightCtrl);
            int vKey2 = KeyInterop.VirtualKeyFromKey(Key.RightShift);
            //キーの状態を取得
            short key1state = GetAsyncKeyState(vKey1);
            short key2state = GetAsyncKeyState(vKey2);

            //右Ctrlキー+右Shiftキーが押されていたら
            if ((key1state & 0x8000) >> 15 == 1 & ((key2state & 1) == 1))
            {
                //画面全体画像取得
                MyBitmap       = ScreenCapture();
                MyImage.Source = MyBitmap;



                //最前面ウィンドウのハンドル取得
                IntPtr hForeWnd = GetForegroundWindow();
                //ウィンドウ名表示
                var windowName = new StringBuilder(65535);
                int temp       = GetWindowText(hForeWnd, windowName, 65535);
                MyTextBlockWindowText.Text = windowName.ToString();

                //各ウィンドウのRectを更新
                //最前面のウィンドウの見た目通りのRect
                DwmGetWindowAttribute(hForeWnd,
                                      DWMWINDOWATTRIBUTE.DWMWA_EXTENDED_FRAME_BOUNDS,
                                      out MyRectForeground,
                                      Marshal.SizeOf(typeof(RECT)));
                //最前面のウィンドウの関連ウィンドウ取得
                var neko = MyComboBox.Items;
                for (int i = 0; i < neko.Count; i++)
                {
                    GETWINDOW_CMD cmd = (GETWINDOW_CMD)neko[i];
                    IntPtr        hw  = GetWindow(hForeWnd, cmd);
                    DwmGetWindowAttribute(hw, DWMWINDOWATTRIBUTE.DWMWA_EXTENDED_FRAME_BOUNDS, out MyRectRelated[i], Marshal.SizeOf(typeof(RECT)));
                }
                //最前面のウィンドウの親ウィンドウ
                IntPtr hWndParent = GetParent(hForeWnd);
                DwmGetWindowAttribute(hWndParent, DWMWINDOWATTRIBUTE.DWMWA_EXTENDED_FRAME_BOUNDS, out MyRectParent, Marshal.SizeOf(typeof(RECT)));
                //最前面のウィンドウの祖先Rect
                neko = MyComboBox2.Items;
                for (int i = 0; i < neko.Count; i++)
                {
                    IntPtr hw = GetAncestor(hForeWnd, (GETANCESTOR_FLAGS)neko[i]);
                    DwmGetWindowAttribute(hw, DWMWINDOWATTRIBUTE.DWMWA_EXTENDED_FRAME_BOUNDS, out MyRectAncestor[i], Marshal.SizeOf(typeof(RECT)));
                }

                MyTextBlock1.Text = $"{MyRectForeground} 最前面のウィンドウ";
                MyTextBlock2.Text = $"{MyRectRelated[MyComboBox.SelectedIndex]} 最前面の関連ウィンドウ";
                MyTextBlock3.Text = $"{MyRectParent} 最前面の親ウィンドウ";
                MyTextBlock4.Text = $"{MyRectAncestor[MyComboBox2.SelectedIndex]} 最前面の祖先ウィンドウ";


                //アクティブウィンドウテスト
                IntPtr aw = GetActiveWindow();
                DwmGetWindowAttribute(aw, DWMWINDOWATTRIBUTE.DWMWA_EXTENDED_FRAME_BOUNDS, out MyRectActive, Marshal.SizeOf(typeof(RECT)));
                MyTextBlock5.Text = $"{MyRectActive} アクティブウィンドウ";

                //表示画像更新
                UpdateImage();
            }
        }