Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Screen saver coding

Status
Not open for further replies.

sggaunt

Programmer
Jul 4, 2001
8,620
GB
I am trying to adapt a sample screen saver project I found so it just displays an image of the desktop (aim is to get any image)
The original took a snapshot of the desktop and used this to create a moving 'spot' but had no commenting at all!!.
I have tried to remove the 'spot' stuff and get just the desktop image but just get a blank screen.

This is my revised code there is more to it than this but I think the problem lies somewhere in here.
The project file seems to make a copy of the desktop into the variable desktopbitmap and this seems to be loaded into the main image at formactivate, but I must be missing something (simple)?
Project source
Code:
program Simple;

uses
  Forms, SysUtils, Windows, Graphics, Classes,
  Ssave in 'SSave.pas' {Scrn},
  Ssetup in 'SSetup.pas' {Setup},
  Globals in 'Globals.pas';

{$E SCR}
{$R *.RES}

var
  MySem       : THandle;
  Arg1, Arg2  : String;
  DemoWnd     : HWnd;
  MyRect      : TRect;
  MyCanvas    : TCanvas;
  x, y,
  dx, dy      : Integer;
  MyBkgBitmap,
  InMemBitmap : TBitmap;
  ScrWidth,
  ScrHeight   : Integer;

begin
  Arg1 := UpperCase(ParamStr(1));
  Arg2 := UpperCase(ParamStr(2));

  if (Copy(Arg1,1,2) = '/P') or (Copy(Arg1,1,2) = '-P') or
     (Copy(Arg1,1,1) = 'P') then
    SSMode := ssPreview;

  if (Copy(Arg1,1,2) = '/C') or (Copy(Arg1,1,2) = '-C') or
     (Copy(Arg1,1,1) = 'C') or (Arg1 = '') then
    SSMode := ssConfig;

  MySem := CreateSemaphore(nil,0,1,'SimpleSaverSemaphore');
  if ((MySem <> 0) and (GetLastError = ERROR_ALREADY_EXISTS)) then
     begin
        CloseHandle(MySem);
        Halt;
     end;

  Application.Initialize;

  if SSMode = ssPreview then
      begin
         DemoWnd := StrToInt(Arg2);
         while not IsWindowVisible(DemoWnd) do
            Application.ProcessMessages;
         GetWindowRect(DemoWnd,MyRect);
         ScrWidth := MyRect.Right-MyRect.Left+1;
         ScrHeight := MyRect.Bottom-MyRect.Top+1;
         MyRect := Rect(0,0,ScrWidth-1,ScrHeight-1);
         MyCanvas := TCanvas.Create;
         MyCanvas.Handle := GetDC(DemoWnd);
         MyCanvas.Pen.Color := clWhite;
         x := (ScrWidth div 2)-16;
         y := (ScrHeight div 2)-16;
         dx := 1;
         dy := 1;
         MyBkgBitmap := TBitmap.Create;
         with MyBkgBitmap do
             begin
                Width := ScrWidth;
                Height := ScrHeight;
             end;
         MyBkgBitmap.Canvas.FillRect(Rect(0,0,ScrWidth-1,ScrHeight-1));
         InMemBitmap := TBitmap.Create;
         with InMemBitmap do
            begin
               Width := ScrWidth;
               Height := ScrHeight;
            end;
        while IsWindowVisible(DemoWnd) do
          begin
             InMemBitmap.Canvas.CopyRect(MyRect,MyBkgBitmap.Canvas,MyRect);
             InMemBitmap.Canvas.Draw(x,y,Application.Icon);
             MyCanvas.CopyRect(MyRect,InMemBitmap.Canvas,MyRect);
             Sleep(10);
             Application.ProcessMessages;
             if (x = 0) or (x = (ScrWidth-33)) then
                dx := -dx;
             if (y = 0) or (y = (ScrHeight-33)) then
                dy := -dy;
             x := x + dx;
             y := y+dy;
          end;
       MyBkgBitmap.Free;
       InMemBitmap.Free;
       MyCanvas.Free;
       CloseHandle(MySem);
       Halt;
  end;

  DesktopBitmap := TBitmap.Create;
  with DesktopBitmap do
     begin
        Width := Screen.Width;
        Height := Screen.Height;
     end;

  // copy desktop into desktopbitmap
  BitBlt(DesktopBitmap.Canvas.Handle,0,0,Screen.Width,Screen.Height,
          GetDC(GetDesktopWindow),0,0,SrcCopy);

  if SSMode = ssConfig then
     begin
        Application.CreateForm(TSetup, Setup);
  end
  else
      Application.CreateForm(TScrn,Scrn);

  Application.Run;

  DesktopBitmap.Free;
  CloseHandle(MySem);
end.
Main file
Code:
procedure TScrn.FormActivate(Sender: TObject);
var
  Dummy : Boolean;
begin
  if LoadingApp then
     begin
       LoadingApp := False;
       Scrn.Top := 0;
       Scrn.Left := 0;
       Scrn.Width := Screen.Width;
       Scrn.Height := Screen.Height;
       // Image becomes desktop snapshot??
       Scrn.Image1.Picture.Bitmap := DesktopBitmap;
       Mouse.X := -1;
       Mouse.Y := -1;
       Application.OnIdle := Trigger;
       SetWindowPos(Handle,HWND_TOPMOST,0,0,0,0,SWP_NOSIZE + SWP_NOMOVE);
       SystemParametersInfo(SPI_SCREENSAVERRUNNING,1,@Dummy,0);
       CursorOff;
       Scrn.Visible := True;
       SetCapture(Scrn.Handle);
     end;
end;

Steve: Delphi a feersum engin indeed.
 
Are you starting the application with the correct parameters /p as first param and a handle of a windows (not sure about argument2). If you dont set a param you wont go into
if SSMode = ssPreview then
begin
part.

My second idea is
Code:
procedure TScrn.FormActivate(Sender: TObject);
var
  Dummy : Boolean;
begin
  if LoadingApp then
     begin
       LoadingApp := False;
       Scrn.Top := 0;
       Scrn.Left := 0;
       Scrn.Width := Screen.Width;
       Scrn.Height := Screen.Height;
       
        // WHY DONT YOU LOAD THE IMAGE FROM FILE,BECAUSE
        // BITMAP IS USED ONLY FOR CAPTURING THE SCREEN
        // OR USE OpenPictureDialog
       Scrn.Image1.Picture.LoadFromFile('c:\MyImage1.bmp');
     
       Mouse.X := -1;
       Mouse.Y := -1;
       Application.OnIdle := Trigger;
       SetWindowPos(Handle,HWND_TOPMOST,0,0,0,0,SWP_NOSIZE + SWP_NOMOVE);
       SystemParametersInfo(SPI_SCREENSAVERRUNNING,1,@Dummy,0);
       CursorOff;
       Scrn.Visible := True;
       SetCapture(Scrn.Handle);
     end;
end;

Good Luck
 
I'm starting it ouside of the IDE by selecting Test from the right click popup of the .scr file (as you would with any screen saver) I am assuming this passes the correct parameter.

I have tried preloading an image and loading from file at the that point, niether works, I think the problem is somthing to do with the image initalisation or the way the form is initalised.



Steve: Delphi a feersum engin indeed.
 
Sorry to bump this, but I know someone who has done 'savers' has been online today, just trying to attract their attention.


Steve: Delphi a feersum engin indeed.
 
Doh! Sorry same again.


Steve: Delphi a feersum engin indeed.
 
hi Steve

First thing, did you know you can run it inside the IDE by adding the appropriate parameter in the menu (Run | Parameters) edit box?

Will have another look at your code now...

lou
 
Bumped F.A.O LucieLastic

[red]GNBM 4th Feb[/red] More on and other neat UK stuff at forum1091
Steve: Delphi a feersum engin indeed.
 
hi Steve

I must admit, I don't know what's wrong. Do you know if BitBlt is returning 0? did you have the original version of the code working? And have you tried running it within the IDE?

Sorry I'm not more helpful.

lou
 
Cheers Lou I will try that.

I cant remember if I have tried it in the IDE.
Yes the original did work, It took a snapshot of the desktop and displayed a moving round window onto this against a black background.


[red]GNBM 4th Feb[/red] More on and other neat UK stuff at forum1091
Steve: Delphi a feersum engin indeed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top