65.9K
CodeProject is changing. Read more.
Home

Color gradient in fullscreen window

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.26/5 (9 votes)

Nov 15, 2005

2 min read

viewsIcon

42823

downloadIcon

1026

Creates a full screen window and shows a color gradient on it

Download source files - 22.4 Kb
Download demo project - 69.2 Kb

Gradient

Introduction

This program displays a color gradient in a full screen window. Uses win32 visual c++ style of programming. I have also covered compilation using Visual C++ 6 and Borland free C++ compiler 5.5 in this article.

Background

I am new to windows programming and wanted to learn how to use graphics in a full screen window - this program is a result.

Working

Calls GetSystemMetrics() with argument SM_CXSCREEN for getting maximum x co-ordinate and with argument SM_CYSCREEN for getting maximum y co-ordinate and uses them to give window size to CreateWindowEx(). WS_POPUP window style is used because it does not have a title bar with maximize and other buttons – it is ideal for our purpose.

In the message processing routine, MainWndProc(), only WM_PAINT and WM_DESTROY messages are processed. In the WM_PAINT message handler, BeginPaint() is used to get the HDC and CreatePen() is used to set drawing color. MoveToEx() and LineTo() are used to move to position in window and draw a line respectively. The pens are destroyed after use to free memory using DeleteObject(). Finally, EndPaint() does the cleaning up. A code fragment is included below.

case WM_PAINT:
   mx=GetSystemMetrics(SM_CXSCREEN);
   my=GetSystemMetrics(SM_CYSCREEN);
   mx--;my--;
   h1 = BeginPaint(hwnd, &ps);
   for(i=63,rev=0;i>=0;i--,rev++)
     hpa[rev]=CreatePen( PS_SOLID, 1, RGB(0,i*4,0));
   for(i=0;i<=my;i++)
    {
      in=i*63/(my+1);
      SelectObject(h1, hpa[in]);
      MoveToEx(h1,0,i,NULL);
      LineTo(h1,mx,i);
      LineTo(h1,mx-1,i);
    }
   for(i=0;i<=63;i++)
     DeleteObject(hpa[i]);
   EndPaint(hwnd, &ps);
   return TRUE;

Compiling

Visual C++ 6:

   · Start Visual C++ 6

   · Access New option from File menu

   · Select “Win32 Application”

   · Give a project name

   · Click OK button

   · Select “An empty project” in the new window

   · Click Finish button

   · Click OK button in new window

   · Select “Project” menu, “Add To Project”, ”Files”

   · Select source file(gradr.c) in the dialog box

   · Select “Build” menu, “Build <proj name>.exe”

   · Executable will be in project folder in subdirectory
     Debug or Release depending on settings in IDE.

Borland free C++ Compiler 5.5:

   · Get freecommandLinetools.exe from
      http://www.borland.com/bcppbuilder/freecompiler/

   · Run it and extract to the default folder

   · Copy the three .cfg files in the source zip file provided in this
      page to the C:\Borland\BCC55\Bin directory

   · Copy the mkexe.bat file in the source zip file to the folder
      containing the gradr.c source file

   · Run the mkexe.bat file

   · The executable will be in the same folder

Running

Start the executable. Because the program does little other than showing the color gradient, all you can do is press Alt and F4 key combination to close the window.

Feedback

Please feel free to give any feedback.