TN002: CreateWindow and DestroyWindow (Win32)

Where are HWND created?

All HWND(W32) are created inside Widget::createHandle method using the CreateWindowEx(W32) routine. But createHandle is privated, you can't use it (although you can override it), the only one that calls this method is Widget::create. Its main task is to setup the Widget::m_handle member and to do the common Win32 subclassing:
m_baseWndProc = (WNDPROC)
See Widget::subClass for more information.


HWND Life Time

All HWND have the same life time that its container Widget. For example the next code shows you when a HWND is created and destroyed:
  Frame frame(...);
    Button myButton("OK", &frame); // the HWND is created
  } // the Button's destructor calls DestroyWindow
  // at this point myButton (and its HWND) doesn't exist anymore

Overriding Widget::createHandle Method

This shouldn't be your case, but if you want to make something like MdiClient::createHandle, in other words, to make your customized CreateWindowEx(W32) call, you can override Widget::createHandle method.

Remember that you must to call Widget::create inside the constructor of your own class, and give WidgetClassName::None as className in Widget's constructor.

Here is an example that shows you how to override createHandle:

class MyWidgetClass : public WidgetClass
  // This returns the name of your class for the Win32's API
  static MyWidgetClass getClassName()
  { return MyWidgetClass("Vaca.MyWidgetClass"); }

class MyWidget : public Register<MyWidgetClass>
               , public Widget

  MyWidget(Widget* parent)
    : Widget(WidgetClassName::None, parent, NoStyle)
           parent, ChildStyle);


  // customized call to CreateWindowEx
  virtual HWND createHandle(LPCTSTR className,
                            Widget* parent,
                            Style style)
    return CreateWindowEx(style.extended, className, _T(""),
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          parent ? parent->getHandle(): (HWND)NULL,

The WebCam Example overrides the createHandle method to use a HWND(W32) created from other routine that is not CreateWindowEx(W32).

See also:
TN001: RegisterClass process (Win32), Widget::Widget, Widget::~Widget