home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 June / Chip_2002-06_cd1.bin / zkuste / delphi / kolekce / d6 / rxlibsetup.exe / {app} / units / SPLSHWND.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  2002-02-19  |  5.4 KB  |  189 lines

  1. {*******************************************************}
  2. {                                                       }
  3. {         Delphi VCL Extensions (RX)                    }
  4. {                                                       }
  5. {         Copyright (c) 2001,2002 SGB Software          }
  6. {         Copyright (c) 1997, 1998 Fedor Koshevnikov,   }
  7. {                        Igor Pavluk and Serge Korolev  }
  8. {                                                       }
  9. {*******************************************************}
  10.  
  11. unit SplshWnd;
  12.  
  13. interface
  14.  
  15. {$I RX.INC}
  16.  
  17. uses SysUtils, {$IFDEF WIN32} Windows, {$ELSE} WinTypes, WinProcs, {$ENDIF}
  18.   Messages, Classes, Graphics, Controls, Forms, StdCtrls, ExtCtrls, 
  19.   Animate, VCLUtils;
  20.  
  21. type
  22.   TSplashWindow = class(TForm)
  23.   private
  24.     { Private declarations }
  25.     FTextMessage: TLabel;
  26.     function GetMessageText: string;
  27.     procedure SetMessageText(const Value: string);
  28.   protected
  29.     procedure CreateParams(var Params: TCreateParams); override;
  30.   public
  31.     { Public declarations }
  32.     Image: TImage;
  33.     Animation: TAnimatedImage;
  34.     procedure CenterFor(Form: TCustomForm);
  35.     property MessageText: string read GetMessageText write SetMessageText;
  36.   end;
  37.  
  38. function ShowSplashWindow(Graphic: TGraphic; const MsgText: string;
  39.   Animate: Boolean; AlignForm: TCustomForm): TSplashWindow;
  40.  
  41. const
  42.   SplashStayOnTop: Boolean = True;  
  43.  
  44. implementation
  45.  
  46. uses MaxMin;
  47.  
  48. const
  49.   defSplashHeight = 64;
  50.   defImageLeft = 16;
  51.   defImageTop = 16;
  52.   defTextWidth = 238;
  53.   defTextLeft = 56;
  54.   defTextRight = 16;
  55.  
  56. function CreateSplashWindow: TSplashWindow;
  57. begin
  58. {$IFDEF CBUILDER}
  59.   Result := TSplashWindow.CreateNew(Application, 0);
  60. {$ELSE}
  61.   Result := TSplashWindow.CreateNew(Application);
  62. {$ENDIF}
  63.   with Result do begin
  64.     BorderIcons := [];
  65.     BorderStyle := bsNone;
  66.     if SplashStayOnTop then 
  67.       FormStyle := fsStayOnTop
  68.     else
  69.       FormStyle := fsNormal;
  70.     ClientHeight := defSplashHeight;
  71.     ClientWidth := defImageLeft + defTextRight + 32;
  72.     Enabled := False;
  73.     Font.Height := -11;
  74.     Font.Name := 'MS Sans Serif';
  75.     Font.Style := [];
  76.     Font.Color := clWindowText;
  77.     PixelsPerInch := 96;
  78.     Scaled := True;
  79.  
  80.     Image := TImage.Create(Result);
  81.     Image.Parent := Result;
  82.     Image.Left := defImageLeft;
  83.     Image.Top := defImageTop;
  84.     Image.Width := 32;
  85.     Image.Height := 32;
  86.     Image.AutoSize := False;
  87.     Image.Stretch := True;
  88.     Image.Visible := False;
  89.  
  90.     FTextMessage := TLabel.Create(Result);
  91.     FTextMessage.Parent := Result;
  92.     FTextMessage.Left := defTextLeft;
  93.     FTextMessage.Width := defTextWidth;
  94.     FTextMessage.AutoSize := False;
  95.     FTextMessage.Alignment := taCenter;
  96.     FTextMessage.WordWrap := True;
  97.  
  98.     Animation := TAnimatedImage.Create(Result);
  99.     Animation.Parent := Result;
  100.     Animation.Left := defImageLeft;
  101.     Animation.Top := defImageTop;
  102.     Animation.Width := 32;
  103.     Animation.Height := 32;
  104.     Animation.Active := False;
  105.     Animation.AutoSize := False;
  106.     Animation.Stretch := True;
  107.     Animation.Visible := False;
  108.   end;
  109. end;
  110.  
  111. function ShowSplashWindow(Graphic: TGraphic; const MsgText: string;
  112.   Animate: Boolean; AlignForm: TCustomForm): TSplashWindow;
  113. begin
  114.   Result := CreateSplashWindow;
  115.   with Result do begin
  116.     if Animate and (Graphic <> nil) then begin
  117.       Animation.Glyph := Graphic as TBitmap;
  118.       Animation.Visible := True;
  119. {$IFDEF RX_D3}
  120.       Animation.AsyncDrawing := True;
  121. {$ENDIF}
  122.       Animation.Active := True;
  123.     end
  124.     else if (Graphic <> nil) then begin
  125.       Image.Picture.Graphic := Graphic;
  126.       Image.Visible := True;
  127.     end
  128.     else begin
  129.       FTextMessage.Left := defImageLeft;
  130.     end;
  131.     FTextMessage.Caption := MsgText;
  132.     MessageText := MsgText;
  133.     CenterFor(AlignForm);
  134.     Show;
  135.     Update;
  136.   end;
  137. end;
  138.  
  139. procedure TSplashWindow.CreateParams(var Params: TCreateParams);
  140. begin
  141.   inherited CreateParams(Params);
  142.   Params.Style := Params.Style or WS_DLGFRAME;
  143. end;
  144.  
  145. function TSplashWindow.GetMessageText: string;
  146. begin
  147.   Result := FTextMessage.Caption;
  148. end;
  149.  
  150. procedure TSplashWindow.SetMessageText(const Value: string);
  151. var
  152.   TextRect: TRect;
  153. {$IFNDEF WIN32}
  154.   C: array[0..255] of Char;
  155. {$ENDIF WIN32}
  156.   VertOff: Integer;
  157. begin
  158.   TextRect := Rect(FTextMessage.Left, 0, Max(Screen.Width div 2 - 64,
  159.     defTextWidth), 0);
  160.   DrawText(Canvas.Handle,
  161.     {$IFDEF WIN32} PChar(Value), {$ELSE} StrPCopy(C, Value), {$ENDIF WIN32}
  162.     -1, TextRect, DT_CALCRECT or DT_WORDBREAK);
  163.   VertOff := (ClientHeight div 2) - ((TextRect.Bottom - TextRect.Top) div 2);
  164.   if VertOff < 0 then VertOff := 10;
  165.   TextRect.Top := VertOff;
  166.   TextRect.Bottom := TextRect.Bottom + VertOff;
  167.   FTextMessage.BoundsRect := TextRect;
  168.   ClientWidth := Max(ClientWidth, TextRect.Right + defTextRight);
  169.   ClientHeight := Max(ClientHeight, VertOff * 2);
  170.   if Value <> FTextMessage.Caption then begin
  171.     FTextMessage.Caption := Value;
  172.     Update;
  173.   end;
  174. end;
  175.  
  176. procedure TSplashWindow.CenterFor(Form: TCustomForm);
  177. var
  178.   NewLeft, NewTop: Integer;
  179.   DstRect: TRect;
  180. begin
  181.   if Form = nil then DstRect := Rect(0, 0, Screen.Width, Screen.Height)
  182.   else DstRect := Form.BoundsRect;
  183.   NewLeft := DstRect.Left + ((DstRect.Right - DstRect.Left) div 2) - (Width div 2);
  184.   NewTop := DstRect.Top + ((DstRect.Bottom - DstRect.Top) div 2) - (Height div 2);
  185.   SetBounds(NewLeft, NewTop, Width, Height);
  186. end;
  187.  
  188. end.
  189.