Win32 Programming Basics: A Simple Application

jwatte's picture

Todays assignment is to get a red block on the screen of a Win32 program.

Let's assume you're using Win32. Let's assume you're using Visual Studio 6, or some later version. To draw a colored block, do this:

  • 1) New Project...
  • 2) Win32 Application; give it a project name ("block") and location on disk
  • 3) Select "A typical Hello World application" in the next dialog
  • 4) Press OK, go to File View, open up Source Files -> block.cpp
  • 5) F3 WM_PAINT ENTER F3

This should take you to this code position:

case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
EndPaint(hWnd, &ps);
break;

6) Remove the two lines that starts with "GetClientRect" and with "DrawText" and instead put in:

rt.left = 10; rt.top = 10; rt.right = 30; rt.bottom = 30;
static HBRUSH brRed;
if( !brRed ) brRed = CreateSolidBrush( RGB( 200, 0, 0 ) );
FillRect( hdc, &rt, brRed );

7) Press F5, say OK. Watch your red block appear.