Thursday, September 26, 2024

Dim the main application form when a modal dialog box is displayed

I don't remember how I stumbled upon this web page, but the idea of dimming the main form is quite interesting. To give a quick explanation: whenever a modal dialog is displayed, apparently a message gets sent to a program's main form; this message causes a predefined dialog to appear that 'dims' whatever windows the program is currently displaying. When the modal dialog closes, another message gets sent that causes the dimming window to hide itself. Modal dialogs stop the program from working until they are closed so they should be used sparingly1.

There is a minor bug with this code - in the 'Display' procedure, reference is made to 'MainForm', but it is not mentioned that one has to include the main form in the unit's 'uses' clause. I found what is virtually the same code elsewhere: here the 'main form' bug is handled by calling 'Display' with the calling form as a parameter.

Both versions of this code use the OnModalBegin and OnModalEnd methods of the TApplicationEvents component; unfortunately I discovered that the version of TApplicationEvents defined in the Delphi version that I use (the old but excellent version 7) lacks these methods. So how could I write something that would achieve the same effect with the tools that I have?

My first version went as follows: I went through the test program (the blog manager, not that it matters) looking for calls to 'showmodal' - there are five or six scattered throughout the program. I bracketed these calls with two calls to SendMessage that would send a message to the Dimmer form. When the program starts up, the Dimmer form saves its handle in a variable in the data module, so every form in the program can send a message to the Dimmer form without the form having to be declared as used by each form (itself an interesting idea).

procedure TDimmerForm.FormCreate(Sender: TObject); begin AlphaBlend:= true; AlphaBlendValue:= 128; BorderStyle:= bsNone; dimhandle:= handle; // this variable is saved in the data module end; procedure TDimmerForm.DefaultHandler(var Message); begin case TMessage (Message).msg of SC_startdim: show; SC_enddim: hide; else inherited DefaultHandler (Message); end; end; ... in a calling form - sendmessage (dimhandle, SC_startdim, 0, 0); showmodal; sendmessage (dimhandle, SC_enddim, 0, 0); ...

As can be seen, I didn't bother with a 'display' procedure; as I know that the entire program is maximised/full screen, the dimmer form is also set to be maximised and is simply shown or hidden.This system worked although it was somewhat tedious to add all those sendmessages. I thought that there should be a simpler solution, and indeed there is.

In the data module that also functions as a library holding functions, procedures and queries that are used at least once by other parts of the program, I added a new function as shown below.

Function MyShowModal (sender: tobject): integer; begin DimmerForm.Show; result:= TForm (sender).ShowModal; DimmerForm.Hide; end;

The dimmer form appears only in the 'uses' clause of the data module; no other form knows of its existence. As a result, I can call the dimmer form directly by its automatically defined form variable which normally gets deleted. I then replaced all the appearances of 'showmodal' with 'myshowmodal (self)' with the global replace function, compiled (no errors) and ran the program. Every time a modal form appears (which actually is not very often in the blog manager program), everything behind it is dimmed.

Neat but ultimately useless. This is what can be termed 'eye candy'.

Internal links
[1] 225



This day in history:

Blog #Date TitleTags
28726/09/2010Prague log #4Holiday, Prague
28826/09/2010Prague log #5Holiday, Prague
63526/09/2013More holidayDBA, Statistics, Sandy Denny, Old recordings
117526/09/2018TurnitinDBA
125926/09/2019Two important eventsDBA, Health
133826/09/2020Priority procedures cross referencerProgramming, Priority tips
166826/09/2023"The great escape", aka "The 7/4 song"Song writing

No comments: