Question

I possède une poignée sur un autre processus dans la fenêtre principale .net (proc.MainWindowHandle). Comment maximiser l'intérieur de la fenêtre de .net?

Était-ce utile?

La solution

Vous pouvez Pinvoke à ShowWindow avec SW_SHOWMAXIMIZED pour agrandir la fenêtre.

Pinvoke.net a une entrée pour ShowWindow .

Par exemple,

// Pinvoke declaration for ShowWindow
private const int SW_SHOWMAXIMIZED = 3;

[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

// Sample usage
ShowWindow(proc.MainWindowHandle, SW_SHOWMAXIMIZED);

Autres conseils

Vous pouvez également utiliser SetWindowPlacement . Il y a plus d'info à ce sujet sur Pinvoke.net.

J'ai eu quelques problèmes avec cela et finalement réussi à le résoudre. Dans mon cas, j'avais une application WinForm qui avait besoin pour maximiser ou minimiser une application WPF.

, nous devons d'abord à l'importation InteropServices

using System.Runtime.InteropServices;

Ensuite, nous avons besoin de méthodes pour les actions que nous avons besoin:

[DllImport("user32.dll")]
private static extern bool SetWindowPlacement(IntPtr hWnd, [In] ref WINDOWPLACEMENT lpwndpl);

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);

Ensuite, nous pouvons vérifier le processus par son nom, obtenir son emplacement de la fenêtre, puis mettre à jour son emplacement de la fenêtre:

/// <summary>
/// WINDOWPLACEMENT showCmd - 1 for normal, 2 for minimized, 3 for maximized, 0 for hide 
/// </summary>
public static void MaximizeProcessWindow(string processName)
{ 
    foreach (Process proc in Process.GetProcesses())
    {
        if (proc.ProcessName.Equals(processName))
        {
            try
            { 
                WINDOWPLACEMENT wp = new WINDOWPLACEMENT();
                GetWindowPlacement(proc.MainWindowHandle, ref wp); 

                // Maximize window if it is in a normal state
                // You can also do the reverse by simply checking and setting 
                // the value of wp.showCmd
                if (wp.showCmd == 1)
                {
                    wp.showCmd = 3; 
                } 
                SetWindowPlacement(proc.MainWindowHandle, ref wp);                         
                break;
            }
            catch(Exception ex)
            {
                // log exception here and do something
            }
        }
    }
}

Vous pouvez également obtenir le processus par le titre de la fenêtre:

if (proc.MainWindowTitle.Equals(processTitle))

En fonction du processus, votre application peut avoir besoin d'être exécuté en vertu des privilèges d'administrateur. Cela peut être fait par l'ajout d'un fichier manifeste, puis en ajoutant le privilège d'administrateur suivant:

<requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top