Solved

Screen Saver Problems

Posted on 1997-07-16
4
378 Views
Last Modified: 2013-12-03
I'm new to windows programming.

I used the example screen saver code with the SDK.  The screen saver worked when I would preview it, but when I hit the settings button nothing would happen.  I defined the following three funtions and linked with the scrnsave.lib.  I also passed any unprocessed messages to
DefScreenSaverProc(hwnd, message, wParam, lParam).

BOOL WINAPI ScreenSaverConfigureDialog(HWND hDlg, UINT message, WPARAM
wParam, LONG lParam)

BOOL WINAPI RegisterDialogClasses(HANDLE hInst)

LONG WINAPI ScreenSaverProc(HWND hwnd, UINT message, WPARAM wParam, LONG
lParam)

Can anyone answer these questions or have other source code that I could look at that does not use MFC?


/*-----------------------------------------------------------------------

      sstest.c

      Dale Lepine
      7/14/97
      Screen Saver Program

-----------------------------------------------------------------------*/

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

#define  DLG_SCRNSAVECONFIGURE 2003
 
 
//The following example shows the ScreenSaverConfigureDialog function found in the sample application.
 
#define MINVEL  1               /* minimum redraw-speed value    */
#define MAXVEL  10              /* maximum redraw-speed value    */
#define DEFVEL  1               /* default redraw-speed value    */
 
LONG    lSpeed = DEFVEL;        /* redraw-speed variable         */
 
extern HINSTANCE hMainInstance; /* screen saver instance handle  */
 
CHAR   szTemp[20];              /* temporary array of characters */
CHAR   szRedrawSpeed[] = "Redraw Speed";   /* .INI speed entry   */


BOOL WINAPI ScreenSaverConfigureDialog(HWND hDlg, UINT message, WPARAM wParam, LONG lParam)
{
    static HWND hSpeed; /* handle of speed scroll bar */
    static HWND hOK;    /* handle of OK push button */
 
    switch(message)
    {
        case WM_INITDIALOG:
 
            /* Retrieve the application name from the .RC file. */
 
            LoadString(hMainInstance, idsAppName, szAppName, 40);
 
            /* Retrieve the .INI (or registry) filename. */
 
            LoadString(hMainInstance, idsIniFile, szIniFile,
                MAXFILELEN);
 
            /* Retrieve any redraw-speed data from the registry. */
 
            lSpeed = GetPrivateProfileInt(szAppName, szRedrawSpeed,
                DEFVEL, szIniFile);
 
            /*
             * If the initialization file does not contain an entry
             * for this screen saver, use the default value.
             */
 
            if(lSpeed > MAXVEL || lSpeed < MINVEL)
                lSpeed = DEFVEL;
 
            /* Initialize the redraw-speed scroll bar control. */
 
            hSpeed = GetDlgItem(hDlg, ID_SPEED);
            SetScrollRange(hSpeed, SB_CTL, MINVEL, MAXVEL, FALSE);
            SetScrollPos(hSpeed, SB_CTL, lSpeed, TRUE);
 
            /* Retrieve a handle of the OK push button control. */
 
            hOK = GetDlgItem(hDlg, ID_OK);
 
            return TRUE;
 
        case WM_HSCROLL:
 
            /*
             * Process scroll bar input, adjusting the lSpeed
             * value as appropriate.
             */
 
            switch (LOWORD(wParam))
                {
                    case SB_PAGEUP:
                        --lSpeed;
                    break;
 
                    case SB_LINEUP:
                        --lSpeed;
                    break;
 
                    case SB_PAGEDOWN:
                        ++lSpeed;
                    break;
 
                    case SB_LINEDOWN:
                        ++lSpeed;
                    break;
 
                    case SB_THUMBPOSITION:
                        lSpeed = HIWORD(wParam);
                    break;
 
                    case SB_BOTTOM:
                        lSpeed = MINVEL;
                    break;
 
                    case SB_TOP:
                        lSpeed = MAXVEL;
                    break;
 
                    case SB_THUMBTRACK:
                    case SB_ENDSCROLL:
                        return TRUE;
                    break;
                }
                if ((int) lSpeed <= MINVEL)
                    lSpeed = MINVEL;
                if ((int) lSpeed >= MAXVEL)
                    lSpeed = MAXVEL;
 
                SetScrollPos((HWND) lParam, SB_CTL, lSpeed, TRUE);
            break;
 
        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case ID_OK:
 
                   /*
                    * Write the current redraw-speed variable to
                    * the .INI file.
                    */
 
                    wsprintf(szTemp, "%ld", lSpeed);
                    WritePrivateProfileString(szAppName, szRedrawSpeed,
                        szTemp, szIniFile);
 
                case ID_CANCEL:
                    EndDialog(hDlg, LOWORD(wParam) == ID_OK);
                return TRUE;
            }
    }
    return FALSE;
}



BOOL WINAPI RegisterDialogClasses(HANDLE hInst)
{

      return TRUE;

}



LONG WINAPI ScreenSaverProc(HWND hwnd, UINT message, WPARAM wParam, LONG lParam)
{
static HDC          hdc;    /* device-context handle */
static RECT         rc;     /* RECT structure */
static UINT         uTimer; /* timer identifier */
static int i = 0;
 
    switch(message)
    {
        case WM_CREATE:
 
            /* Retrieve the application name from the .RC file. */
 
            LoadString(hMainInstance, idsAppName, szAppName, 40);
 
            /* Retrieve the .INI (or registry) filename. */
 
            LoadString(hMainInstance, idsIniFile, szIniFile,
                MAXFILELEN);
 
            /* Retrieve any redraw-speed data from the registry. */
 
            lSpeed = GetPrivateProfileInt(szAppName, szRedrawSpeed,
                DEFVEL, szIniFile);
 
           /*
            * Set a timer for the screen saver window using the
            * redraw-rate stored in REGEDIT.INI.
            */
 
            uTimer = SetTimer(hwnd, 1, lSpeed * 1000, NULL);
            break;
 
        case WM_ERASEBKGND:
 
           /*
            * The WM_ERASEBKGND message is issued before the
            * WM_TIMER message, allowing the screen saver to
            * paint the background as appropriate.
            */
 
            hdc = GetDC(hwnd);
            GetClientRect (hwnd, &rc);
                  FillRect (hdc, &rc, GetStockObject(WHITE_BRUSH));
            ReleaseDC(hwnd,hdc);
            break;
 
        case WM_TIMER:
 
           /*
            * The WM_TIMER message is issued at (lSpeed * 1000)
            * intervals, where lSpeed == .001 seconds. This
            * code repaints the entire desktop with a white,
            * light gray, dark gray, or black brush each
            * time a WM_TIMER message is issued.
            */
 
            hdc = GetDC(hwnd);
            GetClientRect(hwnd, &rc);
            if (i++ <= 4)
            {
                        FillRect(hdc, &rc, GetStockObject(i));
                        DrawText(hdc, "Hello, Windows95!", -1, &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
                  }
                  else
                (i = 0);
            ReleaseDC(hwnd,hdc);
            break;
 
        case WM_DESTROY:
 
           /*
            * When the WM_DESTROY message is issued, the screen saver
            * must destroy any of the timers that were set at WM_CREATE
            * time.
            */
 
            if (uTimer)
                KillTimer(hwnd, uTimer);
            break;
    }
 
   /*
    * DefScreenSaverProc processes any messages
    * ignored by ScreenSaverProc.
    */
 
    return DefScreenSaverProc(hwnd, message, wParam, lParam);
}

sstest.rc

#include <winnt.h>
#include <winuser.h>
#include "sstest.h"


DLG_SCRNSAVECONFIGURE DIALOG 6, 18, 160, 63
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION |
    WS_SYSMENU
CAPTION "Sample Screen-Saver Setup"
FONT 8, "MS Sans Serif"
BEGIN
    GROUPBOX      "Redraw Speed", 101, 0, 6, 98, 40
    SCROLLBAR     ID_SPEED, 5, 31, 89, 10
    LTEXT         "Fast", 103, 6, 21, 20, 8
    LTEXT         "Slow", 104, 75, 21, 20, 8
    PUSHBUTTON    "OK", ID_OK, 117, 10, 40, 14
    PUSHBUTTON    "Cancel", ID_CANCEL, 117, 32, 40, 14
END

sstest.h:

#define ID_SPEED 109
#define ID_OK       108
#define ID_CANCEL 107

sstest.def:

DESCRIPTION 'SCRNSAVE : Test'
 
 
CODE    MOVEABLE
DATA    MOVEABLE MULTIPLE
 
HEAPSIZE  1024
STACKSIZE 4096
 
EXPORTS
        ScreenSaverConfigureDialog
        ScreenSaverProc

Dale Lepine
UNH
del@hopper.unh.edu

0
Comment
Question by:del
  • 2
4 Comments
 
LVL 3

Expert Comment

by:vinniew
ID: 1401049
Post more code.  It could be anything.
0
 

Author Comment

by:del
ID: 1401050
/*-----------------------------------------------------------------------

      sstest.c

      Dale Lepine
      7/14/97
      Screen Saver Program

-----------------------------------------------------------------------*/

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

#define  DLG_SCRNSAVECONFIGURE 2003
 
 
//The following example shows the ScreenSaverConfigureDialog function found in the sample application.
 
#define MINVEL  1               /* minimum redraw-speed value    */
#define MAXVEL  10              /* maximum redraw-speed value    */
#define DEFVEL  1               /* default redraw-speed value    */
 
LONG    lSpeed = DEFVEL;        /* redraw-speed variable         */
 
extern HINSTANCE hMainInstance; /* screen saver instance handle  */
 
CHAR   szTemp[20];              /* temporary array of characters */
CHAR   szRedrawSpeed[] = "Redraw Speed";   /* .INI speed entry   */


BOOL WINAPI ScreenSaverConfigureDialog(HWND hDlg, UINT message, WPARAM wParam, LONG lParam)
{
    static HWND hSpeed; /* handle of speed scroll bar */
    static HWND hOK;    /* handle of OK push button */
 
    switch(message)
    {
        case WM_INITDIALOG:
 
            /* Retrieve the application name from the .RC file. */
 
            LoadString(hMainInstance, idsAppName, szAppName, 40);
 
            /* Retrieve the .INI (or registry) filename. */
 
            LoadString(hMainInstance, idsIniFile, szIniFile,
                MAXFILELEN);
 
            /* Retrieve any redraw-speed data from the registry. */
 
            lSpeed = GetPrivateProfileInt(szAppName, szRedrawSpeed,
                DEFVEL, szIniFile);
 
            /*
             * If the initialization file does not contain an entry
             * for this screen saver, use the default value.
             */
 
            if(lSpeed > MAXVEL || lSpeed < MINVEL)
                lSpeed = DEFVEL;
 
            /* Initialize the redraw-speed scroll bar control. */
 
            hSpeed = GetDlgItem(hDlg, ID_SPEED);
            SetScrollRange(hSpeed, SB_CTL, MINVEL, MAXVEL, FALSE);
            SetScrollPos(hSpeed, SB_CTL, lSpeed, TRUE);
 
            /* Retrieve a handle of the OK push button control. */
 
            hOK = GetDlgItem(hDlg, ID_OK);
 
            return TRUE;
 
        case WM_HSCROLL:
 
            /*
             * Process scroll bar input, adjusting the lSpeed
             * value as appropriate.
             */
 
            switch (LOWORD(wParam))
                {
                    case SB_PAGEUP:
                        --lSpeed;
                    break;
 
                    case SB_LINEUP:
                        --lSpeed;
                    break;
 
                    case SB_PAGEDOWN:
                        ++lSpeed;
                    break;
 
                    case SB_LINEDOWN:
                        ++lSpeed;
                    break;
 
                    case SB_THUMBPOSITION:
                        lSpeed = HIWORD(wParam);
                    break;
 
                    case SB_BOTTOM:
                        lSpeed = MINVEL;
                    break;
 
                    case SB_TOP:
                        lSpeed = MAXVEL;
                    break;
 
                    case SB_THUMBTRACK:
                    case SB_ENDSCROLL:
                        return TRUE;
                    break;
                }
                if ((int) lSpeed <= MINVEL)
                    lSpeed = MINVEL;
                if ((int) lSpeed >= MAXVEL)
                    lSpeed = MAXVEL;
 
                SetScrollPos((HWND) lParam, SB_CTL, lSpeed, TRUE);
            break;
 
        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case ID_OK:
 
                   /*
                    * Write the current redraw-speed variable to
                    * the .INI file.
                    */
 
                    wsprintf(szTemp, "%ld", lSpeed);
                    WritePrivateProfileString(szAppName, szRedrawSpeed,
                        szTemp, szIniFile);
 
                case ID_CANCEL:
                    EndDialog(hDlg, LOWORD(wParam) == ID_OK);
                return TRUE;
            }
    }
    return FALSE;
}



BOOL WINAPI RegisterDialogClasses(HANDLE hInst)
{

      return TRUE;

}



LONG WINAPI ScreenSaverProc(HWND hwnd, UINT message, WPARAM wParam, LONG lParam)
{
static HDC          hdc;    /* device-context handle */
static RECT         rc;     /* RECT structure */
static UINT         uTimer; /* timer identifier */
static int i = 0;
 
    switch(message)
    {
        case WM_CREATE:
 
            /* Retrieve the application name from the .RC file. */
 
            LoadString(hMainInstance, idsAppName, szAppName, 40);
 
            /* Retrieve the .INI (or registry) filename. */
 
            LoadString(hMainInstance, idsIniFile, szIniFile,
                MAXFILELEN);
 
            /* Retrieve any redraw-speed data from the registry. */
 
            lSpeed = GetPrivateProfileInt(szAppName, szRedrawSpeed,
                DEFVEL, szIniFile);
 
           /*
            * Set a timer for the screen saver window using the
            * redraw-rate stored in REGEDIT.INI.
            */
 
            uTimer = SetTimer(hwnd, 1, lSpeed * 1000, NULL);
            break;
 
        case WM_ERASEBKGND:
 
           /*
            * The WM_ERASEBKGND message is issued before the
            * WM_TIMER message, allowing the screen saver to
            * paint the background as appropriate.
            */
 
            hdc = GetDC(hwnd);
            GetClientRect (hwnd, &rc);
                  FillRect (hdc, &rc, GetStockObject(WHITE_BRUSH));
            ReleaseDC(hwnd,hdc);
            break;
 
        case WM_TIMER:
 
           /*
            * The WM_TIMER message is issued at (lSpeed * 1000)
            * intervals, where lSpeed == .001 seconds. This
            * code repaints the entire desktop with a white,
            * light gray, dark gray, or black brush each
            * time a WM_TIMER message is issued.
            */
 
            hdc = GetDC(hwnd);
            GetClientRect(hwnd, &rc);
            if (i++ <= 4)
            {
                        FillRect(hdc, &rc, GetStockObject(i));
                        DrawText(hdc, "Hello, Windows95!", -1, &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
                  }
                  else
                (i = 0);
            ReleaseDC(hwnd,hdc);
            break;
 
        case WM_DESTROY:
 
           /*
            * When the WM_DESTROY message is issued, the screen saver
            * must destroy any of the timers that were set at WM_CREATE
            * time.
            */
 
            if (uTimer)
                KillTimer(hwnd, uTimer);
            break;
    }
 
   /*
    * DefScreenSaverProc processes any messages
    * ignored by ScreenSaverProc.
    */
 
    return DefScreenSaverProc(hwnd, message, wParam, lParam);
}


sstest.h

#define ID_SPEED 109
#define ID_OK       108
#define ID_CANCEL 107

sstest.def

DESCRIPTION 'SCRNSAVE : Test'
 
 
CODE    MOVEABLE
DATA    MOVEABLE MULTIPLE
 
HEAPSIZE  1024
STACKSIZE 4096
 
EXPORTS
        ScreenSaverConfigureDialog
       ScreenSaverProc

sstest.rc

#include <winnt.h>
#include <winuser.h>
#include "sstest.h"


DLG_SCRNSAVECONFIGURE DIALOG 6, 18, 160, 63
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION |
    WS_SYSMENU
CAPTION "Sample Screen-Saver Setup"
FONT 8, "MS Sans Serif"
BEGIN
    GROUPBOX      "Redraw Speed", 101, 0, 6, 98, 40
    SCROLLBAR     ID_SPEED, 5, 31, 89, 10
    LTEXT         "Fast", 103, 6, 21, 20, 8
    LTEXT         "Slow", 104, 75, 21, 20, 8
    PUSHBUTTON    "OK", ID_OK, 117, 10, 40, 14
    PUSHBUTTON    "Cancel", ID_CANCEL, 117, 32, 40, 14
END

0
 

Author Comment

by:del
ID: 1401051
Edited text of question
0
 
LVL 1

Accepted Solution

by:
dtowell earned 50 total points
ID: 1401052
Just uses the SDK, no MFC needed.
-------------------------------cut-----------------------------
#define STRICT
#include <windows.h>
#include <stdio.h>
#include <io.h>
#include <string.h>
#include <stdlib.h>
#include <commdlg.h>
#include <ctype.h>

static HINSTANCE hInstance;
static HWND window;
static HDC dc;


DWORD Doodle(LPVOID)
{
    int x = RandomRange(0,1280);
    int y = RandomRange(0,1024);
    int r = 0;
    int g = 0;
    int b = 0;

    while (TRUE) {  
        SetPixelV(dc,x,y,RGB(r,g,b));
        int t = Rand();
        switch (t & 7) {
            case 0: x++;      break;
            case 1: x++; y++; break;
            case 2:      y++; break;
            case 3: x--; y++; break;
            case 4: x--;      break;
            case 5: x--; y--; break;
            case 6:      y--; break;
            case 7: x++; y--; break;
            }
        if (x > 1279)
            x -= 1280;
        if (x < 0)
            x += 1280;
        y &= 1023;
        if ((t & 0xF000F0) == 0xF000F0)
            r = (r+1) & 255;
        if ((t & 0xF000F00) == 0xF000F00)
            g = (g+1) & 255;
        if ((t & 0x7000F008) == 0x7000F008)
            b = (b+1) & 255;
        }

    return 0;
}

LRESULT CALLBACK SaverWindProc(HWND hWnd,UINT wMsg,UINT wParam,LONG lParam)
{
    static LPARAM lastPos = -1;
    static DWORD id;
    static HANDLE thread = NULL;

    switch (wMsg) {
        case WM_SYSCOMMAND:
            if ((wParam == SC_SCREENSAVE) || (wParam == SC_CLOSE))
                return FALSE;
            break;

        case WM_DESTROY:
            TerminateThread(thread,0);  /////////////////////////
            PostQuitMessage(0);
            break;

        case WM_SETCURSOR:
            SetCursor(NULL);
            break;

        case WM_NCACTIVATE:
            if (wParam == FALSE)
                return FALSE;
            break;

        case WM_ACTIVATE:
        case WM_ACTIVATEAPP:
            if (!wParam)
                PostMessage(hWnd,WM_CLOSE, 0, 0);
            break;              

        case WM_MOUSEMOVE:
            if (lastPos == -1)
                lastPos = lParam;
            else if (lastPos != lParam)
                PostMessage(hWnd,WM_CLOSE, 0, 0);
            break;

        case WM_LBUTTONDOWN:
        case WM_MBUTTONDOWN:
        case WM_RBUTTONDOWN:
        case WM_KEYDOWN:
        case WM_SYSKEYDOWN:
            PostMessage(hWnd,WM_CLOSE, 0, 0);
            break;

        case WM_CREATE:
            thread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Doodle,NULL,0,&id);
            if (thread)
                SetThreadPriority(thread,THREAD_PRIORITY_LOWEST);
            break;
        }

      return DefWindowProc(hWnd,wMsg,wParam,lParam);
}


void MakeSaver()
{
    // register class
    WNDCLASS wndClass;
    memset(&wndClass,0,sizeof(wndClass));
    wndClass.style         = CS_HREDRAW|CS_VREDRAW|CS_SAVEBITS|CS_DBLCLKS|CS_OWNDC;
    wndClass.lpfnWndProc   = SaverWindProc;
    wndClass.hInstance     = hInstance;
    wndClass.hIcon         = LoadIcon(hInstance,MAKEINTRESOURCE(1));
    wndClass.hCursor       = LoadCursor(hInstance,MAKEINTRESOURCE(1));
    wndClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    wndClass.lpszClassName = "Saver";
    RegisterClass(&wndClass);

    // create window
    window = CreateWindowEx(WS_EX_TOPMOST,"Saver","",WS_POPUP|WS_VISIBLE,0,0,
        GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN),
        NULL,NULL,hInstance,NULL);

    dc = GetDC(window);
}


int PASCAL _export WinMain(HINSTANCE hInst, HINSTANCE /*hPrevInstance*/, LPSTR cmdLine, int /*cmdShow*/)
{
    hInstance = hInst;

    if (stricmp(cmdLine,"/s")==0 ||
        stricmp(cmdLine,"-s")==0 ||
        stricmp(cmdLine,"s")==0) {
        // run as a screen saver
        MakeSaver();

        MSG msg;
        while (GetMessage(&msg,0,0,0)==1) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
            }

        return TRUE;
        }

    // run configuration for the screen saver
    // if command line specifies -c use active window as parent
    HWND parent = NULL;
    if (stricmp(cmdLine,"/c")==0 ||
        stricmp(cmdLine,"-c")==0 ||
        stricmp(cmdLine,"c")==0) {
        parent = GetActiveWindow();
        }
    MessageBox(parent,"No configuation available.","ScreenSaver",MB_OK);
    return 0;
}

0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

11 Experts available now in Live!

Get 1:1 Help Now