MFCGrid control是一款非常优秀的网格控件,支持非常丰富的界面元素,如下图:

因而在数据库程序及报表程序应用较为广泛,其源码可以在下面下载到:

MFC Grid control2.27源码下载

MFC Gridcontrol的作者并没有将其封装为DLL,因为在程序的多个模块复用这个控件比较麻烦(需要在多个工程中加入其源码),因此最好将其封装为一个DLL。今天有同事反映在DLL的对话框中无法使用封装好的MFC Grid control,于是上网查了下资料,晚上摸索了一下,解决了这个问题。

MFC Grid control的作者在设计该控件并没考虑到在DLL中使用该控件,因此在其控件类CGridCtrl类的构造函数注册窗口类时指定该窗口类的窗口过程所属的应用实例句柄皆为主程序的程序句柄(一般为exe程序),因此在DLL中无法显示。因此要解决该问题,就必须将注册窗口类函数改写,将应用实例句柄传给控件类。具体注册函数为:

  1. BOOL CGridCtrl::RegisterWindowClass(HINSTANCE hInstance)
  2. {
  3. WNDCLASS wndcls;
  4. HINSTANCE hInst = hInstance ? hInstance : AfxGetInstanceHandle();
  5. //HINSTANCE hInst = AfxGetResourceHandle();
  6. if (!(::GetClassInfo(hInst, GRIDCTRL_CLASSNAME, &wndcls)))
  7. {
  8. // otherwise we need to register a new class
  9. wndcls.style            = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
  10. wndcls.lpfnWndProc      = ::DefWindowProc;
  11. wndcls.cbClsExtra       = wndcls.cbWndExtra = 0;
  12. wndcls.hInstance        = hInst;
  13. wndcls.hIcon            = NULL;
  14. #ifndef _WIN32_WCE_NO_CURSOR
  15. wndcls.hCursor          = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
  16. #else
  17. wndcls.hCursor          = 0;
  18. #endif
  19. wndcls.hbrBackground    = (HBRUSH) (COLOR_3DFACE + 1);
  20. wndcls.lpszMenuName     = NULL;
  21. wndcls.lpszClassName    = GRIDCTRL_CLASSNAME;
  22. if (!AfxRegisterClass(&wndcls))
  23. {
  24. AfxThrowResourceException();
  25. return FALSE;
  26. }
  27. }
  28. return TRUE;
  29. }

下面介绍如何在DLL工程中使用CGridCtrl类。新建一个MFC 常规DLL,接着新建一个对话框资源,拖一个Custom Control进对话框,Class填:MFCGridCtrl,ID取为IDC_GRID,

如下图:

在对话框类中添加成员变量:

  1. CGridCtrl m_Grid;

在对话框的实现代码中添加控件绑定以及注册窗口代码:

  1. void CGridDlg::DoDataExchange(CDataExchange* pDX)
  2. {
  3. CDialog::DoDataExchange(pDX);
  4. DDX_Control(pDX, IDC_GRID, m_Grid);
  5. }
  6. extern CDllGridApp theApp;
  7. CGridDlg::CGridDlg(CWnd* pParent /*=NULL*/)
  8. : CDialog(CGridDlg::IDD, pParent)
  9. {
  10. // 最好在构造函数注册,其它地方不保证成功,传入是DLL的模块句柄
  11. m_Grid.RegisterWindowClass(theApp.m_hInstance);
  12. }

效果图如下,其中弹出对话框的操作在DLL实现:

相关源码下载:

MFC Gridcontrol封装为DLL的源码

将MFC Grid control封装为DLL的做法及其在DLL中的使用方法的更多相关文章

  1. MFC Grid control 2.27

    原文链接地址:http://www.codeproject.com/Articles/8/MFC-Grid-control MFCGridCtrl是个强大的类,用于数据的表格显示. 1.类特征 Cel ...

  2. MFC 使用MFC EditBrowse Control控件选择文件或者文件夹

    从工具箱中拖拽一个MFC EditBrowse Control到窗体中, 通过设置“Browse Mode”属性指定“文件浏览”还是“文件夹浏览” 可以通过添加对象的方式将其与一个CString se ...

  3. mfc中Button、Edit Control和MFC EditBrowse Control的用法

    [前(fei)言(hua)] 写LL(1)分析器被CString转string卡了一个多小时也是醉了. 趁着还算清醒写下这次用到的控件的使用方法好了. 这次实验的mfc用到了四个控件:Edit Con ...

  4. OCX控件在IE中无法侦测到键盘消息( MFC ActiveX Control in IE Doesn't Detect Keystrokes)

    症状描述: Accelerator keys, such as ARROW keys, are first received by the message pump of the ActiveX co ...

  5. Oracle Grid control 11g及Active DataGuard 11g安装部署

    Oracle Grid control 11g及Active DataGuard 11g安装部署(一) 原贴 http://blog.csdn.net/lichangzai/article/detai ...

  6. grid control 11.1.0.1 安装指南

    grid control 11.1.0.1 安装指南 废话少说,进入正题 系统版本号 [root@gridcontrol ~]# lsb_release -a LSB Version:    :bas ...

  7. 转 11g Grid Control: Overview of the EMCTL Options Available for Managing the Agent

    1.概念: The Enterprise Manager DBConsole consists of the following components: - A Standalone OC4J Man ...

  8. 10.2.0.1.1 grid control的启动和关闭

    一.Stopping Grid Control and All Its Components 1.停止OMS服务 [oracle@ocm2 oms10g]$ cd /u01/app/oracle/Or ...

  9. Access Grid Control Properties 访问网格控件属性

    In this lesson, you will learn how to access the properties of a list form's Grid Control in WinForm ...

随机推荐

  1. 让你在DOS中任意切换目录

    尽管Windows图形界面早已经取代了无趣的DOS字符界面(废话,Vista都呼之欲出了),不过在日常操作中,还是有很多时候需要用到命令提示符.比如批量重命名文件时.执行字符命令时.在命令行下恢复系统 ...

  2. SQL Server 查看数据表占用空间大小的SQL语句

    ) ) if object_id('tempdb..#space') is not null drop table #space ),rows ),data ),index_size ),unused ...

  3. day7_python学习笔记_chapter9_文件

    1. open(), file(), 作用完全相同 2. 语法: file_object = open(file_name, access_mode='r', buffering='-1') acce ...

  4. R与数据分析旧笔记(十七) 主成分分析

    主成分分析 主成分分析 Pearson于1901年提出的,再由Hotelling(1933)加以发展的一种多变量统计方法 通过析取主成分显出最大的个别差异,也用来削减回归分析和聚类分析中变量的数目 可 ...

  5. git配置ssh

    $ git config --global user.name "yourname"$ git config --global user.email "youremail ...

  6. [LeetCode]题解(python):136-Single Number

    题目来源: https://leetcode.com/problems/single-number/ 题意分析: 给定一个数组,每个数都出现了2次,只有一个出现了一次,找出这个数.要求时间复杂度O(n ...

  7. 关于strcpy的实现.

    #include <stdio.h> #include <stdlib.h> int strlen(const char *str) { ; while(*str++!='\0 ...

  8. Linux下C编程通过宏定义打开和关闭调试信息

    GCC支持宏定义 gcc -Dmacro,将macro定义为1,我们可以利用这点在我们的代码中加入宏定义开关. #ifdef DEBUG #define pdebug(format, args...) ...

  9. Qt qss一些伪装态,以及margin与padding区别

    伪状态    描述 :checked    button部件被选中:disabled    部件被禁用:enabled    部件被启用:focus    部件获得焦点:hover    鼠标位于部件 ...

  10. QT小记之在VS2005中使用(设置QMAKESPEC环境变量,以及编译QT Lib)

    QT的结构很清晰明了,看过第一个HELLO WORLD便爱上了它,感觉CEGUI有借鉴过QT的设计.如何在Windows平台下使用QT开发?一,下载SDK包请去官网(QT被NOKIA收购,貌似使用协议 ...