Question

Dans mon C # .NET 4 application, j'utilise WndProc pour traiter certains messages pour la plupart traitant de redimensionnement de l'application et de plein écran.

En ce moment je traitais juste SC_MAXIMIZE et WM_NCLBUTTONDBLCLK pour déterminer si la fenêtre est redimensionnée ou d'un état maximisé (je sais que je ne crois pas besoin WndProc de poignée SC_MAXIMIZE, mais Form_Resize ne pas le feu pour une WM_NCLBUTTONDBLCLK message lorsque je double-cliquez sur la barre de titre de l'application.

Maintenant, je remarque que si je Aero Snap la fenêtre en haut de l'écran pour le maximiser, aucun des messages ci-dessus sont affichées de manière certaine logique n'est pas appliquée lorsque la fenêtre est maximisée par Aero Snap. Je veux seulement gérer le message si la fenêtre est accrochée au-dessus de l'écran plutôt que la droite ou à gauche, ou si une fenêtre est unsnappped de la position maximisée.

Je ne pouvais pas trouver des messages de fenêtre liés à Aero Snap. Quelqu'un sait-il de toutes les références pour ces messages?

Autres conseils

Voici le code pour le traitement WM_WINDOWPOSCHANGING message pour Maximize au lieu de WM_SIZE message. Merci aux 20 questions ou plus pour que je devais lire pour trouver tous les bits pour le mettre ensemble et le faire fonctionner. Ce traite les problèmes que je rencontrais avec plusieurs moniteurs en utilisant des résolutions différentes.

//register the hook
public static void WindowInitialized(Window window)
{
    IntPtr handle = (new WindowInteropHelper(window)).Handle;
    var hwndSource = HwndSource.FromHwnd(handle);
    if (hwndSource != null) 
    {
        hwndSource.AddHook(WindowProc);
    }
}

//the important bit
private static IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
    switch (msg)
    {
        case 0x0046: //WINDOWPOSCHANGING
            var winPos = (WINDOWPOS)Marshal.PtrToStructure(lParam, typeof(WINDOWPOS));
            var monitorInfo = new MONITORINFO();
            IntPtr monitorContainingApplication = MonitorFromWindow(hwnd, MonitorDefaultToNearest);
            GetMonitorInfo(monitorContainingApplication, monitorInfo);
            RECT rcWorkArea = monitorInfo.rcWork;
            //check for a framechange - but ignore initial draw. x,y is top left of current monitor so must be a maximise
            if (((winPos.flags & SWP_FRAMECHANGED) == SWP_FRAMECHANGED) && (winPos.flags & SWP_NOSIZE) != SWP_NOSIZE && winPos.x == rcWorkArea.left && winPos.y == rcWorkArea.top)
            {
                //set max size to the size of the *current* monitor
                var width = Math.Abs(rcWorkArea.right - rcWorkArea.left);
                var height = Math.Abs(rcWorkArea.bottom - rcWorkArea.top);
                winPos.cx = width;
                winPos.cy = height;
                Marshal.StructureToPtr(winPos, lParam, true);
                handled = true;
            }                       
            break;
    }
    return (IntPtr)0;
}


//all the helpers for dealing with this COM crap
[DllImport("user32")]
internal static extern bool GetMonitorInfo(IntPtr hMonitor, MONITORINFO lpmi);

[DllImport("user32")]
internal static extern IntPtr MonitorFromWindow(IntPtr handle, int flags);

private const int MonitorDefaultToNearest = 0x00000002;

[StructLayout(LayoutKind.Sequential)]
public struct WINDOWPOS
{
    public IntPtr hwnd;
    public IntPtr hwndInsertAfter;
    public int x;
    public int y;
    public int cx;
    public int cy;
    public int flags;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class MONITORINFO
{
    public int cbSize = Marshal.SizeOf(typeof(MONITORINFO));
    public RECT rcMonitor;
    public RECT rcWork;
    public int dwFlags;
}

[StructLayout(LayoutKind.Sequential, Pack = 0)]
public struct RECT
{
    public int left;
    public int top;
    public int right;
    public int bottom;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top