VS2010添加自定义控件
2015-04-20 13:57
211 查看
VS2010添加自定义控件
一:自定义一个空白控件1 :先创建一个MFC工程
NEW Project-->MFC-->MFC Application-->name: CustomCtr-->Type选择“Dialog based”;
2: 在窗口中添加一个自定义控件
Toolbox-->“Custom Control”-->属性-->class随便填写一个控件类名“CMyWin”, 这个名字用于以后注册控件用的,注册函数 RegisterWindowClass();
3:创建一个类
在窗口中,右击custom control 控件-->ClassWizard-->ClassWizard-->Add Class-->类名CMyTest以C开头-->Base class:CWnd;
4:注册自定义控件MyWin
在MyTest类.h文件中声明注册函数BOOL RegisterWindowClass(HINSTANCE hInstance = NULL);
BOOL CMyTest::RegisterWindowClass(HINSTANCE hInstance)
{
LPCWSTR className = L"CMyWin";//"CMyWin"控件类的名字
WNDCLASS windowclass;
if(hInstance)
hInstance = AfxGetInstanceHandle();
if (!(::GetClassInfo(hInstance, className, &windowclass)))
{
windowclass.style = CS_DBLCLKS;
windowclass.lpfnWndProc = ::DefWindowProc;
windowclass.cbClsExtra = windowclass.cbWndExtra = 0;
windowclass.hInstance = hInstance;
windowclass.hIcon = NULL;
windowclass.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
windowclass.hbrBackground = ::GetSysColorBrush(COLOR_WINDOW);
windowclass.lpszMenuName = NULL;
windowclass.lpszClassName = className;
if (!AfxRegisterClass(&windowclass))
{
AfxThrowResourceException();
return FALSE;
}
}
return TRUE;
}
5:在MyTest类的构造器中调用,RegisterWindowClass();
CMyTest::CMyTest()
{
RegisterWindowClass();
}
6:控件与对话框数据交换
在CustomCtrDlg.h中定义一个变量
CMyTest m_draw;
在对话框类的CustomCtrDlg.cpp的DoDataExchange函数中添加 DDX_Control(pDX,IDC_CUSTOM1,m_draw);
void CCustomCtrDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX,IDC_CUSTOM1,m_draw);
}
以上是自定义一个空白控件。
二: 在控件上绘图
7: 在CMyTest类中添加一个绘图消息
在VS2010最左侧Class View中右击CMyTest类-->ClassWizard-->Messages-->WM_PAINT-->双击
开发环境自动添加OnPaint()函数及消息队列。
8:编写OnPaint()函数
例如:画一条直线
void CMykk::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
// Do not call CWnd::OnPaint() for painting messages
CRect rect;
this->GetClientRect(rect);
dc.MoveTo(0,0);
dc.LineTo(rect.right,rect.bottom);
}
转自:/article/10427173.html
相关文章推荐
- VS2010添加自定义控件
- vs2010怎么添加自定义控件呢
- VS2010添加自定义控件以及使用第三方控件的方法 自定义图标设置方法
- VS2010添加自定义控件
- vs2010中添加自定义控件或第三方控件
- SP2010开发和VS2010专家"食谱"--第一章节--列表和事件接收器(4)--添加自定义错误信息到事件接收器
- 为VS2010默认模板添加版权信息
- [VC++]为自定义控件添加ToolTip
- VS2010 如何添加H文件目录和LIB目录
- VS2010/MFC编程入门之八(对话框:为控件添加消息处理函数)
- VS2010添加新类时自动添加public关键字
- VS2010 工具箱组件添加重复
- 安装VS2010后,向SQL Server 2008 Express添加SQL Server Management
- VS2010 如何添加H文件目录和LIB目录
- 为ActiveX窗口上的自定义控件添加ToolTip
- Qt Creator 和在 vs2010 里 怎么添加信号和槽
- VS2010添加汇编
- VS2010 C++ 添加注释模板
- VS2010 MFC对话框程序用CButtonST给按钮添加图标
- vs2010添加库文件的方法