vs2008中,在OCX控件中应用doc/view基本步骤

2023-11-16

 

1、利用向导创建一个MFC ActiveX Control控件CMyOCX;
2、在工程中加入ActivDoc头文件和执行文件;
class CActiveXDocTemplate : public CSingleDocTemplate
{
    enum { IDR_NOTUSED = 0x7FFF };

    CWnd* m_pParentWnd;
    CFrameWnd* m_pFrameWnd;
    CString m_docFile;
 CView*  m_pView;

public:
    CActiveXDocTemplate(CRuntimeClass* pDocClass,
        CRuntimeClass* pFrameClass, CRuntimeClass* pViewClass);

    CFrameWnd* CreateDocViewFrame(CWnd* pParentWnd);
    void SaveDocumentFile();

    virtual CFrameWnd* CreateNewFrame(CDocument* pDoc,
        CFrameWnd* pOther);
    virtual CDocument* OpenDocumentFile(
        LPCTSTR lpszPathName, BOOL bVerifyExists = TRUE);
};

/

class CActiveXDocControl : public COleControl
{
    enum { WM_IDLEUPDATECMDUI = 0x0363 };

    static BOOL m_bDocInitialized;
    CActiveXDocTemplate* m_pDocTemplate;
    CFrameWnd* m_pFrameWnd;

    DECLARE_DYNAMIC(CActiveXDocControl)

protected:
    void AddDocTemplate(CActiveXDocTemplate* pDocTemplate);
    CDocTemplate* GetDocTemplate() { return m_pDocTemplate; }

    //{{AFX_MSG(CActiveXDocControl)
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    afx_msg void OnSize(UINT nType, int cx, int cy);
    afx_msg void OnTimer(UINT nIDEvent);
    afx_msg void OnDestroy();
    //}}AFX_MSG
    //{{AFX_DISPATCH(CActiveXDocControl)
    //}}AFX_DISPATCH
    //{{AFX_EVENT(CActiveXDocControl)
    //}}AFX_EVENT

    DECLARE_MESSAGE_MAP()
    DECLARE_DISPATCH_MAP()
    DECLARE_EVENT_MAP()

public:
 CFrameWnd* GetFrameWnd();
    CActiveXDocControl();
    virtual ~CActiveXDocControl();

    enum {
    //{{AFX_DISP_ID(CActiveXDocControl)
    //}}AFX_DISP_ID
    };
};

///
// ActivDoc.cpp : implementation file
//

#include "stdafx.h"
#include "ActivDoc.h"

CActiveXDocTemplate::CActiveXDocTemplate(CRuntimeClass* pDocClass,
    CRuntimeClass* pFrameClass, CRuntimeClass* pViewClass)
        : CSingleDocTemplate(IDR_NOTUSED, pDocClass, pFrameClass,
            pViewClass)
{
    ASSERT(pFrameClass);
 m_pView = (CView*)pViewClass;
}

CFrameWnd* CActiveXDocTemplate::CreateDocViewFrame(CWnd* pParentWnd)
{
    ASSERT(pParentWnd && IsWindow(*pParentWnd));
    ASSERT_KINDOF(CActiveXDocControl, pParentWnd);
    m_pParentWnd = pParentWnd;
    m_pFrameWnd = NULL;

    // OpenDocumentFile is a virtual method (implemented in
    // the CSingleDocTemplate base class) that creates
    // the document (either empty or loaded from the specified
    // file) and calls our CreateNewFrame function. Passing
    // NULL to the function creates a new document. Incidentally,
    // the view is created by the CFrameWnd's OnCreateClient()
    // method.

    if (!OpenDocumentFile(NULL))
        return NULL;

    // Since OpenDocumentFile sets m_pFrame, we can now use it.

    ASSERT(m_pFrameWnd);
    ASSERT_KINDOF(CFrameWnd, m_pFrameWnd);
    m_pFrameWnd->ShowWindow(SW_SHOWNORMAL);
    return m_pFrameWnd;
}

CFrameWnd* CActiveXDocTemplate::CreateNewFrame(CDocument* pDoc,
        CFrameWnd* pOther)
{
    ASSERT(pOther == NULL);
    ASSERT(m_pFrameClass != NULL);
    if (pDoc != NULL)
        ASSERT_VALID(pDoc);

    // Create a frame wired to the specified document

    CCreateContext context;
    context.m_pCurrentFrame = pOther;
    context.m_pCurrentDoc = pDoc;
    context.m_pNewViewClass = m_pViewClass;
    context.m_pNewDocTemplate = this;

    m_pFrameWnd = (CFrameWnd*)m_pFrameClass->CreateObject();
    if (m_pFrameWnd == NULL)
    {
        TRACE1("Warning: Dynamic create of frame %hs failed.\n",
            m_pFrameClass->m_lpszClassName);
        return NULL;
    }
    ASSERT_KINDOF(CFrameWnd, m_pFrameWnd);

    if (context.m_pNewViewClass == NULL)
        TRACE0("Warning: creating frame with no default view.\n");

    // The frame is created as a menu-less child of the
    // CActiveXDocControl in which it will reside.

    ASSERT_KINDOF(CActiveXDocControl, m_pParentWnd);
    if (!m_pFrameWnd->Create(NULL, "", WS_CHILD|WS_VISIBLE,
        CFrameWnd::rectDefault, m_pParentWnd, NULL, 0, &context))
    {
        TRACE0("Warning: CDocTemplate couldn't create a frame.\n");
        return NULL;
    }

    return m_pFrameWnd;
}

CDocument* CActiveXDocTemplate::OpenDocumentFile(
    LPCTSTR lpszPathName, BOOL bVerifyExists)
{
    SaveDocumentFile();
    m_docFile = lpszPathName;

    if (bVerifyExists)
    {
        DWORD dwAttrib = GetFileAttributes(m_docFile);
        if (dwAttrib == 0xFFFFFFFF ||
            dwAttrib == FILE_ATTRIBUTE_DIRECTORY)
        {
            lpszPathName = NULL;
        }
    }

    return CSingleDocTemplate::OpenDocumentFile(
        lpszPathName, TRUE);
}

void CActiveXDocTemplate::SaveDocumentFile()
{
    if (m_pOnlyDoc != NULL)
    {
        if (!m_docFile.IsEmpty())
            m_pOnlyDoc->OnSaveDocument(m_docFile);
        else
            m_pOnlyDoc->SetModifiedFlag(FALSE);
    }
}

/
// CActiveXDocControl

IMPLEMENT_DYNAMIC(CActiveXDocControl, COleControl)
BEGIN_MESSAGE_MAP(CActiveXDocControl, COleControl)
    //{{AFX_MSG_MAP(CActiveXDocControl)
    ON_WM_CREATE()
    ON_WM_SIZE()
    ON_WM_TIMER()
    ON_WM_DESTROY()
    //}}AFX_MSG_MAP
    ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
END_MESSAGE_MAP()
BEGIN_DISPATCH_MAP(CActiveXDocControl, COleControl)
    //{{AFX_DISPATCH_MAP(CActiveXDocControl)
    //}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()
BEGIN_EVENT_MAP(CActiveXDocControl, COleControl)
    //{{AFX_EVENT_MAP(COleFrameCtrl)
    //}}AFX_EVENT_MAP
END_EVENT_MAP()

int CActiveXDocControl::m_bDocInitialized = FALSE;

CActiveXDocControl::CActiveXDocControl()
{
    m_pDocTemplate = NULL;
    m_pFrameWnd = NULL;

    // Since we're in an OCX, CWinApp::InitApplication() is
    // not called by the framework. Unfortunately, that method
    // performs CDocManager initialization that is necessary
    // in order for our DocTemplate to clean up after itself.
    // We simulate the same initialization by calling the
    // following code the first time we create a control.

    if (!m_bDocInitialized)
    {
        CDocManager docManager;
        docManager.AddDocTemplate(NULL);
        m_bDocInitialized = TRUE;
    }
}

CActiveXDocControl::~CActiveXDocControl()
{
    // Note that the frame, the document, and the view are
    // all deleted automatically by the framework!
   
    delete m_pDocTemplate;
}

void CActiveXDocControl::AddDocTemplate(CActiveXDocTemplate* pDocTemplate)
{
    // I've decided to call this function AddDocTemplate to
    // be consistent with naming of CWinApp::AddDocTemplate.
    // However, only one DocTemplate is allowed per control.
   
    ASSERT(pDocTemplate);
    ASSERT(m_pDocTemplate == NULL);
    m_pDocTemplate = pDocTemplate;
}

int CActiveXDocControl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (COleControl::OnCreate(lpCreateStruct) == -1)
        return -1;

    // The CActiveXDocTemplate object will create the
    // document, the view, and the frame inside the
    // control. The reason we need a handle to the frame
    // is so that we can resize it when the control is
    // resized.
   
    ASSERT(m_pDocTemplate);    // Set in call to AddDocTemplate
    m_pFrameWnd = m_pDocTemplate->CreateDocViewFrame(this);
    ASSERT_KINDOF(CFrameWnd, m_pFrameWnd);

    // By default, we'll create the control with a border,
    // since it looks better for frames containing a toolbar.

    SetBorderStyle(TRUE);
    SetTimer(WM_IDLEUPDATECMDUI, 300, NULL);
    return 0;
}

void CActiveXDocControl::OnSize(UINT nType, int cx, int cy)
{
    COleControl::OnSize(nType, cx, cy);

    // The CFrameWnd should always fill up the entire client
    // area of the control.

    if (m_pFrameWnd != NULL)
    {
        ASSERT(IsWindow(*m_pFrameWnd));
        CRect area;
        GetClientRect(area);
        m_pFrameWnd->MoveWindow(area.left, area.top, area.right,
            area.bottom);
    }
}

void CActiveXDocControl::OnTimer(UINT nIDEvent)
{
    // Since we're in an OCX, we don't control the message loop,
    // so CWinThread::OnIdle is never called. That means we have
    // to periodically pump the ON_UPDATE_COMMAND_UI messages
    // by hand.
   
    SendMessageToDescendants(WM_IDLEUPDATECMDUI, TRUE);
    COleControl::OnTimer(nIDEvent);
}


void CActiveXDocControl::OnDestroy()
{
    AfxGetApp()->m_pMainWnd = NULL;
    m_pDocTemplate->SaveDocumentFile();
    COleControl::OnDestroy();
}

CFrameWnd* CActiveXDocControl::GetFrameWnd()
{
 return m_pFrameWnd;
}

3、在CMyOCX头文件中包含#include "ActivDoc.h";
4、将CMyOCX头文件和执行文件中的COleControl全部用CActiveXDocControl替换;
5、添加CMyView、CMyDocument、CMyFrame三个类,它们分别继承于CView、CDocument、CFrameWnd,在它们的头文件中分别将它们
的构造函数和析构函数有protected该成public;
6、在CMyOCX执行文件中包含#include "MyDocument.h"  #include "MyView.h"   #include "MyFrame.h";
7、在CMyOCX执行文件中的构造函数中添加语句:
 AddDocTemplate(new CActiveXDocTemplate(
  RUNTIME_CLASS(CMyDocument),
  RUNTIME_CLASS(CMyFrame),
  RUNTIME_CLASS(CMyView)));
基本模型形成,然后就可以按照自己的需要向里面添加东西了。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

vs2008中,在OCX控件中应用doc/view基本步骤 的相关文章

  • 为什么可以从 console.log 访问 JavaScript 私有方法

    我写了一个简单的代码 const secure new class privateProperty 4 privateMethod console log The property this privateProperty should n
  • 如何表示类的实例与将其作为输入的类之间的关系?

    我有一堂课叫House 这个类的实例是house class House def init self height length self height height self length length def housePlan hou
  • 将 __DIR__ 常量与字符串连接作为数组值,该数组值是 PHP 中的类成员

    谁能告诉我为什么这不起作用 这只是我在其他地方尝试做的事情的一个粗略的例子 stuff array key gt DIR value 但是 这会产生错误 PHP Parse error syntax error unexpected exp
  • 如何使用javascript通过类名更改html元素的值

    这是我用来更改 html 元素值的代码 a class classname href Vtech com This text to be chnage a 如何在页面加载瞬间更改此文本 看来你需要添加DOMContentLoaded或者把你
  • CSS 3.0 用户选择属性替换

    我正在使用 CSS 3 0 它抱怨 用户选择 属性不存在 有谁知道合适的替代品或替代品是什么 user select is 回到规范 https drafts csswg org css ui 4 propdef user selectCS
  • CPP中原始数据类型的构造函数初始化

    在cpp中 我们可以将原始数据类型初始化为 int a 32 这个构造函数初始化是如何工作的 C 是否将其视为对象 这在以下内容中得到了最好的描述 C 03 8 5 初始化器 第 12 和 13 段 new 表达式 5 3 4 static
  • 在类中使用 std::chrono::high_resolution_clock 播种 std::mt19937 的正确方法是什么?

    首先 大家好 这是我在这里提出的第一个问题 所以我希望我没有搞砸 在写这篇文章之前我用谷歌搜索了很多 我对编码 C 很陌生 我正在自学 考虑到有人告诉我 只为任何随机引擎播种一次是一个很好的做法 我在这里可能是错的 什么是正确 最佳 更有效
  • 如何为带有继承的 C++ 类编写 C 包装器

    我只是想知道是否有一种方法可以为具有继承的 C 类创建 C 包装 API 考虑以下 class sampleClass1 public sampleClass public int get return this data 2 void s
  • 参考接口创建对象

    引用变量可以声明为类类型或接口类型 如果变量声明为接口类型 则它可以引用实现该接口的任何类的任何对象 根据上面的说法我做了一个理解上的代码 正如上面所说声明为接口类型 它可以引用实现该接口的任何类的任何对象 但在我的代码中显示display
  • Magento:设置刚刚创建的网站的配置值?

    我正在以编程方式创建网站 用户等 问题是 创建网站时 我无法立即设置配置值 Code
  • 如何在 Angular 2 应用程序中从 TypeScript/JavaScript 中的字符串获取类?

    在我的应用程序中 我有这样的内容 user ts export class User 现在 我这样做 应用程序组件 ts callAnotherFunction User 如果我将类名作为字符串 即 我该如何做到这一点 User 如果可能的
  • AppDelegate 的变量用作全局变量不起作用

    我想使用我的 AppDelegate 来存储任何其他类都可以访问的对象 我已经像这样声明了这个 AppDelegate interface MyAppDelegate UIResponder
  • 如何在netbeans中创建属性文件

    我正在开发一个 struts2 Web 应用程序项目并使用 netbeans 6 9 我想为我的项目创建一个属性文件 我该如何在net beans中做到这一点 右键单击要添加属性文件的位置 new gt other gt other gt
  • Android/Java 创建辅助类来创建图表

    Goal 创建用于图形生成的辅助类 背景 我有 3 个片段 每个片段收集一些传感器数据 加速度计 陀螺仪 旋转 并使用 GraphView 绘制图表 以下是其中一个片段的代码 该代码当前工作正常 public class Gyroscope
  • 动态创建类 - Python

    我需要动态创建一个类 为了更详细地讲 我需要动态创建 Django 的子类Form class 通过 动态 我打算根据用户提供的配置创建一个类 e g 我想要一个名为CommentForm这应该子类化Form class 该类应该有一个选定
  • 内联函数以及类和头文件

    头文件中定义的任何函数都会自动内联吗 如果我在类中声明一个函数并使用关键字 inline 在外部给出定义 那么这个函数会是内联的吗 如果是 为什么这不违反内联函数应在声明时赋予主体的法律 类定义中定义的任何函数都是内联的 任何标记的功能in
  • R中整数类和数字类有什么区别

    我想先说我是一个绝对的编程初学者 所以请原谅这个问题是多么基本 我试图更好地理解 R 中的 原子 类 也许这适用于一般编程中的类 我理解字符 逻辑和复杂数据类之间的区别 但我正在努力寻找数字类和整数类之间的根本区别 假设我有一个简单的向量x
  • 如何使用 std::array 模拟 C 数组初始化“int arr[] = { e1, e2, e3, ... }”行为?

    注意 这个问题是关于不必指定元素数量并且仍然允许直接初始化嵌套类型 这个问题 https stackoverflow com questions 6111565 now that we have stdarray what uses are
  • 无法从 C# WPF 中的另一个窗口调用方法

    好吧 假设我有两个窗户 在第一个中我有一个方法 public void Test Label Content works 在第二个方法中 我称此方法为 MainWindow mw new MainWindow mw Test 但什么也没发生
  • 如何初始化静态地图?

    你会如何初始化静态Map在Java中 方法一 静态初始化方法二 实例初始化 匿名子类 或者 还有其他方法吗 各自的优点和缺点是什么 这是说明这两种方法的示例 import java util HashMap import java util

随机推荐

  • ElasticSearch第十二讲 ES 集群脑裂问题

    ES集群出现脑裂 脑裂这个词 我们肯定不会陌生 在zk集群 mq集群搭建就考虑过这个问题 为保证部署在不同机房的集群始终保证任何时候只会有一个Leader来协调处理问题 当集群其他机器或者主节点出现故障时 保证重新选举出主节点不影响整个系统
  • vue三种常用获取input值写法

    1 v model 表单输入绑定 使用v model创建双向数据绑定 用来监听用户的输入事件以更新数据 并对一些极端场景进行一些特殊处理
  • 【数据处理】 python 常用操作整理

    python 数据分析常用操作 这是本人在数据分析中 记不住 反复查询的一些命令汇总 在此做个归纳汇总 并不定期更新 Dataframe import pandas as pd 合并DF 需求 有的时候需要将多个列相同的数据集 如别人的训练
  • Python调用OpenAI接口的简单封装

    1 注册OpenAI账号 获取OpenAI API key 网上有很多资料 这里就不再叙述了 科学上网 懂得都懂 一个小坑 在生成API key之后需立刻复制下来 否则将无法再次打开 当然如果错过复制了 也可以再重新生成一个key 2 安装
  • FinalShell 介绍

    官网 http www hostbuf com FinalShell是一体化的的服务器 网络管理软件 不仅是ssh客户端 还是功能强大的开发 运维工具 充分满足开发 运维需求 特色功能 免费海外服务器远程桌面加速 ssh加速 双边tcp加速
  • 设置linux ip命令

    设置linux ip命令 http hi baidu com BD F1 C8 D5 C8 C8 B5 E3 blog item 0b7256308902e19da9018e11 html ifconfig eth0 192 168 168
  • pytorch 自学笔记@_@

    课程 dataset 类 from torch utils data import Dataset from PIL import Image import os class MyData Dataset def init self roo
  • java处理跨域处理

    经常遇到加了跨域但是前端访问还是会跨域 很有可能是没有走你写的跨域的流程 这时候加一个优先级就可以解决这个问题了 一般都是用这个方法 Bean public CorsFilter corsFilter UrlBasedCorsConfigu
  • 用C语言表白

    int day for day 0 day lt Mylife day printf i love you 我们相遇的那一刻起 我愿用一辈子 每天爱你
  • 整十粉丝庆祝文章系列内容征集建议

    亲爱的读者们 大家好 作为一名文章作者 我深知没有读者的支持和喜爱 我的文字就只是无意义的文字堆积 因此 为了庆祝与感谢大家长久以来的支持 我准备举办一场特别的活动 粉丝庆祝文章系列内容征集建议 我想听听你们的声音 了解你们对我写作的喜好
  • 堆和栈的区别以及联系

    堆与栈的区别有 栈内存存储的是局部变量而堆内存是实体 栈内存的更新速度高于堆内存 栈内存的生命周期一结束就会被释放而堆内存会被垃圾回收机制不定时回收 栈中存放的是对象的引用及对象方法中的局部变量的值 参数的值 堆中存放的是实例对象及成员变量
  • 树状结构数据的数据库表设计及使用 - 4. 嵌套集(Nested Set)模型

    本文以 MySQL 为例 文档比较长 故分为5部分发出 邻接表 Adjacency List 模型 路径枚举 Path Enumeration 模型 闭包表 Closure Table 模型 嵌套集 Nested Set 模型 性能比较与分
  • Mac快速打开terminal终端快捷键操作

    Command 空格键跳出搜索框 输入ter 按 enter即可打开
  • 调试osgEarth(33)分页瓦片卸载器子节点的作用-(3)渲染遍历的帧号和时间设置-_真正的terrain使用TerrainCuller---水平方向剔除

    如果还记得来自于何方 看看一个月前如何引入TerrainCuller的 如何判断是否该cull呢 在这里先打个断点 用的包围球 可见 通过VIEW FRUSTUM CULLING SMALL FEATURE CULLING SHADOW O
  • webrtc服务器搭建

    两年前写的笔记 可能有些链接和方式已经不对了 自己评估 文章目录 名词解释 概要 房间服务 信令服务 ICE STUN TURN 服务 Web服务的安装与配置 房间服务 安装与配置 安装 信令服务 turn服务 参考 扩展阅读 other
  • 常用的医学图像分割评价指标

    常用的图像分割评价指标非常多 论文中常用的指标包括像素准确率 交并比 IOU Dice系数 豪斯多夫距离 体积相关误差 下面提到的所有案例都是二分类 标签中只有0和1 目录 一 像素准确率 二 交并比IOU 三 骰子系数Dice 四 Hau
  • 【JavaScript高级】原型和继承相关:原型对象、函数原型、原型链和继承、继承的优化、对象判断相关方法

    文章目录 原型对象 对象的原型 函数的原型 函数原型作用 new操作原型的赋值 将方法放在原型上 constructor属性 在原型中新增属性 重写函数原型对象 原型链和继承 原型链 原型链实现方法的继承 借用构造函数属性继承 继承的优化
  • java8 stream 转换list、map、set

    一 Collection Collections collect Collector Collectos Collection是Java集合的祖先接口 Collections是java util包下的一个工具类 内涵各种处理集合的静态方法
  • 基于FPGA的正弦波发生器设计与实现

    基于FPGA的正弦波发生器设计与实现 摘要 本文介绍了一种基于FPGA的正弦波发生器的设计与实现 通过使用FPGA的数字信号处理功能 可以实现高精度 高性能的正弦波生成 文章首先介绍了DDS Direct Digital Synthesis
  • vs2008中,在OCX控件中应用doc/view基本步骤

    1 利用向导创建一个MFC ActiveX Control控件CMyOCX 2 在工程中加入ActivDoc头文件和执行文件 class CActiveXDocTemplate public CSingleDocTemplate enum