Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 1 year has passed since last update.

[C#] 見えている全ウインドウのハンドルを取得

Posted at

別記事で、ウインドウのハンドルを全部取ってきて、それを使ってウインドウを全部最小化、とかをしたが、その「ウインドウのハンドルを全部取ってくる」方法の別解があったのでメモ。

別記事はこちら

見えてるウインドウ全部取得 サンプルコード

        public static void aaa()
        {
            var top = NativeMethods.GetTopWindow(IntPtr.Zero);

            DispHwndTitle(top);

            IntPtr w = NativeMethods.GetWindow(top, NativeMethods.GetWindowType.GW_HWNDNEXT);
            while (w != IntPtr.Zero)
            {
                    DispHwndTitle(w);

                w = NativeMethods.GetWindow(w, NativeMethods.GetWindowType.GW_HWNDNEXT);
            }

            void DispHwndTitle(IntPtr w)
            {

                if (NativeMethods.IsWindowVisible(w) == true)
                {
                    int textLen = NativeMethods.GetWindowTextLength(w);

                    if (textLen > 0)
                    {
                        StringBuilder tsb = new StringBuilder(textLen + 1);
                        NativeMethods.GetWindowText(w, tsb, tsb.Capacity);
                        StringBuilder csb = new StringBuilder(256);
                        NativeMethods.GetClassName(w, csb, csb.Capacity);

                        if (!(csb.ToString().Contains("Windows.UI.Core.CoreWindow"))
                            && !(csb.ToString().Contains("ApplicationFrameWindow")))
                        {
                            // デスクトップアプリのウインドウ
                            Debug.WriteLine("desk : " + tsb.ToString());
                        }
                        else if (csb.ToString().Contains("ApplicationFrameWindow"))
                        {
                            // UWPのウインドウ
                            Debug.WriteLine("uwp  : " + tsb.ToString());
                        }
                    }
                }

            }
        }
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?