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__
#define MYWNDCLASS "mycontrol"
#include <afxtempl.h>
class CMycontrol: public CWnd
{
private:
public:
static BOOL RegisterWndClass();
CMycontrol();
void customfun();//一個自定義方法
};
#endif
2.4 Mycontrol.cpp中的實現部分
#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 在點擊事件中加入測試代碼:
void CCustomcontrolDlg::OnButton1()
{
// TODO: Add your control notification handler code here
m_mycontrol.customfun();
}
四、編譯運行vc++自定義控件的對話框窗體.編譯成功但運行什么也不顯示的解決
右鍵自定義控件->屬性->類型中填寫"mycontrol"再次允許OK!
到此VC++自定義控件就全部介紹完畢,你可以在類型中加入你要實現的方法。
以上所述就是本文的全部內容了,希望大家能夠喜歡。
相關文章
C++命名空間using?namespace?std是什么意思
namespace中文意思是命名空間或者叫名字空間,傳統(tǒng)的C++只有一個全局的namespace,下面這篇文章主要給大家介紹了關于C++命名空間using?namespace?std是什么意思的相關資料,需要的朋友可以參考下2023-01-01
C或C++報錯:ld returned 1 exit status報錯的原因及解
這篇文章主要介紹了C或C++報錯:ld returned 1 exit status報錯的原因及解決方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-02-02
詳解c++ atomic原子編程中的Memory Order
在多核編程中,我們使用內核對象【如:事件對象(Event)、互斥量對象(Mutex,或互斥體對象)、信號量對象(Semaphore)等】來避免多個線程修改同一個數據時產生的競爭條件。本文將詳細介紹c++ atomic原子編程中的Memory Order。2021-06-06
C++中靜態(tài)初始化數組與動態(tài)初始化數組詳解
今天小編就為大家分享一篇C++中靜態(tài)初始化數組與動態(tài)初始化數組詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07

