Question

Je suis en train de mettre un formulaire Windows sur le moniteur secondaire, comme suit:

private void button1_Click(object sender, EventArgs e)
{
    MatrixView n = new MatrixView();
    Screen[] screens = Screen.AllScreens;
    setFormLocation(n, screens[1]);
    n.Show();
}

private void setFormLocation(Form form, Screen screen)
{
    // first method
    Rectangle bounds = screen.Bounds;
    form.SetBounds(bounds.X, bounds.Y, bounds.Width, bounds.Height);

    // second method
    //Point location = screen.Bounds.Location;
    //Size size = screen.Bounds.Size;

    //form.Left = location.X;
    //form.Top = location.Y;
    //form.Width = size.Width;
    //form.Height = size.Height;
}

Les propriétés des limites semblent correctes, mais dans les deux méthodes que je l'ai essayé, cela permettrait de maximiser la forme sur le moniteur principal. Toutes les idées?

Était-ce utile?

La solution

Essayez de réglage des paramètres WindowStartUpLocation comme "manuel" à l'intérieur de votre méthode SetFormLocation.

Autres conseils

this.Location = Screen.AllScreens[1].WorkingArea.Location;

est la référence du formulaire.

@ La réponse de Gengi est succinct et fonctionne bien. Si la fenêtre est agrandie, il ne se déplace pas la fenêtre. Cet extrait permet de résoudre que (bien que je soupçonne les fenêtres dimensions « normales » doivent être plus petits que les nouvelles dimensions de l'écran pour que cela fonctionne):

    void showOnScreen(int screenNumber)
    {
        Screen[] screens = Screen.AllScreens;

        if (screenNumber >= 0 && screenNumber < screens.Length)
        {
            bool maximised = false;
            if (WindowState == FormWindowState.Maximized)
            {
                WindowState = FormWindowState.Normal;
                maximised = true;
            }
            Location = screens[screenNumber].WorkingArea.Location;
            if (maximised)
            {
                WindowState = FormWindowState.Maximized;
            }
        }
    }

Êtes-vous sûr screens[1] est votre secondaire? Donnez screens[0] un essai. Votre code est fondamentalement correct.


Ok, j'ai vérifié, vous devrez le faire après le spectacle ():

n.Show();
setFormLocation(n, screens[1]);

ce qui donne une scintillement indésirable. Mais vous pouvez probablement faire:

n.SetBounds(-100, -100, 10, 10);  // or similar
n.Show();
setFormLocation(n, screens[1]);

J'ai utilisé cela pour un XNA 4 Application double écran (plein écran XNA Window Game + WinForm)

Dans le procédé Form_Load (), placer le code suivant:

var primaryDisplay = Screen.AllScreens.ElementAtOrDefault(0);  
var extendedDisplay = Screen.AllScreens.FirstOrDefault(s => s != primaryDisplay) ?? primaryDisplay;

this.Left = extendedDisplay.WorkingArea.Left + (extendedDisplay.Bounds.Size.Width / 2) - (this.Size.Width / 2);
this.Top = extendedDisplay.WorkingArea.Top + (extendedDisplay.Bounds.Size.Height / 2) - (this.Size.Height / 2);

Pour afficher la forme sur l'écran secondaire:

    Screen primaryFormScreen = Screen.FromControl(primaryForm);
    //Use this if you are looking for secondary screen that is not primary
    Screen secondaryFormScreen = Screen.AllScreens.FirstOrDefault(s => !s.Primary) ?? primaryFormScreen;
    //Use this if you are looking for screen that is not being used by specific form
    Screen secondaryFormScreen = Screen.AllScreens.FirstOrDefault(s => !s.Equals(primaryFormScreen)) ?? primaryFormScreen;
    //Putting the form on the other screen
    secondaryForm.Left = secondaryFormScreen.Bounds.Width;
    secondaryForm.Top = secondaryFormScreen.Bounds.Height;
    //Recommended to use, You can change it back later to the settings you wish
    secondaryForm.StartPosition = FormStartPosition.Manual;
    secondaryForm.Location = secondaryFormScreen.Bounds.Location;
    Point p = new Point(secondaryFormScreen.Bounds.Location.X, secondaryFormScreen.Bounds.Location.Y);
    secondaryForm.Location = p;
    secondaryForm.Show();

Si vous êtes à la recherche vers l'avant pour un écran spécifique, vous pouvez faire une boucle sur « Screen.AllScreens » et utiliser le processus ci-dessus.

Définir forme propriété Position de démarrage sur Manuel

    public void MoveWindowToProjector ()  
    { 
           Rectangle rectMonitor;

            // Create New Process
            Process objProcess = new Process();

            //Get All the screens associated with this Monitor
            Screen[] screens = Screen.AllScreens;

            // Get Monitor Count
            int iMonitorCount = Screen.AllScreens.Length;

            // Get Parameters of Current Project
            string[] parametros = Environment.GetCommandLineArgs();
           // if (parametros.Length > 0)
           // {
                //objProcess.StartInfo.FileName = parametros[0];
               // objProcess.Start();
          //  }
            // Get Window Handle of this Form
            IntPtr hWnd = this.Handle;

            Thread.Sleep(1000);


            if (IsProjectorMode)
            {
                if (iMonitorCount > 1) // If monitor Count 2 or more
                {
                    //Get the Dimension of the monitor
                    rectMonitor = Screen.AllScreens[1].WorkingArea;
                }
                else
                {
                    //Get the Dimension of the monitor
                    rectMonitor = Screen.AllScreens[0].WorkingArea;
                }

            }
            else
            {
                rectMonitor = Screen.AllScreens[0].WorkingArea;

            }
            if (hWnd != IntPtr.Zero)
            {
                SetWindowPos(hWnd, 0,
                    rectMonitor.Left, rectMonitor.Top, rectMonitor.Width,
                    rectMonitor.Height, SWP_SHOWWINDOW);
            }

        }
  Screen[] screens = Screen.AllScreens;
                sc aoc = new sc();
                aoc.Show();
    aoc.Location = Screen.AllScreens[INDEX OF YOUR AVAILABLE SCREENS TARGET].WorkingArea.Location;
  

POUR L'ÉTAT MAXIMISEE FENÊTRE

aoc.WindowState = FormWindowState.Maximized;
  

POUR TOUT X, POSITION Y

aoc.Location = new Point(TARGET X POSITION, TARGET Y POSITION);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top