Fading Controls

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.
nashing_nasherAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SlavakCommented:
If your program should run on Windows NT/2000/XP you can try layered windows functions.

Look at
 SetLayeredWindowAttributes
 UpdateLayeredWindow

0
nashing_nasherAuthor Commented:
Oh, sorry, the program has to run on win 9x/2K/NT4/ME/XP
0
SlavakCommented:
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
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

nashing_nasherAuthor Commented:
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
SlavakCommented:
Take it to image, now it possible
0
nashing_nasherAuthor Commented:
how do you get the TPanel to the bitmap image (sorry, i'm a newbie) and how do you draw it back?
TBitmap?
0
SlavakCommented:
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
nashing_nasherAuthor Commented:
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
SlavakCommented:
I compiled it without errors
0
nashing_nasherAuthor Commented:
OK. Thanx very, very, very much. Just one FINAL question. Is there any way to make it less flickery:-/
0
nashing_nasherAuthor Commented:
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
SlavakCommented:
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
nashing_nasherAuthor Commented:
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
SlavakCommented:
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
Slick812Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nashing_nasherAuthor Commented:
This is a superb method of fading TGroupBoxes both in and out, flickerlessly, smoothly and cleanly.
0
nashing_nasherAuthor Commented:
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
nashing_nasherAuthor Commented:
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
nashing_nasherAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.