禁用标题栏上的最大化、最小化或关闭按钮
(
二十五
)
本文讲述如何把标题栏上的最大化,最小化和关闭按钮禁用和启用。
1.
创建一个单文档工程。
2.
添加一个菜单项,分别为“最小化禁用”,“最大化禁用”,“关闭禁用”和相应的启用。添加两个变量如下:
LONG
Style
;
CRect
Rect
;
3.
“最小化禁用”的代码如下:
//
获得窗口风格
Style
= ::
GetWindowLong
(
m_hWnd
,
GWL_STYLE
);
//
设置新的风格
Style
&= ~(
WS_MINIMIZEBOX
);
::
SetWindowLong
(
m_hWnd
,
GWL_STYLE
,
Style
);
GetWindowRect
(&
Rect
);
//
重画窗口边框
::
SetWindowPos
(
m_hWnd
,
HWND_TOP
,
Rect
.
left
,
Rect
.
top
,
Rect
.
Width
(),
Rect
.
Height
(),
SWP_DRAWFRAME
);
4.
“最大化禁用”的代码如下:
// TODO: Add your command handler code here
Style
=::
GetWindowLong
(
m_hWnd
,
GWL_STYLE
);
Style
&= ~(
WS_MAXIMIZEBOX
);
::
SetWindowLong
(
m_hWnd
,
GWL_STYLE
,
Style
);
GetWindowRect
(&
Rect
);
::
SetWindowPos
(
m_hWnd
,
HWND_TOP
,
Rect
.
left
,
Rect
.
top
,
Rect
.
Width
(),
Rect
.
Height
(),
SWP_DRAWFRAME
);
5.
“关闭禁用”的代码如下:
//
获得系统采单
CMenu
*
pMenu
=
GetSystemMenu
(
false
);
//
获得关闭按钮
ID
UINT
ID
=
pMenu
->
GetMenuItemID
(
pMenu
->
GetMenuItemCount
()-1);
//
是关闭按钮无效
pMenu
->
EnableMenuItem
(
ID
,
MF_GRAYED
);
6.
“最小化启用”的代码如下:
//
获得窗口风格
Style
= ::
GetWindowLong
(
m_hWnd
,
GWL_STYLE
);
//
设置新的风格
Style
|=
WS_MINIMIZEBOX
;
::
SetWindowLong
(
m_hWnd
,
GWL_STYLE
,
Style
);
GetWindowRect
(&
Rect
);
//
重画窗口边框
::
SetWindowPos
(
m_hWnd
,
HWND_TOP
,
Rect
.
left
,
Rect
.
top
,
Rect
.
Width
(),
Rect
.
Height
(),
SWP_DRAWFRAME
);
7.
“最大化启用”的代码如下:
//
获得窗口风格
Style
= ::
GetWindowLong
(
m_hWnd
,
GWL_STYLE
);
//
设置新的风格
Style
|=
WS_MAXIMIZEBOX
;
::
SetWindowLong
(
m_hWnd
,
GWL_STYLE
,
Style
);
GetWindowRect
(&
Rect
);
//
重画窗口边框
::
SetWindowPos
(
m_hWnd
,
HWND_TOP
,
Rect
.
left
,
Rect
.
top
,
Rect
.
Width
(),
Rect
.
Height
(),
SWP_DRAWFRAME
);
8.
“关闭启用”代码如下:
//
获得窗口风格
Style
= ::
GetWindowLong
(
m_hWnd
,
GWL_STYLE
);
//
设置新的风格
Style
|=
WS_MAXIMIZEBOX
;
::
SetWindowLong
(
m_hWnd
,
GWL_STYLE
,
Style
);
GetWindowRect
(&
Rect
);
//
重画窗口边框
::
SetWindowPos
(
m_hWnd
,
HWND_TOP
,
Rect
.
left
,
Rect
.
top
,
Rect
.
Width
(),
Rect
.
Height
(),
SWP_DRAWFRAME
);
完成。