Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
114 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

Introduction

Welcome back! In this, the second article in the series, we'll be covering the features of CWindowImpl in much greater detail. As we cover each of the various topics we'll also be developing our own derivative of CWindowImpl called CDxWindowImpl. CDxWindowImpl will be a reusable window implementation that will provide many of the features needed by DirectX application windows. Using an existing, tested, and feature rich framework will make it less buggy and more extendable than most alternatives.

You may recall last time that I mentioned we'd be covering message loops and idle processing in this article. Well, I lied. Once I sat down and planned the content of this article I realized how poorly that topic fits in with the rest of this article, so I decided to bump it to part 3.

Requirements

I make many of the same assumptions in this article I made in the first. I assume that the reader has a basic understanding of Win32 programming and should be capable of creating a simple Win32 application, and of course, that he or she has read the first article. Some familiarity with using C++ templates would be helpful as well. I also assume that the reader is using VC 6.0 SP 5 or VC .NET. Other compilers may work but again, I cannot make any guarantees. You'll also want to make sure you have the WTL 7.0 or 7.1 libraries and have added the WTL\Include path to your list of include directories. This is a minor change from the first article which compiled with the 3.1 library. The code included in this article will likely compile with the older library but I've not tested against it. And last but not least, you'll want to make sure you're building against the latest Platform SDK. Below are links to both the Microsoft Download Center where the 7.1 libraries can be obtained, and the Platform SDK Update site.

WTL

Platform SDK

A New Project

Before going any further, let's create a new project for this article. Make it an empty Win32 Application project. Add a new header file to the project called CDxWindowImpl.h and a code file called main.cpp. The source for these two files is listed below.

[listing 2.1]
CDxWindowImpl.h

#ifndef __ATLWIN_H__
       #error CDxWindowImpl.h requires atlwin.h to be included first
#endif

template <class T, class TBase = CWindow >
class CDxWindowImpl : public CWindowImpl<T,TBase >
{
public:  
  BEGIN_MSG_MAP(CDxWindowImpl)
    MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
  END_MSG_MAP()
 
 LRESULT OnDestroy( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled )
  {
      PostQuitMessage(0);
      bHandled = FALSE;
      return 0;
  }

};

[listing 2.2]
Main.cpp

//#define _UNICODE

#include <atldef.h>
#include <windows.h>
#include <atlbase.h>
#include <atlapp.h>

CAppModule _Module;

#include <atlwin.h>
#include <atlmisc.h>
#include "CDxWindowImpl.h"

class CDxAppWindow : public CDxWindowImpl<CDxAppWindow>
{
public:

  BEGIN_MSG_MAP(CDxAppWindow)
   CHAIN_MSG_MAP(CDxWindowImpl<CDxAppWindow>)
  END_MSG_MAP()
};

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, 
                     int nCmdShow)
{
  CMessageLoop messageLoop;
  CDxAppWindow mainwnd;

  _Module.Init(NULL, hInstance);
  _Module.AddMessageLoop(&messageLoop);


  mainwnd.Create(NULL,CWindow::rcDefault,_T("Main Window"));
  if(mainwnd == NULL)
    return 0;

  mainwnd.ShowWindow(nCmdShow);
  mainwnd.UpdateWindow();

  int nRet = messageLoop.Run();

  _Module.RemoveMessageLoop();
  _Module.Term();

  return nRet;
}

The first thing that needs to be pointed out is that although this code will compile, it won't actually create a window. This is because I've not passed any window styles to the call to Create(). I've done this on purpose and my reason for doing so will become clear in the next section.

There's not much in these two files that's new. In CDxWindowImpl.h we create our new template class called CDxWindowImpl which derives directly from CWindowImpl and adds a message handler for WM_DESTROY. In Main.cpp we create another class called CDxAppWindow which derives from CDxWindowImpl (similar to Mainwnd and CWindowImpl from the previous article). CDxAppWindow is then instantiated, and a call to Create() finishes the setup. What's different this time around is that CDxAppWindow 's message map contains the CHAIN_MSG_MAP macro. We'll cover this in more detail in the next article. All we really need to know at this point is that CHAIN_MSG_MAP will make all the message handlers in the message map of the class passed to it available to this class's message map. Now, all message handlers created in CDxWindowImpl will be available to CDxAppWindow.





Window Traits


Contents
  Introduction
  Window Traits
  Window Class Registration
  Full Screen and Back

  Source code
  Printable version
  Discuss this article

The Series
  Getting Started
  Windowing