CWinApp

Win32++

CWinApp Class

Description

The class responsible for initializing Win32++. You  inherit from this class to start the application.

CWinApp Members

Construction

CWinApp
CWinApp();
Constructs a CWinApp object.

Attributes

GetInstanceHandle
HINSTANCE GetInstanceHandle() const;
Returns the instance handle (HINSTANCE) of the application
GetResourceHandle
HINSTANCE GetResourceHandle() const;
Returns the instance handle of resources. This can the the HINSTANCE of the application or the HINSTANCE of a resource dll.
SetResourceHandle
void SetResourceHandle(HINSTANCE hResource);
Sets the instance handle of resources.

Operations

LoadCursor
LoadCursor(LPCTSTR lpszResourceName) const;
LoadCursor(int nIDCursor) const;
Loads the specified cursor. The cursor is defined in the resource script (resource.rc).
LoadStandardCursor
HCURSOR LoadStandardCursor(LPCTSTR lpszCursorName) const;
Returns the handle of a standard cursor. Standard cursors include:
IDC_APPSTARTING, IDC_ARROW, IDC_CROSS, IDC_HAND, IDC_HELP, IDC_IBEAM, IDC_NO, IDC_SIZEALL, IDC_SIZENESW, IDC_SIZENS, IDC_SIZENWSE, IDC_SIZEWE, IDC_UPARROW, IDC_WAIT.
LoadIcon
HICON LoadIcon(LPCTSTR lpszResourceName) const;
HICON LoadIcon(int nIDIcon) const;
Loads the icon resource whose size conforms to the SM_CXICON and SM_CYICON system metric values. For other icon sizes, use the LoadImage windows API function.
LoadImage
HANDLE LoadImage(LPCTSTR lpszResourceName, UINT uType, int cx, int cy, UINT fuLoad = LR_DEFAULTCOLOR) const;
HANDLE LoadImage(int nIDImage, UINT uType, int cx, int cy, UINT fuLoad = LR_DEFAULTCOLOR) const;
Loads an icon, cursor, animated cursor or bitmap image. uType can be IMAGE_BITMAP, IMAGE_CURSOR or IMAGE_ICON. cx and cy are the desired width and height in pixels. fuLoad can be LR_DEFAULTCOLOR, LR_CREATEDIBSECTION, LR_DEFAULTSIZE, LR_LOADFROMFILE, LR_LOADMAP3DCOLORS, R_LOADTRANSPARENT, LR_MONOCHROME, LR_SHARED, LR_VGACOLOR.
LoadStandardIcon
HICON LoadStandardIcon(LPCTSTR lpszIconName) const;
Returns the handle of a standard Icon. Standard Icons include:
IDI_APPLICATION, IDI_ASTERISK, IDI_ERROR, IDI_EXCLAMATION, IDI_HAND, IDI_INFORMATION, IDI_QUESTION, IDI_WARNING
SetCursor
HCURSOR SetCursor(HCURSOR hCursor) const;
Sets the current cursor and returns the previous one.
Note: The cursor will be set to the window's class cursor (if one is set) each time the mouse is moved over the window. You can specify different cursors for different conditions while processing WM_SETCURSOR.

Overridables

InitInstance
virtual BOOL InitInstance();
This function is called when the application starts. Override this to perform tasks such as creating a window.
Run
virtual int Run();
Calls InitInstance and runs the message loop. Use this in WinMain to run the application.
Base class Members

For base class members, refer to the members of CWinThread.htm

.

Remarks

Starting a Win32++ application

CWinApp (or a class inherited from CWinApp) must be used to run a Win32++ application.  Here we see a simple example of a class inherited from CWinApp.

////////////////////////////////////////////////////////
// The class inherited from CWinApp which starts Win32++
class CSimpleApp : public CWinApp
{
public:
  CSimpleApp() {}
  virtual ~CSimpleApp() {}
  virtual BOOL InitInstance();

private:
  CView m_View;
};

BOOL CSimpleApp::InitInstance()
{
  // This function is called by Run

  m_View.Create(); // Create the Window
  return TRUE;
}

Notice that InitInstance is used to specify what happens when we use the Run function.  In this case it is used to create the CView window.

Running a Win32++ application

Once we have our CWinApp derived class, we use it in WinMain start our application.  The following code demonstrates a typical use of the class inherited from CWinApp.

//////////////////////////////////////////////
// The entry point for our windows application
INT WINAPI WinMain(HINSTANCE, HINSTANCE, LPTSTR, int)
{
  // Start Win32++
  CSimpleApp MyApp;

  // Run the application until the window is destroyed
  return MyApp.Run();
}

Note that this simple program has two key steps

  • Constructs MyApp, which is a CSimpleApp object. CSimpleApp is inherited from WinApp.
  • Uses the Run function to call MyApp's InitInstance, and run the message loop.

Separating the construction and the running of CWinApp like this allows the CWinApp derived class (including all member objects) to be fully constructed, before attempting to do things which could generate exceptions, such as creating windows.

A complete simple application

Sometimes it is easier to fit the pieces together when we see a complete application rather than a collection of code snippets. The following code sample is a complete simple windows application.  It creates an ordinary window, and ends the application when the window is destroyed.

#include "../Win32++/Wincore.h"

/////////////////////////////////////////////
// A class inherited from CWnd for the window
class CView : public CWnd
{
public:
  CView() {}
  virtual ~CView() {}
  virtual LRESULT WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
};

LRESULT CView::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
  // Handle the messages for this window
  switch (uMsg)
  {
  case WM_DESTROY:
    //End the program when window is destroyed
    ::PostQuitMessage(0);
    break;
  }

  //Use the default message handling for remaining messages
  return WndProcDefault(hWnd, uMsg, wParam, lParam);
}

////////////////////////////////////////////////////////
// The class inherited from CWinApp which starts Win32++
class CSimpleApp : public CWinApp
{
public:
  CSimpleApp() {}
  virtual ~CSimpleApp() {}
  virtual BOOL InitInstance();

private:
  CView m_View;
};

BOOL CSimpleApp::InitInstance()
{
  // This function is called by Run

  m_View.Create(); // Create the Window
  return TRUE;
}

//////////////////////////////////////////////
// The entry point for our windows application
INT WINAPI WinMain(HINSTANCE, HINSTANCE, LPTSTR, int)
{
  // Start Win32++
  CSimpleApp MyApp;

  // Run the application until the window is destroyed
  return MyApp.Run();
}

Summary Information

Header file wincore.h
Win32/64 support Yes
WinCE support Yes