Lazarus

Programming => General => Topic started by: dietmar on August 02, 2018, 07:01:17 pm

Title: "Darkening" main window when modal form is shown above it
Post by: dietmar on August 02, 2018, 07:01:17 pm
Hi,

some programs are "darkening" oder "greying out the main form, when a modul window is shown above it so the user easier knows that he has to to something with the modal form here... Sorry I don't know how to describe this better...

Any ideas how to achieve this?

Many thx and happy coding,
Dietmar
Title: Re: "Darkening" main window when modal form is shown above it
Post by: engkin on August 02, 2018, 08:50:07 pm
Windows only. Maybe overlay another form on top of it with some transparency. Along:
Code: Pascal  [Select][+][-]
  1. var
  2.   f:TForm;
  3. begin
  4.   f:=TForm.Create(nil);
  5.   f.ShowInTaskBar:=stNever;
  6.   f.BoundsRect := self.BoundsRect;
  7.   f.AlphaBlend:=True;
  8.   f.AlphaBlendValue:=120;
  9.   f.Color:=clBlack;
  10.   f.Show;
Title: Re: "Darkening" main window when modal form is shown above it
Post by: Handoko on August 02, 2018, 09:24:51 pm
How about this one:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, Forms, Controls, Graphics, StdCtrls, ExtCtrls, BGRAShape;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     BGRAShape1: TBGRAShape;
  16.     Button1: TButton;
  17.     Timer1: TTimer;
  18.     procedure Button1Click(Sender: TObject);
  19.     procedure Timer1Timer(Sender: TObject);
  20.   end;
  21.  
  22. var
  23.   Form1: TForm1;
  24.  
  25. implementation
  26.  
  27. {$R *.lfm}
  28.  
  29. { TForm1 }
  30.  
  31. procedure TForm1.Button1Click(Sender: TObject);
  32. var
  33.   ModalForm: TForm;
  34. begin
  35.   BGRAShape1.Left        := 0;
  36.   BGRAShape1.Top         := 0;
  37.   BGRAShape1.Width       := Width;
  38.   BGRAShape1.Height      := Height;
  39.   BGRAShape1.FillOpacity := 0;
  40.   BGRAShape1.Visible     := True;
  41.   Timer1.Enabled         := True;
  42.   ModalForm              := TForm.Create(Self);
  43.   ModalForm.Caption      := 'This is a modal form';
  44.   ModalForm.Top          := Top  + 100;
  45.   ModalForm.Left         := Left + 120;
  46.   ModalForm.ShowModal;
  47.   ModalForm.Free;
  48.   BGRAShape1.Visible     := False;
  49.   Timer1.Enabled         := False;
  50. end;
  51.  
  52. procedure TForm1.Timer1Timer(Sender: TObject);
  53. begin
  54.   BGRAShape1.FillOpacity := BGRAShape1.FillOpacity + 1;
  55.   if (BGRAShape1.FillOpacity > 50) then
  56.     Timer1.Enabled := False;
  57. end;
  58.  
  59. end.

Note:
It needs BGRAControls package.
Title: Re: "Darkening" main window when modal form is shown above it
Post by: lainz on August 03, 2018, 04:54:43 pm
Good way Handoko.

The problem is that it will not cover controls (TCustomControl), because BGRAShape uses the canvas of the form. But it will cover controls that in fact are like paintboxes (TGraphicControl) if placed on top of them, for example TBCButton, TSpeedButton, TImage and so on.

Title: Re: "Darkening" main window when modal form is shown above it
Post by: lainz on August 04, 2018, 03:23:03 am
I think on another solution just now.

Take a screenshot of the contents of the form and gray them. Put the shot in a tpanel paint event in top of everything.
Title: Re: "Darkening" main window when modal form is shown above it
Post by: taazz on August 04, 2018, 03:52:15 am
Hi,

some programs are "darkening" oder "greying out the main form, when a modul window is shown above it so the user easier knows that he has to to something with the modal form here... Sorry I don't know how to describe this better...

Any ideas how to achieve this?

Many thx and happy coding,
Dietmar
well I'll let the darkening to others, how about fading out? Set the forms AlphaBlend to true and the AlphaBlendValue to something around 128 (or anything else you like). That will have the same, although a bit more annoying, effect.
Title: Re: "Darkening" main window when modal form is shown above it
Post by: Handoko on August 04, 2018, 04:04:03 am
Both suggestions (using TPanel and AlphaBlend) sound possible.

8-) But lucky for Linux users. You can instal ConfigCompiz and enable Dim Inactive and/or Opacity then those effects will be automatically happened. I just tested, it worked.
Title: Re: "Darkening" main window when modal form is shown above it
Post by: guest60499 on August 04, 2018, 04:15:18 am
A program called ConEmu (https://github.com/Maximus5/ConEmu) implements this when the window is not focused. It's rather complicated and written in C++; I apologize for not being able to find the relevant code for you (this might be close (https://github.com/Maximus5/ConEmu/blob/master/src/ConEmuTh/Display.cpp)).

I suspect ConEmu just dims the contents it is drawing because it has so much control over its components.

The solution with Lazarus controls is almost assuredly an overlay. Are transparent TComponents not possible?
Title: Re: "Darkening" main window when modal form is shown above it
Post by: taazz on August 04, 2018, 04:18:43 am
A program called ConEmu (https://github.com/Maximus5/ConEmu) implements this when the window is not focused. It's rather complicated and written in C++; I apologize for not being able to find the relevant code for you (this might be close (https://github.com/Maximus5/ConEmu/blob/master/src/ConEmuTh/Display.cpp)).

I suspect ConEmu just dims the contents it is drawing because it has so much control over its components.

The solution with Lazarus controls is almost assuredly an overlay. Are transparent TComponents not possible?
I'm assuming you mean TWinControls (in which case), they do not support transparency no, they are mostly system controls and if the system does not support it the control do not either. There are a number of workarounds to add the functionality in for some systems but not all of them.
Title: Re: "Darkening" main window when modal form is shown above it
Post by: lainz on August 04, 2018, 04:21:22 am
Sorry I did it quickly and I don't tested it cross platform *maybe you need to change the take screenshot part to work cross platform, but is possible to work on linux Mac and windows.

Here is the code attached. Requires bgracontrols.


edit> take screenshot thread https://forum.lazarus-ide.org/index.php?topic=27896.0
Title: Re: "Darkening" main window when modal form is shown above it
Post by: Handoko on August 04, 2018, 04:38:08 am
It worked on my system.
Title: Re: "Darkening" main window when modal form is shown above it
Post by: lainz on August 04, 2018, 04:41:09 am
Thanks for testing. I tested it on Mac but I get a black image. So it currently works only on Linux and Windows.
Title: Re: "Darkening" main window when modal form is shown above it
Post by: taazz on August 04, 2018, 05:02:08 am
Thanks for testing. I tested it on Mac but I get a black image. So it currently works only on Linux and Windows.
a couple of comments after a fast read of your code.
if the virtualscreen is parented by the panel why is the panel needed? From your first message I assumed that the panel will be invisible and host only the virtualscreen when the button is pressed a screen grab of the form is added to the virtualscreen and the panel become visible and on top hiding the rest of the controls. If virtualscreen can do that it self what is the use of the panel and why the GetFormImage is not enough?
Title: Re: "Darkening" main window when modal form is shown above it
Post by: lainz on August 04, 2018, 06:09:55 am
All good points!

Is too late here and an entire day of work and technical support. Any improvements are welcome.

I just made a fast copy paste from here and there that's the main reason.
Title: Re: "Darkening" main window when modal form is shown above it
Post by: taazz on August 04, 2018, 06:18:42 am
All good points!

Is too late here and an entire day of work and technical support. Any improvements are welcome.

I just made a fast copy paste from here and there that's the main reason.
Just making sure that I did not miss something crucial.
Title: Re: "Darkening" main window when modal form is shown above it
Post by: lainz on August 04, 2018, 04:20:02 pm
Just making sure that I did not miss something crucial.

No problem, thanks for pointing them, these are good improvements.
Title: Re: "Darkening" main window when modal form is shown above it
Post by: Soner on August 04, 2018, 07:17:44 pm
Engkin's code with some modifications works very good on windows:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   f:TForm;
  4. begin
  5.   f:=TForm.Create(self);
  6.   f.ShowInTaskBar:=stNever;
  7.   f.BorderStyle:=bsNone;
  8.   f.BoundsRect := Rect(0,0,ClientWidth, ClientHeight);
  9.   f.AlphaBlend:=True;
  10.   f.AlphaBlendValue:=120;
  11.   f.Color:=clBlack;
  12.   f.Parent:=self;
  13.   f.Show;
  14.  
  15.   MessageDlg('Hello, this is modal dialog.', mtInformation, [mbOK],0);
  16.  
  17.   f.Free;
  18. end;
  19.  

Maybe it works on linux (with enabled compositor) and macos too.
Title: Re: "Darkening" main window when modal form is shown above it
Post by: lainz on August 04, 2018, 07:57:19 pm
Cool! We have a lot of good options to choose.  :)
Title: Re: "Darkening" main window when modal form is shown above it
Post by: dietmar on August 07, 2018, 09:17:21 pm
Thanks a lot for the many appreciated answers!

Dietmar
Title: Re: "Darkening" main window when modal form is shown above it
Post by: dietmar on August 09, 2018, 06:21:31 pm
Hm, the next challenge could be not to just darkening the background, but to "blur" it, so that you e.g. cannot read the background form any more... ;-)
Title: Re: "Darkening" main window when modal form is shown above it
Post by: lainz on August 10, 2018, 10:41:21 pm
Hm, the next challenge could be not to just darkening the background, but to "blur" it, so that you e.g. cannot read the background form any more... ;-)

With the example I submitted just apply blur with BGRABitmap and that's all.
Title: Re: "Darkening" main window when modal form is shown above it
Post by: dietmar on August 12, 2018, 07:28:26 pm
Thanks a lot! I will give it a try... ;-)
TinyPortal © 2005-2018