6: Customising Window Creation

Win32++

Tutorials

Menu of tutorials

Tutorial 1:   The Simplest WindowTutorial 2:   Using Classes and Inheritance
Tutorial 3:   Using Messages to Create a Scribble Window
Tutorial 4:   Repainting the Window
Tutorial 5:   Wrapping a Frame around our Scribble Window
Tutorial 6:   Customising Window Creation
Tutorial 7:   Customising the ToolBar
Tutorial 8:   Loading and Saving Files
Tutorial 9:   Printing
Tutorial 10: Finishing Touches

Tutorial 6:  Customising Window Creation

Up until now we have used the default parameters supplied by Win32++ to create the view window.  Here we use the PreRegisterClass to specify the Window Class (not to be confused with a C++ class) parameters prior to window creation.  This will allow us to create a window with a coloured background and set it's cursor.

void CView::PreRegisterClass(WNDCLASS &wc)
{
  // Set the background brush and cursor
  wc.hbrBackground = m_Brush;
  wc.lpszClassName = "Scribble Window";
  wc.hCursor = GetApp()->LoadCursor(IDC_CURSOR1);
}

We also use PreCreate to give the window a 3D look by giving it's border a sunken edge.

void CView::PreCreate(CREATESTRUCT &cs)
{
  // Set the extra style to provide a sunken edge
  cs.dwExStyle = WS_EX_CLIENTEDGE;
}

The cursor IDC_CURSOR1 used in PreRegisterClass is specified in resource.rc. The resource.rc file is our resource script and contains the specifications for various window resources such as bitmaps, dialogs, cursors, icons, menus etc. The resources specified in resource.rc are compiled and linked into our application. This is the specification for the cursor in the resource.rc file.

/////////////////////////////////////////////////////////////////////////////
//
// Cursor
//

IDC_CURSOR1             CURSOR                  "res/Cursor.cur"

We create the background brush in CView's constructor. The brush is a CBrush object which automatically deletes its brush when its destructor is called. This is how the brush is created.

// Constructor
CView::CView() : m_PenColor(RGB(0,0,0))
{
     m_Brush.CreateSolidBrush(RGB(255,255,230));
}

The source code for this tutorial is located within the Tutorial folder of the software available from SourceForge at http://sourceforge.net/projects/win32-framework.