2016-06-07 102 views
2

我想知道如何調用HWND的ShowWindow()方法時,抑制動畫。這是我的代碼:HWND API:如何調用的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); 
} 

問題是,動畫執行,並且該方法不會返回,直到動畫完成。

我嘗試使用DwmSetWindowAttribute()方法:

[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); 
} 

但動畫並沒有抑制。 我的操作系統是Windows 7,32位。

+0

檢查'DwmSetWindowAttribute'的返回值以查看它是否失敗,如果是這樣,爲什麼。 –

+0

@Jonathan波特的返回值是零,即操作成功 –

+0

查看答案http://stackoverflow.com/questions/6160118/disable-aero-peek-in-wpf-application,它看起來像你傳遞數據指針不正確。 –

回答

-1

不是最好的選擇,但你可以嘗試調用SystemParametersInfo()指定SPI_GETANIMATION以獲取窗口動畫的當前設置,如果啓用,則使用SPI_SETANIMATION顯示窗口前禁用它們,然後恢復以前的設置。例如:

[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

一般稱在舊的新博客爲「使用全局設置來解決局部問題」,通常[不贊成](https://blogs.msdn.microsoft.com/oldnewthing/20081211-00/?p= 19873) –

+0

奇怪的是,即使我使用這個全局解決方案,動畫仍然顯示:( –

+0

@Damien_The_Unbeliever:已授予,但我不知道任何其他方式來關閉本地窗口動畫 –