Implementation of normal windows in Delphi

Source: Internet
Author: User

Abstract:

In the VCL library of Delphi, for ease of use and implementation, the application object application creates
A hidden window for processing message responses. This window makes the program developed with VCL different from other windows.
It may be malformed. Through in-depth analysis of VCL, this article provides
You can solve the problem by modifying three lines of code in the program project file without any changes in the original programming method.

I. Introduction

Windows applications written using the VCL class library provided by Delphi have a significantly different windows window
Feature-the system menu of the main window is different from the system menu on the taskbar. Generally, the system menu of the Main Window
There are six menu items, and the taskbar System menu has only three menu items. In actual use, we found that the program developed with VCL has
The following embarrassment:

1) not beautiful enough. This is positive. If it is not consistent with the standard, it will naturally look malformed.
2) There is no animation effect when the main window is minimized.
3) The window cannot be normally tiled with other windows.
4) the taskbar System menu has the highest priority. If a modal window exists, the entire program can still be
Small, contrary to the design of the modal window.

The problem of minimizing the animation effect in the main window has been solved through
The showwinnoanimate function solves the problem, but the remaining issues remain. Although in most cases
What is the impact of collation, but it is unacceptable in some situations that pursue professional effects. Because c ++ builder and Delph
I uses the same class library, so the above problems also exist in Windows applications written using C ++ builder.
. I have discussed this issue in my previous articles (which can be found in the home of Gump). The description at that time seems basic.
Is a clever method, and I found that method by accident. The task in this article is
Library for some analysis, illustrate the principle of doing so, and then give a method that only uses three lines of code, completely solve Delphi
Abnormal window.

Ii. Principles

2.1 application creation process

The following is a typical application Delphi project file. We noticed that there was
For reference of the initialize method of ion object, our analysis starts from here:

Program project1;

Uses
Forms,
Unit1 in 'unit1. pa' {form1 };

{$ R *. Res}

Begin
Application. initialize;
Application. createform (tform1, form1 );
Application. Run;
End.

The hidden window is created by the Application object. Where does the application object come from? In Delphi
In the code editing window, press Ctrl and click Application to find that the application object is in the forms. Pas ticket
It is one of several global objects defined in the element. This is not enough. We want to know where the application object is.
You must have successfully created an instance of the tapplication class before you can reference it. Think about the code
Will it be executed before application. initialize? By the way, it is the code in the initialization code segment. Earnest
After debugging the VCL source code, you can know that many units in VCL have initialization code segments. When you start the Delphi program
First, execute the code of the initialization code segment in each unit in the order of uses to complete all initialization operations.
To initialize the application.
The object is created in the initialization code segment of a unit. Take "tapplication. Create" as the key
In the VCL source code directory, we found the application object created in the controls. Pas unit.
. In the initialization code segment of the controls. Pas Unit, there is a call to the initcontrols process,
The implementation of initcontrols is as follows:

Unit controls;
...
Initialization
...
Initcontrols;

Procedure initcontrols;
Begin
...
Mouse: = tmouse. Create;
Screen: = tscreen. Create (NiL );
Application: = tapplication. Create (NiL );
...
End;

Well, our analysis completes the first step here, because to solve the problem of abnormal windows, we must
Before the application object is initialized, it is very important to understand the initialization process of the application.


2.2 islibrary Variables

Islibrary variables are one of the global flag variables defined in the system. Pas unit. If the islibrary value is
True indicates that the program module is a dynamic link library, and vice versa is an executable program. Some processes in the VCL class library
Perform different actions based on different values of the variable. This variable is used to solve the abnormal window of Delphi.
The problem plays a key role. As mentioned above, for convenience, a view is created during application object initialization.
Invisible window (that is, the window with "tapplication" as the class name seen by tools such as spy ++),
It is precisely because of this invisible window that the program developed with Delphi presents many malformed features. Okay, if I
You can remove this invisible window (also remove the taskbar System menu) and replace it with our application main window.
Isn't all the problems solved? Is it simple, but does it need to perform major VCL source code operations? If
Isn't it a little wrong? Of course, the answer is no. Otherwise, there will be no such article. What I want to say here is,
In the next analysis, we will see that the so-called "programming path, saving your mind" is unintentional in the tapplication design.
The practice of inserting Liu leaves an interface for us to solve this problem. Without source code analysis, you may need to bypass the circle,
In fact, we can see that there are not many and just a few things left for us by the design of genius. Open tapplication
Class constructor create, we will find such a line of code.

Constructor tapplication. Create (aowner: tcomponent );
Begin
...
If not islibrary then createhandle;
...
End;

If the program module is not a dynamic link library, execute createhandle, while createhandle
The work done in the help is as follows: "If there is no application window, create a", here "should
Use the program window "is the invisible window mentioned above, that is, the culprit. It is used in the tapplication class.
Fhandle variable to save its window handle. Here, different actions are completed based on the islibrary value, because
Message loops are generally not required in the state Link Library, but the application object is still used to develop a dynamic link library using VCL,
So we have the design here. Well, we only need to cheat the application object and put the islibrary
If the value is true, the createhandle execution can be filtered out, removing this annoying window. Assign values to islibrary
The code should also be placed in the initialization code segment of a unit, and because of the initialization code
The code in the section is executed in the order of contained units. To ensure that the islibrary
If the value is true, in the project file, we must put the unit containing the value assignment code before the forms unit, as shown below (
Assume that the unit name is unitdllexe. Pas ):

Program template;

Uses
Unitdllexe in 'unitdllexe. pa ',
Forms,
Formmain in 'formmain. pa' {mainform },
...

The following is a list of unitdllexe. Pas code:

Unit unitdllexe;

Interface

Implementation

Initialization
Islibrary: = true;
// Tell applciation object, which is a dynamic link library and does not need to create a hidden window.
End.

Okay. Compile and run the command. We can see that the system menu on the taskbar is deleted because no hidden window is created.
The system menu of the main window is replaced, and the main window can be arranged and tiled normally with other windows Windows. However
The problem is that Windows cannot be minimized. What's going on? Keep track of the old method.


2.3 minimize the Main Window

Minimization is a system command, and ultimately it is to call the API function defwindowproc to minimize the window, so I
We can find the wmsyscommand function in tcustomform to respond to the wm_syscommand message without any difficulty.
Write down and redirect the minimal message to application. wndproc for processing:

Procedure tcustomform. wmsyscommand (VAR message: twmsyscommand );
Begin
With message do
Begin
If (condition type and $ fff0 = SC _minimize) and (application. mainform = self) then
Application. wndproc (tmessage (Message ))
...
End;
End;

In application. wndproc, the minimize method of application is called when the response is minimized,
Therefore, the crux of the problem must be in the minimize process.

Procedure tapplication. wndproc (VAR message: tmessage );
...
Begin
...
With message do
Case MSG
Wm_syscommand:
Case wparam and $ fff0
SC _minimize: Minimize;
SC _restore: Restore;
Else
Default;
...
End;

Finally, find tapplication. Minimize and you will understand everything. For the defwindowproc function
The call has no effect. Why? The createhandle is filtered out because we have cheated the application object before.
The application object does not have the window needed to respond to the message, so the handle fhandle is 0,
Of course the call fails. If you can direct fhandle to the main application window, the problem can be solved.

Procedure tapplication. Minimize;
Begin
...
Defwindowproc (fhandle, wm_syscommand, SC _minimize, 0 );
// Here, the fhandle value is 0.
...
End;


Iii. Implementation

Borland's talented people did not care about the design. Once again, we found a solution to the problem. By analyzing me
We know that there is no hidden window in the dynamic link library developed with VCL to receive Windows messages (createhandle
Do not execute), but in the dynamic link library, if you want to display the window, you need another parent window. How to solve this problem
Question? The VCL designer designs the fhandle variable that saves invisible window handles as Writable, so we can
Simply assign a value to fhandle to provide a parent window for the Child window to be displayed. For example, in a dynamic link
To display the form in the library plug-in, we usually use the dynamic
A function of the Linked Library is passed in and assigned to the application. Handle of the Dynamic Linked Library, which is similar:

Procedure setapplicationhandle (mainappwnd: hwnd)
Begin
Application. Handle: = mainappwnd;
End;

Well, since aplication. Handle is actually a window handle internally used to respond to messages
We have removed the invisible window that should be created, so we only need to provide a window handle to replace that
Isn't the handle of the previously redundant hidden window ready? Where can I find such a window? The main window of the application is just above
Select, so the following code is available.

Program template;

Uses
Unitdllexe in 'unitdllexe. pa ',
Forms,
Formmain in 'formmain. pa' {mainform };

{$ R *. Res}

Begin
Application. initialize;
Application. createform (tformmain, formmain );
Application. Handle: = formmain. Handle;
Application. Run;
End.

As a result, all problems are solved. You do not need to make any changes to the VCL source code, and do not need to make any changes to the original program
Modify the code by adding two lines of code to the project file and one line in unitdllexe. Pas. There are three lines of code in total.
Make your application window as normal as any standard Windows window.

1) the taskbar and window title bar have the same system menu.
2) When the main window is minimized, there will be an animated effect.
3) The window can be normally arranged and tiled with other windows.
4) When a modal window exists, its parent window cannot be operated.

The above implementation code is used in all versions of Delphi.


>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>
<<<
Note:
1. Add formmain. bringtofront after application. Handle: = formmain. Handle;
Otherwise, the main form is inactive (not focused ).

2. Such an application may have hidden potential and unknown issues. We recommend that you do not use it easily.

 

Implementation of normal windows in Delphi

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.