Click here to Skip to main content
15,886,590 members

Procedure to create Screen Saver in only C through Win32

AshakiranBhatter asked:

Open original thread
Hi,
I am trying to create a win32 screen saver. I know a bit of windows programming.
Could you please help me how do i actually make necessary modifications of my win32 xyz.c file so that I could load it as a successful screen saver.

Code from OP posted in comments:

C++
/********************************************************************************
*                                                                               *
* Name: Code rain                                                               *
* Date: 11/08/2012                                                              *
* Author: Kiran Bhatter.                                                        *
* Version: 0.1                                                                  *
* Source: C                                                                     *
*                                                                               *
********************************************************************************/
#include <windows.h>
#include <tchar.h>
#include <scrnsave.h>

const TCHAR szSaverName[] = _T("example");
HWND hWnd = NULL;
int cxClient = 0, cyClient = 0;

LONG WINAPI ScreenSaverProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
    MSG msg;
    WNDCLASSEX wc = {0};

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_VREDRAW | CS_HREDRAW;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    wc.lpfnWndProc = ScreenSaverProc;
    wc.hInstance = hInstance;
    wc.lpszMenuName = NULL;
    wc.lpszClassName = szSaverName;
    wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, _T("RegisterClassEx() failed!"), szSaverName, MB_ICONERROR);
        return 1;
    }

    hWnd = CreateWindowEx(0, szSaverName, szSaverName, WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);

    if(!hWnd)
    {
        MessageBox(NULL, _T("CreateWindowEx() failed!"), szSaverName, MB_ICONERROR);
        return 1;
    }

    ShowWindow(hWnd, iCmdShow); UpdateWindow(hWnd);

    while(1)
    {
        if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            if(msg.message == WM_QUIT)
                break;

            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    UnregisterClass(szSaverName, hInstance);

    return msg.wParam;
}

LONG WINAPI ScreenSaverProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
    switch(iMsg)
    {
        case WM_CREATE:
            return 0;

        case WM_SIZE:
            cxClient = LOWORD(lParam);
            cyClient = HIWORD(lParam);
            return 0;

        case WM_KEYDOWN:
            switch(wParam)
            {
                case VK_ESCAPE:
                    SendMessage(hWnd, WM_DESTROY, 0, 0);
                    break;
            }
            return 0;

        case WM_CLOSE:
        case WM_DESTROY:
            PostQuitMessage(WM_QUIT);
            return 0;
    }
    
    return DefScreenSaverProc(hWnd, iMsg, wParam, lParam);
}
Tags: C, Win32

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900