첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
0.00MB

▶ WindowHandleHelper.cs

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace TestProject
{
    /// <summary>
    /// 윈도우 핸들 헬퍼
    /// </summary>
    public static class WindowHandleHelper
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Delegate
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 윈도우 열거하기 대리자 - EnumWindowsProc(windowHandle, longParameter)

        /// <summary>
        /// 윈도우 열거하기 대리자
        /// </summary>
        /// <param name="windowHandle">윈도우 핸들</param>
        /// <param name="longParameter">LONG 매개 변수</param>
        /// <returns>처리 결과</returns>
        private delegate bool EnumWindowsProc(IntPtr windowHandle, int longParameter);

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Import
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 쉘 윈도우 구하기 - GetShellWindow()

        /// <summary>
        /// 쉘 윈도우 구하기
        /// </summary>
        /// <returns>쉘 윈도우 핸들</returns>
        [DllImport("user32.dll")]
        private static extern IntPtr GetShellWindow();

        #endregion
        #region 윈도우 열거하기 - EnumWindows(enumWindowsProc, longParameter)

        /// <summary>
        /// 윈도우 열거하기
        /// </summary>
        /// <param name="enumWindowsProc">윈도우 열거하기 대리자</param>
        /// <param name="longParameter">LONG 매개 변수</param>
        /// <returns>처리 결과</returns>
        [DllImport("user32.dll")]
        private static extern bool EnumWindows(EnumWindowsProc enumWindowsProc, int longParameter);

        #endregion
        #region 윈도우 표시 여부 구하기 - IsWindowVisible(windowHandle)

        /// <summary>
        /// 윈도우 표시 여부 구하기
        /// </summary>
        /// <param name="windowHandle">윈도우 핸들</param>
        /// <returns>윈도우 표시 여부</returns>
        [DllImport("user32.dll")]
        private static extern bool IsWindowVisible(IntPtr windowHandle);

        #endregion
        #region 윈도우 텍스트 길이 구하기 - GetWindowTextLength(windowHandle)

        /// <summary>
        /// 윈도우 텍스트 길이 구하기
        /// </summary>
        /// <param name="windowHandle">윈도우 핸들</param>
        /// <returns>윈도우 텍스트 길이</returns>
        [DllImport("user32.dll")]
        private static extern int GetWindowTextLength(IntPtr windowHandle);

        #endregion
        #region 윈도우 텍스트 구하기 - GetWindowText(windowHandle, stringBuilder, maximumCount)

        /// <summary>
        /// 윈도우 텍스트 구하기
        /// </summary>
        /// <param name="windowHandle">윈도우 핸들</param>
        /// <param name="stringBuilder">문자열 빌더</param>
        /// <param name="maximumCount">최대 카운트</param>
        /// <returns>윈도우 텍스트</returns>
        [DllImport("user32.dll")]
        private static extern int GetWindowText(IntPtr windowHandle, StringBuilder stringBuilder, int maximumCount);

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 오픈 윈도우 핸들 딕셔너리 구하기 - GetOpenWindowHandleDictionary()

        /// <summary>
        /// 오픈 윈도우 핸들 딕셔너리 구하기
        /// </summary>
        /// <returns>윈도우 핸들 딕셔너리</returns>
        public static Dictionary<IntPtr, string> GetOpenWindowHandleDictionary()
        {
            IntPtr shellWindowHandle = GetShellWindow();

            Dictionary<IntPtr, string> windowHandleDictionary = new Dictionary<IntPtr, string>();

            EnumWindows
            (
                delegate(IntPtr windowHandle, int longParameter)
                {
                    if(windowHandle == shellWindowHandle)
                    {
                        return true;
                    }

                    if(!IsWindowVisible(windowHandle))
                    {
                        return true;
                    }

                    int windowTextLength = GetWindowTextLength(windowHandle);

                    if(windowTextLength == 0)
                    {
                        return true;
                    }

                    StringBuilder stringBuilder = new StringBuilder(windowTextLength);

                    GetWindowText(windowHandle, stringBuilder, windowTextLength + 1);

                    windowHandleDictionary[windowHandle] = stringBuilder.ToString();

                    return true;
                },
                0
            );

            return windowHandleDictionary;
        }

        #endregion
    }
}

 

728x90

 

▶ Program.cs

using System;
using System.Collections.Generic;

namespace TestProject
{
    /// <summary>
    /// 프로그램
    /// </summary>
    class Program
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 프로그램 시작하기 - Main()

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        private static void Main()
        {
            Dictionary<IntPtr, string> dictionary = WindowHandleHelper.GetOpenWindowHandleDictionary();

            foreach(KeyValuePair<IntPtr, string> keyValuePair in dictionary)
            {
                IntPtr windowHandle = keyValuePair.Key;
                string windowText   = keyValuePair.Value;

                Console.WriteLine($"{windowText} : {windowHandle}");
            }
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요