[Dev-C++] Newbie: building a screen-saver
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Philippe de R. <phi...@ww...> - 2003-01-16 15:22:11
|
Hello,
I am trying to compile the code below, which comes from Schildt's
Windows 2000 Programming book and builds a screen-saver.
Unfortunately, I am not sure what to build the project as: Win32 GUI
(it has no GUI), Win32 Console (it is not a console app), Win32 Static
Lib (not a static lib), or Win32 DLL (not a DLL).
Furthermore, I had to import the SCRNSAVE.LIB from a demo copy of VC++,
since dev-C++ does not include such libraries.
What should I build my project as (Win32 GUI, Win32 Console, etc.)?
Where can you get LIBs on the Web when you compile a project with
Dev-C++ that requires one?
Many thanks.
Philippe de Rochambeau
========
listing 1
// A miminal screen saver.
#include <windows.h>
#include <scrnsave.h>
// This is the message displayed on the screen.
char str[80] = "Screen Saver #1";
// This is timer delay.
int delay = 200;
// Screen Saver Function
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 = SetTimer(hwnd, 1, delay, NULL);
hBlkBrush = (HBRUSH) GetStockObject(BLACK_BRUSH);
break;
case WM_ERASEBKGND:
hdc = GetDC(hwnd);
// get coordinates of 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 string to 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 ScreenSaverConfigureDialog(HWND hdwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
return 0;
}
// No classes to register.
BOOL WINAPI RegisterDialogClasses(HANDLE hInst)
{
return 1;
}
listing 2
#include <windows.h>
#include <scrnsave.h>
ID_APP ICON SCRICON.ICO
STRINGTABLE
{
IDS_DESCRIPTION "My Screen Saver #1"
}
listing 3
// A Configurable Screen Saver
#include <windows.h>
#include <scrnsave.h>
#include <commctrl.h>
#include <cstring>
#include "scr.h"
#define DELAYMAX 999
#define MSGSIZE 80
// This is the screen saver's message.
char str[MSGSIZE+1] = "Screen Saver #2";
// This is timer delay.
long delay;
unsigned long datatype, datasize;
unsigned long result;
// This is a registry key.
HKEY hRegKey;
// Screen Saver Function
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:
// open screen saver key or create, if necessary
RegCreateKeyEx(HKEY_CURRENT_USER,
"Control Panel\\Screen Saver.MyScrSaver",
0, "Screen Saver", 0, KEY_ALL_ACCESS,
NULL, &hRegKey, &result);
// if key was created
if(result==REG_CREATED_NEW_KEY) {
// set its initial value
delay = 100;
RegSetValueEx(hRegKey, "delay", 0,
REG_DWORD, (LPBYTE) &delay, sizeof(DWORD));
RegSetValueEx(hRegKey, "message", 0,
REG_SZ, (LPBYTE) str, strlen(str)+1);
}
else { // key was already in registry
// get delay value
datasize = sizeof(DWORD);
RegQueryValueEx(hRegKey, "delay", NULL,
&datatype, (LPBYTE) &delay, &datasize);
// get message
datasize = MSGSIZE;
RegQueryValueEx(hRegKey, "message", NULL,
&datatype, (LPBYTE) str, &datasize);
}
RegCloseKey(hRegKey);
timer = SetTimer(hwnd, 1, delay, NULL);
hBlkBrush = (HBRUSH) GetStockObject(BLACK_BRUSH);
break;
case WM_ERASEBKGND:
hdc = GetDC(hwnd);
// Get coordinates of 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 string to 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;
}
// Configuration Dialog Box Function
BOOL WINAPI ScreenSaverConfigureDialog(HWND hdwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
static HWND hEboxWnd;
static HWND udWnd;
switch(message) {
case WM_INITDIALOG:
// open screen saver key or create, if necessary
RegCreateKeyEx(HKEY_CURRENT_USER,
"Control Panel\\Screen Saver.MyScrSaver",
0, "Screen Saver", 0, KEY_ALL_ACCESS,
NULL, &hRegKey, &result);
// if key was created
if(result==REG_CREATED_NEW_KEY) {
// set its initial value
delay = 100;
RegSetValueEx(hRegKey, "delay", 0,
REG_DWORD, (LPBYTE) &delay, sizeof(DWORD));
RegSetValueEx(hRegKey, "message", 0,
REG_SZ, (LPBYTE) str, strlen(str)+1);
}
else { // key was already in registry
// get delay value
datasize = sizeof(DWORD);
RegQueryValueEx(hRegKey, "delay", NULL,
&datatype, (LPBYTE) &delay, &datasize);
// get message
datasize = MSGSIZE;
RegQueryValueEx(hRegKey, "message", NULL,
&datatype, (LPBYTE) str, &datasize);
}
// create delay spin control
hEboxWnd = GetDlgItem(hdwnd, IDD_EB1);
udWnd = CreateUpDownControl(
WS_CHILD | WS_BORDER | WS_VISIBLE |
UDS_SETBUDDYINT | UDS_ALIGNRIGHT,
20, 10, 50, 50,
hdwnd,
IDD_UPDOWN,
hMainInstance,
hEboxWnd,
DELAYMAX, 1, delay);
// initialize edit box with current message
SetDlgItemText(hdwnd, IDD_EB2, str);
return 1;
case WM_COMMAND:
switch(LOWORD(wParam)) {
case IDOK:
// set delay value
delay = GetDlgItemInt(hdwnd, IDD_EB1, NULL, 1);
// get message string
GetDlgItemText(hdwnd, IDD_EB2, str, MSGSIZE);
// update registry
RegSetValueEx(hRegKey, "delay", 0,
REG_DWORD, (LPBYTE) &delay, sizeof(DWORD));
RegSetValueEx(hRegKey, "message", 0,
REG_SZ, (LPBYTE) str, strlen(str)+1);
// fall through to next case ...
case IDCANCEL:
RegCloseKey(hRegKey);
EndDialog(hdwnd, 0);
return 1;
}
break;
}
return 0;
}
// No classes to register.
BOOL WINAPI RegisterDialogClasses(HANDLE hInst)
{
return 1;
}
listing 4
// Dialog box for screen saver.
#include <windows.h>
#include <scrnsave.h>
#include "scr.h"
ID_APP ICON SCRICON.ICO
STRINGTABLE
{
IDS_DESCRIPTION "My Screen Saver #2"
}
DLG_SCRNSAVECONFIGURE DIALOGEX 18, 18, 110, 60
CAPTION "Set Screen Saver Options"
STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION |
WS_SYSMENU
{
PUSHBUTTON "OK", IDOK, 20, 40, 30, 14
PUSHBUTTON "Cancel", IDCANCEL, 60, 40, 30, 14
EDITTEXT IDD_EB1, 5, 5, 24, 12, ES_LEFT | WS_TABSTOP | WS_BORDER
EDITTEXT IDD_EB2, 5, 20, 65, 12, ES_LEFT | WS_TABSTOP |
WS_BORDER | ES_AUTOHSCROLL
LTEXT "Delay in milliseconds", IDD_TEXT1, 35, 7, 100, 12
LTEXT "Message", IDD_TEXT2, 76, 22, 30, 12
}
listing 5
#define IDD_EB1 200
#define IDD_EB2 201
#define IDD_UPDOWN 202
#define IDD_TEXT1 203
#define IDD_TEXT2 204
listing 6
// Configuration Dialog Box Function
BOOL WINAPI ScreenSaverConfigureDialog(HWND hdwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
static HWND hEboxWnd;
static HWND udWnd;
switch(message) {
case WM_INITDIALOG:
// open screen saver key or create, if necessary
RegCreateKeyEx(HKEY_CURRENT_USER,
"Control Panel\\Screen Saver.MyScrSaver",
0, "Screen Saver", 0, KEY_ALL_ACCESS,
NULL, &hRegKey, &result);
// if key was created
if(result==REG_CREATED_NEW_KEY) {
// set its initial value
delay = 100;
RegSetValueEx(hRegKey, "delay", 0,
REG_DWORD, (LPBYTE) &delay, sizeof(DWORD));
RegSetValueEx(hRegKey, "message", 0,
REG_SZ, (LPBYTE) str, strlen(str)+1);
}
else { // key was already in registry
// get delay value
datasize = sizeof(DWORD);
RegQueryValueEx(hRegKey, "delay", NULL,
&datatype, (LPBYTE) &delay, &datasize);
// get message
datasize = MSGSIZE;
RegQueryValueEx(hRegKey, "message", NULL,
&datatype, (LPBYTE) str, &datasize);
}
// create delay spin control
hEboxWnd = GetDlgItem(hdwnd, IDD_EB1);
udWnd = CreateUpDownControl(
WS_CHILD | WS_BORDER | WS_VISIBLE |
UDS_SETBUDDYINT | UDS_ALIGNRIGHT,
20, 10, 50, 50,
hdwnd,
IDD_UPDOWN,
hMainInstance,
hEboxWnd,
DELAYMAX, 1, delay);
// initialize edit box with current message
SetDlgItemText(hdwnd, IDD_EB2, str);
return 1;
case WM_COMMAND:
switch(LOWORD(wParam)) {
case IDOK:
// set delay value
delay = GetDlgItemInt(hdwnd, IDD_EB1, NULL, 1);
// get message string
GetDlgItemText(hdwnd, IDD_EB2, str, MSGSIZE);
// update registry
RegSetValueEx(hRegKey, "delay", 0,
REG_DWORD, (LPBYTE) &delay, sizeof(DWORD));
RegSetValueEx(hRegKey, "message", 0,
REG_SZ, (LPBYTE) str, strlen(str)+1);
// fall through to next case ...
case IDCANCEL:
RegCloseKey(hRegKey);
EndDialog(hdwnd, 0);
return 1;
}
break;
}
return 0;
}
|