Click here to Skip to main content
15,793,045 members
Articles / Desktop Programming / MFC

Creating and Using custom controls in VC++

Rate me:
Please Sign up or sign in to vote.
4.66/5 (81 votes)
21 Sep 20036 min read 249.2K   4.3K   110   42
This article will create a custom control using the custom control on the control bar.

Sample Image - CustomControl.gif


Hi there! This is my fourth article for The Code Project. Migrating towards the VC++, I have firstly concerned with the custom controls that can be created with the help of VC++, since that is a very helpful feature when you want to modify the contents of any control or you want to create your own. So, I have decided to write this article so the new developers or the beginners who want to develop any controls will get some help from my small knowledge.

Now, that’s all for the introduction now, I am moving towards the original point of view: that is, how and why to create any custom control. As I have interest in developing applications in Win32 API because of its small size and stand alone executables, I never ever had worked on the VC++ but, it is too much powerful language and the power-features in it have attracted me towards it. One of them is the custom control. Too much of articles on the CodeProject have used the custom controls. But when I have read them firstly, I haven’t understood how to create and get the messages and then process on that messages in the simple Windows applications. The custom controls give the developer a convenient way to create the control and visualize that as the regular control.

(As I am a beginner with VC++, please tell me if there are any mistakes in this article).

Where is it?

Now, the question is that, where is the custom control? So, answer is below. The picture below shows the custom control, it lies in the control bar.

Image 2

The picture shows the position of the custom control. You are able to select it and draw directly on your form resource.

The main problem arises after you have put that control on your form that if you build and execute the program the view is not available since you haven't selected any class for your control, so that part is discussed in a later section.

Creating a Class

Now, the figure above shows the custom control as drawn on the form view. Now, you have to right click on that and select ClassWizard form the popup menu.

Image 3

Selecting a class

After you have clicked the Class wizard, the dialog shown here appears on the screen. From it, select Add Class and then New.

Image 4

Now, once you have clicked the New button, the dialog to select the base class for our custom control will appear as below. Here, you have multiple choices to select any base class. That means you can customize the basic control like, static control or the edit control, by adding the new features, or you are able to create a fully new control. I have decided to create a fully new control like a pad, and so, I have selected a basic CWnd class as a base class.

Image 5

And finally, you have created a class for your control. Now, the serious part begins....

As the class has been created by using the CWnd as a base, we will have to register this class since this is a custom class. So, we will have to write the function RegisterWndClass() to do that. It may be coded as below...

BOOL MyCustomControl::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;
        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))
            return FALSE;

    return TRUE;

In this way, we have registered the new window class. Now, you will have to add that function in your default class constructor as follows:

    //Register My window class

I think anyone will think what is MYWNDCLASS. The answer is that it is the defined class name for our custom class. It is defined at the top of the MyCustomControl.h file as follows:

#define MYWNDCLASS "MyDrawPad"

Now, we have our own class called MyDrawPad.

Attaching Class to the Custom Control:

With all things going right, we are approaching towards completing the creation of the custom control. The last thing remaining is to set the custom control as our created window class. For that, right click on the custom control in the resource view and then select the properties of the custom control. The dialog box will appear as shown below...

Image 6

Then, set the class name as MyDrawPad that we have created earlier. Here you can select the window style by changing the hexadecimal value in the style edit box.

I have experimented with some of the values, you can also try them.

Doing the DataExchange

Now, all the things are set up. But the data must be exchanged between the window and our application. So, add a variable for our custom control to your dialog class as follows:

// Implementation
HICON m_hIcon;
MyCustomControl m_drawpad;//This is our custom control

After that, you have to add the following code in the DoDataExchage() function to interact with the custom control.

void CCustomControlDlg::DoDataExchange(CDataExchange* pDX)
    // NOTE: the ClassWizard will add DDX and DDV calls here

Now, are you ready for the action??? Then, press Ctrl+F5 to compile and execute the program. (Wish you all the best... I think there is no error!!!)

Do not forget to write #include "MyCustomControl.h" in the Dialog's Header file, else it will generate too many errors. (I Think you will not blame me HHAA HHAA HHAA).

Adding and processing the messages

After succeeding in the critical part above, you should get the Dialog box containing the white rectangle on it. That is our custom control (Belive Me!). That's only a small window. Now, we will have to add some Windows messages to interact with that control. Read carefully...

To add Windows messages to the window, right click on the class MyCustomControl and select Add Windows Message Handler to add the messages like, mouse move, click etc...

Image 7

In this way, after a long work (is it?), you have created your own custom control. Now relax, and start writing your own. And please rate my article (I like it). For example, I have written a simple DrawPad in the included source code.

Now, we will go through a short summary of this article:

To create the custom control, we will have to do the following things:

  • Create a simple MFC Application containing Dialog resource.
  • Select the custom control form the control bar.
  • Draw the custom control on the Dialog resource.
  • Right click the custom control and select the Class Wizard.
  • From Add class popup, add new custom class selecting the appropriate base class.
  • Add the code and register the Custom Window class.
  • Add the member variable for a base class (custom class) in the dialog.
  • Set the custom control's class to the registered window class name.
  • Add the DoDataExchange code.
  • Now, press Ctrl+F5 to compile and execute the application.
  • Add/Edit the Windows message handlers by right clicking the Custom control's class in the class view.

If you really like it, then mail me at, and mail me your suggestions and spelling Missssstakes in this article. Also any bugs in the source code (as I am the BugHunter{ I think so, Do you?}).


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Written By
Software Developer AVAYA India Pvt Ltd.
India India
I have completed my B.E. Degree in IT form Aditya Engineering College, Beed. (Maharashtra) India.

I have completed Diploma In Advanced Computing (DAC) in Feb 07.

Now I am working for AVAYA India Pvt. LTD as software engineer.
Platform : C/C++, Solaris 10, AIX and Windows

Comments and Discussions

GeneralRe: General function Pin
Yogesh M Joshi7-Jan-05 8:32
Yogesh M Joshi7-Jan-05 8:32 
GeneralHelp: To generate Drive control in VC++ Pin
Lalit Kandi10-Jul-06 22:33
Lalit Kandi10-Jul-06 22:33 
GeneralExcellent Pin
kobuster19-Sep-04 17:25
kobuster19-Sep-04 17:25 
GeneralRe: Excellent Pin
Yogesh M Joshi5-Feb-05 6:28
Yogesh M Joshi5-Feb-05 6:28 
GeneralOnCreate Pin
ZejulioZ7-Sep-04 22:43
ZejulioZ7-Sep-04 22:43 
GeneralRe: OnCreate Pin
Anonymous12-Feb-05 2:56
Anonymous12-Feb-05 2:56 
GeneralGood job... Pin
manthrax22-Mar-04 16:09
manthrax22-Mar-04 16:09 
GeneralRe: Good job... Pin
Yogesh M Joshi12-May-04 23:02
Yogesh M Joshi12-May-04 23:02 
Thanks.Blush | :O
GeneralGood effort but... Pin
Paul Vickery22-Sep-03 23:37
professionalPaul Vickery22-Sep-03 23:37 
GeneralRe: Good effort but... Pin
SoothingMist11-Feb-04 10:29
SoothingMist11-Feb-04 10:29 
GeneralRe: Good effort but... Pin
Yogesh M Joshi12-May-04 23:00
Yogesh M Joshi12-May-04 23:00! Pin
Anonymous22-Sep-03 8:53
Anonymous22-Sep-03 8:53 
GeneralRe:! Pin
Juan Carlos Cobas22-Sep-03 11:22
Juan Carlos Cobas22-Sep-03 11:22 
GeneralRe:! Pin
Anonymous22-Sep-03 12:18
Anonymous22-Sep-03 12:18 
GeneralRe:! Pin
Lars [Large] Werner23-Sep-03 21:56
professionalLars [Large] Werner23-Sep-03 21:56 
GeneralRe:! Pin
Francisco José Sen del Prado25-Sep-03 21:19
Francisco José Sen del Prado25-Sep-03 21:19 
GeneralRe:! Pin
WildWildWind12-Mar-05 7:39
WildWildWind12-Mar-05 7:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.