Solved

Fading Controls

Posted on 2002-06-05
19
439 Views
Last Modified: 2010-04-04
OK. The flickering was ok, but what I do need is a way to fade a TGroupBox in and out (two seperate procedures or functions) as well as their contents. Any ideas:-/ P.S. Really, really sorry about the last question.
0
Comment
Question by:nashing_nasher
  • 11
  • 7
19 Comments
 
LVL 3

Expert Comment

by:Slavak
ID: 7058774
If your program should run on Windows NT/2000/XP you can try layered windows functions.

Look at
 SetLayeredWindowAttributes
 UpdateLayeredWindow

0
 

Author Comment

by:nashing_nasher
ID: 7058866
Oh, sorry, the program has to run on win 9x/2K/NT4/ME/XP
0
 
LVL 3

Expert Comment

by:Slavak
ID: 7058888
Ok, I don't know a way to do a window semi-transparent on win9x. So the way I see to do the same visual effect is:

Fade Out:

1. Get appearence of the window to the bitmap image.
2. Hide the window
3. show/draw the image semitransparent (you can use AlphaBlend function) in loop.

0
 

Author Comment

by:nashing_nasher
ID: 7058902
I managed to fade the window itself out using the AlphaBlend and AlphaBlendValue properties of the form, but i don't want the window to be transparent, only my TPanel and its contents. Is this possible?
0
 
LVL 3

Expert Comment

by:Slavak
ID: 7058914
Take it to image, now it possible
0
 

Author Comment

by:nashing_nasher
ID: 7058930
how do you get the TPanel to the bitmap image (sorry, i'm a newbie) and how do you draw it back?
TBitmap?
0
 
LVL 3

Expert Comment

by:Slavak
ID: 7059194
Try something like this:

procedure FadeControl(AControl : TWinControl);
Var
 Bitmap  : TBitmap;
 Canvas  : TControlCanvas;

 Func    : TBlendFunction;
 R       : TRect;

 Opacity : Integer;
Begin
 If (AControl = nil) or (AControl.Parent = nil) or not AControl.Visible Then
   Exit;

 Bitmap  := TBitmap.Create;
 Canvas  := TControlCanvas.Create;
 Try
   With AControl Do Begin
    Bitmap.Width   := Width;
    Bitmap.Height  := Height;

    Canvas.Control :=  AControl;
    Bitmap.Canvas.CopyRect(ClientRect, Canvas, ClientRect);

    Hide;

    Func.BlendOp             := AC_SRC_OVER;
    Func.BlendFlags          := 0;
    Func.AlphaFormat         := 0;


    Canvas.Control         := Parent;
    Parent.DoubleBuffered  := True;

    Opacity := 255;
    While Opacity > 20 Do Begin
      R := ClientRect;
      OffsetRect(R, Left, Top);
      InvalidateRect(Parent.Handle, @R, False);
      Parent.Update;

      Func.SourceConstantAlpha := Opacity;
      AlphaBlend(Canvas.Handle, Left, Top, Width, Height,
                 Bitmap.Canvas.Handle, 0, 0, Width, Height,
                 Func);

      Sleep(50);
      Dec(Opacity, 20);
    End;
   End;

   Canvas.Control.Invalidate;

 finally
   Bitmap.Free;
   Canvas.Free;
 End;
End;

procedure TForm1.Button1Click(Sender: TObject);
begin
 FadeControl(Panel1);
end;
0
 

Author Comment

by:nashing_nasher
ID: 7059217
Almost there:)
Just one more problem.

This line returns the error "Missing Operator or semi colon":
AlphaBlend(Canvas.Handle, Left, Top, Width, Height,

And this line returns the error "Statement expected but expression of type 'boolean' found":
Func);

How do I fix this:-/
0
 
LVL 3

Expert Comment

by:Slavak
ID: 7059230
I compiled it without errors
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:nashing_nasher
ID: 7059403
OK. Thanx very, very, very much. Just one FINAL question. Is there any way to make it less flickery:-/
0
 

Author Comment

by:nashing_nasher
ID: 7059422
Oh....I'm really, really, really sorry about this and very gratefull to you for your help, but how do I fade the control back in again:-/
0
 
LVL 3

Expert Comment

by:Slavak
ID: 7059524
Save the bitmap from fadeout.
Give it to fade in and reverse the loop.

Do it less flickery may be a little difficult with delphi own components. I suppose to use some 3rd party image components.
For example, TDIBComponents from Peter Morris:
http://www.torry.net/vcl/graphics/bitmap/dibcomponents.zip
0
 

Author Comment

by:nashing_nasher
ID: 7059527
Oh....I'm really, really, really sorry about this and very gratefull to you for your help, but how do I fade the control back in again:-/
0
 
LVL 3

Expert Comment

by:Slavak
ID: 7059582
Try it:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, imgList;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Label1: TLabel;
    Edit1: TEdit;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    ControlImage : TBitmap;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure FadeOutControl(AControl : TWinControl; var AControlImage : TBitmap);
Var
 Bitmap  : TBitmap;
 Canvas  : TControlCanvas;

 Func    : TBlendFunction;
 R       : TRect;

 Opacity : Integer;
Begin
 If (AControl = nil) or (AControl.Parent = nil) or not AControl.Visible Then
   Exit;

 Bitmap  := TBitmap.Create;
 Canvas  := TControlCanvas.Create;
 Try
   With AControl Do Begin
    Bitmap.Width   := Width;
    Bitmap.Height  := Height;

    Canvas.Control :=  AControl;
    Bitmap.Canvas.CopyRect(ClientRect, Canvas, ClientRect);

    Hide;

    Func.BlendOp             := AC_SRC_OVER;
    Func.BlendFlags          := 0;
    Func.AlphaFormat         := 0;

    Canvas.Control         := Parent;
    Parent.DoubleBuffered  := True;

    Opacity := 255;
    While Opacity > 20 Do Begin
      R := ClientRect;
      OffsetRect(R, Left, Top);
      InvalidateRect(Parent.Handle, @R, False);
      Parent.Update;

      Func.SourceConstantAlpha := Opacity;
      AlphaBlend(Canvas.Handle, Left, Top, Width, Height,
                 Bitmap.Canvas.Handle, 0, 0, Width, Height,
                 Func);

      Sleep(50);
      Dec(Opacity, 20);
    End;
   End;

   Canvas.Control.Invalidate;

   AControlImage.Assign(Bitmap);
 finally
   Bitmap.Free;
   Canvas.Free;
 End;
End;

procedure FadeInControl(AControl : TWinControl; AControlImage : TBitmap);
Var
 Bitmap  : TBitmap;
 Canvas  : TControlCanvas;

 Func    : TBlendFunction;
 R       : TRect;

 Opacity : Integer;
Begin
 If (AControl = nil) or (AControl.Parent = nil) or AControl.Visible or
 (AControlImage = nil) Then Exit;

 Bitmap  := TBitmap.Create;
 Bitmap.Assign(AControlImage);

 Canvas  := TControlCanvas.Create;
 Try
   With AControl Do Begin
    Func.BlendOp             := AC_SRC_OVER;
    Func.BlendFlags          := 0;
    Func.AlphaFormat         := 0;

    Canvas.Control         := Parent;
    Parent.DoubleBuffered  := True;

    Opacity := 0;
    While Opacity < 230 Do Begin
      R := ClientRect;
      OffsetRect(R, Left, Top);
      InvalidateRect(Parent.Handle, @R, False);
      Parent.Update;

      Func.SourceConstantAlpha := Opacity;
      AlphaBlend(Canvas.Handle, Left, Top, Width, Height,
                 Bitmap.Canvas.Handle, 0, 0, Width, Height,
                 Func);

      Sleep(50);
      Inc(Opacity, 20);
    End;

    Show;
   End;

 finally
   Bitmap.Free;
   Canvas.Free;
 End;
End;



procedure TForm1.Button1Click(Sender: TObject);
begin
 ControlImage.Free;
 ControlImage := TBitmap.Create;
 FadeOutControl(Panel1, ControlImage);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
 FadeInControl(Panel1, ControlImage);
end;

end.
0
 
LVL 33

Accepted Solution

by:
Slick812 earned 50 total points
ID: 7059892
hello nashing_nasher . Here's the code for a program that fades a GroupBox Out and back In. It has a SpeedButton sbut_FadeIn, that when clicked will do the fade. The GroupBox has 2 CheckBoxes, 3 Radio Buttons and a SpeedButton on it. This was done in Delphi 5, and should work in various Win systems



unit Fader1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Buttons, StdCtrls;

type
ThreadRec = record
    Interval: Cardinal;
    isTiming: Boolean;
    ID: Byte;
    end;

  TForm1 = class(TForm)
    sbut_FadeIn: TSpeedButton;
    GroupBox1: TGroupBox;
    {the next controls are all on the GroupBox1}
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    RadioButton3: TRadioButton;
    SpeedButton1: TSpeedButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure sbut_FadeInClick(Sender: TObject);
  private
    { Private declarations }
    FadeBmp: TBitmap;
    FadeOut: Boolean;
    FadeNum: Byte;
    hThread: THandle;
    pColor1: TColor;
    procedure GetBlendBmps;
    procedure DoThreadTimer;
    procedure FadeIt;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  TimerR: ThreadRec;

implementation

{$R *.DFM}

function ThreadFunc(Parameter: Pointer): Integer; stdcall;
begin
Result := 0;
while TimerR.isTiming do
  begin
  {isTiming cuts this thread off, it uses a while loop with a
  SleepEx to get a timer interval effect}
  SleepEx(TimerR.Interval, False);

  if TimerR.isTiming then
    Form1.FadeIt;
  end;
TimerR.ID := 0;
EndThread(Result);
{no code below EndThread will be executed}
end;

procedure TForm1.DoThreadTimer;
var
ThreadId: LongWord;
begin
{The interval should be MORE than 10 or alot of the processor
time cycles are used, if you need an interval less than 10 then
you may want to set the Thread Priority higher with
SetThreadPriority( )}
if (TimerR.ID <> 0) or (not Assigned(FadeBmp)) then Exit;
{TimerR.ID is set to 0 when the Thread ends, so check for 0
to make sure you don't start a thread if one is still running}
{if TimerR.Interval < 10 then
TimerR.Interval := 10;}
TimerR.Interval := 92;
TimerR.ID := 1;
CloseHandle(hThread);
{since a thread may be created more than once,
you should Close the Handle}

{if you wanted to use more than one thread then you should keep track
of each thread's handle and close it}
hThread := BeginThread(nil, 0, @ThreadFunc, nil, 0, ThreadId);
if hThread < 2 then
FreeAndNil(FadeBmp);
end;

procedure TForm1.FadeIt;
var
GboxDC: HDC;
begin
{FadeOut determines if the GroupBox Fades In or Out}
if FadeOut then
  begin
  GboxDC := GetWindowDC(GroupBox1.Handle);
  {I use GetWindowDC to avoid some child controls not showing}
  BitBlt(GboxDC, 0, 0, GroupBox1.Width, GroupBox1.Height, FadeBmp.Canvas.Handle, (14-FadeNum)*GroupBox1.Width, 0, SRCCOPY);
  ReleaseDC(GroupBox1.Handle,GboxDC);
  end else
  BitBlt(Canvas.Handle, GroupBox1.Left, GroupBox1.Top, GroupBox1.Width, GroupBox1.Height, FadeBmp.Canvas.Handle, FadeNum*GroupBox1.Width, 0, SRCCOPY);

Inc(FadeNum);
if FadeNum > 14 then
  begin
  FadeNum := 0;
  TimerR.isTiming := False;
  if FadeOut then
    GroupBox1.Hide
    else
    begin
    GroupBox1.Show;
    FreeAndNil(FadeBmp);
    end;
  end;
end;

procedure TForm1.GetBlendBmps;
Type
  TpRGB = packed record
    b, g, r : Byte;
    end;
var
DestRGBPixel: ^TpRGB;
Bmp1RGBPixel: ^TpRGB;
Bmp2RGBPixel: ^TpRGB;
y, x, pic: Integer;
BlendFactor  : real;
BlankBmp, ResultBmp, GroupBmp: TBitmap;
Amount,Amt: Real;
GboxDC: HDC;
begin
Amount := 0.063;
GroupBmp := TBitmap.Create;
GroupBmp.Width := GroupBox1.Width;
GroupBmp.Height := GroupBox1.Height;
Groupbmp.PixelFormat  := pf24Bit;
GboxDC := GetWindowDC(GroupBox1.Handle);
BitBlt(GroupBmp.Canvas.Handle,0,0,GroupBmp.Width,GroupBmp.Height,GboxDC,0,0, SRCCOPY);
ReleaseDC(GroupBox1.Handle,GboxDC);

BlankBmp := TBitmap.Create;
BlankBmp.PixelFormat := pf24bit;
BlankBmp.Canvas.Brush := GroupBox1.Parent.Brush;
BlankBmp.Width := GroupBmp.Width;
BlankBmp.Height := GroupBmp.Height;

ResultBmp := TBitmap.Create;
ResultBmp.Width       := GroupBmp.Width;
ResultBmp.Height      := GroupBmp.Height;
ResultBmp.PixelFormat := pf24Bit;
Amt := Amount;
if Assigned(FadeBmp) then
FreeAndNil(FadeBmp);

  FadeBmp := TBitmap.Create;
  FadeBmp.Canvas.Brush := GroupBox1.Parent.Brush;
  FadeBmp.PixelFormat := pf24bit;
  FadeBmp.Width := GroupBmp.Width*15;
  FadeBmp.Height := GroupBmp.Height;

for pic := 0 to 14 do
  begin // 1 to 14
  BlendFactor := 1.0 - Amt;

  For y := 0 to GroupBmp.Height -1 do
    begin // y to height
    Bmp1RGBPixel := GroupBmp.ScanLine[y];
    Bmp2RGBPixel := BlankBmp.ScanLine[y];
    DestRGBPixel := ResultBmp.ScanLine[y];
    For x := 0 to GroupBmp.Width -1 do
      Begin
      DestRGBPixel.r := ROUND(BlendFactor * Bmp2RGBPixel.r + Amt * Bmp1RGBPixel.r);
      DestRGBPixel.g := ROUND(BlendFactor * Bmp2RGBPixel.g  + Amt * Bmp1RGBPixel.g);
      DestRGBPixel.b := ROUND(BlendFactor * Bmp2RGBPixel.b  + Amt * Bmp1RGBPixel.b);
      Inc(DestRGBPixel);
      Inc(Bmp1RGBPixel);
      Inc(Bmp2RGBPixel);
      End;
    end; //y to height
     Amt := Amt+Amount;
     BitBlt(FadeBmp.Canvas.Handle, pic*GroupBmp.Width, 0, ResultBmp.Width, ResultBmp.Height,
      ResultBmp.Canvas.Handle, 0, 0, SRCCOPY);
    end; // 1 to 14
BlankBmp.Free;
ResultBmp.Free;
GroupBmp.Free;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
FadeOut := False;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
TimerR.isTiming := False;
CloseHandle(hThread);
FreeAndNil(FadeBmp);
end;

procedure TForm1.sbut_FadeInClick(Sender: TObject);
begin
{This SpeedButton will fade the GroupBox Out and back In}
FadeOut := not FadeOut;
FadeNum := 0;
if FadeOut then
  begin
  {if the GroupBox is not visible don't try fade}
  if not GroupBox1.Visible then
    Exit;
  pColor1 := GroupBox1.Parent.Brush.Color;
  {get parent's color}
  GetBlendBmps;
  end else
  if GroupBox1.Visible or (not Assigned(FadeBmp)) or
     (pColor1 <> GroupBox1.Parent.Brush.Color) then
    begin
    {test to see if visible, a FadeBmp exists, and Parent Color}
    GroupBox1.Show;
    FreeAndNil(FadeBmp);
    Exit;
    end;

TimerR.isTiming := True;
DoThreadTimer;
end;

end.


- - - - -  -- - - - - - - - - - -- -  - - - --  -- - -

This works with a TGroupBox, but if you want to use another control type then it should be a TWinControl for this code. Ask Questions if you need some more info.
0
 

Author Comment

by:nashing_nasher
ID: 7060015
This is a superb method of fading TGroupBoxes both in and out, flickerlessly, smoothly and cleanly.
0
 

Author Comment

by:nashing_nasher
ID: 7061379
One last question. With this code, I want to fade the TGroupBox out, set a couple of properties (i.e. Left, Top, Width and Height) and then fade it in again. I can do the first two parts, but when I try to fade it in again, I get the error message:
"Exception EAccessViolation in module TWINDY.EXE at 0023A68
 Access violation at address 00423A68 in module 'TWINDY.EXE' Read of address FFFFFFFF"
I have figured out that this means that you can not fade out and then in in the same function, or in another function. It must be done by two seperate button clicks. How do I solve this problem:-/
0
 

Author Comment

by:nashing_nasher
ID: 7061410
Ok sorted that out, just now I want to get it to fade in an update TBitmap of the TGroupBox in its new position and with its new dimensions. How do I do that?
0
 

Author Comment

by:nashing_nasher
ID: 7061413
Ok sorted that out, just now I want to get it to fade in an update TBitmap of the TGroupBox in its new position and with its new dimensions. How do I do that?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to center only a line in richedit? 4 51
Performance of SQL statement 37 95
Dynamically Created Query 3 48
Graphics32 under Delphi 10.1 Berlin 2 34
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

937 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now