CMainWnd


Description:
The class provides the basic encapsulation necessary for a window procedure. Since most of my projects don't use MFC, this tends to come in quite handy.
 
Code:
// -------------------- Header

#pragma once

#include "main.h"

#define MAINWND_GUID    "{}"  // TODO: Include your program's GUID
#define MAINWND_CLASS   "Undefined" //TODO: Insert a name here
#define MAINWND_TITLE   "Undefined" //TODO: Caption for main window

#define WM_USER_OBJECT      WM_USER + 1
#define WM_USER_SYSTRAY     WM_USER + 2

class CMainWnd {
public:
                                CMainWnd();
                                ~CMainWnd();

            BOOL                PrevInst();
            HWND                Create(HINSTANCE);

            LRESULT             OnCreate();
            LRESULT             OnDestroy();
            LRESULT             OnClose();

    static  LRESULT CALLBACK    MainWndProc(HWND, UINT, WPARAM, LPARAM);

private:

    HWND            m_hWnd;
    HINSTANCE       m_hInstance;
    HANDLE          m_hMutex;
};

// -------------------- Code

#include "MainWnd.h"

CMainWnd::CMainWnd() {
    MainWndProc ( NULL, WM_USER_OBJECT, (WPARAM) this, NULL);
}

CMainWnd::~CMainWnd() {
    MainWndProc ( NULL, WM_USER_OBJECT, (WPARAM) NULL, NULL);
}

BOOL CMainWnd::PrevInst() {
    m_hMutex = CreateMutex(NULL, TRUE, MAINWND_GUID);

    return (GetLastError() == ERROR_ALREADY_EXISTS);
}

HWND CMainWnd::Create(HINSTANCE hInstance) {
    HWND        hWnd;
    WNDCLASSEX  wndclass;

    m_hInstance = hInstance;

    //set parameters for window
    wndclass.cbSize = sizeof(wndclass);
    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc = MainWndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance;
    wndclass.hIcon = LoadIcon(hInstance, 
                     MAKEINTRESOURCE(IDI_MAINICON));
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
    wndclass.lpszMenuName = NULL; 
                            /* MAKEINTRESOURCE(IDR_MAINMENU); */
    wndclass.lpszClassName = MAINWND_CLASS;
    wndclass.hIconSm = (HICON) LoadImage(m_hInstance, 
                           MAKEINTRESOURCE(IDI_MAINICON), 
                           IMAGE_ICON, 16, 16, 0);

    //register window
    RegisterClassEx ( &wndclass );

    //create main window
    hWnd = CreateWindowEx (NULL, MAINWND_CLASS, MAINWND_TITLE, 
        WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, 
        CW_USEDEFAULT, CW_USEDEFAULT, 325, 370, 
        NULL, NULL, hInstance, NULL);

    return hWnd;
}

//Main window Prodecure ************
LRESULT CALLBACK CMainWnd::MainWndProc ( HWND hWnd, UINT iMsg, 
   WPARAM wParam, LPARAM lParam) {

    static CMainWnd * parent = (CMainWnd *) NULL;


    if (iMsg == WM_USER_OBJECT) {
        parent = (CMainWnd *) wParam;
        return 0;
    } else if ( parent != NULL ) {
        switch (iMsg) {

        case WM_CREATE :
            parent->m_hWnd = hWnd;
            return parent->OnCreate();

        case WM_CLOSE :
            return parent->OnClose();

        case WM_DESTROY :
            return parent->OnDestroy();

        }
    }


    return DefWindowProc (hWnd, iMsg, wParam, lParam);
}

LRESULT CMainWnd::OnClose() {

    DestroyWindow(m_hWnd);

    return 0;

}

LRESULT CMainWnd::OnDestroy() {

    PostQuitMessage(0);

    return 0;
}

LRESULT CMainWnd::OnCreate() {
    ShowWindow (m_hWnd, SW_NORMAL);
    UpdateWindow (m_hWnd);

    return 0;
}
 
Sample Usage:
 
n/a