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!


钦佩原作者,问题解决了,还认真的总结了发出来。