C# winForm 切换屏幕小工具

IDE:VS2022
.NET 5.0

项目文件:
链接:https://pan.baidu.com/s/1jBeXLwWPBvxWk5mrrLLYDw?pwd=9966
提取码:9966

主要代码来源:
https://www.cnblogs.com/zzr-stdio/p/12093159.html

使用场景:方便那些不会使用 win+p 的老年人使用

image

image

C# 代码:
Form1.cs

using System;
using System.Diagnostics;
using System.Windows.Forms;
namespace switchMonitor
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// 显示当前接入屏幕数量
Text = "当前切入显示器数:" + Screen.AllScreens.Length.ToString();
//label2.Text = Screen.AllScreens.Length.ToString();
}
// 显示器切换参数
public enum DisplaySwitchEnum
{
Default,
Internal, // 仅第一屏幕
Clone, // 复制屏幕
Extend, // 扩展屏幕
External // 仅第二屏幕
}
public static void DisplaySwitch(DisplaySwitchEnum displaySwitch)
{
Process process = new();
#pragma warning disable CS8600 // 将 null 字面量或可能为 null 的值转换为非 null 类型。
string str = Environment.GetEnvironmentVariable("windir");//获取系统目录
#pragma warning restore CS8600 // 将 null 字面量或可能为 null 的值转换为非 null 类型。
string dir = "System32";
if (!Environment.Is64BitProcess)
{
dir = "SysNative";//非64位进程的使用这个目录
}
process.StartInfo.WorkingDirectory = System.IO.Path.Combine(str, dir);
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.CreateNoWindow = true;
process.Start();
string cmd = string.Empty;
switch (displaySwitch)
{
case DisplaySwitchEnum.Clone:
cmd = "displayswitch.exe /clone"; // 复制屏幕
break;
case DisplaySwitchEnum.Extend:
cmd = "displayswitch.exe /extend"; // 扩展屏幕
break;
case DisplaySwitchEnum.External:
cmd = "displayswitch.exe /external"; // 仅第二屏幕
break;
case DisplaySwitchEnum.Internal:
cmd = "displayswitch.exe /internal"; // 仅第一屏幕
break;
}
process.StandardInput.WriteLine(cmd);
process.Close();
}
// 按钮 仅第一屏幕
private void Button1_Click(object sender, EventArgs e)
{
DisplaySwitch(DisplaySwitchEnum.Internal);
}
// 按钮 仅第二屏幕
private void Button2_Click(object sender, EventArgs e)
{
DisplaySwitch(DisplaySwitchEnum.External);
}
// 按钮 复制屏幕
private void Button3_Click(object sender, EventArgs e)
{
DisplaySwitch(DisplaySwitchEnum.Clone);
}
// 按钮 扩展屏幕
private void Button4_Click(object sender, EventArgs e)
{
DisplaySwitch(DisplaySwitchEnum.Extend);
}
// 托盘图标
private void NotifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
// 还原窗体显示
WindowState = FormWindowState.Normal;
// 激活窗体并给予它焦点
Activate();
// 任务栏区显示图标
ShowInTaskbar = true;
// 托盘区图标隐藏
NotifyIcon1.Visible = false;
}
}
// 窗体尺寸改变: 判断是否最小化,然后显示托盘
private void Form1_StyleChanged(object sender, EventArgs e)
{
// 判断是否选择的是最小化按钮
if (WindowState == FormWindowState.Minimized)
{
// 隐藏任务栏区图标
//ShowInTaskbar = false;
// 图标显示在托盘区
NotifyIcon1.Visible = true;
}
}
// 窗体关闭时: 退出
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// 关闭所有的线程
//Dispose();
//Close();
WindowState = FormWindowState.Minimized;
e.Cancel = true;
// 隐藏任务栏区图标
ShowInTaskbar = false;
// 图标显示在托盘区
NotifyIcon1.Visible = true;
}
// 托盘菜单
private void 仅第一屏幕ToolStripMenuItem_Click(object sender, EventArgs e)
{
DisplaySwitch(DisplaySwitchEnum.Internal);
}
private void 仅第二屏幕ToolStripMenuItem_Click(object sender, EventArgs e)
{
DisplaySwitch(DisplaySwitchEnum.External);
}
private void 复制屏幕ToolStripMenuItem_Click(object sender, EventArgs e)
{
DisplaySwitch(DisplaySwitchEnum.Clone);
}
private void 扩展屏幕ToolStripMenuItem_Click(object sender, EventArgs e)
{
DisplaySwitch(DisplaySwitchEnum.Extend);
}
private void 显示程序ToolStripMenuItem_Click(object sender, EventArgs e)
{
WindowState = FormWindowState.Normal;
}
private void 退出程序ToolStripMenuItem_Click(object sender, EventArgs e)
{
// 关闭所有的线程
Dispose();
Close();
}
}
}
posted @   履霜.1989  阅读(777)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示