C# AppBar多监视器
我制作了一个简单的应用程序条,屏幕顶部只有一个标签,可以缩小桌面,但我在让它出现在第二个显示器上时遇到了麻烦。我一直在四处寻找,但我找到的一切都是为了WPF。这些很可能是我犯错误的地方,但是如果您需要查看任何其他代码,请告诉我C# AppBar多监视器,c#,appbar,multiple-monitors,C#,Appbar,Multiple Monitors,我制作了一个简单的应用程序条,屏幕顶部只有一个标签,可以缩小桌面,但我在让它出现在第二个显示器上时遇到了麻烦。我一直在四处寻找,但我找到的一切都是为了WPF。这些很可能是我犯错误的地方,但是如果您需要查看任何其他代码,请告诉我 private void InitializeComponent() { this.ClientSize = new System.Drawing.Size(SystemInformation.WorkingArea.Width, -1); this.Fo
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;
}
}
通过迭代
屏幕.AllScreens
数组,可以使用不同的屏幕。例如,以下是如何获得第一个非主监视器:
Screen nonPrimaryScreen = Screen.AllScreens.FirstOrDefault(x => !x.Primary);
然后,无论您在哪里使用SystemInformation.WorkingArea
(始终使用主屏幕),都可以使用:
nonPrimaryScreen.WorkingArea
假设非主屏幕!=空
。。。当然
编辑:
不要复制代码,而是使其更通用:
public static Rectangle GetWorkingArea() {
if (UseWantsItOnPrimaryScreen) {
return SystemInformation.WorkingArea;
}
else {
return Screen.AllScreens.FirstOrDefault(x => !x.Primary).WorkingArea;
}
}
您可以计算将AppBar移动到第二个监视器的正确公式,而无需使用除
PrimaryMonitorSize
以外的任何东西。例如,对于第二个监视器上的左侧AppBar,您可以使用:
if (abd.uEdge == (int)ABEdge.ABE_LEFT)
{
abd.rc.left = SystemInformation.PrimaryMonitorSize.Width;
abd.rc.right = SystemInformation.PrimaryMonitorSize.Width + Size.Width;
}
所以我必须写两个版本的代码,一个是SystemInformation.WorkingArea,另一个是nonPrimaryScreen.WorkingArea?不。。您可以通过使用
矩形
使其更通用。我会编辑我的答案。好的,但这会在一个屏幕上还是另一个屏幕上,或者两个屏幕上?问题是,我不知道是否在程序运行时将矩形从监视器1更改为监视器2,它是否会注意到,或者appbar是否已经创建,而没有注意到监视器2的更改。否。。我没有用勺子喂你。我给了你一个例子,你可以把它插入你的代码库的其他部分。我不知道你的代码库是什么样子的。使用泛型函数。。每次用户更改“主屏幕或非主屏幕”选项时调用它。我发现,您的代码很有用,但我花了一些时间在程序中实现它,因为我不习惯Windows Shell编程。API中的示例代码也很有帮助:。我无法从一个可执行文件中生成2个AppBar,尽管它是多线程的,所以我让它在Monitor2中运行,如果相同的进程已经在运行(可能在monitor 1中)。请提供代码示例之外的其他说明。此示例支持更多屏幕以显示appbar。主要更改如下:此.GetScreenObject(ScreenName)部分。只需定义,appbar从所选屏幕的位置。当您在垂直屏幕上使用-1080时,它是功能性的。这是getscreenobject(其他特殊)的示例,只需返回屏幕对象liek screen.PrimaryScreen或其他,然后在appbar位置使用它。
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;
}
if (abd.uEdge == (int)ABEdge.ABE_LEFT)
{
abd.rc.left = SystemInformation.PrimaryMonitorSize.Width;
abd.rc.right = SystemInformation.PrimaryMonitorSize.Width + Size.Width;
}