Delphi模拟最小化恢复关闭按纽
https://yq.aliyun.com/wenji/96083
本文讲的是Delphi模拟最小化恢复关闭按纽, 我们做多文档应用程序开发时,如果在主From中指定mainMenu时,在主菜单上右角上会自动出现最小化,恢复,关闭按纽,但主菜单放入Toolbar等中时,该三个按纽不会自动出现,因此需要编程实现
我们做多文档应用程序开发时,如果在主From中指定mainMenu时,在主菜单上右角上会自动出现最小化,恢复,关闭按纽,但主菜单放入Toolbar等中时,该三个按纽不会自动出现,因此需要编程实现。
实现原理:
按纽的实现,从Tbitbtn继承下来最理想,但需要过滤TbitBtn的焦点响应消息,使其不能获得焦点状态。
按纽的功能的实现是比较关键的,Delphi中提供了标准action对象(Twindowclose)来实现关闭当前激活的子窗体的功能。
当没有提供最小化及恢复功能的Action,因此有必须编程实现该两个对象分别命名为TWindowMinimize和TWindowRestore;并且编程是十分简单的。
为什么要采用action来实现最小化,恢复和关闭MDI子窗体具体的功能呢,这是因为,Delphi已经实现了其状态的自动变更。
另外,这三按纽必须保持在主界面的位置一直处于右上角.因此,需要在主窗体改变大小的时候,重新计算其位置。
由于只有子窗体最大化时,这三个按纽才能出现,因此,需要在idel事件中去判断当前的子窗体的状态,以便决定这三个按纽是否隐藏或可见.
具体代码如下:
unit ufrmMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ActnList, AppEvnts, ImgList, StdCtrls, Buttons, StdActns, Menus,
ToolWin, ComCtrls;
type
//最大最小化按纽类
TMDIButton = class(TBitbtn)
private
public
//由于由Tbitn继承而来,因此需要屏蔽其获得焦点的消息
procedure wndproc(var message: Tmessage); override;
end;
TWindowMinimize = class(TWindowAction)
public
//按纽功能,将当前最前的子window状态改为wsMinimize;
procedure ExecuteTarget(Target: TObject); override;
end;
TWindowRestore = class(TWindowAction)
public
//按纽功能,将当前最前的子window状态改为wsNormal;
procedure ExecuteTarget(Target: TObject); override;
end;
TFrmMain = class(TForm)
//保存windows的最小化,恢复,关闭的图标
MDIImageList: TImageList;
//当程序不忙时,判断最大,最小化按纽的是否应该隐藏还是可见
ApplicationEvents1: TApplicationEvents;
//
ActMdiForm: TActionList;
ToolBar1: TToolBar;
MainMenu1: TMainMenu;
N1: TMenuItem;
open1: TMenuItem;
help1: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
procedure FormResize(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure open1Click(Sender: TObject);
private
//模拟mdiform窗体最小化,关闭及恢复按纽对象
BtnMin, BtnRestore, BtnClose: TMDIButton;
Windowclose: TWindowClose;
WindowMinimize: TWindowMinimize;
WindowRestore: TWindowRestore;
procedure SetMDIFormActionPos;
public
{ Public declarations }
end;
var
FrmMain: TFrmMain;
implementation
{$R *.dfm}
procedure TFrmMain.FormCreate(Sender: TObject);
begin
//建立关闭Button
BtnClose := TMDIButton.Create(self);
BtnClose.Visible := false;
BtnClose.Parent := self;
BtnClose.Width := 16;
Btnclose.Height := 15;
//建立关闭功能Action
WindowClose := TWindowClose.Create(nil);
//指定其图标
WindowClose.ActionList := ActMdiForm;
WindowClose.ImageIndex := 2; //关闭;
WindowClose.Caption := '';
//将action与button关联
BtnClose.Action := WindowClose;
BtnClose.BringToFront;
BtnClose.Visible := false;
//建立最小化Button
BtnMin := TMDIButton.Create(self);
BtnMin.Visible := false;
BtnMin.Parent := self;
BtnMin.width := 16;
BtnMin.height := 15;
//建立最小化功能action
WindowMinimize := TWindowMinimize.Create(nil);
//指定其图标
WindowMinimize.ActionList := ActMdiForm;
WindowMinimize.Caption := '';
WindowMinimize.ImageIndex := 0;
//将action与button关联
BtnMin.Action := WindowMinimize; //最小化
BtnMin.BringToFront;
BtnMin.Visible := false;
//建立恢复功能Button
BtnRestore := TMDIButton.Create(self);
BtnRestore.Visible := false;
BtnRestore.Parent := self;
BtnRestore.Width := 16;
BtnRestore.height := 15;
//建立恢复功能action
WindowRestore := TWindowRestore.Create(nil);
//指定其图标
WindowRestore.ActionList := ActMdiForm;
WindowRestore.Caption := '';
WindowRestore.ImageIndex := 1;
//将action与button关联
BtnRestore.Action := WindowRestore;
BtnRestore.BringToFront;
BtnRestore.Visible := false;
//设置按纽位置,位置保持在主界面上相对不变
SetMDIFormActionPos;
end;
procedure TFrmMain.ApplicationEvents1Idle(Sender: TObject;
var Done: Boolean);
var
show: boolean;
begin
//当前子窗体的状态为最大化时,显示三个按纽
show := (self.ActiveMDIChild <> nil) and (self.ActiveMDIChild.WindowState =
wsMaximized);
if assigned(BtnClose) and BtnClose.Visible <> Show then
BtnClose.Visible := Show;
if assigned(BtnMin) and BtnMin.Visible <> Show then
BtnMin.Visible := Show;
if assigned(BtnRestore) and BtnRestore.Visible <> Show then
BtnRestore.Visible := Show;
end;
//设置按纽的相对位置不变
procedure TfrmMain.SetMDIFormActionPos;
begin
if assigned(BtnClose) then
begin
BtnClose.left := Width - 26;
BtnClose.top := 6;
end;
if assigned(BtnRestore) then
begin
BtnRestore.Left := Width - 44;
BtnRestore.Top := 6;
end;
if assigned(BtnMin) then
begin
BtnMin.Left := Width - 60;
BtnMin.Top := 6;
end;
end;
procedure TFrmMain.FormResize(Sender: TObject);
begin
SetMDIFormActionPos;
end;
procedure TFrmMain.FormDestroy(Sender: TObject);
begin
//释放资源
if assigned(BtnClose) then
begin
WindowClose.ActionList := nil;
WindowClose.free;
BtnClose.Free;
BtnClose := nil;
end;
if assigned(BtnRestore) then
begin
WindowRestore.ActionList := nil;
WindowRestore.free;
BtnRestore.Free;
BtnRestore := nil;
end;
if assigned(BtnMin) then
begin
WindowMinimize.ActionList := nil;
WindowMinimize.free;
BtnMin.Free;
BtnMin := nil;
end;
end;
{ TWindowRestore }
procedure TWindowRestore.ExecuteTarget(Target: TObject);
begin
inherited;
with GetForm(Target) do
ActiveMDIChild.WindowState := wsnormal;
end;
{ TMDIButton }
procedure TMDIButton.wndproc(var message: Tmessage);
begin
if message.msg = wm_SetFocus then exit;
inherited wndproc(message);
end;
{ TWindowMinimize }
procedure TWindowMinimize.ExecuteTarget(Target: TObject);
begin
inherited;
with GetForm(Target) do
ActiveMDIChild.WindowState := wsMinimized;
end;
procedure TFrmMain.open1Click(Sender: TObject);
begin
with Tform.create(self) do
begin
formstyle := fsMDIChild;
end;
end;
end.
Delphi模拟最小化恢复关闭按纽的更多相关文章
- delphi -----(去掉窗口最大化,最小化、关闭),主窗口,和子窗口之间的设置
一.去掉窗口最大化,最小化.关闭 borderIcons:biSystemMenu:false borderStyle:bsSizeable 二.主子窗口 主main: //调用子窗体procedur ...
- Winform 基础二 最小化 最大化 关闭 点击任务栏隐藏显示 点击鼠标左键移动窗体
一 最大化 二 最小化 三 关闭 四 点击任务栏隐藏显示 五 点击鼠标左键移动窗体 六 阴影效果鼠标左键移动窗口 #region UI设置 最大化.最小化.关闭.鼠标移动窗口.点击任务栏切换窗口 th ...
- JS实现漂亮的窗口拖拽效果(可改变大小、最大化、最小化、关闭)
转自<JS实现漂亮的窗口拖拽效果(可改变大小.最大化.最小化.关闭)>:http://www.jb51.net/article/73157.htm 这篇文章主要介绍了JS实现漂亮的窗口 ...
- delphi 子窗体只能最小化不能关闭的解决方案
cnpack下载地址:http://www.cnpack.org/showdetail.php?id=726&lang=zh-cn 时候创建的子窗体不能关闭,点关闭按钮时子窗体最小化了. 出现 ...
- Delphi 自定义窗体(最大化、最小化、关闭、窗体的移动)
Uses ShellAPI; 1.//最小化procedure TForm1.btn1Click(Sender: TObject);var I, J, X, Y: Word;begin //第一种 ...
- MFC的最大化,最小化,关闭
最大化.最小化和关闭按钮是窗口中最主要的元素.首先要说明,说他们是按钮其实是不准确的,按钮是一种窗口,而这三个组件根本就不是窗口,而是一个窗口常见的组成部分.出于习惯的原因,这里还是称呼他们为按钮. ...
- C# 隐藏最大化、最小化和关闭三个按钮
在Windows的窗体编程中,基本上每一个窗体都是一个最小化.最大化和关闭按钮的. 一.禁用最大化和最小化 对于最大化和最小化按钮,在C#窗体开发时,各一个属性来启用或禁用这两个按钮. this.Ma ...
- QWidget 自带的最大化,最小化,关闭按键的设置
使用函数 setWindowFlags 参数: CustomizeWindowHint 去掉窗口所有自带按钮 Qt::CustomizeWindowHint | Qt::WindowCloseButt ...
- delphi中最小化其他程序及所有程序最小化(使用 shell.minimizeAll 和自己寻找窗口这两种办法)
1.所有程序最小化 uses ComObj; var shell : OleVariant; begin shell := CreateOleObject('Shell.Appli ...
随机推荐
- [转]C++11 随机数学习
相对于C++ 11之前的随机数生成器来说,C++11的随机数生成器是复杂了很多.这是因为相对于之前的只需srand.rand这两函数即可获取随机数来说,C++11提供了太多的选择和东西. 随机数生成算 ...
- 2017/05/16 java 基础 随笔
1,成员变量和局部变量的区别 1)在内存中的位置不同 成员变量:在堆内存(成员变量属于对象,对象进堆内存) 局部变量:在栈内存(局部变量属于方法,方法进栈内存) 2)初始化值不同 成员变量:有默认初始 ...
- 【技术知识】恶意PDF文件分析-PDFdump的问题
1.提醒 百度分析恶意PDF文件,很多都是推荐PDFdump.在某次沙箱产品分析出疑似高级威胁的PDF样本后,我使用PDFdump查看ShellCode的加密数据,分析后并没有找到相关的ShellCo ...
- QTP图片验证/图片比较/二进制流对比法
图片验证主要是文件对比,其中我们可以利用二进制的方法读取图片信息,然后进行对比,达到对比的效果,本例子利用fso对象的文件流的方法实现,代码如下: Public Function CompareFil ...
- Linux 定时器应用【转】
Linux 定时器应用 实验目的 阅读 Linux 相关源代码,学习 Linux 系统中的时钟和定时器原理,即,ITIMER_REAL实时计数,ITIMER_VIRTUAL 统计进程在用户模式执行的时 ...
- spring-dao.xml 模板
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- plaidctf-2016 Pwn试题小结
回顾了一下今年plaidctf Pwn部分的题目,感觉还是蛮有意思的,值得研究一下. 1.unix_time_formatter-76 最简单的一道题,考点是UAF.说是UAF但是其实根本就不算是真正 ...
- 20165330《网络对抗技术》Exp0 Kali安装
Kali安装 下载地址 Kali官网 VMware 安装步骤 参考在虚拟机中安装kali linux 安装Kali Linux的镜像和VMware 打开VMware,选择文件-新建虚拟机,出现对话框选 ...
- ERP渠道管理添加验证和查询(二十二)
添加联系人的后台代码: protected void btnSubmit_Click(object sender, EventArgs e) { BioErpCrmManageChannel chan ...
- 当mysql 遇到 ctrl+c
目的 为了理解MySQL在执行大SQL时,对执行CTRL+C产生的疑惑,本文通过实验测试和源码分析两个方面,对MySQL处理CTRL+C的详细过程进行分析和讲解,从而解除DBA及开发人员对CTRL+C ...