VC++ 自定义控件的建立及使用方法
一、VC++定义自定义控件与delphi,VB有些差异。
delphi,vb在file-new-other中建立。vc++在工具栏中就有自定义控件,但必须加入控件类型。
许多书籍都在类向导中建立。我这里介绍的是手动建立,其结果是一样的。
二.建立过自定义控件类型:
2.1、把工具栏上的自定义控件放入对话框中
2.2、建立Mycontrol.h,Mycontrol.cpp文件
2.3、Mycontrol.h中的定义是
#ifndef__MYCTROLTRL_H__
#define__MYCTROLTRL_H__
#defineMYWNDCLASS"mycontrol"
#include<afxtempl.h>
classCMycontrol:publicCWnd
{
private:
public:
staticBOOLRegisterWndClass();
CMycontrol();
voidcustomfun();//一个自定义方法
};
#endif
2.4Mycontrol.cpp中的实现部分
#include"StdAfx.h"
#include"mycontrol.h"
CMycontrol::CMycontrol()
{
CMycontrol::RegisterWndClass();
}
//注册控件RegisterWndClass格式是固定的不要记忆没有那个必要直接拷贝粘贴就可以。
CMycontrol::RegisterWndClass()
{
WNDCLASSwindowclass;
HINSTANCEhInst=AfxGetInstanceHandle();
//Checkweathertheclassisregisterdalready
if(!(::GetClassInfo(hInst,MYWNDCLASS,&windowclass)))
{
//Ifnotthenwehavetoregisterthenewclass
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();
returnFALSE;
}
}
returnTRUE;
}
//自定义方法
voidCMycontrol::customfun()
{
AfxMessageBox(_T("mycontrol!"));
}
三、使用自定义控件
3.1.在类向导中绑定自定义控件时你是找不到刚才你定义的类型的,所以我采用手动加入代码方法。
3.2.在对话框.h文件中手动加入:public:CMycontrolm_mycontrol;
3.3.在对话框.cpp文件中手动加入:DDX_Control(pDX,IDC_CUSTOM1,m_mycontrol);
3.4.在对话框中加入Button在点击事件中加入测试代码:
voidCCustomcontrolDlg::OnButton1()
{
//TODO:Addyourcontrolnotificationhandlercodehere
m_mycontrol.customfun();
}
四、编译运行vc++自定义控件的对话框窗体.编译成功但运行什么也不显示的解决
右键自定义控件->属性->类型中填写"mycontrol"再次允许OK!
到此VC++自定义控件就全部介绍完毕,你可以在类型中加入你要实现的方法。
以上所述就是本文的全部内容了,希望大家能够喜欢。
热门推荐
10 小红书平安祝福语简短
11 生日祝福语大全女孩简短
12 收生日红包祝福语 简短
13 领证幽默祝福语简短
14 法考面试祝福语简短
15 老哥出门祝福语简短语
16 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语