冰楓論壇

 找回密碼
 立即註冊
搜索
查看: 3627|回覆: 0
打印 上一主題 下一主題

[心得] [C#] 獲取視窗資訊

[複製鏈接]

1114

主題

0

好友

1042

積分

高級贊助會員

Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20

UID
390967
帖子
2182
主題
1114
精華
0
積分
1042
楓幣
26484
威望
578
存款
10100
贊助金額
1800
推廣
0
GP
3799
閱讀權限
150
在線時間
272 小時
註冊時間
2023-5-18
最後登入
2025-12-25

2023年紀念勳章 太陽勳章 神手勳章 高級客戶 VIP會員 私服達人 懶人勳章 幼兒勳章 音樂勳章 屁孩勳章 貢獻王 性別(女) 性別(男) 積分勳章 發帖達人 2023端午節紀念勳章 富豪勳章 富可敵國 2023中秋節紀念勳章 解說達人 論壇粉絲 2023聖誕節紀念勳章 2024年紀念勳章 2024端午節紀念勳章 2024中秋節紀念勳章 2024聖誕節紀念勳章 2025年紀念勳章 2025端午節紀念勳章 2025中秋節紀念勳章 2025聖誕節紀念勳章

跳轉到指定樓層
1
發表於 2023-6-2 17:50:40 |只看該作者 |倒序瀏覽
  1. public static WindowInfo GetWindowDetail(IntPtr hWnd)
  2. {
  3.     // 視窗類別名
  4.     var lpString = new StringBuilder(512);
  5.     User32.GetClassName(hWnd, lpString, lpString.Capacity);
  6.     var className = lpString.ToString();

  7.     // 視窗標題
  8.     var lptrString = new StringBuilder(512);
  9.     User32.GetWindowText(hWnd, lptrString, lptrString.Capacity);
  10.     var title = lptrString.ToString().Trim();

  11.     // 視窗可視性
  12.     var isVisible = User32.IsWindowVisible(hWnd);

  13.     // 視窗位置與大小
  14.     User32.LPRECT rect = default;
  15.     User32.GetWindowRect(hWnd, ref rect);
  16.     var bounds = new Rect(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);

  17.     // 視窗所在進程資訊
  18.     var processInfo = ProcessInfosByHwnd.GetInfo(hWnd);
  19.     return new WindowInfo(hWnd, className, title, isVisible, bounds, processInfo);
  20. }
複製代碼
User32函數
  1. public static class User32
  2. {
  3.     [DllImport("user32.dll", SetLastError = true)]
  4.     public static extern IntPtr GetWindow(IntPtr hwnd, uint windowType);

  5.     public delegate bool WndEnumProc(IntPtr hWnd, int lParam);
  6.     [DllImport("user32")]
  7.     public static extern bool Enumwindows(WndEnumProc lpEnumFunc, int lParam);

  8.     [DllImport("user32")]
  9.     public static extern IntPtr GetParent(IntPtr hWnd);

  10.     [DllImport("user32")]
  11.     public static extern bool IsWindowVisible(IntPtr hWnd);

  12.     [DllImport("user32")]
  13.     public static extern int GetWindowText(IntPtr hWnd, StringBuilder lptrString, int nMaxCount);

  14.     [DllImport("user32")]
  15.     public static extern int GetClassName(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

  16.     [DllImport("user32")]
  17.     public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);

  18.     [DllImport("user32")]
  19.     public static extern bool GetWindowRect(IntPtr hWnd, ref LPRECT rect);


  20.     [StructLayout(LayoutKind.Sequential)]
  21.     public readonly struct LPRECT
  22.     {
  23.         public readonly int Left;
  24.         public readonly int Top;
  25.         public readonly int Right;
  26.         public readonly int Bottom;
  27.     }
  28. }
複製代碼
收藏收藏0 推0 噓0

分享這篇文章



把本文推薦給朋友或其他網站上,每次被點擊增加您在本站積分: 1鑰匙
複製連結並發給好友,以賺取推廣點數
簡單兩步驟,註冊、分享網址,即可獲得獎勵! 一起推廣文章換商品、賺$$
高級模式
B Color Image Link Quote Code Smilies |上傳

廣告刊登意見回饋關於我們管群招募本站規範DMCA隱私權政策詐騙防範及資料調閱

Copyright © 2011-2025 冰楓論壇, All rights reserved

免責聲明:本網站是以即時上載留言的方式運作,本站對所有留言的真實性、完整性及立場等,不負任何法律責任。

而一切留言之言論只代表留言者個人意見,並非本網站之立場,用戶不應信賴內容,並應自行判斷內容之真實性。

小黑屋|手機版|冰楓論壇

GMT+8, 2025-12-26 07:10

回頂部