查找显示器订购

时间:2019-01-09 01:56:16

标签: c# multiple-monitors

如图所示。我可以通过Screen库找到主监视器。 但是一旦我也想找到显示器的顺序。我要制作的就像是屏幕保护程序。

Display Options

首先,第一个监视器(即主监视器)将在左侧,第二个监视器将在右侧。但是,如果我更改顺序2-> 1 ..主监视器将在右侧。而我所做的表格将在其他意想不到的地方使用。无论如何,有没有检测到这种顺序变化?现在我的代码是这个。

private void ResizeScreen()
    {
        // 전체 화면으로 사이즈 설정
        int form_width = 0;
        int form_height = 0;
        int form_x = 0;
        int form_y = 0;
        int sub_screen_width = 0;
        int sub_screen_height = 0;
        foreach (Screen screen in Screen.AllScreens)
        {
            form_width += screen.Bounds.Width;
            NLog.LogManager.GetCurrentClassLogger().Trace("form_width : " + form_width);
            form_height += screen.Bounds.Height;
            NLog.LogManager.GetCurrentClassLogger().Trace("form_height : " + form_height);
            if (form_x > screen.Bounds.X)
                form_x = screen.Bounds.X;
            if (form_y > screen.Bounds.Y)
                form_y = screen.Bounds.Y;

            //form_x = Math.Abs(form_x);
            NLog.LogManager.GetCurrentClassLogger().Trace("form_x : " + screen.Bounds.X);
            NLog.LogManager.GetCurrentClassLogger().Trace("form_y : " + screen.Bounds.Y);

            if (screen.Bounds.X < 0)
                sub_screen_width += screen.Bounds.Width;
            if (screen.Bounds.Y < 0)
                sub_screen_height += screen.Bounds.Height;
        }
        this.Width = form_width;
        this.Height = form_height;
        this.CenterToScreen();
        this.Location = new Point(form_x, form_y);

        if (this.InvokeRequired)
        {
            this.Invoke(new MethodInvoker(delegate
            {
                NLog.LogManager.GetCurrentClassLogger().Trace("PrimaryScreen : " + Screen.PrimaryScreen.Bounds.Width + ", " + Screen.PrimaryScreen.Bounds.Height);
                mainPanel.Location = new Point((Screen.PrimaryScreen.Bounds.Width - mainPanel.Bounds.Width) / 2 + Screen.PrimaryScreen.Bounds.X, (Screen.PrimaryScreen.Bounds.Height - mainPanel.Bounds.Height) / 2 + Screen.PrimaryScreen.Bounds.Y);
                mainPanel.Visible = true;
                NLog.LogManager.GetCurrentClassLogger().Trace("MainPanel : " + (Screen.PrimaryScreen.Bounds.Width - mainPanel.Bounds.Width) / 2 + Screen.PrimaryScreen.Bounds.X + ", " + (Screen.PrimaryScreen.Bounds.Height - mainPanel.Bounds.Height) / 2 + Screen.PrimaryScreen.Bounds.Y);
                approvalRequestPanel.Location = new Point((Screen.PrimaryScreen.Bounds.Width - approvalRequestPanel.Bounds.Width) / 2 + Screen.PrimaryScreen.Bounds.X, (Screen.PrimaryScreen.Bounds.Height - approvalRequestPanel.Bounds.Height) / 2 + Screen.PrimaryScreen.Bounds.Y);
                approvalRequestPanel.Visible = false;
                NLog.LogManager.GetCurrentClassLogger().Trace("approvalRequestPanel : " + (Screen.PrimaryScreen.Bounds.Width - approvalRequestPanel.Bounds.Width) / 2 + Screen.PrimaryScreen.Bounds.X + ", " + (Screen.PrimaryScreen.Bounds.Height - approvalRequestPanel.Bounds.Height) / 2 + Screen.PrimaryScreen.Bounds.Y);
                meetingTextPanel.Location = new Point((Screen.PrimaryScreen.Bounds.Width - meetingTextPanel.Bounds.Width) / 2 + Screen.PrimaryScreen.Bounds.X, (Screen.PrimaryScreen.Bounds.Height - meetingTextPanel.Bounds.Height) / 2 + Screen.PrimaryScreen.Bounds.Y);
                meetingTextPanel.Visible = false;
                NLog.LogManager.GetCurrentClassLogger().Trace("meetingTextPanel : " + (Screen.PrimaryScreen.Bounds.Width - meetingTextPanel.Bounds.Width) / 2 + Screen.PrimaryScreen.Bounds.X + ", " + (Screen.PrimaryScreen.Bounds.Height - meetingTextPanel.Bounds.Height) / 2 + Screen.PrimaryScreen.Bounds.Y);
                approvalpopuppanel.Location = new Point((Screen.PrimaryScreen.Bounds.Width - mainPanel.Bounds.Width) / 2 + Screen.PrimaryScreen.Bounds.X, (Screen.PrimaryScreen.Bounds.Height - mainPanel.Bounds.Height) / 2 + Screen.PrimaryScreen.Bounds.Y);
                approvalpopuppanel.Visible = false;
                meetingpopuppanel.Location = new Point((Screen.PrimaryScreen.Bounds.Width - mainPanel.Bounds.Width) / 2 + Screen.PrimaryScreen.Bounds.X, (Screen.PrimaryScreen.Bounds.Height - mainPanel.Bounds.Height) / 2 + Screen.PrimaryScreen.Bounds.Y);
                meetingpopuppanel.Visible = false;
                pc_off_panel.Location = new Point((Screen.PrimaryScreen.Bounds.Width - mainPanel.Bounds.Width) / 2 + Screen.PrimaryScreen.Bounds.X, (Screen.PrimaryScreen.Bounds.Height - mainPanel.Bounds.Height) / 2 + Screen.PrimaryScreen.Bounds.Y);
                pc_off_panel.Visible = false;
            }));
        }
        else
        {
            NLog.LogManager.GetCurrentClassLogger().Trace("PrimaryScreen2 : " + Screen.PrimaryScreen.Bounds.Width + ", " + Screen.PrimaryScreen.Bounds.Height);
            mainPanel.Location = new Point((Screen.PrimaryScreen.Bounds.Width - mainPanel.Bounds.Width) / 2 + Screen.PrimaryScreen.Bounds.X, (Screen.PrimaryScreen.Bounds.Height - mainPanel.Bounds.Height) / 2 + Screen.PrimaryScreen.Bounds.Y);
            mainPanel.Visible = true;
            NLog.LogManager.GetCurrentClassLogger().Trace("MainPanel2 : " + (Screen.PrimaryScreen.Bounds.Width - mainPanel.Bounds.Width) / 2 + Screen.PrimaryScreen.Bounds.X + ", " + (Screen.PrimaryScreen.Bounds.Height - mainPanel.Bounds.Height) / 2 + Screen.PrimaryScreen.Bounds.Y);
            approvalRequestPanel.Location = new Point((Screen.PrimaryScreen.Bounds.Width - approvalRequestPanel.Bounds.Width) / 2 + Screen.PrimaryScreen.Bounds.X, (Screen.PrimaryScreen.Bounds.Height - approvalRequestPanel.Bounds.Height) / 2 + Screen.PrimaryScreen.Bounds.Y);
            approvalRequestPanel.Visible = false;
            NLog.LogManager.GetCurrentClassLogger().Trace("approvalRequestPanel2 : " + (Screen.PrimaryScreen.Bounds.Width - approvalRequestPanel.Bounds.Width) / 2 + Screen.PrimaryScreen.Bounds.X + ", " + (Screen.PrimaryScreen.Bounds.Height - approvalRequestPanel.Bounds.Height) / 2 + Screen.PrimaryScreen.Bounds.Y);
            meetingTextPanel.Location = new Point((Screen.PrimaryScreen.Bounds.Width - meetingTextPanel.Bounds.Width) / 2 + Screen.PrimaryScreen.Bounds.X, (Screen.PrimaryScreen.Bounds.Height - meetingTextPanel.Bounds.Height) / 2 + Screen.PrimaryScreen.Bounds.Y);
            meetingTextPanel.Visible = false;
            NLog.LogManager.GetCurrentClassLogger().Trace("meetingTextPanel2 : " + (Screen.PrimaryScreen.Bounds.Width - meetingTextPanel.Bounds.Width) / 2 + Screen.PrimaryScreen.Bounds.X + ", " + (Screen.PrimaryScreen.Bounds.Height - meetingTextPanel.Bounds.Height) / 2 + Screen.PrimaryScreen.Bounds.Y);
            approvalpopuppanel.Location = new Point((Screen.PrimaryScreen.Bounds.Width - mainPanel.Bounds.Width) / 2 + Screen.PrimaryScreen.Bounds.X, (Screen.PrimaryScreen.Bounds.Height - mainPanel.Bounds.Height) / 2 + Screen.PrimaryScreen.Bounds.Y);
            approvalpopuppanel.Visible = false;
            meetingpopuppanel.Location = new Point((Screen.PrimaryScreen.Bounds.Width - mainPanel.Bounds.Width) / 2 + Screen.PrimaryScreen.Bounds.X, (Screen.PrimaryScreen.Bounds.Height - mainPanel.Bounds.Height) / 2 + Screen.PrimaryScreen.Bounds.Y);
            meetingpopuppanel.Visible = false;
            pc_off_panel.Location = new Point((Screen.PrimaryScreen.Bounds.Width - mainPanel.Bounds.Width) / 2 + Screen.PrimaryScreen.Bounds.X, (Screen.PrimaryScreen.Bounds.Height - mainPanel.Bounds.Height) / 2 + Screen.PrimaryScreen.Bounds.Y);
            pc_off_panel.Visible = false;
        }
    }

0 个答案:

没有答案