2016-06-07 8 views
2

Ich würde gerne wissen, wie die Animationen beim Aufruf der HWnd ShowWindow() -Methode unterdrückt werden. Hier ist mein Code:HWND API: So deaktivieren Sie Fensteranimationen beim Aufruf von ShowWindow (...)

[DllImport("user32.dll")] 
[return: MarshalAs(UnmanagedType.Bool)] 
static extern bool ShowWindow(IntPtr hWnd, ShowWindowCommands nCmdShow); 

public enum ShowWindowCommands 
{ 
    HIDE = 0, 
    SHOWNORMAL = 1, 
    SHOWMINIMIZED = 2, 
    MAXIMIZE = 3, 
    SHOWNOACTIVATE = 4, 
    SHOW = 5, 
    MINIMIZE = 6, 
    SHOWMINNOACTIVE = 7, 
    SHOWNA = 8, 
    RESTORE = 9, 
    SHOWDEFAULT = 10, 
    FORCEMINIMIZE = 11 
} 

public static void MinimizeWindow(IntPtr hWnd) 
{ 
    ShowWindow(hWnd, ShowWindowCommands.MINIMIZE); 
} 

Das Problem ist, die Animation ausgeführt wird, und die Methode nicht zurück, bis die Animation beendet ist.

Ich versuchte, die DwmSetWindowAttribute() Methode:

[DllImport("dwmapi.dll", PreserveSig = true)] 
static extern int DwmSetWindowAttribute(IntPtr hWnd, uint attr, ref int attrValue, int size); 

const uint DWM_TransitionsForceDisabled = 3; 

public static void SetEnabled(IntPtr hWnd, bool enabled) 
{ 
    int attrVal = enabled ? 0 : 1; 
    DwmSetWindowAttribute(hWnd, DWM_TransitionsForceDisabled, ref attrVal, 4); 
} 

Aber die Animationen nicht unterdrückt wurden. Mein Betriebssystem ist Windows 7, 32-Bit.

+0

Überprüfen Sie den Rückgabewert von 'DwmSetWindowAttribute', um zu sehen, ob es fehlschlägt und wenn ja, warum. –

+0

@ Jonathan Potter Der Rückgabewert ist Null, dh Operation erfolgreich –

+0

Siehe die Antwort auf http://StackOverflow.com/Questions/6160118/disable-Aero-Peek-in-WPF-Application, es sieht so aus, als ob Sie die übergeben Datenzeiger falsch. –

Antwort

-1

nicht die beste Option, aber können Sie versuchen, SystemParametersInfo() Aufruf SPI_GETANIMATION Angabe die aktuelle Einstellung für Fensteranimationen zu bekommen, und wenn dann SPI_SETANIMATION sich aktiviert haben zu deaktivieren, bevor Sie das Fenster zeigt, dann die vorherige Einstellung gestellt. Zum Beispiel:

[StructLayout(LayoutKind.Sequential)] 
public struct ANIMATIONINFO 
{ 
    uint cbSize; 
    int iMinAnimate; 
} 

[DllImport("User32.dll", SetLastError=true)] 
static extern bool SystemParametersInfo(uint uiAction, uint uiParam, ref ANIMATIONINFO pvParam, uint fWinIni); 

const uint SPI_GETANIMATION = 72; 
const uint SPI_SETANIMATION = 73; 

public static void MinimizeWindow(IntPtr hWnd) 
{ 
    ANIMATIONINFO anim; 
    anim.cbSize = Marshal.SizeOf(anim); 
    anim.iMinAnimate = 0; 
    SystemParametersInfo(SPI_GETANIMATION, 0, anim, 0); 

    if (anim.iMinAnimate != 0) 
    { 
     anim.iMinAnimate = 0; 
     SystemParametersInfo(SPI_SETANIMATION, 0, anim, 0); 

     ShowWindow(hWnd, ShowWindowCommands.MINIMIZE); 

     anim.iMinAnimate = 1; 
     SystemParametersInfo(SPI_SETANIMATION, 0, anim, 0); 
    } 
    else 
     ShowWindow(hWnd, ShowWindowCommands.MINIMIZE); 
} 
+2

Allgemein wird im alten New Thing-Blog als "globale Einstellung zum Beheben eines lokalen Problems" bezeichnet und normalerweise [verpönt] (https://blogs.msdn.microsoft.com/oldnewthing/20081211-00/?p= 19873) –

+0

Die seltsame Sache ist, auch wenn ich diese globale Lösung verwenden zeigt die Animation immer noch :( –

+0

@Damien_The_Unbeliever: gewährt, aber ich weiß keine andere Möglichkeit, Fensteranimationen lokal zu deaktivieren. –