跳到主要内容

github/tai

公开

Tai · Windows 软件与网站使用时长统计。上游 Planshit/Tai 的源码镜像,非灵栈原创;MIT 许可。来源:https://github.com/Planshit/Tai 。本次为手动同步,暂不提供安装包。

浏览文件

WindowManager.cs

57 行 · 1.6 KB历史下载
1using Core.Librarys;
2using Core.Models.AppObserver;
3using Core.Servicers.Interfaces;
4using System;
5using System.Collections.Generic;
6using System.Linq;
7using System.Text;
8using System.Threading.Tasks;
9
10namespace Core.Servicers.Instances
11{
12 public class WindowManager : IWindowManager
13 {
14
15 public WindowInfo GetWindowInfo(IntPtr handle_)
16 {
17 try
18 {
19 string title = GetWindowTitle(handle_);
20 var rect = Win32API.GetWindowRect(handle_);
21 string className = Win32API.GetWindowClassName(handle_);
22 int width = rect.Width;
23 int height = rect.Height;
24 int x = rect.Left;
25 int y = rect.Top;
26
27 return new WindowInfo(className, title, handle_, width, height, x, y);
28 }
29 catch (Exception e)
30 {
31 return WindowInfo.Empty;
32 }
33
34 }
35
36 /// <summary>
37 /// 获取窗口标题
38 /// </summary>
39 /// <param name="handle_"></param>
40 /// <returns></returns>
41 private string GetWindowTitle(IntPtr handle_)
42 {
43 try
44 {
45 int titleCapacity = Win32API.GetWindowTextLength(handle_) * 2;
46 StringBuilder stringBuilder = new StringBuilder(titleCapacity);
47 Win32API.GetWindowText(handle_, stringBuilder, stringBuilder.Capacity);
48 string title = stringBuilder.ToString();
49 return stringBuilder.ToString();
50 }
51 catch (Exception e)
52 {
53 return string.Empty;
54 }
55 }
56 }
57}