Delphi Simulation Minimize Restore Shutdown button

Source: Internet
Author: User

When we do a long document application development, if you specify MainMenu in the main from, you will automatically minimize, restore, and turn off the button on the right corner of the main menu, but when the main menu is placed in toolbar, the three buttons will not appear automatically, so you need to implement it programmatically.

Implementation principle:

The implementation of the button, inherited from the tbitbtn is ideal, but need to filter TBITBTN Focus response message, so that it can not get the focus state.

The implementation of the button's function is more critical, and Delphi provides a standard action object (Twindowclose) to enable the ability to close the currently active subform.

When there is no action to minimize and restore functionality, it is necessary to programmatically implement the two objects named Twindowminimize and Twindowrestore respectively, and programming is very simple.

Why use the action to minimize, restore, and turn off the MDI child form's specific functionality, because Delphi has implemented automatic changes to its state.

In addition, the three buttons must remain in the upper-right corner of the main interface. Therefore, you need to recalculate the position of the main form as it changes size.

Since the three buttons only appear when the subform is maximized, it is necessary to determine the status of the current subform in the Idel event to determine whether the three buttons are hidden or visible.

The specific code is as follows:

Unit Ufrmmain;
Interface
Uses
Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
Dialogs, Actnlist, Appevnts, Imglist, Stdctrls, Buttons, Stdactns, Menus,
Toolwin, Comctrls;
Type
Max Minimum button
Tmdibutton = Class (TBITBTN)
Private
Public
Because it is inherited by TBITN, you need to mask the message that it gets focus
Procedure WndProc (var message:tmessage); Override
End
Twindowminimize = Class (Twindowaction)
Public
button to change the current first child window state to wsminimize;
Procedure Executetarget (Target:tobject); Override
End
Twindowrestore = Class (Twindowaction)
Public
button to change the current first child window state to Wsnormal;
Procedure Executetarget (Target:tobject); Override
End
Tfrmmain = Class (Tform)
Save Windows Minimize, restore, and close icons
Mdiimagelist:timagelist;
When the program is not busy, to determine the largest, minimize the button should be hidden or visible
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
Simulate MDIForm form minimize, close and resume a button object
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
Establish the Close button
Btnclose: = Tmdibutton.create (self);
Btnclose.visible: = false;
Btnclose.parent: = self;
Btnclose.width: = 16;
Btnclose.height: = 15;
Set off function action
Windowclose: = Twindowclose.create (nil);
Specify its icon
Windowclose.actionlist: = Actmdiform;
Windowclose.imageindex: = 2; Close
Windowclose.caption: = ';
Associating an action with a button
Btnclose.action: = Windowclose;
Btnclose.bringtofront;
Btnclose.visible: = false;
Create a Minimize button
Btnmin: = Tmdibutton.create (self);
Btnmin.visible: = false;
Btnmin.parent: = self;
Btnmin.width: = 16;
Btnmin.height: = 15;
Create a Minimize function action
Windowminimize: = Twindowminimize.create (nil);
Specify its icon
Windowminimize.actionlist: = Actmdiform;
Windowminimize.caption: = ';
Windowminimize.imageindex: = 0;
Associating an action with a button
Btnmin.action: = windowminimize; Minimize
Btnmin.bringtofront;
Btnmin.visible: = false;
Set Up Restore Function button
Btnrestore: = Tmdibutton.create (self);
Btnrestore.visible: = false;
Btnrestore.parent: = self;
Btnrestore.width: = 16;
Btnrestore.height: = 15;
Create a Restore function action
Windowrestore: = Twindowrestore.create (nil);
Specify its icon
Windowrestore.actionlist: = Actmdiform;
Windowrestore.caption: = ';
Windowrestore.imageindex: = 1;
Associating an action with a button
Btnrestore.action: = Windowrestore;
Btnrestore.bringtofront;
Btnrestore.visible: = false;
Set the button position, the position remains in the main interface is relatively unchanged
Setmdiformactionpos;
End
Procedure Tfrmmain.applicationevents1idle (Sender:tobject;
var done:boolean);
Var
Show:boolean;
Begin
Displays three buttons when the status of the current subform is maximized
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
Set the relative position of the button unchanged
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
Releasing resources
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.