Creating the Main Frame Window

[Previous] [Next]

As mentioned, ATL Scribble is going to be an SDI application, so we'll create a frame window using the ATL CFrameWinTraits styles, which we covered in Chapter 14. We'll create a new class derived from CWindowImpl named CMainFrame. There is no wizard support for creating ATL windowing class derivatives specifically, so we'll have to add a generic class with the New Class dialog box and then modify it. Launch the dialog box by right-clicking on the root node in ClassView, and then choose New Class from the context menu. Type in CMainFrame as the class name and CWindowImpl as the base class. Figure 16-1 shows what the fields in the New Class dialog box should contain.

Figure 16-1. The New Class dialog box.

We didn't type in the template parameters for CWindowImpl in the edit box because it's so small, so we need to open the mainframe.h header file and add them. We also need to add the message map and the window class name manually using the DECLARE_WND_CLASS macro. After that's done, we can use the wizard bar to add message handlers to the CMainFrame message map. A quit message should be posted when the main frame window closes, so we'll override OnFinalMessage to do that. ATL calls OnFinalMessage when a WM_NCDESTROY message is received. OnFinalMessage is a member of CWindowImplBaseT and is one of the few virtual functions in ATL. The current state of the mainframe.h header file is shown here:

 class CMainFrame : public CWindowImpl<CMainFrame, CWindow,      CFrameWinTraits> { public:     CMainFrame();     virtual ~CMainFrame();     DECLARE_WND_CLASS(_T("InAtl:Frame"))     BEGIN_MSG_MAP(CMainFrame)     END_MSG_MAP()     void OnFinalMessage(HWND /*hWnd*/)     {         ::PostQuitMessage(0);     } }; 

We'll add some code to the WinMain function to create the frame window and run a message loop. We'll add a menu resource so that we can specify its handle in the CWindowImpl Create function, and we'll also add an icon, which we'll plug into the WNDCLASSEX structure directly. The Win32 project doesn't have a resource file by default, but inserting a resource from the Insert menu will create one that you can then add to the project. (If you plan to use MFC-defined resources such as ID_FILE_OPEN, you'll need to include afxres.h in stdafx.h.) Finally, we have to add a message pump. Our WinMain is now functional. Here's the code:

 int APIENTRY WinMain(HINSTANCE hInstance,     HINSTANCE hPrevInstance,     LPSTR     lpCmdLine,     int       nCmdShow) {     lpCmdLine = GetCommandLine();     HRESULT hRes = CoInitialize(NULL);     _ASSERTE(SUCCEEDED(hRes));     _Module.Init(NULL, hInstance, NULL);     CMainFrame mainframe;         // Specify the icon in the WNDCLASSEX structure.     HICON hIcon = LoadIcon(_Module.GetResourceInstance(),         MAKEINTRESOURCE(IDI_ICON1));     mainframe.GetWndClassInfo().m_wc.hIcon = hIcon;     // Menu handle is a parameter to Create.     HMENU hMenu = LoadMenu(_Module.GetResourceInstance(),         MAKEINTRESOURCE(IDR_MENU1));     mainframe.Create(GetDesktopWindow(), CWindow::rcDefault,         _T("ATL Scribble"), 0, 0, (UINT)hMenu);     // Make visible.     mainframe.ShowWindow(SW_SHOWNORMAL);     MSG msg;     while (GetMessage(&msg, 0, 0, 0))     {         TranslateMessage(&msg);         DispatchMessage(&msg);     }     _Module.Term();     CoUninitialize();     return 0; } 

The application now consists of a frame window with a menu and looks like Figure 16-2.

Figure 16-2. ATL Scribble application main frame.

Inside Atl
Inside ATL (Programming Languages/C)
ISBN: 1572318589
EAN: 2147483647
Year: 1998
Pages: 127 © 2008-2017.
If you may any questions please contact us: