Sign in to follow this  
Wampus

Screensaver Template

Recommended Posts

Does anyone know of any good blank template projects or wizards for a DirectX screensaver? I have the Moire sample from the 8.1 SDK, which I'm sure I could pull from for a project of my own. I just thought I'd ask before I started on that as a blank project would save some time. Thanks for any info.

Share this post


Link to post
Share on other sites
You can go ahead and try this... it comes from Windows 98 Programming from the Ground up...
(I am pretty much just typing it out of the book, so I haven't tried it...)

//Code for a minimal screensaver
//
#include <windows.h>
#include <scrnsave.h>

char str[80] = "Windows 98 Screensaver";

int delay = 200;

LRESULT WINAPI ScreenSaverProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HDC hdc;
static unsigned int timer;
static RECT srcdim;
static SIZE size;
static int X = 0, Y = 0;
static HBRUSH hBlkBrush;
static TEXTMETRIC tm;

switch(message)
{
case WM_CREATE:
timer = SetTimer(hwnd, 1, delay, NULL);
hBlkBrush = (HBRUSH) GetStockObject(BLACK_BRUSH);
break;

case WM_ERASEBKGND:

hdc = GetDC(hwnd);

//get coordinates of the screen
GetClientRect(hwnd, &scrdim);

//erase the screen
SelectObject(hdc, hBlkBrush);
PatBlt(hdc, 0, 0, scrdim.right, scrdim.bottom, PATCOPY);

//get and save height and length of string
GetTextMetrics(hdc, &tm);
GetTextExtentPoint32(hdc, str, strlen(str), &size);

ReleaseDC(hwnd, hdc);
break;

case WM_TIMER:
hdc = GetDC(hwnd);

//erase previous output
SelectObject(hdc, hBlkBrush);
PatBlt(hdc, X, Y, X + size.cx, Y + size.cy, PATCOPY);

//move the string to a new location
X += 10; Y += 10;
if(X > scrdim.right) X = 0
if(Y > scrdim.bottom) Y = 0

//output string
SetBkColor(hdc, RGB(0,0,0));
SetTextColor(hdc, RGB(0,255,255));
TextOut(hdc, X, Y, str, strlen(str));

ReleaseDC(hwnd, hdc);
break;

case WM_DESTROY:
KillTimer(hwnd, timer);
break;

default:
return DefScreenSaverProc(hwnd, message, wParam, lParam);
}

return 0;
}


//placeholder dialog box function
BOOL WINAPI ScreenSaverCofigureDialog(HWND, hdwnd, UNIT message, WPARAM wParam, LPARAM lParam)
{
return 0;
}

//no class to register
BOOL WINAPI RegisterDialogClasses(HANDLE hInst)
{
return 1;
}


//it requires the following resource file:
/*
#include <windows.h>
#include <scnsave.h>

ID_APP ICON SCRICON.ICO

STRINGTABLE
{
IDS_DESCRIPTION "My Screen Saver #1"
}

*/





Like I said, I haven't tried compiling it, so I have no idea if it works...

Share this post


Link to post
Share on other sites
A screensaver is a normal windows application that's invoked with some specific command-line parameters. I can't remember them off the top of my head, but things like "yourscreensaver /p window-handle" when the user is previewing it, and you're supposed to render into the window with the given handle. You get the idea.

A google for "screensaver tutorial" would give you some good results. As soon as you know the command-line parameters needed, just add code to handle them in your own application-framework and you've got a screensaver.

Share this post


Link to post
Share on other sites
Well, I gave it a shot, but its giving me some link errors.

saver.obj : error LNK2019: unresolved external symbol _DefScreenSaverProc@16 referenced in function _ScreenSaverProc@16

I think I'll just work backwards from the Moire project.

Share this post


Link to post
Share on other sites
Quote:
Original post by Wampus

I think I'll just work backwards from the Moire project.

Can you possibly post the code from the Moire project?

Share this post


Link to post
Share on other sites
Yes, Moe's screensaver works, after a few corrections:


#include <windows.h>
#include <scrnsave.h>

char str[80] = "Windows Screensaver";

int delay = 200;

#pragma comment ( lib, "scrnsave.lib" )
#pragma comment ( lib, "comctl32.lib" )


LRESULT WINAPI ScreenSaverProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HDC hdc;
static unsigned int timer;
static RECT scrdim;
static SIZE size;
static int X = 0, Y = 0;
static HBRUSH hBlkBrush;
static TEXTMETRIC tm;

switch(message)
{
case WM_CREATE:
timer = (UINT) SetTimer(hwnd, 1, delay, NULL);
hBlkBrush = (HBRUSH) GetStockObject(BLACK_BRUSH);
break;

case WM_ERASEBKGND:

hdc = GetDC(hwnd);

//get coordinates of the screen
GetClientRect(hwnd, &scrdim);

//erase the screen
SelectObject(hdc, hBlkBrush);
PatBlt(hdc, 0, 0, scrdim.right, scrdim.bottom, PATCOPY);

//get and save height and length of string
GetTextMetrics(hdc, &tm);
GetTextExtentPoint32(hdc, str, (int) strlen(str), &size);

ReleaseDC(hwnd, hdc);
break;

case WM_TIMER:
hdc = GetDC(hwnd);

//erase previous output
SelectObject(hdc, hBlkBrush);
PatBlt(hdc, X, Y, X + size.cx, Y + size.cy, PATCOPY);

//move the string to a new location
X += 10; Y += 10;
if(X > scrdim.right) X = 0;
if(Y > scrdim.bottom) Y = 0;

//output string
SetBkColor(hdc, RGB(0,0,0));
SetTextColor(hdc, RGB(0,255,255));
TextOut(hdc, X, Y, str, (int) strlen(str));

ReleaseDC(hwnd, hdc);
break;

case WM_DESTROY:
KillTimer(hwnd, timer);
break;

default:
return DefScreenSaverProc(hwnd, message, wParam, lParam);
}

return 0;
}


//placeholder dialog box function
BOOL WINAPI ScreenSaverConfigureDialog(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
return 0;
}

//no class to register
BOOL WINAPI RegisterDialogClasses(HANDLE hInst)
{
return 1;
}



... and resource.rc ...


#include <windows.h>
#include <scrnsave.h>

ID_APP ICON SCRICON.ICO

STRINGTABLE
{
IDS_DESCRIPTION "My Screen Saver #1"
}



... and don't forget to put scricon.ico into the project's directory.

Share this post


Link to post
Share on other sites
Quote:
Original post by ReaVeR-Andrey
Yes, Moe's screensaver works, after a few corrections:

*** Source Snippet Removed ***
... and resource.rc ...
*** Source Snippet Removed ***
... and don't forget to put scricon.ico into the project's directory.

Excellent! I think I might have to try playing around with it some time... (when I have time).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this