2016-06-07 103 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:已授予,但我不知道任何其他方式来关闭本地窗口动画 –