【发布时间】:2016-06-07 04:44:40
【问题描述】:
我想知道在调用 HWnd 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 位。
【问题讨论】:
-
检查
DwmSetWindowAttribute的返回值,看看它是否失败,如果失败,原因是什么。 -
@Jonathan Potter 返回值为零,即操作成功
-
查看stackoverflow.com/questions/6160118/…的答案,看来您传递的数据指针不正确。
-
当我将方法签名更改为使用 IntPtr 时,它返回代码 0x800703e6(对内存位置的无效访问)。我使用的签名应该没问题,因为它是我问题链接中使用的签名。
-
发布的sn-ps是正确的。寻找一个简单的错误,例如太晚调用 SetEnabled() 或反转其 enabled 参数的含义或使用错误的窗口句柄。