Creating a button at Runtime

You can create  a button on a dialog at runtime in few steps.

 

Step 1: Create an instance of CButton. Write this line in the header file of your CDialog derived class CButton m_Button; 

Step 2: Define a resource ID. #define MyButton 11111 . I have defined this resource id as a global variable in my cpp file. You can define in your resource's header file.

Step 3: Now get the size of your dialog and create the button. Write this code on OnInitDialog of your dialog class:

// Creating a button on runtime 

RECT rc;

GetWindowRect (&rc);

rc.bottom += 30;

MoveWindow (rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top);

// Convert to client coordinates

ScreenToClient (&rc);

// m_Button is of type CButton and is a member of CMySheet

m_Button.Create ("&Runtime Button", WS_CHILD | WS_VISIBLE | WS_TABSTOP,

CRect (5, rc.bottom-30, 120, rc.bottom-5),

this, MyButton);