AppleSeeker's Tech Blog
Welcome to AppleSeeker's space

在开发过程中都会遇到一些小小的问题,可能这个问题看似简单,但处理起来就相当的麻烦也说不定.
我将一些自己开发中的小技巧写出来告诉给大家,希望大家有所帮助.

这篇文章中将介绍如何隐藏起Form右上角的OK/X按钮,有时候用户觉得不需要这个按钮,那去掉它吧~~~~

再次介绍下该按钮的功能,X表示最小化,OK表示确认并关闭.通常新建一个Form后,默认窗体的最小化是True,所以如果需要显示OK的话,就需要将该属性设为False.

隐藏该按钮需要调用3个API

1 [DllImport("aygshell.dll")]
2 private static extern bool SHDoneButton(IntPtr hWnd, UInt32 dwState);
3 
4 [DllImport("coredll.dll")]
5 public static extern UInt32 SetWindowLong(IntPtr hWnd, int nIndex, UInt32 dwNewLong);
6 
7 [DllImport("coredll.dll")]
8 public static extern UInt32 GetWindowLong(IntPtr hWnd, int nIndex);


SHDoneButton--窗体为OK时,调用此方法即可.
GetWindowLong--获得指定窗口的信息
SetWindowLong--设置指定窗口的信息,这2个方法结合起来用,才能隐藏X按钮

具体使用

 1 public const UInt32 SHDB_SHOW = 0x0001;
 2 public const UInt32 SHDB_HIDE = 0x0002;
 3 public const int GWL_STYLE = -16;
 4 public const UInt32 WS_NONAVDONEBUTTON = 0x00010000;
 5 
 6 /// <summary>
 7 /// 隐藏OK按钮
 8 /// </summary>
 9 /// <param name="hWnd"></param>
10 public static void HideDoneButton(IntPtr hWnd)
11 {
12     SHDoneButton(hWnd, SHDB_HIDE);
13 }
14 
15 /// <summary>
16 /// 隐藏X按钮
17 /// </summary>
18 /// <param name="hWnd"></param>
19 public static void HideXButton(IntPtr hWnd)
20 {
21     UInt32 dwStyle = GetWindowLong(hWnd, GWL_STYLE);
22 
23     if ((dwStyle & WS_NONAVDONEBUTTON) == 0)
24         SetWindowLong(hWnd, GWL_STYLE, dwStyle | WS_NONAVDONEBUTTON);
25 }


在实际调用时,只要将窗体的Handle作为参数传入即可.

隐藏按钮后,如何打开?
只需要设置窗体的MinimizeBox属性即可.想显示OK,设为False.想显示X,设为True即可.

显示效果:
显示OK按钮


显示X按钮

隐藏按钮

代码下载:DeviceTechDemo.rar

运行环境:VS2008 + WM6.0 + .net cf3.5

Author:AppleSeeker(冯峰)
Date:2009-2-2

文章导读:移动开发索引贴

posted on 2009-02-02 16:51  AppleSeeker(冯峰)  阅读(3059)  评论(5编辑  收藏  举报