4

デスクトップを縮小する画面の上部にラベルを付けるだけのシンプルなappBarを作成しましたが、2番目のモニターに表示するのに問題があります。私は周りを検索してきましたが、私が見つけたものはすべて 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;
        }
    }
4

4 に答える 4

2

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;
    }
}
于 2013-02-05T01:37:25.883 に答える
1
private Screen GetScreenObject(String Name)
{
    logger.Info(GlobalModulename + "@ ScreenList::looking for screen:"+Name);
    if ((Name == "Primary"))
    {
        bool ExpectedParameter = true;
        foreach (var screen in Screen.AllScreens)
        {
            // For each screen, add the screen properties to a list box.
            logger.Info(GlobalModulename + "@ ScreenList::("+screen.DeviceName.ToString()+")Primary Screen: " + screen.Primary.ToString());
            if (screen.Primary==ExpectedParameter)
            {
                return screen;
            }
        }
    }
    if ((Name == "Secondary"))
    {
        bool ExpectedParameter = false;
        foreach (var screen in Screen.AllScreens)
        {
            // For each screen, add the screen properties to a list box.
            logger.Info(GlobalModulename + "@ ScreenList::(" + screen.DeviceName.ToString() + ")Primary Screen: " + screen.Primary.ToString());
            if (screen.Primary == ExpectedParameter)
            {
                return screen;
            }
        }
    }

    // konkretni jmeno obrazovky tak jak je to v systemu
    try
    {
        foreach (var screen in Screen.AllScreens)
        {
            // For each screen, add the screen properties to a list box.
            logger.Info("UEFA_Core @ ScreenList::Device Name: " + screen.DeviceName);
            logger.Info("UEFA_Core @ ScreenList::Bounds: " + screen.Bounds.ToString());
            logger.Info("UEFA_Core @ ScreenList::Type: " + screen.GetType().ToString());
            logger.Info("UEFA_Core @ ScreenList::Working Area: " + screen.WorkingArea.ToString());
            logger.Info("UEFA_Core @ ScreenList::Primary Screen: " + screen.Primary.ToString());
            if (screen.DeviceName == Name) return screen;
        }

    }
    catch { }

    // podobne jmeno obrazovky tak jak je to v systemu
    try
    {
        foreach (var screen in Screen.AllScreens)
        {
            // For each screen, add the screen properties to a list box.
            logger.Info("UEFA_Core @ ScreenList::Device Name: " + screen.DeviceName);
            logger.Info("UEFA_Core @ ScreenList::Bounds: " + screen.Bounds.ToString());
            logger.Info("UEFA_Core @ ScreenList::Type: " + screen.GetType().ToString());
            logger.Info("UEFA_Core @ ScreenList::Working Area: " + screen.WorkingArea.ToString());
            logger.Info("UEFA_Core @ ScreenList::Primary Screen: " + screen.Primary.ToString());
            if (screen.DeviceName.Contains(Name)) return screen;
        }

    }
    catch { }

    logger.Info("UEFA_Core @ ScreenList::No screen found by name");
    return Screen.PrimaryScreen;
}
于 2015-08-31T09:09:47.803 に答える