最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于C#實(shí)現(xiàn)獲取Windows所有窗口句柄

 更新時(shí)間:2023年12月17日 08:09:19   作者:rjcql  
在做錄屏或截屏操作時(shí),需要獲取當(dāng)前正在運(yùn)行中的桌面程序句柄,所以這篇文章主要為大家詳細(xì)介紹了如何使用C#實(shí)現(xiàn)獲取Windows所有窗口句柄,需要的可以參考下

寫(xiě)在前面

在做錄屏或截屏操作時(shí),需要獲取當(dāng)前正在運(yùn)行中的桌面程序句柄,在網(wǎng)上查找資源的的時(shí)候,發(fā)現(xiàn)了一個(gè)工具類(lèi)還不錯(cuò),這邊做個(gè)驗(yàn)證記錄。

參考代碼

    public class WindowApi
    {
        //尋找目標(biāo)進(jìn)程窗口       
        [DllImport("USER32.DLL")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        //設(shè)置進(jìn)程窗口到最前       
        [DllImport("USER32.DLL")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
 
        #region GetWindowCapture的dll引用
        [DllImport("user32.dll")]
        private static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rectangle rect);
 
        [DllImport("gdi32.dll")]
        private static extern IntPtr CreateCompatibleDC(
         IntPtr hdc // handle to DC
         );
        [DllImport("gdi32.dll")]
        private static extern IntPtr CreateCompatibleBitmap(
         IntPtr hdc,         // handle to DC
         int nWidth,      // width of bitmap, in pixels
         int nHeight      // height of bitmap, in pixels
         );
        [DllImport("gdi32.dll")]
        private static extern IntPtr SelectObject(
         IntPtr hdc,           // handle to DC
         IntPtr hgdiobj    // handle to object
         );
        [DllImport("gdi32.dll")]
        private static extern int DeleteDC(
         IntPtr hdc           // handle to DC
         );
        [DllImport("user32.dll")]
        private static extern bool PrintWindow(
         IntPtr hwnd,                // Window to copy,Handle to the window that will be copied.
         IntPtr hdcBlt,              // HDC to print into,Handle to the device context.
         UInt32 nFlags               // Optional flags,Specifies the drawing options. It can be one of the following values.
         );
        [DllImport("user32.dll")]
        private static extern IntPtr GetWindowDC(
         IntPtr hwnd
         );
        #endregion
        /// <summary>
        /// 根據(jù)句柄獲取截圖
        /// </summary>
        /// <param name="hWnd"></param>
        /// <returns></returns>
        public static Bitmap GetWindowCapture(IntPtr hWnd)
        {
            IntPtr hscrdc = GetWindowDC(hWnd);
            Rectangle windowRect = new Rectangle();
            GetWindowRect(hWnd, ref windowRect);
            int width = Math.Abs(windowRect.X - windowRect.Width);
            int height = Math.Abs(windowRect.Y - windowRect.Height);
            IntPtr hbitmap = CreateCompatibleBitmap(hscrdc, width, height);
            IntPtr hmemdc = CreateCompatibleDC(hscrdc);
            SelectObject(hmemdc, hbitmap);
            PrintWindow(hWnd, hmemdc, 0);
            Bitmap bmp = Image.FromHbitmap(hbitmap);
            DeleteDC(hscrdc);//刪除用過(guò)的對(duì)象
            DeleteDC(hmemdc);//刪除用過(guò)的對(duì)象
            return bmp;
        }
        /// <summary>
        /// 根據(jù)句柄獲取截圖路徑
        /// </summary>
        /// <param name="hWnd"></param>
        /// <returns></returns>
        public static string GetCapturePath(IntPtr hWnd)
        {
            string path = string.Empty;
            string dicPath = AppDomain.CurrentDomain.BaseDirectory + "Intercept";
            if (!Directory.Exists(dicPath))
            {
                Directory.CreateDirectory(dicPath);
            }
            using (Bitmap bitmap = GetWindowCapture(hWnd))
            {
                path = dicPath + "\\" + Guid.NewGuid().ToString().Replace("-", "") + ".png";
                bitmap.Save(path);
            }
            return path;
        }
 
 
        private delegate bool WNDENUMPROC(IntPtr hWnd, int lParam);
 
        //用來(lái)遍歷所有窗口 
        [DllImport("user32.dll")]
        private static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, int lParam);
 
        //獲取窗口Text 
        [DllImport("user32.dll")]
        private static extern int GetWindowTextW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder lpString, int nMaxCount);
 
        //獲取窗口類(lèi)名 
        [DllImport("user32.dll")]
        private static extern int GetClassNameW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder lpString, int nMaxCount);
 
        //自定義一個(gè)結(jié)構(gòu),用來(lái)保存句柄信息
        public struct WindowInfo
        {
            public IntPtr hWnd;
            public string szWindowName;
            public string szClassName;
        }
 
        public static WindowInfo[] GetAllDesktopWindows()
        {
            //用來(lái)保存窗口對(duì)象 列表
            List<WindowInfo> wndList = new List<WindowInfo>();
 
            //enum all desktop windows 
            EnumWindows(delegate (IntPtr hWnd, int lParam)
            {
                WindowInfo wnd = new WindowInfo();
                StringBuilder sb = new StringBuilder(256);
 
                //get hwnd 
                wnd.hWnd = hWnd;
 
                //get window name  
                GetWindowTextW(hWnd, sb, sb.Capacity);
                wnd.szWindowName = sb.ToString();
 
                //get window class 
                GetClassNameW(hWnd, sb, sb.Capacity);
                wnd.szClassName = sb.ToString();
 
                //add it into list 
                wndList.Add(wnd);
                return true;
            }, 0);
 
            return wndList.ToArray();
        }
 
        [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
        public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
 
        /// <summary>
        /// 釋放內(nèi)存
        /// </summary>
        public async static Task ClearMemory()
        {
            //獲得當(dāng)前工作進(jìn)程
            Process proc = Process.GetCurrentProcess();
            long usedMemory = proc.PrivateMemorySize64;
            if (usedMemory > 1024 * 1024 * 10)
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
                if (Environment.OSVersion.Platform == PlatformID.Win32NT)
                {
                    SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1);
                }
                await Task.Delay(10);
            }
        }
    }

調(diào)用結(jié)果

調(diào)用示意:

var programList = WindowApi.GetAllDesktopWindows().Where(x => !string.IsNullOrEmpty(x.szWindowName)).ToList();
listBoxWindows.DataSource = programList.Select(x => x.szWindowName).ToList(); 

以上就是基于C#實(shí)現(xiàn)獲取Windows所有窗口句柄的詳細(xì)內(nèi)容,更多關(guān)于C#獲取Windows窗口句柄的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

北川| 呼图壁县| 赣榆县| 彩票| 东明县| 炉霍县| 轮台县| 临汾市| 东乌珠穆沁旗| 烟台市| 苍山县| 舒城县| 来凤县| 高邮市| 化德县| 永登县| 西宁市| 日喀则市| 固原市| 温宿县| 长寿区| 通江县| 淮南市| 富裕县| 连山| 甘孜县| 溧水县| 合阳县| 金平| 太康县| 饶阳县| 阿坝县| 北票市| 西畴县| 汤阴县| 玛曲县| 东安县| 健康| 八宿县| 常德市| 永靖县|