Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为每个屏幕创建应用程序实例_C#_.net_Winforms - Fatal编程技术网

C# 为每个屏幕创建应用程序实例

C# 为每个屏幕创建应用程序实例,c#,.net,winforms,C#,.net,Winforms,我希望我的WinForms应用程序为每个屏幕创建一个自身实例,并显示在该屏幕上。 我有以下代码: 主要形式: class MainForm { public MainForm() { string[] args = Environment.GetCommandLineArgs(); foreach (string arg in args) { if (arg == "TakeOverAllScr

我希望我的WinForms应用程序为每个屏幕创建一个自身实例,并显示在该屏幕上。 我有以下代码:

主要形式:

class MainForm
{
    public MainForm()
    {        
        string[] args = Environment.GetCommandLineArgs();

        foreach (string arg in args)
        {
          if (arg == "TakeOverAllScreens") { TakeOverAllScreens(); }
          if (arg.StartsWith("Screen|"))
             {
               string[] s;
               s = arg.Split('|');
               int xPos , yPos, screenNum ;
               int.TryParse(s[1], out xPos);
               int.TryParse(s[2], out yPos);
               int.TryParse(s[3], out screenNum);
               Screen[] sc;
               sc = Screen.AllScreens;
               this.Left = sc[screenNum].Bounds.Width;
               this.Top = sc[screenNum].Bounds.Height;
               this.StartPosition = FormStartPosition.Manual;
             }
        }

        InitializeComponent();
    }

    private void MainForm_Load(object sender, EventArgs e)
    {
        this.TopMost = true;
        this.FormBorderStyle = FormBorderStyle.None;
        this.WindowState = FormWindowState.Maximized;
    }
}
TakeOverAllScreens表格:

private void TakeOverAllScreens()
{
    int i = 0;
    foreach (Screen s in Screen.AllScreens)
    {
        if (s != Screen.PrimaryScreen)
        {
            i++;
            Process.Start(Application.ExecutablePath, "Screen|" + s.Bounds.X + "|" + s.Bounds.Y+"|" + i);
        }
    }
}
我的应用程序确实为每个屏幕创建了一个新实例,但是它只显示在我的主屏幕上,而不显示在其他屏幕上。

这看起来很可疑:

int.TryParse(s[1], out xPos);
int.TryParse(s[2], out yPos);
int.TryParse(s[3], out screenNum);
Screen[] sc;
sc = Screen.AllScreens;
this.Left = sc[screenNum].Bounds.Width;
this.Top = sc[screenNum].Bounds.Height;
在命令行上传递
x
y
值,然后忽略它们,并使用屏幕的宽度和高度设置x/y值。如果所有屏幕的分辨率相同,并且水平或垂直排列,则所有这些窗口都可能位于屏幕任何可见部分的下方(或右侧)

我也无法保证总是以相同的顺序返回屏幕,因此
screenNum
值可能引用了不同的屏幕


我还希望看到此代码出现在调用
InitializeComponents
之后,而不是之前,这样您就知道任何设计器集属性都将被您的代码覆盖,而不是相反


所以,我的代码是:

public MainForm()
{   
    InitializeComponent();

    string[] args = Environment.GetCommandLineArgs();

    foreach (string arg in args)
    {
        if (arg == "TakeOverAllScreens") { TakeOverAllScreens(); }
        if (arg.StartsWith("Screen|"))
        {
            string[] s;
            s = arg.Split('|');
            int xPos, yPos, screenNum;
            int.TryParse(s[1], out xPos);
            int.TryParse(s[2], out yPos);
            this.Left = xPos;
            this.Top = yPos;
            this.StartPosition = FormStartPosition.Manual;
        }
    }
}
以及:

当然,
TryParse
调用是毫无意义的,如果您只是想忽略返回值,可以用
Parse
来代替。

这看起来很可疑:

int.TryParse(s[1], out xPos);
int.TryParse(s[2], out yPos);
int.TryParse(s[3], out screenNum);
Screen[] sc;
sc = Screen.AllScreens;
this.Left = sc[screenNum].Bounds.Width;
this.Top = sc[screenNum].Bounds.Height;
在命令行上传递
x
y
值,然后忽略它们,并使用屏幕的宽度和高度设置x/y值。如果所有屏幕的分辨率相同,并且水平或垂直排列,则所有这些窗口都可能位于屏幕任何可见部分的下方(或右侧)

我也无法保证总是以相同的顺序返回屏幕,因此
screenNum
值可能引用了不同的屏幕


我还希望看到此代码出现在调用
InitializeComponents
之后,而不是之前,这样您就知道任何设计器集属性都将被您的代码覆盖,而不是相反


所以,我的代码是:

public MainForm()
{   
    InitializeComponent();

    string[] args = Environment.GetCommandLineArgs();

    foreach (string arg in args)
    {
        if (arg == "TakeOverAllScreens") { TakeOverAllScreens(); }
        if (arg.StartsWith("Screen|"))
        {
            string[] s;
            s = arg.Split('|');
            int xPos, yPos, screenNum;
            int.TryParse(s[1], out xPos);
            int.TryParse(s[2], out yPos);
            this.Left = xPos;
            this.Top = yPos;
            this.StartPosition = FormStartPosition.Manual;
        }
    }
}
以及:


当然,
TryParse
调用是毫无意义的,如果您只是想忽略返回值,可以用
Parse
来代替。

太棒了!它起作用了!:D.我认为问题是试图指定屏幕
this.Left=sc[screenNum].Bounds.Width
而不是仅仅使用
this.Left=xPos谢谢!!令人惊叹的!它起作用了!:D.我认为问题是试图指定屏幕
this.Left=sc[screenNum].Bounds.Width
而不是仅仅使用
this.Left=xPos谢谢!!