编程语言
首页 > 编程语言> > C#-AppBar多显示器

C#-AppBar多显示器

作者:互联网

我制作了一个简单的appBar,在屏幕顶部只有一个标签,可以缩小桌面,但是我很难让它出现在第二台显示器上.我一直在搜索,但发现的所有内容都用于WPF.这些很可能是我犯错的地方,但是如果您需要查看其他代码,请告诉我.

private void InitializeComponent()
{
    this.ClientSize = new System.Drawing.Size(SystemInformation.WorkingArea.Width, -1);
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
    this.Name = "MainForm";
    this.Text = "AppBar";
    this.Closing += new System.ComponentModel.CancelEventHandler(this.OnClosing);
    this.Load += new System.EventHandler(this.OnLoad);
    this.BackColor = Color.Green;
    this.Padding = new Padding(0, 0, 0, 0);
    Label label1 = new Label();
    label1.Text = "TEXT";
    label1.Width = 270;
    label1.Margin = new Padding(0,0,0,0);
    label1.Padding = new Padding(0,0,0,0);
    label1.TextAlign = ContentAlignment.MiddleCenter;
    label1.ForeColor = Color.White;
    label1.Font = new Font(FontFamily.GenericSansSerif, 12,FontStyle.Regular);
    label1.Location = new Point((SystemInformation.WorkingArea.Width - 270) / 2, 0);
    this.Controls.Add(label1);
}

private void ABSetPos()
{
    APPBARDATA abd = new APPBARDATA();
    abd.cbSize = Marshal.SizeOf(abd);
    abd.hWnd = this.Handle;
    abd.uEdge = (int)ABEdge.ABE_TOP;

    if (abd.uEdge == (int)ABEdge.ABE_LEFT || abd.uEdge == (int)ABEdge.ABE_RIGHT)
    {
        abd.rc.top = 0;
        abd.rc.bottom = SystemInformation.PrimaryMonitorSize.Height;
        if (abd.uEdge == (int)ABEdge.ABE_LEFT)
        {
            abd.rc.left = 0;
            abd.rc.right = Size.Width;
        }
        else
        {
            abd.rc.right = SystemInformation.PrimaryMonitorSize.Width;
            abd.rc.left = abd.rc.right - Size.Width;
        }

    }
    else
    {
        abd.rc.left = 0;
        abd.rc.right = SystemInformation.PrimaryMonitorSize.Width;
        if (abd.uEdge == (int)ABEdge.ABE_TOP)
        {
            abd.rc.top = 0;
            abd.rc.bottom = Size.Height;
        }
        else
        {
            abd.rc.bottom = SystemInformation.PrimaryMonitorSize.Height;
            abd.rc.top = abd.rc.bottom - Size.Height;
        }
    }

解决方法:

您可以通过迭代Screen.AllScreens数组来使用其他屏幕.例如,以下是获取第一个非主监视器的方式:

Screen nonPrimaryScreen = Screen.AllScreens.FirstOrDefault(x => !x.Primary);

然后,在使用SystemInformation.WorkingArea(始终使用主屏幕)的任何地方,都可以使用:

nonPrimaryScreen.WorkingArea

当然,假设nonPrimaryScreen!= null….

编辑:

与其重复代码,不如使它们通用:

public static Rectangle GetWorkingArea() {
    if (UseWantsItOnPrimaryScreen) {
        return SystemInformation.WorkingArea;
    }
    else {
        return Screen.AllScreens.FirstOrDefault(x => !x.Primary).WorkingArea;
    }
}

标签:multiple-monitors,appbar,c
来源: https://codeday.me/bug/20191031/1974692.html