Skip navigation

make a screensaver in C++ win32

citing my collective sources.

MSDN link

I’ve always wanted to create a screensaver, just never got around to it.

There are a few things on codeproject that explain how to create a screensaver in C#.

But its easier in C++, in my opinion

Finally, here it is! A starter.

You CAN hook in OpenGL or whatever, but this just uses a simple backbuffer

You could easily create something like mystify or some kind of fractal thing using this starter kit

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef UNICODE
#pragma comment(lib, "scrnsavw.lib")
#else
#pragma comment(lib, "scrnsave.lib")
#endif
#pragma comment(lib, "comctl32.lib")




// COMPILE AND RENAME .EXE FILE TO .SCR.
// DOUBLE CLICK TO LAUNCH.
// MOVE TO C:\WINDOWS\SYSTEM32 DIRECTORY
// (AS WHATEVER.SCR) AND IT WILL AUTOMATICALLY
// APPEAR IN THE DROPDOWN MENU AS A VALID
// SCREENSAVER TO CHOOSE!

// WORKS WHEN COMPILED UNDER VISUAL STUDIO 2005.

// DOES __NOT__ WORK UNDER VISUAL STUDIO 2008.



#pragma region globals
HWND winhwnd ;
HBITMAP backBMP ;
HDC backDC ;
int backBufferCX, backBufferCY ;

int totalPoints ;


#define TIMER 1
#pragma endregion




LRESULT WINAPI ScreenSaverProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  switch(message)
  {
  case WM_CREATE:
    {
      // Create a compatible bitmap with the width, height
      // of the desktop.
      winhwnd = hwnd ; // save.

      backBufferCX = GetSystemMetrics( SM_CXVIRTUALSCREEN ) ;  // SM_CXSCREEN is just the primary.  this is BOTH.
      backBufferCY = GetSystemMetrics( SM_CYVIRTUALSCREEN ) ;

      //HDC desktopHdc = GetDC( NULL ) ; // give me the hdc of the desktop.
      HDC winhdc = GetDC( winhwnd ) ; // give me the hdc of this app.
      backBMP = (HBITMAP)CreateCompatibleBitmap( winhdc, backBufferCX, backBufferCY ) ;
      



      // now, you need to associate a dc with this
      // bitmap.  the DC holds all the info about brushes, etc.
      backDC = CreateCompatibleDC( winhdc ) ;
      ReleaseDC( winhwnd, winhdc ) ;


      // select it in.  here we have to associate
      // the DC we made (compatible with the window's hdc)
      // with the bitmap we made (compatible with the window's bitmap)
      SelectObject( backDC, backBMP ) ;

      
      totalPoints = 0 ;

      char buf[300] ;
      
      sprintf( buf, "desktop width:  %d     height:  %d", backBufferCX, backBufferCY ) ;
      TextOutA( backDC, 20, 20, buf, strlen( buf ) );
      //MessageBoxA( NULL, buf, "Debug info", MB_OK ) ;





      // Timer for animation
      SetTimer( winhwnd, TIMER, 24, NULL ); 
    }
    break;
  case WM_DESTROY:
    PostQuitMessage(0);
    break;
  case WM_PAINT:
    // blit the back buffer onto the screen
    {
      PAINTSTRUCT ps ;

      HDC hdc = BeginPaint( hwnd, &ps );
      BitBlt( hdc, 0, 0, backBufferCX, backBufferCY, backDC, 0, 0, SRCCOPY ) ;

      EndPaint( hwnd, &ps ) ;
    }
    break;

  case WM_TIMER:

    // draw an extra point in a random spot.
    SetPixel( backDC, rand()%backBufferCX, rand()%backBufferCY, RGB( 255,0,0 ) ) ;

    TextOutA( backDC, rand()%backBufferCX, rand()%backBufferCY, "B", 1 );
    totalPoints++ ;

    if( totalPoints > 300 )
    {
      // clear by filling out the back buffer with black rectangle
      HBRUSH oldBrush = (HBRUSH)SelectObject( backDC, GetStockObject( BLACK_BRUSH ) ) ;
      Rectangle( backDC, 0, 0, backBufferCX, backBufferCY );
      SelectObject( backDC, oldBrush ) ; // put the old brush back
      // Not keeping the system's BLACK_BRUSH

      totalPoints = 0 ;
    }

    RECT r ;
    GetClientRect( winhwnd, &r );
    InvalidateRect( hwnd, &r, false ) ;
    

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

BOOL WINAPI ScreenSaverConfigureDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
  return FALSE;
}

BOOL WINAPI RegisterDialogClasses(HANDLE hInst)
{
  return TRUE;
}



Get the code from my esnips (thanks esnips!)

Advertisements

One Comment

    • ناصر
    • Posted January 23, 2010 at 3:48 am
    • Permalink

    tanks very much
    I hope goodlock in your life for you!!!
    (I cant correct Speak & writiing English)
    Excusme


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: