C#桌面程序监听显示器更改事件
来源:互联网 发布:可视化常用数据集 编辑:程序博客网 时间:2024/06/06 02:50
项目中使用外接HDMI显示器,显示通知内容。最开始处理的方式是使用Screen.AllScreens获取要显示通知的显示器,然后设置通知窗体的位置在显示器上,这样就能将通知显示到第二个显示器上。而主显示器仍然显示程序主界面内容。但在应用中遇到这样一个问题,当第二显示器关闭的时候,通知窗体,就自动的弹回到主显示器上显示了。然而,当第二显示器,再开的时候,也不会自动将通知窗体显示到第二显示器上。经过努力搜索,有人遇到过我这样的场景。文章中完美的解决了这个问题。为了让下次自己遇到类似情况,能搜索到,现将内容搬到这里。原文地址:https://stackoverflow.com/questions/5020559/screen-allscreen-is-not-giving-the-correct-monitor-count
Building on the previous reply by driis, this is how I handled it. I should note that the following code lives in my Program.cs file.
First the links to external resources and data structures:
[DllImport("user32")]
private static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lpRect, MonitorEnumProc callback, int dwData);
private delegate bool MonitorEnumProc(IntPtr hDesktop, IntPtr hdc, ref Rect pRect, int dwData);
[StructLayout(LayoutKind.Sequential)]
private struct Rect
{
public int left;
public int top;
public int right;
public int bottom;
}
Now create a simple object to contain monitor information:
public class MonitorInfo
{
public bool IsPrimary = false;
public Rectangle Bounds = new Rectangle();
}
And a container to hold these objects:
public static List<MonitorInfo> ActualScreens = new List<MonitorInfo>();
and a method to refresh the container:
public static void RefreshActualScreens()
{
ActualScreens.Clear();
MonitorEnumProc callback = (IntPtr hDesktop, IntPtr hdc, ref Rect prect, int d) =>
{
ActualScreens.Add(new MonitorInfo()
{
Bounds = new Rectangle()
{
X = prect.left,
Y = prect.top,
Width = prect.right - prect.left,
Height = prect.bottom - prect.top,
},
IsPrimary = (prect.left == 0) && (prect.top == 0),
});
return true;
};
EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, callback, 0);
}
Then later on a Form, If I wanted to detect that a display had been added or removed ...
private const int WM_DISPLAYCHANGE = 0x007e;
protected override void WndProc(ref Message message)
{
base.WndProc(ref message);
if (message.Msg == WM_DISPLAYCHANGE)
{
Program.RefreshActualScreens();
// do something really interesting here
}
}
Might be a few typos in there, but that is the basic idea. Good luck!
钦佩原作者,问题解决了,还认真的总结了发出来。
Building on the previous reply by driis, this is how I handled it. I should note that the following code lives in my Program.cs file.
First the links to external resources and data structures:
[DllImport("user32")]
private static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lpRect, MonitorEnumProc callback, int dwData);
private delegate bool MonitorEnumProc(IntPtr hDesktop, IntPtr hdc, ref Rect pRect, int dwData);
[StructLayout(LayoutKind.Sequential)]
private struct Rect
{
public int left;
public int top;
public int right;
public int bottom;
}
Now create a simple object to contain monitor information:
public class MonitorInfo
{
public bool IsPrimary = false;
public Rectangle Bounds = new Rectangle();
}
And a container to hold these objects:
public static List<MonitorInfo> ActualScreens = new List<MonitorInfo>();
and a method to refresh the container:
public static void RefreshActualScreens()
{
ActualScreens.Clear();
MonitorEnumProc callback = (IntPtr hDesktop, IntPtr hdc, ref Rect prect, int d) =>
{
ActualScreens.Add(new MonitorInfo()
{
Bounds = new Rectangle()
{
X = prect.left,
Y = prect.top,
Width = prect.right - prect.left,
Height = prect.bottom - prect.top,
},
IsPrimary = (prect.left == 0) && (prect.top == 0),
});
return true;
};
EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, callback, 0);
}
Then later on a Form, If I wanted to detect that a display had been added or removed ...
private const int WM_DISPLAYCHANGE = 0x007e;
protected override void WndProc(ref Message message)
{
base.WndProc(ref message);
if (message.Msg == WM_DISPLAYCHANGE)
{
Program.RefreshActualScreens();
// do something really interesting here
}
}
Might be a few typos in there, but that is the basic idea. Good luck!
钦佩原作者,问题解决了,还认真的总结了发出来。
阅读全文
0 0
- C#桌面程序监听显示器更改事件
- linux 更改桌面程序图标的方法
- 程序安装/卸载事件监听
- 封装添加事件监听程序
- c# 与桌面程序交互
- C#桌面软件程序重新启动
- c#程序更改项目名称
- C# Socket 监听程序:IllusiveServer
- 利用JavaFx开发RIA桌面应用-事件监听
- C#关闭显示器,打开显示器
- js输入框文本值更改监听事件
- C# DataGridView中ComboBox下拉更改事件
- wince 桌面更改及用户程序自启动
- c# WinForm 全局键盘事件怎么监听
- C# 监听串口数据并触发事件
- c# WinForm 全局键盘事件怎么监听
- C#全局监听Windows键盘事件
- 监听所有的Java程序的事件
- 对移动开发中蓝牙温湿度源码的部分分析
- java语言基础(53)——String 字面值对象和构造方法创建对象的区别
- ssh远程登录
- c++指针的指针和指针的引用
- cef BranchesAndBuilding
- C#桌面程序监听显示器更改事件
- Linux静态IP配置
- linux(五)关闭和重启命令
- Mac开机后键盘和触控板失灵
- indeed2017-4-22笔试题C-Network Configuration
- 【DFS】HDU
- uva 12493 欧拉函数 板子
- 2017 计蒜之道 初赛 第三场
- Linux配置jdk