CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 19

Threaded View

  1. #10
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: [RESOLVED] Creating DialogBar with my own controls (MFC)

    Quote Originally Posted by JazzDev View Post
    Quote Originally Posted by ovidiucucu View Post
    Nope, there is not absolutely necessary to derive from CDialogBar.
    CDialogBar forwards its control child notifications (either received via WM_COMMAND or WM_NOTIFY) to its owner frame.
    Just handle them in the owner frame.
    Ok, probably I'm misunderstanding but.. this can work if I just want to handle a button click event, adding it's respective event handler.

    But, what about adding strings to a listbox? I usually add them this way:
    1. Create member variable m_list
    2. m_list.AddString(...)
    [...]
    Again, in other words: derive from CDialogBar (or other controlbar class derived from CControlBar) ONLY if you deal with a very complex one or having a customized behavior. Otherwise, deriving may give you more headaches than help, as we can notice from this thread.

    Let's say we have a dialog bar having a button and a listbox control. If you want to add a string in the listbox, as a response to button click, all you have to do is to handle BN_CLICKED notification (note that BN_CLICKED is also sent via WM_COMMAND) in the main frame class (or in view, or in document class), then use GetDlgItem to get a temporary pointer to listbox object. Finally, use that pointer to add strings or do something else your muscles want.

    Code:
    // MainFrm.h
    
    class CMainFrame : public CFrameWnd
    {
       CDialogBar m_dlgBar;
    
       // ...
       afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
       afx_msg void OnButtonAdd(); // Note: this is manually added
       DECLARE_MESSAGE_MAP()
    };
    Code:
    // MainFrm.cpp
    
    // ...
       ON_WM_CREATE()
       ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd) // Note: this is manually added
    END_MESSAGE_MAP()
    // ...
    
    int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
       // ...
       VERIFY(m_dlgBar.Create(this, IDD_DIALOGBAR, CBRS_LEFT, IDD_DIALOGBAR)); 
       m_dlgBar.EnableDocking(CBRS_ALIGN_RIGHT|CBRS_ALIGN_LEFT);
       // ...
       EnableDocking(CBRS_ALIGN_ANY);
       // ...
       DockControlBar(&m_dlgBar);
       return 0;
    }
    
    void CMainFrame::OnButtonAdd() // Note: this is manually added
    {
       // get a pointer to listbox control contained in the dialog bar
       CListBox* pListBox = (CListBox*)m_dlgBar.GetDlgItem(IDC_LIST_MESSAGES);
       // verify it's a pointer to a valid object
       ASSERT_VALID(pListBox);
       // add string to list box (just for demo purpose)
       int nIndex = pListBox->AddString(_T("BOB WUZ ERE!")); 
       // select newly added item
       pListBox->SetCurSel(nIndex);
    }
    Of course, you have to manually map BN_CLICKED, as well as other notifications sent by dialog bar, but that's not so much to sweat.
    Last edited by ovidiucucu; May 29th, 2013 at 02:09 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured