用代码最小化 Windows Mobile 窗体
以前写过一篇随笔《PPC上窗体的关闭与最小化》,向 Windows Mobile 初学者介绍窗体关闭和最小化的区别。
关于窗体最小化,在桌面版的 Windows Form 开发我们可以通过设置窗体的 WindowsState 属性来实现,而 .NET Compact Framework 不支持 Minimized 枚举值。
成员名称 | 说明 | |
---|---|---|
![]() | Normal | 默认大小的窗口。 |
Minimized | 最小化的窗口。 | |
![]() | Maximized | 最大化的窗口。 |
因此,我们只能通过 Win32 API 来实现了,代码很简单:
//
声明 Win32 API 函数和常量
[System.Runtime.InteropServices.DllImport( " coredll " )]
static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
const int SW_MINIMIZE = 6 ;
// 实现窗体最小化,this 为窗体实例
ShowWindow( this .Handle, SW_MINIMIZE);
[System.Runtime.InteropServices.DllImport( " coredll " )]
static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
const int SW_MINIMIZE = 6 ;
// 实现窗体最小化,this 为窗体实例
ShowWindow( this .Handle, SW_MINIMIZE);
这样就可以通过代码实现窗体的最小化啦!
但是使用上面的方法一定注意:
窗体的 BorderStyle 属性不能设置为 “None”,否则执行后,会出现被最小化的窗体最小化后立即又恢复正常。