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. WCF Service Configuration Editor的使用

    原文:http://www.cnblogs.com/Ming8006/p/3772221.html 通过WCF Service Configuration Editor的配置修改Client端 参考 ...

  2. FAT下的winhex数据恢复

    一·我在自己的U盘中建一个一个文件abc.word,然后删除 二·用winhex打开需要恢复的磁盘 我这是U盘 I:按确定打开它 三.来到它的根目录下 四·查找文件名,找到文件目录项 文件起始簇号:5 ...

  3. BZOJ 1974: [Sdoi2010]auction 代码拍卖会( dp )

    在1, 11, 111……中选<=8个, + 11..(n个1)拼出所有可能...这些数mod p至多有p中可能, 找出循环的处理一下. 那么dp就很显然了...dp(i, j, k)表示前i种 ...

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

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

  5. [互联网面试笔试汇总C/C++-9] 实现赋值运算符函数-剑指offer

    题目:如下为类型CMyString的声明,请为该类型添加赋值运算符函数. class CMyString { public: CMyString(char* pData = NULL); CMyStr ...

  6. bootstrap你让前端小狮子们又喜又恨

    看了一个用bootstrap框架建的页面,简直方便至极!各种添加类就可以实现各种功能,各种添加data-toggle让你不需要懂javascript就可以做出很炫的效果! 最重要的它做出来的页面还是响 ...

  7. nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException

    You should autowire interface AbstractManager instead of class MailManager. If you have different im ...

  8. Oracle的TPCC测试,原来也是个作弊的东西...

    http://www.oaktable.net/content/sorted-hash-clusters-rip 根据Jonathan Lewis老先生的测试实例,发觉cluster 的sort功能, ...

  9. mysql 批量删除分区

    alter table titles drop partition p01; use zabbix; mysql> source drop_par.sql [oracle@oadb mysql] ...

  10. cpu亲和力总结taskset和setcpu及其他相关

    一:taskset -- 获取或指定进程运行的CPU. man taskset出现 CPU affinity is a scheduler property that "bonds" ...