欢迎来到山村网

VC++ 自定义控件的建立及使用方法

2019-03-02 14:29:32浏览:405 来源:山村网   
核心摘要:  这篇文章主要介绍了VC++ 自定义控件的建立及使用方法的相关资料,十分的详细,需要的朋友可以参考下  一、VC++定义自定义控

  这篇文章主要介绍了VC++ 自定义控件的建立及使用方法的相关资料,十分的详细,需要的朋友可以参考下

  一、VC++定义自定义控件与delphi,VB有些差异。

  delphi,vb在 file-new-other中建立。vc++在工具栏中就有自定义控件,但必须加入控件类型。

  许多书籍都在类向导中建立。我这里介绍的是手动建立,其结果是一样的。

  二.建立过自定义控件类型:

  2.1、把工具栏上的自定义控件放入对话框中

  2.2、建立Mycontrol.h, Mycontrol.cpp文件

  2.3、Mycontrol.h中的定义是

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 #ifndef __MYCTROLTRL_H__ #define __MYCTROLTRL_H__ #define MYWNDCLASS "mycontrol" #include <afxtempl.h> class CMycontrol: public CWnd { private: public: static BOOL RegisterWndClass(); CMycontrol(); void customfun();//一个自定义方法 }; #endif

  2.4 Mycontrol.cpp中的实现部分

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include "StdAfx.h" #include "mycontrol.h" CMycontrol::CMycontrol() { CMycontrol::RegisterWndClass(); } //注册控件RegisterWndClass格式是固定的不要记忆没有那个必要直接拷贝粘贴就可以。 CMycontrol::RegisterWndClass() { WNDCLASS windowclass; HINSTANCE hInst = AfxGetInstanceHandle(); //Check weather the class is registerd already if (!(::GetClassInfo(hInst, MYWNDCLASS, &windowclass))) { //If not then we have to register the new class windowclass.style = CS_DBLCLKS;// | CS_HREDRAW | CS_VREDRAW; windowclass.lpfnWndProc = ::DefWindowProc; windowclass.cbClsExtra = windowclass.cbWndExtra = 0; windowclass.hInstance = hInst; windowclass.hIcon = NULL; windowclass.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW); windowclass.hbrBackground = ::GetSysColorBrush(COLOR_WINDOW); windowclass.lpszMenuName = NULL; windowclass.lpszClassName = MYWNDCLASS; if (!AfxRegisterClass(&windowclass)) { AfxThrowResourceException(); return FALSE; } } return TRUE; } //自定义方法 void CMycontrol::customfun() { AfxMessageBox(_T("my control!")); }

  三、使用自定义控件

  3.1.在类向导中绑定自定义控件时你是找不到刚才你定义的类型的,所以我采用手动加入代码方法。

  3.2.在对话框.h文件中手动加入:public: CMycontrol m_mycontrol;

  3.3.在对话框.cpp文件中手动加入:DDX_Control(pDX,IDC_CUSTOM1,m_mycontrol);

  3.4.在对话框中加入Button 在点击事件中加入测试代码:

  ?

1 2 3 4 5 void CCustomcontrolDlg::OnButton1() { // TODO: Add your control notification handler code here m_mycontrol.customfun(); }

  四、编译运行vc++自定义控件的对话框窗体.编译成功但运行什么也不显示的解决

  右键自定义控件->属性->类型中填写"mycontrol"再次允许OK!

  到此VC++自定义控件就全部介绍完毕,你可以在类型中加入你要实现的方法。

  以上所述就是本文的全部内容了,希望大家能够喜欢。

(责任编辑:豆豆)
下一篇:

C#连接Oracle数据库的方法

上一篇:

C#中委托用法实例详解

  • 信息二维码

    手机看新闻

  • 分享到
打赏
免责声明
• 
本文仅代表作者个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,作者需自行承担相应责任。涉及到版权或其他问题,请及时联系我们 xfptx@outlook.com