目录写在前面参考代码调用结果写在前面在做录屏或截屏操作时,需要获取当前正在运行中的桌面程序句柄,在网上查找资源的的时候,发现了一个工具类还不错,这边做个验证记录。
参考代码 public class WindowApi{//寻找目标进程窗口[DllImport("USER32.DLL")]public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);//设置进程窗口到最前[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///
private delegate bool WNDENUMPROC(IntPtr hWnd, int lParam);
//用来遍历所有窗口[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);
//获取窗口类名[DllImport("user32.dll")]private static extern int GetClassNameW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder lpString, int nMaxCount);
//自定义一个结构,用来保存句柄信息public struct WindowInfo{public IntPtr hWnd;public string szWindowName;public string szClassName;}
public static WindowInfo[] GetAllDesktopWindows(){//用来保存窗口对象 列表List
//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);
///
调用示意:
var programList = WindowApi.GetAllDesktopWindows().Where(x => !string.IsNullOrEmpty(x.szWindowName)).ToList();
listBoxWindows.DataSource = programList.Select(x => x.szWindowName).ToList();以上就是基于C#实现获取Windows所有窗口句柄的详细内容,更多关于C#获取Windows窗口句柄的资料请关注脚本之家其它相关文章!
您可能感兴趣的文章:C#获取进程的主窗口句柄的实现方法C#实现获取鼠标句柄的方法C# Windows API应用之基于GetDesktopWindow获得桌面所有窗口句柄的方法详细解析C#多线程同步事件及等待句柄
