Solved

Creating a screen saver

Posted on 1998-05-18
11
290 Views
Last Modified: 2010-04-06
A long time ago i had created some screen savers with Delphi 1.0 for windows 3.1. Now in windows 95 i want to know how i can show a preview of my screen saver (in the propetries window of the screen)

    Thanks
       Harisis
0
Comment
Question by:harisis
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 5

Expert Comment

by:ronit051397
Comment Utility
Change the executable file extention,say project1.exe to project1.scr and copy it to the windows directory. Automatically, it's properties will be shown in the Display properties Screen. For more information see http://www.mindspring.com/~cityzoo/scrnsavr.html
at the bottom of the html page.
0
 
LVL 4

Expert Comment

by:d003303
Comment Utility
Yo,
this description deals with the 16bit interface for screen savers in Win3.x. To get the possibilities e.g. to draw a preview you need to implement the 32bit interface.

Slash/d003303
0
 

Author Comment

by:harisis
Comment Utility
When i select my screen saver, it runs with a "/p" parameter. What is this? This is i want to know. The little screen in "Display parameters" is black.
0
 
LVL 1

Expert Comment

by:ssite
Comment Utility
To me /p means /preview ? makes sense ?
0
 

Author Comment

by:harisis
Comment Utility
Thank you very much.... I Know that already. I want to know HOW i will show the preview...
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 1

Expert Comment

by:pjdb
Comment Utility
You have the following parameters :
/c -> config (should be the default in the M$ standard)
/p -> preview
/a -> password
/s -> screen saver

However M$ ask also to manage the -c, -p, -a and -s parameters.

JDB
0
 

Author Comment

by:harisis
Comment Utility
What is the M$?
I Know all the parameters. My problem is what code i must write for the /p parameter.
0
 
LVL 1

Expert Comment

by:pjdb
Comment Utility
Sorry i've check the bad box, i was sending a comment.
M$ is Microsoft.

JDB

0
 
LVL 4

Accepted Solution

by:
d003303 earned 120 total points
Comment Utility
Hi harisis,
here's some code that does the job. The main thing is that after the /p parameter there's a second one containing the window handle of the preview window. First code is the main screensaver form. Put it in your link path and add it to your form repository. The rest is a sample screen saver app.

// screen saver form class
unit ssaverform;

interface

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

type
  TScreenSaverForm = class(TForm)
  private
    PreviewMode : Boolean;
    OldX,
    OldY        : Integer;
  protected
    procedure CreateParams(var Params: TCreateParams); override;
    procedure DoShow; override;
    procedure Deactivate; override;
    procedure KeyDown(var Key: Word; Shift: TShiftState); override;
    procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
    procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
      X, Y: Integer); override;
  public
    constructor Create(AOwner: TComponent); override;
    procedure InitSaver; virtual;
    function TerminateScreenSaver: Boolean;
  end;

var
  ScreenSaverForm: TScreenSaverForm;

implementation

{$R *.DFM}

const MinDistance = 2;

procedure TScreenSaverForm.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);
  if not PreviewMode
   then Exit;
  StrCopy(Params.WinClassName, 'WindowsScreenSaverClass');
  Params.Style := WS_CHILD or WS_CLIPCHILDREN or WS_OVERLAPPED or WS_VISIBLE;
  Params.ExStyle := WS_EX_LEFT or WS_EX_LTRREADING or WS_EX_RIGHTSCROLLBAR;
  Params.WindowClass.style := CS_VREDRAW or CS_HREDRAW or CS_SAVEBITS or CS_DBLCLKS;
  Params.WindowClass.lpszClassName := 'WindowsScreenSaverClass';
end;

function TScreenSaverForm.TerminateScreenSaver: Boolean;
begin
  Result := not PreviewMode;
  if Result
   then Close;
end;

procedure TScreenSaverForm.DoShow;
begin
  if not PreviewMode
   then Cursor := crNone;
  ShowWindow(Application.Handle, SW_HIDE);
  inherited DoShow;
end;

procedure TScreenSaverForm.Deactivate;
begin
  TerminateScreenSaver;
  inherited Deactivate;
end;

procedure TScreenSaverForm.KeyDown(var Key: Word; Shift: TShiftState);
begin
  TerminateScreenSaver;
  inherited KeyDown(Key, Shift);
end;

procedure TScreenSaverForm.MouseMove(Shift: TShiftState; X, Y: Integer);
begin
  if not ((OldX = 0) and (OldY = 0)) then
   begin
     if (Abs(OldX - X) > MinDistance) or
        (Abs(OldY - Y) > MinDistance)
      then if Visible
            then TerminateScreenSaver;
   end;
  OldX := X;
  OldY := Y;
  inherited MouseMove(Shift, X, Y);
end;

procedure TScreenSaverForm.MouseDown(Button: TMouseButton; Shift: TShiftState;
                           X, Y: Integer);
begin
  TerminateScreenSaver;
  inherited MouseDown(Button, Shift, X, Y);
end;

constructor TScreenSaverForm.Create(AOwner: TComponent);
var rc : TRect;
begin
  if (AOwner = nil) and (ParentWindow <> 0)
   then PreviewMode := true
   else PreviewMode := false;
  inherited Create(AOwner);
  Top := 0;
  Left := 0;
  OldX := 0;
  OldY := 0;
  if PreviewMode = true then
   begin
     Windows.GetWindowRect(ParentWindow, rc);
     Height := rc.Bottom - rc.Top;
     Width := rc.Right - rc.Left;
   end
  else
   begin
     Height := Screen.Height;
     Width := Screen.Width;
   end;
end;

procedure TScreenSaverForm.InitSaver;
begin
  // as a non-abstract interface
end;

end.

// sample app screen saver form code
unit _saver1;

interface

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

type
  TMyScreenSaver = class(TScreenSaverForm)
    Timer1: TTimer;
    procedure Timer1Timer(Sender: TObject);
  private
    FSpeed,
    FMaxSize : Integer;
  public
    procedure InitSaver; override;
  end;

var
  MyScreenSaver: TMyScreenSaver;

implementation

{$R *.DFM}

uses _cfg;

procedure TMyScreenSaver.InitSaver;
var MaxSizePercent : Integer;
begin
  inherited;
  ReadSettings(FSpeed, MaxSizePercent);
  FMaxSize := (Height div 100) * MaxSizePercent;
  Timer1.Interval := 10 * (101 - FSpeed);
  Timer1.Enabled := true;
end;

procedure TMyScreenSaver.Timer1Timer(Sender: TObject);
var Size,
    X,
    Y    : Integer;
begin
  Timer1.Enabled := false;
  inherited;
  Canvas.Pen.Style := psClear;
  Canvas.Brush.Color := (Random(255) shl 16) or (Random(255) shl 8) or Random(255);
  Size := Random(FMaxSize);
  X := Random(Width - Size);
  Y := Random(Height - Size);
  Canvas.Ellipse(X, Y, X + Size, Y + Size);
  Timer1.Enabled := true;
end;

end.

// sample app screen saver form object
inherited MyScreenSaver: TMyScreenSaver
  Caption = 'MyScreenSaver'
  Color = clBlack
  PixelsPerInch = 96
  TextHeight = 13
  object Timer1: TTimer
    Enabled = False
    OnTimer = Timer1Timer
    Left = 12
    Top = 8
  end
end

// sample app config form code
unit _cfg;

interface

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

type
  TConfigForm = class(TForm)
    GroupBox1: TGroupBox;
    Button1: TButton;
    Button2: TButton;
    ScrollBar1: TScrollBar;
    Slow: TLabel;
    Fast: TLabel;
    GroupBox2: TGroupBox;
    SpinEdit1: TSpinEdit;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  protected
    procedure DoShow; override;
  end;

procedure ReadSettings(var Speed, PercentSize : Integer);
procedure WriteSettings(Speed, PercentSize : Integer);

var
  ConfigForm: TConfigForm;

implementation

{$R *.DFM}

uses Registry;

procedure ReadSettings(var Speed, PercentSize : Integer);
var Registry : TRegistry;
begin
  Speed := 50;
  PercentSize := 10;
  Registry := TRegistry.Create;
  try
    Registry.RootKey := HKEY_CURRENT_USER;
    if not Registry.OpenKey('SOFTWARE\Slash\ScreenSaverTest', false)
     then Exit;
    try
      Speed := Registry.ReadInteger('Speed');
    except
      Speed := 50;
    end;
    try
      PercentSize := Registry.ReadInteger('PercentSize');
    except
      PercentSize := 10;
    end;
  finally
    Registry.Free;
  end;
end;

procedure WriteSettings(Speed, PercentSize : Integer);
var Registry : TRegistry;
begin
  Registry := TRegistry.Create;
  try
    Registry.RootKey := HKEY_CURRENT_USER;
    Registry.OpenKey('SOFTWARE\Slash\ScreenSaverTest', true);
    Registry.WriteInteger('Speed', Speed);
    Registry.WriteInteger('PercentSize', PercentSize);
  finally
    Registry.Free;
  end;
end;

procedure TConfigForm.DoShow;
begin
  ShowWindow(Application.Handle, SW_HIDE);
  inherited DoShow;
end;

procedure TConfigForm.Button1Click(Sender: TObject);
begin
  WriteSettings(ScrollBar1.Position, SpinEdit1.Value);
  Close;
end;

procedure TConfigForm.Button2Click(Sender: TObject);
begin
  Close;
end;

procedure TConfigForm.FormCreate(Sender: TObject);
var NewSpeed,
    NewPercentage : Integer;
begin
  ReadSettings(NewSpeed, NewPercentage);
  ScrollBar1.Position := NewSpeed;
  SpinEdit1.Value := NewPercentage;
end;

end.

// sample app config form object
object ConfigForm: TConfigForm
  Left = 237
  Top = 127
  ActiveControl = ScrollBar1
  BorderIcons = []
  BorderStyle = bsDialog
  Caption = 'Configure Settings'
  ClientHeight = 169
  ClientWidth = 157
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object GroupBox1: TGroupBox
    Left = 4
    Top = 4
    Width = 149
    Height = 69
    Caption = 'Redraw Speed'
    TabOrder = 0
    object Slow: TLabel
      Left = 8
      Top = 20
      Width = 23
      Height = 13
      Caption = 'Slow'
    end
    object Fast: TLabel
      Left = 120
      Top = 20
      Width = 20
      Height = 13
      Caption = 'Fast'
    end
    object ScrollBar1: TScrollBar
      Left = 8
      Top = 40
      Width = 133
      Height = 16
      TabOrder = 0
    end
  end
  object Button1: TButton
    Left = 8
    Top = 140
    Width = 67
    Height = 25
    Caption = '&OK'
    Default = True
    ModalResult = 1
    TabOrder = 1
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 80
    Top = 140
    Width = 65
    Height = 25
    Caption = '&Cancel'
    ModalResult = 2
    TabOrder = 2
    OnClick = Button2Click
  end
  object GroupBox2: TGroupBox
    Left = 4
    Top = 80
    Width = 149
    Height = 53
    Caption = 'Maximum Circle Size'
    TabOrder = 3
    object Label1: TLabel
      Left = 64
      Top = 24
      Width = 76
      Height = 13
      Caption = '% of screen size'
    end
    object SpinEdit1: TSpinEdit
      Left = 8
      Top = 20
      Width = 53
      Height = 22
      MaxLength = 3
      MaxValue = 100
      MinValue = 1
      TabOrder = 0
      Value = 0
    end
  end
end

// sample app project code
program saver1;

uses
  SysUtils,
  Forms,
  _cfg in '_cfg.pas' {ConfigForm},
  _saver1 in '_saver1.pas' {MyScreenSaver};

{$E scr}

{$R *.RES}

var ActionParam : Char;
    Index,
    AParent : Integer;

begin
  if ParamCount = 0
   then Exit;
  ActionParam := LowerCase(ParamStr(1))[2];
  case ActionParam of
  'c': begin
         // configuration dialog
         Index := Pos(':', ParamStr(1));
         try
           AParent := StrToInt(Copy(ParamStr(1), Index + 1, Length(ParamStr(1)) - Index));
           ConfigForm := TConfigForm.CreateParented(AParent);
           ConfigForm.ShowModal;
         except
           // silent exception
         end;
       end;
  'p': begin
         // preview screensaver mode
         try
           AParent := StrToInt(ParamStr(2));
           MyScreenSaver := TMyScreenSaver.CreateParented(AParent);
           MyScreenSaver.InitSaver;
           MyScreenSaver.ShowModal;
         except
           // silent exception
         end;
       end;
  's': begin
         // normal screensaver mode
         Application.CreateForm(TMyScreenSaver, MyScreenSaver);
         MyScreenSaver.InitSaver;
         Application.Run;
       end;
  end;
end.


Have fun,
Slash/d003303
0
 
LVL 4

Expert Comment

by:d003303
Comment Utility
oops, i forgot the screen saver form object of the base class. Here it is:

object ScreenSaverForm: TScreenSaverForm
  Left = 200
  Top = 108
  BorderStyle = bsNone
  Caption = 'ScreenSaverForm'
  ClientHeight = 534
  ClientWidth = 782
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  FormStyle = fsStayOnTop
  KeyPreview = True
  PixelsPerInch = 96
  TextHeight = 13
end

Slash/d003303
0
 

Author Comment

by:harisis
Comment Utility
Thank you! This is an answer! I give you some points more...
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

771 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

14 Experts available now in Live!

Get 1:1 Help Now