Tùy chọn multiple top-level documents trong mfc năm 2024

While the project file links the various source documents into a single project, the document-to-document and net connective relationships are defined by information in the documents themselves. A multi-sheet design project is arranged as a hierarchical structure of logical blocks, where each block can be either a schematic sheet or a HDL file (VHDL or Verilog). At the head of this structure is a single master schematic sheet, more commonly referred to as the project's top sheet.

L

p trình Windows v

i VC/MFC

Trang 1

TÀI LI

U THAM KH

O

Sách:

o

Các sách ti

ế

ng Vi

t v

Visual C++ /l

p trình Windows (c

a SAMIS, c

a nhóm tác gi

ELICOM, hay c

a các tác gi

khác)

o

Sách ti

ế

ng Anh:

Beginning Visual C++ 6

Professional Visual C++ 6 (c

a nhà xu

t b

n WROX)

o

Các eBook ti

ế

ng Anh v

Visual C++ hay l

p trình Windows nh

ư

:

Programming Microsoft C++, 5th Edition eBook (c

a Microsoft Press)

Programming Windows with MFC, 2nd Edition eBook (c

a Microsoft Press)

Ch

ươ

ng trình tham kh

o:

o

MSDN (b

đĩ

a CD tài li

u tham kh

o c

a Mircosoft)

o

Source code m

u

website:

http://www.wrox.com

o

Các ví d

đặ

c bi

t

website:

http://www.codeguru.com

http://www.codeproject.com

Tùy chọn multiple top-level documents trong mfc năm 2024
Tùy chọn multiple top-level documents trong mfc năm 2024

MFC, out of the box, supports three interface models, MDI, SDI, and dialog based. What it doesn't have is support for MTLW (ok, so MS probably has a different acronym for it, but what I mean is Multiple Top Level Windows). This is the model popularized by those web browsers we all know and love, Netscape Navigator and Internet Explorer.

Problem

The main problem with MTLW and MFC is the main window (aka CWinApp::m_pMainWnd).

  1. MFC expects there to be one.
  2. MFC will close the application as soon as the main window is closed.

Solution

So what we need is a way to keep CWinApp::m_pMainWnd pointed at a valid TLW and switch it from one window to another as needed to make sure that the app doesn't shut down except when the last TLW is closed.

Two classes will help you do this:

  • CMultiTopApp - should be used as the base class for your app object.
  • CTopLevelFrame - should be used as the base class for CMainFrame.

Derive from these classes and wire up the message maps properly, and that's it. You're app now supports MTLW. Of course you need to have ways of creating new TLWs and probably you want to have a command to close all the windows and shut down your app, so the demo project includes those functions.

First, a handler for the "New Window" menu item creates new TLWs.

void CMultiTopSample::OnFileNewwindow() { CMainFrame* pFrame = new CMainFrame; pFrame->LoadFrame(IDR_MAINFRAME,

WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL,  
NULL);
pFrame->ShowWindow(SW_SHOW); pFrame->UpdateWindow(); }

Second, unless you want your users to have to close each window individually, you may want to have an "Exit" menu item which does the following:

void CMultiTopSample::OnAppExit() { CloseAllFrames() ; }

Is this easy, or what!

How it works (briefly), and a BONUS member function:

CMultiTopApp has an STL list for keeping track of TLWs...

list< CTopLevelFrame* > m_listMainFrames ;

... and a few functions for manipulating them:

void AddFrame( CTopLevelFrame* pFrame ) ; void KillFrame( CTopLevelFrame* pFrame ) ; void ReplaceMainFrame( CTopLevelFrame* pFrame ) ;

Plus a special bonus function. I use this to do print preview in YATLW and disable all the regular TLWs while I do so.

void EnableFrames( BOOL bEnable );

CTopLevelFrame uses the CMultiTopApp member functions in response to window creation and destruction messages.

MESSAGEHandlerAction

void CMultiTopSample::OnAppExit() { CloseAllFrames() ; }

3

void CMultiTopSample::OnAppExit() { CloseAllFrames() ; }

4calls

void CMultiTopSample::OnAppExit() { CloseAllFrames() ; }

5 after default processing

void CMultiTopSample::OnAppExit() { CloseAllFrames() ; }

6

void CMultiTopSample::OnAppExit() { CloseAllFrames() ; }

7calls

void CMultiTopSample::OnAppExit() { CloseAllFrames() ; }

8 before default processing

void CMultiTopSample::OnAppExit() { CloseAllFrames() ; }

9

list< CTopLevelFrame* > m_listMainFrames ;

0calls

list< CTopLevelFrame* > m_listMainFrames ;

1 after default processing

This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.