描述:
如何将一个CFrameWnd框架(带有一个CWndSplitter和两个CView继承的视图)放到activex中。我没有试过。
解决方案1:
Designing ActiveX Components with the MFC Document/View Model
Steve Zimmerman
By exploiting MFC's poorly understood document/view model, you can give your ActiveX component many great features with relatively little effort.
W hether you're an experienced Win32® guru or a junior Windows® wanna-be, you've got to love MFC. It removes the drudgery from Windows development by encapsulating many of the menial parts of an application without sacrificing speed. In addition, it provides a very powerful, but often misunderstood, program model called the document/view model. The basic idea behind the document/view architecture is to separate data from its visual representation-the object-oriented programming model. A side benefit of its implementation in MFC is a multitude of built-in features, including toolbars, status bars, and File Save/Open.
The MFC framework provides three base classes that perform a lot of the redundant work of a typical application-CFrameWnd, CDocument, and CView. You create derived classes from each of those classes and override the appropriate virtual functions to tailor your application to your needs. To glue the three objects together, you create a CSingleDocTemplate or CMultiDocTemplate object and presto, MFC makes the thing fly. To make it even easier, AppWizard automatically generates the files containing the skeletons of your CFrameWnd, CDocument, and CView-derived classes.
Although the document/view approach is not appropriate for every problem, Figure 1 shows what it adds to your application with little or no effort.
Document/View, Meet ActiveX
You've probably been using MFC to write standalone apps for years. The real question is, how can you use the MFC document/view model to develop cool Web-based applications with active content? Not all of the features listed in Figure 1 are appropriate for an ActiveX™ control, but many of them are. If you want to do nothing more than separate an ActiveX control's data from its visual representation, using CDocument and CView-derived classes seems like a no-brainer. And if you could find a way to place a CFrameWnd-derived object inside an ActiveX control, you'd have quick and easy access to the added functionality provided by the CToolBar, CStatusBar, and CSplitterWnd classes.
The problem is that MFC does not provide a template class for jump-starting document/view inside an ActiveX control. In a standalone application, you simply create a CSingleDocTemplate or a CMultiDocTemplate, call it AddDocTemplate, and you're off to the races. But if you use these classes to create an ActiveX control, they create a parentless frame window with a menu bar and the WS_OVERLAPPED window style. Since you probably want to drop your ActiveX controls onto HTML pages and display them as child windows of your Web browser, neither of these template classes makes much sense.
To address this difficulty, I've developed two classes-CActiveXDocTemplate and CActiveXDocControl-that make it easy to take advantage of the document/view architecture inside an ActiveX control. In fact, I converted Scribble (the popular sample from the Visual C++® tutorials) into a document/view ActiveX control to show you how it works. You can download the full source code from the link at the top of this article.
Let's start with a brief review of how the document/view architecture works. If you're already a document/view hotshot, feel free to skip over the next section.
Document/View 101: The Basics
Since most of us at one time or another have worked through the scant but oh-so-informative MFC tutorials, let's use the trusty Scribble sample code as the basis for discussion. If you're new to MFC and want to follow along at home, you'll find step-by-step instructions for creating the Scribble sample in the Introducing Visual C++ manual that ships with Visual C++.
In the Scribble sample, the CScribbleApp::InitInstance method contains the following code:
CMultiDocTemplate* pDocTemplate;
pDocTemplate = new CMultiDocTemplate(
IDR_SCRIBBTYPE,
RUNTIME_CLASS(CScribbleDoc),
RUNTIME_CLASS(CChildFrame),
RUNTIME_CLASS(CScribbleView));
AddDocTemplate(pDocTemplate);
•••
if (!ProcessShellCommand(cmdInfo))
return FALSE;
The creation of a CMultiDocTemplate object