In the world of modern computing, where screensavers have become an afterthought due to advancements in display technology and power-saving features, it’s fun to take a trip down memory lane and explore how screensavers were created in the early days of computing. In this article, we’ll dive into the nostalgic world of Windows 95 and learn how to make a simple screensaver using C++.

Prerequisites

Before we dive into coding, make sure you have the following tools set up on your Windows 95 development environment:

  1. Windows 95: Of course, you’ll need a Windows 95 machine or a virtual environment to develop and test the screensaver.
  2. Borland C++ 4.5: Back in the day, Borland C++ was a popular choice for Windows 95 development. You can use any other C++ compiler, but we’ll stick with the classic for this tutorial.

Creating the Project

Let’s get started by creating a new C++ project. Follow these steps:

  1. Launch Borland C++ 4.5.
  2. Create a new project: Go to File -> New... and select “Windows Application.”
  3. Provide a name for your project, such as “Win95Screensaver.”
  4. Choose an appropriate location to save your project files.
  5. Click “OK” to create the project.

Designing the Screensaver

1. Create the Main Window

In Windows 95, screensavers were essentially Windows applications that took over the entire screen. Here’s a basic structure for creating a window:

#include <windows.h>

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
    switch (message) {
        case WM_CLOSE:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    WNDCLASSEX wcex;
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = WndProc;
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = hInstance;
    wcex.hIcon = NULL;
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wcex.lpszMenuName = NULL;
    wcex.lpszClassName = "Win95Screensaver";
    wcex.hIconSm = NULL;

    if (!RegisterClassEx(&wcex)) {
        return 0;
    }

    HWND hWnd = CreateWindow("Win95Screensaver", "Windows 95 Screensaver", WS_POPUP,
        CW_USEDEFAULT, CW_USEDEFAULT, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
        NULL, NULL, hInstance, NULL);

    if (!hWnd) {
        return 0;
    }

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

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

    return (int)msg.wParam;
}

In this code, we create a basic Windows application with a single window. The WinMain function sets up the window, and the WndProc function handles window messages.

2. Displaying Graphics

To create a screensaver effect, we can add simple graphics or animations. Let’s draw some basic shapes on the screen, like bouncing balls. Here’s an example of how you can do this:

#include <windows.h>

// Define your bouncing balls here...

void DrawBalls(HDC hdc) {
    // Implement the drawing logic for bouncing balls here...
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
    switch (message) {
        case WM_CLOSE:
            PostQuitMessage(0);
            break;
        case WM_PAINT:
            {
                PAINTSTRUCT ps;
                HDC hdc = BeginPaint(hWnd, &ps);
                DrawBalls(hdc); // Call the function to draw bouncing balls.
                EndPaint(hWnd, &ps);
            }
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// Rest of the code remains the same...

You can create a function DrawBalls that handles the drawing of bouncing balls. In this function, you can use GDI (Graphics Device Interface) functions to draw shapes and animations on the screen.

3. Animation Loop

To create animations, you’ll need to implement a loop that updates the screen regularly. You can use a timer to achieve this:

#include <windows.h>

// Define your bouncing balls and animation logic here...

void DrawBalls(HDC hdc) {
    // Implement the drawing logic for bouncing balls here...
}

void UpdateAnimation(HWND hWnd) {
    // Implement your animation update logic here...

    // Redraw the screen
    InvalidateRect(hWnd, NULL, TRUE);
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
    switch (message) {
        case WM_CLOSE:
            PostQuitMessage(0);
            break;
        case WM_PAINT:
            {
                PAINTSTRUCT ps;
                HDC hdc = BeginPaint(hWnd, &ps);
                DrawBalls(hdc); // Call the function to draw bouncing balls.
                EndPaint(hWnd, &ps);
            }
            break;
        case WM_TIMER:
            UpdateAnimation(hWnd); // Call the function to update animations.
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    // ... (Previous code remains the same)

    // Set up a timer to trigger the animation loop
    SetTimer(hWnd, 1, 16, NULL); // 16 milliseconds (about 60 FPS)

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

    return (int)msg.wParam;
}

In this code, we’ve added a timer that triggers the WM_TIMER message, which in turn calls the UpdateAnimation function. You can implement your animation logic in this function and redraw the screen to create the illusion of movement.

Building and Testing

Now that you’ve created your screensaver, it’s time to build and test it:

  1. Save your project.
  2. Go to Project -> Build Win95Screensaver.
  3. If the build is successful, you should see an executable file named Win95Screensaver.exe in your project directory.
  4. To test your screensaver, you can copy the Win95Screensaver.exe file to your Windows 95 machine or virtual environment.
  5. Right-click the executable and select “Install” or “Test” to see your screensaver in action.

Conclusion

Creating a simple screensaver for Windows 95 using C++ allows you to take a nostalgic trip back to the early days of computing. While screensavers may not be as prominent as they once were, the process of building one can be a fun and educational experience. Feel free to enhance your screensaver with more advanced graphics and animations to relive the era when screensavers were all the rage on Windows 95-powered PCs.

By Tech Thompson

Tech Thompson is a software blogger and developer with over 10 years of experience in the tech industry. He has worked on a wide range of software projects for Fortune 500 companies and startups alike, and has gained a reputation as a leading expert in software development and design.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

WordPress Appliance - Powered by TurnKey Linux