Bookstore

Recent

Author Topic: "Darkening" main window when modal form is shown above it  (Read 3815 times)

dietmar

  • Jr. Member
  • **
  • Posts: 83
"Darkening" main window when modal form is shown above it
« 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
Lazarus 1.8.4 with FPC 3.0.4 on Windows10 64Bit

engkin

  • Hero Member
  • *****
  • Posts: 2513
Re: "Darkening" main window when modal form is shown above it
« Reply #1 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;

Handoko

  • Hero Member
  • *****
  • Posts: 3434
  • My goal: build my own game engine using Lazarus
Re: "Darkening" main window when modal form is shown above it
« Reply #2 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.

lainz

  • Hero Member
  • *****
  • Posts: 3502
    • Lainz
Re: "Darkening" main window when modal form is shown above it
« Reply #3 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.

« Last Edit: August 03, 2018, 04:56:51 pm by lainz »

lainz

  • Hero Member
  • *****
  • Posts: 3502
    • Lainz
Re: "Darkening" main window when modal form is shown above it
« Reply #4 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.

taazz

  • Hero Member
  • *****
  • Posts: 5365
Re: "Darkening" main window when modal form is shown above it
« Reply #5 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.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Handoko

  • Hero Member
  • *****
  • Posts: 3434
  • My goal: build my own game engine using Lazarus
Re: "Darkening" main window when modal form is shown above it
« Reply #6 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.

guest60499

  • Guest
Re: "Darkening" main window when modal form is shown above it
« Reply #7 on: August 04, 2018, 04:15:18 am »
A program called 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).

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?

taazz

  • Hero Member
  • *****
  • Posts: 5365
Re: "Darkening" main window when modal form is shown above it
« Reply #8 on: August 04, 2018, 04:18:43 am »
A program called 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).

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.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

lainz

  • Hero Member
  • *****
  • Posts: 3502
    • Lainz
Re: "Darkening" main window when modal form is shown above it
« Reply #9 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
« Last Edit: August 04, 2018, 04:37:16 am by lainz »

Handoko

  • Hero Member
  • *****
  • Posts: 3434
  • My goal: build my own game engine using Lazarus
Re: "Darkening" main window when modal form is shown above it
« Reply #10 on: August 04, 2018, 04:38:08 am »
It worked on my system.

lainz

  • Hero Member
  • *****
  • Posts: 3502
    • Lainz
Re: "Darkening" main window when modal form is shown above it
« Reply #11 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.

taazz

  • Hero Member
  • *****
  • Posts: 5365
Re: "Darkening" main window when modal form is shown above it
« Reply #12 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?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

lainz

  • Hero Member
  • *****
  • Posts: 3502
    • Lainz
Re: "Darkening" main window when modal form is shown above it
« Reply #13 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.

taazz

  • Hero Member
  • *****
  • Posts: 5365
Re: "Darkening" main window when modal form is shown above it
« Reply #14 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.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64