Click here to Skip to main content
15,880,364 members
Articles / Desktop Programming / MFC
Article

A simple tab like dialog control

Rate me:
Please Sign up or sign in to vote.
2.76/5 (39 votes)
31 Aug 2003 138.4K   3.8K   39   19
CTabDialog bundles buttons and dialogs, so users can add their owner draw buttons and dialogs

Sample Image - TabDialog.gif

Introduction

Nowadays dialog based applications are becoming more and more popular. To make my future development for dialog based application easier I worked out this CTabDialog, which operates like a Tab control, but gives you more chance to have your owner draw buttons and dialogs added. Hopefully CTabDialog will make your development of dialog based application easier.

Using the code

To use the class

  • Include the class to your application
    C++
    #include 
    "TabDialog.h"
  • Add the CTabDialog data member to your application:
    C++
    private:
        CTabDialog* m_pTabDialog;
  • In OnInitDialog() of your application, initialize the CTabDialog
    C++
    //create the TabDialog
    m_pTabDialog = new CTabDialog(IDD_TABDLG, this);
    
    if (m_pTabDialog->Create(IDD_TABDLG, this) == FALSE)
        return FALSE;
    
    //set the TabDialog's positon
    m_pTabDialog->SetWindowPos(this, rect.left, rect.top, 0 , 0,
        SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW);

    Note: you need to create a dialog resource for the CTabDialog control.

  • You then need to add pages to the control
    C++
    ////////////////////////////////////////////////////////////
    //Add pages (include button and dialog) to TabDialog
    BOOL CTabDialogTestDlg::AddPagesToTabDialog()
    {
        //create first button
        m_pBtnOne = new CButton();
        RECT rectOne;
    
        rectOne.left = BTNONE_LOCATION.x;
        rectOne.right = BTNONE_LOCATION.x+BUTTON_WIDTH;
        rectOne.top = BTNONE_LOCATION.y;
        rectOne.bottom = BTNONE_LOCATION.y+BUTTON_HEIGHT;
    
        m_pBtnOne->Create("One", WS_CHILD | WS_VISIBLE | WS_TABSTOP, rectOne,
            m_pTabDialog, BUTTON_ONE);
    
        //create first dialog
        m_pPageOne = new CPageOneDlg(m_pTabDialog);
    
        if (m_pPageOne->Create(IDD_PAGE_ONE, m_pTabDialog) == FALSE)
            return FALSE;
    
        //add first page
        m_pTabDialog->AddPage(m_pPageOne, m_pBtnOne);
    
        //Create second button
        m_pBtnTwo = new CButton();
        RECT rectTwo;
    
        rectTwo.left = BTNTWO_LOCATION.x;
        rectTwo.right = BTNTWO_LOCATION.x+BUTTON_WIDTH;
        rectTwo.top = BTNTWO_LOCATION.y;
        rectTwo.bottom = BTNTWO_LOCATION.y+BUTTON_HEIGHT;
    
        m_pBtnTwo->Create("Two", WS_CHILD | WS_VISIBLE | WS_TABSTOP, rectTwo,
            m_pTabDialog, BUTTON_TWO);
    
        //create second dialog
        m_pPageTwo = new CPageTwoDlg(m_pTabDialog);
    
        if(m_pPageTwo->Create(IDD_PAGE_TWO, m_pTabDialog) == FALSE)
            return FALSE;
    
        //add second page
        m_pTabDialog->AddPage(m_pPageTwo, m_pBtnTwo);
    
        //Create third button
        m_pBtnThree = new CButton();
        RECT rectThree;
    
        rectThree.left = BTNTHREE_LOCATION.x;
        rectThree.right = BTNTHREE_LOCATION.x+BUTTON_WIDTH;
        rectThree.top = BTNTHREE_LOCATION.y;
        rectThree.bottom = BTNTHREE_LOCATION.y+BUTTON_HEIGHT;
    
        m_pBtnThree->Create("Three", WS_CHILD | WS_VISIBLE | WS_TABSTOP,
            rectThree, m_pTabDialog, BUTTON_THREE);
    
        //create third dialog
        m_pPageThree = new CPageThreeDlg(m_pTabDialog);
    
        if(m_pPageThree->Create(IDD_PAGE_THREE, m_pTabDialog) == FALSE)
            return FALSE;
    
        //add third page
        m_pTabDialog->AddPage(m_pPageThree, m_pBtnThree);
    
        return TRUE;
    }
  • You then need to call InitPagesShow() member function of CTabDialog to set the default showing of the control.
    C++
    //initialize the showing of TabDialog
    m_pTabDialog->InitPagesShow();

License

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
Web Developer
Singapore Singapore
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionMemory leak Pin
msopengl10-Oct-11 18:26
msopengl10-Oct-11 18:26 
GeneralGreat work! Pin
xtjie21-Nov-10 21:52
xtjie21-Nov-10 21:52 
Generalquestion about create a dlg Pin
husoso25-Feb-08 15:55
husoso25-Feb-08 15:55 
QuestionHow to get SetFocus Pin
sheshidar26-Sep-07 20:45
sheshidar26-Sep-07 20:45 
Generalou very much for this beautifull article.How to get SetFocus Pin
sheshidar26-Sep-07 20:41
sheshidar26-Sep-07 20:41 
QuestionHow change the position of the button? and add new one? Pin
freddy1ca29-Aug-04 10:38
freddy1ca29-Aug-04 10:38 
I'm not be able to move the position of your button.
If a chage the line in the testconstant.h

const CPoint BTNONE_LOCATION(0,0);
const CPoint BTNTWO_LOCATION(0,0);
const CPoint BTNTHREE_LOCATION(0,0);

nothing change, i can put any value and nothing change...

if a change
const int BUTTON_WIDTH = 73;
const int BUTTON_HEIGHT = 62;
for other value, the button width and height change...

This is the same case if a change in the program the value of rectOne directly....

What happens, i clean and rebuild the project.
//create first button
m_pBtnOne = new CButton();
RECT rectOne;
rectOne.left = BTNONE_LOCATION.x;
rectOne.right = BTNONE_LOCATION.x+BUTTON_WIDTH;
rectOne.top = BTNONE_LOCATION.y;
rectOne.bottom = BTNONE_LOCATION.y+BUTTON_HEIGHT;

m_pBtnOne->Create("One", WS_CHILD | WS_VISIBLE | WS_TABSTOP, rectOne, m_pTabDialog, BUTTON_ONE);


fred
AnswerRe: How change the position of the button? and add new one? Pin
husoso25-Feb-08 18:50
husoso25-Feb-08 18:50 
GeneralXie xie Wang Yingwu! Pin
Gray Dragon8-Jan-04 18:47
Gray Dragon8-Jan-04 18:47 
GeneralChange tab without really click the button Pin
ycjack29-Nov-03 16:34
ycjack29-Nov-03 16:34 
GeneralRe: Change tab without really click the button Pin
prakashjagdale11-Jan-04 3:20
prakashjagdale11-Jan-04 3:20 
GeneralProject file missing Pin
Steve Messer2-Sep-03 10:59
Steve Messer2-Sep-03 10:59 
GeneralRe: Project file missing Pin
Wang Yingwu2-Sep-03 15:38
Wang Yingwu2-Sep-03 15:38 
GeneralRe: Project file missing Pin
Chris Maunder2-Sep-03 16:14
cofounderChris Maunder2-Sep-03 16:14 
GeneralRe: Project file missing Pin
Wang Yingwu2-Sep-03 16:18
Wang Yingwu2-Sep-03 16:18 
GeneralMemory leak!!! Pin
Chen Jiadong15-Aug-03 18:47
Chen Jiadong15-Aug-03 18:47 
GeneralRe: Memory leak!!! Pin
Wang Yingwu31-Aug-03 22:24
Wang Yingwu31-Aug-03 22:24 
GeneralInteresting! Pin
WREY13-Jun-03 8:48
WREY13-Jun-03 8:48 
GeneralRe: Interesting! Pin
Chris Maunder16-Jun-03 13:08
cofounderChris Maunder16-Jun-03 13:08 
GeneralRe: Interesting! Pin
Wang Yingwu31-Aug-03 22:28
Wang Yingwu31-Aug-03 22:28 

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.