.NET批量操作窗口样式
1. 背景
我们在开发过程中,可能会遇到需要批量控制程序中窗体的大小或其它一些操作, 这些窗体有可能是属于程序本身的,也许是其它程序的窗口。本文就是基于此的一篇关于如何批量操作窗口样式的,我们主要是通过批量最小化窗体来阐述。
2. 窗体类别
- 窗口属于程序自己的
- 窗口不属于程序自己的
下面我们分别针对这两种情况来找出相应的解决方法
2.1 窗口属于程序自己的
具体的来说就是,将要操作的这些窗体或窗口都是由当前程序创建的。
利用Form的WindowState属性, WindowState属性的值是一个FormWindowState枚举值。
- Maximized 窗口最大化状态
- Minimized 窗口最小化状态
- Normal 窗口正常状态
我们可以修改窗体的WindowState属性来变换窗口的状态。 下面是一个简单的例子。
using System;
using System.Windows.Forms; public class Demo
{
static void Main()
{
// Creat 3 Forms
for (int i = 1; i <= 3; i++)
{
CreateForm("Form " + i);
} Console.WriteLine("Do you want to minizie all open windows? (Y/N)");
string minWindows = Console.ReadLine();
if (minWindows == "Y")
{
// 最小化次程序打开的窗口
foreach (Form frm in Application.OpenForms)
{
// Use property WindowState to minizie all opened forms.
frm.WindowState = FormWindowState.Minimized;
}
} Console.WriteLine("Press any key to exit.");
Console.ReadKey();
} static void CreateForm(string title)
{
Form aForm = new Form();
aForm.Text = title;
aForm.Show();
}
}
其实如果窗体时属于当前程序创建的话,建议可以使用MDI窗体来控制,所有子窗体会在父窗体最小化时也最小化。
2.2 窗体属于其他程序的
具体来讲,将要操作的这些窗口/窗体不光是由当前程序创建的,还有系统中其他程序或系统创建。对于自己创建的系统我们可以通过WindowState属性来操作,但外部程序创建的窗体则不能。我们可以在我们的.NET程序中, 通过P/Invoke调用Win32的API函数来实现。
最重要的一个函数就是ShowWindow
[DllImport("User32")]
private static extern int ShowWindow(int hwnd, int nCmdShow);
下面这段代码会最小化目前系统中所有打开的窗体。
using System;
using System.Runtime.InteropServices; namespace PInvokeApps
{
/// <summary>
/// EnumDesktopWindows Demo - shows the caption of all desktop windows,
/// and minimize all these windows
/// Based on the code snippet: http://pinvoke.net/default.aspx/user32.EnumDesktopWindows.
/// </summary>
public class User32
{
/// <summary>
/// filter function
/// </summary>
public delegate bool EnumDelegate(IntPtr hWnd, int lParam); /// <summary>
/// check if windows visible
/// </summary>
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsWindowVisible(IntPtr hWnd); /// <summary>
/// return windows text
/// </summary>
[DllImport("user32.dll", EntryPoint = "GetWindowText",
ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpWindowText, int nMaxCount); /// <summary>
/// enumarator on all desktop windows
/// </summary>
[DllImport("user32.dll", EntryPoint = "EnumDesktopWindows",
ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumDelegate lpEnumCallbackFunction, IntPtr lParam); /// <summary>
/// Sets the specified window's show state
/// </summary>
[DllImport("user32.dll", EntryPoint = "ShowWindow",
ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); static void Main()
{
var collection = new List<string>(); Console.WriteLine("Do you want to minizie all open windows? (Y/N)");
string minWindows = Console.ReadLine();
bool needsMinimize = !string.IsNullOrEmpty(minWindows) && minWindows == "Y"; User32.EnumDelegate filter = delegate(IntPtr hWnd, int lParam)
{
StringBuilder strbTitle = new StringBuilder(255);
int nLength = User32.GetWindowText(hWnd, strbTitle, strbTitle.Capacity + 1);
string strTitle = strbTitle.ToString(); if (User32.IsWindowVisible(hWnd) && string.IsNullOrEmpty(strTitle) == false)
{
collection.Add(strTitle); // Minimize visible windows if needs
if (needsMinimize)
User32.ShowWindow(hWnd, ShowWindowCommands.Minimized.ToInt());
} return true;
}; if (User32.EnumDesktopWindows(IntPtr.Zero, filter, IntPtr.Zero))
{
foreach (var item in collection)
{
Console.WriteLine(item);
}
} Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
} /// <summary>
/// Here we can also directly define some const variable in User32 class.
/// Example:
/// private const int WmSize = 5;
/// private const int SizeRestored = 0;
/// private const int SizeMinimized = 1;
/// private const int SizeMaximized = 2;
/// private const int SizeShow = 3;
/// private const int SizeHide = 4;
/// </summary>
internal enum ShowWindowCommands : int
{
Hide = 0,
Normal = 1,
Minimized = 2,
Maximized = 3,
} public static class EnumExtensions
{
public static int ToInt(this Enum enumValue)
{
return (int)((object)enumValue);
}
}
}
3. 延伸:最小化特定程序的窗口
假定我们有一个名叫 "notepad"的程序,我们还是可以利用P/Invoke来实现最小化其窗口。这个时候的难点是如何得到窗口句柄hWnd,我们可以通过下面这行语句得到窗口句柄:
Process[] p = Process.GetProcessesByName("notepad");
hWnd = (int)p[0].MainWindowHandle;
然后只要调用上面程序中的ShowWindow方法,
User32.ShowWindow(hWnd, ShowWindowCommands.Minimized.ToInt())
4. 参考:
- Controlling Window State Of Other Applications using C#
- EnumDesktopWindows (user32)
- ShowWindow function
- how to notify my application when show desktop/minimize all/ all windows minimized?
- Get window state of another process
.NET批量操作窗口样式的更多相关文章
- DotNetBar 第1课,设置整体窗口样式
1. 先引用 DevComponents.DotNetBar2.dll 2. 窗口继承 Office2007Form public partial class Form1 : Office2007Fo ...
- MFC学习 修改窗口样式
1. 在PreCreateWindow中可用CREATESTRUCT cs, cs.lpszName修改窗口标题, cs.lpszClass = AfxRegisterWndClass 修改图标与样式 ...
- Windows窗口样式速查参考,Delphi窗口控件的风格都有它们来决定(附Delphi何时用到它们,并举例说明)good
/* 窗口样式参考列表(都是GetWindowLong的GWL_STYLE风格,都是TCreateParams.Sytle的一部分),详细列表如下:https://msdn.microsoft.com ...
- DotNetBar怎样控制窗口样式
DotNetBar怎样控制窗口样式 老帅 在C#中使用控件DevComponents.DotNetBar时,怎样创建一个美丽的窗口.并控制窗口样式呢? 1.新建一个DotNetBar窗口 ...
- DotNetBar的窗口样式丢失
DotNetBar的窗口样式丢失 C# 调用DotNetBar很方便,将DevComponents.DotNetBar2.dll和DevComponents.DotNetBar.Design.dll放 ...
- android activity 窗口 样式
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 将 活动 设置成 窗口样式, 可以设置 主题 为 对话框, 或者 半透明. 安卓:主题= ...
- WPF 使用 WindowChrome,在自定义窗口标题栏的同时最大程度保留原生窗口样式(类似 UWP/Chrome)
WPF 自定义窗口样式有多种方式,不过基本核心实现都是在修改 Win32 窗口样式.然而,Windows 上的应用就应该有 Windows 应用的样子嘛,在保证自定义的同时也能与其他窗口样式保持一致当 ...
- win32窗口样式GWL_EXSTYLE
Private Const GWL_STYLE = (-16) '窗口样式 '窗口风格Private Const WS_CAPTION = &HC00000 ...
- C语言Windows程序开发—Windows窗口样式与常用控件样式【第04天】
(一)Windows窗口(MDICLIENT)样式介绍 /* Windows窗口样式 */ WS_BORDER //带有边框的窗口 WS_CAPTION //带有标题栏的窗口 WS_CHILD //子 ...
随机推荐
- Java - Stack源码解析
Java提高篇(三一)-----Stack 在Java中Stack类表示后进先出(LIFO)的对象堆栈.栈是一种非常常见的数据结构,它采用典型的先进后出的操作方式完成的.每一个栈都包含一个栈顶,每次出 ...
- Android - AsyncTask你知道多少?
http://www.cnblogs.com/qlky/p/5658070.html 为什么asyncTask最好在主线程初始化?在子线程怎么办? AsyncTask四个方法的执行顺序? mWorke ...
- idea 快捷键总结
IntelliJ Idea 常用快捷键列表 Ctrl+Shift + Enter,语句完成“!”,否定完成,输入表达式时按 “!”键Ctrl+E,最近的文件Ctrl+Shift+E,最近更改的文件Sh ...
- SD从零开始45-46
[原创] SD从零开始45 运输流程的控制 运输业务场景的例子Examples 一个公司可使用不同的运输业务场景,通过不同的处理类型或者运输方式来刻画: 要模型化这些不同的装运,你可以在配置中定义装运 ...
- FI配置步骤清单.枫
1. 说明 本版本的FI模块配置内容非常少,主要应用的是系统默认的配置参数,但能完成基本的总帐.应收.应付操作. 配置内容包含以下几部分: 1. 基本的组织结构定义及分配,以及公司代码的全局性 ...
- 安装ArcGIS Enterprise WebGIS (Portal ArcGIS Server DataStore ) 系统后如何应对网络环境的配置修改
客户往往在部署完ArcGIS WebGIS系统后,由于需要满足业务或者网络管理的要求,需要修改系统的网络环境的配置,下文将从常见的几个场景来讲述如何去应对这些变动. 1.网络IP地址变动 由于在部署W ...
- 活字格Web应用平台学习笔记1 - 下载安装,ready go
今年有一个很重要的职业目标,就是好好学习活字格这个神器,争取在这两个月拿到活字格初级工程师的认证证书.给自己加个油,今天开始好好学习,好好做笔记. 第一步,下载安装 先去活字格官网:http://ww ...
- android踩坑记录之view.setVisiblity()
问题 在某次做悬浮侧边栏的时候,遇到了一个问题:我用windowManager创建的悬浮侧边栏.点击中心view展开菜单,再次点击则隐藏菜单,如此简单的一个需求,却遇到了奇怪的问题,我没有对view的 ...
- Unity Profiler Memory
当游戏出现闪退时很大概率是内存出现了问题,查找下代码中是否报错导致一直申请内存,还是申请的内存没有释放掉,比如图集等. 比如开着Profiler,一直开关界面看界面用到的图集是否被释放掉. 点击Mem ...
- [iOS] 列表滑动展开隐藏头部HeaderView
平常遇到大多数的带有列表的应用都会遇到这个场景:在列表顶端有一个Header,当向上滑动列表时,压缩header,向下滑动列表到头时,展开header.这种样式在例如微博,twitter这些展示动态的 ...