1. There are some differences between VC++ and custom controls delphi and VB.
delphi,vb is built in file-new-other. vc++ has custom controls in the toolbar, but the control type must be added.
Many books are built in the Class Wizard. What I'm introducing here is manual creation, and the result is the same.
2. Created a custom control type:
2.1. Put the custom controls on the toolbar into the dialog box
2.2. Create, file
2.3. The definition in
#ifndef __MYCTROLTRL_H__ #define __MYCTROLTRL_H__ #define MYWNDCLASS "mycontrol" #include <> class CMycontrol: public CWnd { private: public: static BOOL RegisterWndClass(); CMycontrol(); void customfun();//A custom method }; #endif
Implementation part in 2.4
#include "" #include "" CMycontrol::CMycontrol() { CMycontrol::RegisterWndClass(); } //RegisterWndClass format is fixed. Don't remember it. There is no need to copy and paste it directly. 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 = CS_DBLCLKS;// | CS_HREDRAW | CS_VREDRAW; = ::DefWindowProc; = = 0; = hInst; = NULL; = AfxGetApp()->LoadStandardCursor(IDC_ARROW); = ::GetSysColorBrush(COLOR_WINDOW); = NULL; = MYWNDCLASS; if (!AfxRegisterClass(&windowclass)) { AfxThrowResourceException(); return FALSE; } } return TRUE; } //Customize method void CMycontrol::customfun() { AfxMessageBox(_T("my control!")); }
3. Use custom controls
3.1. When binding custom controls in the class wizard, you cannot find the type you defined just now, so I used the manual code addition method.
3.2. Manually add in the dialog box.h file: public: CMycontrol m_mycontrol;
3.3. Manually add: DDX_Control(pDX,IDC_CUSTOM1,m_mycontrol);
3.4. Add Button to the dialog box Add test code to the click event:
void CCustomcontrolDlg::OnButton1() { // TODO: Add your control notification handler code here m_mycontrol.customfun(); }
4. Compile the dialog box form that runs vc++ custom controls. The solution is that the compilation is successful but nothing is displayed.
Right-click to customize the control->Properties->Type and fill in "mycontrol" and allow it again OK!
At this point, the VC++ custom controls are all introduced. You can add the method you want to implement in the type.
The above is the entire content of this article, I hope you like it.