禁用窗体关闭按钮(使用GetWindowLong修改GWL_STYLE)
2016-08-27 03:18
375 查看
一般我们不想让窗体能够关闭, 首先想到的是在OnCloseQuery事件里设置CanClose := False, 不过在某些情况下这个会和程序关闭窗体的业务逻辑产生冲突
所以写了下面这个函数, 可以设置窗体上的部分控制按钮禁用和启用, 仅仅是按钮显示及对鼠标键盘的响应, 按钮功能方面不影响
http://www.cnblogs.com/hs-kill/p/4650684.html
所以写了下面这个函数, 可以设置窗体上的部分控制按钮禁用和启用, 仅仅是按钮显示及对鼠标键盘的响应, 按钮功能方面不影响
type TSetWinButtons = set of (swb_Close, swb_Minimize, swb_Maximize); {设置窗体关闭按钮状态} procedure SetWindowButton(AButtons: TSetWinButtons; AEnabled: Boolean; AWindowHandles: array of THandle); var i: Integer; nHasMenu, nHasWinLong: Boolean; nMValue: UINT; nWinLong: NativeInt; begin if Length(AWindowHandles) = 0 then Exit; nHasMenu := swb_Close in AButtons; nHasWinLong := (swb_Minimize in AButtons) or (swb_Maximize in AButtons); if nHasMenu then begin if AEnabled then nMValue := MF_BYCOMMAND or MF_ENABLED else nMValue := MF_BYCOMMAND or MF_DISABLED or MF_GRAYED; end; for i := Low(AWindowHandles) to High(AWindowHandles) do begin if nHasMenu then EnableMenuItem(GetSystemMenu(AWindowHandles[i], FALSE), SC_CLOSE, nMValue); if nHasWinLong then begin nWinLong := GetWindowLong(AWindowHandles[i], GWL_STYLE); if AEnabled then begin if swb_Minimize in AButtons then nWinLong := nWinLong or WS_MINIMIZEBOX; if swb_Maximize in AButtons then nWinLong := nWinLong or WS_MAXIMIZEBOX; end else begin if swb_Minimize in AButtons then nWinLong := nWinLong and not WS_MINIMIZEBOX; if swb_Maximize in AButtons then nWinLong := nWinLong and not WS_MAXIMIZEBOX; end; SetWindowLong(AWindowHandles[i], GWL_STYLE, nWinLong); end; end; end; //调用方式 SetWindowButton([swb_Close, swb_Minimize, swb_Maximize], False, [Application.Handle, Handle]);
http://www.cnblogs.com/hs-kill/p/4650684.html
相关文章推荐
- 一个关于Delphi XML处理单元的BUG
- 2015 USP Try-outs Gym101047
- 获取进程列表的单元
- 一个修改过简化版的InputQuery(简单实用,用到了Canvas)
- 粘滞位
- 2、成功运行pcl allinone的demo
- Delphi 缩放图像代码 - 支持PNG透明通道(利用了Windows的windowscodecs.dll)
- 关于创建无窗体程序的一点心得
- 一个支持FMX.Win框架的托盘控件
- THE END OF HOLIDAY
- C# Datagridview完整攻略
- 9patch制作dd
- 透视投影矩阵推导
- Tortoise SVN系列:(2)使用
- 汇编器构造
- could not locate executable null\bin\winutils.exe in the Hadoop binaries.
- 圆角输入框, 选择器自定义设置
- NPOI MVC 模型导出Excel通用类
- PHPExcel读取excel文件
- 入金第一周