CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Join Date
    May 2013
    Posts
    4

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

    I'm turning mad with a little problem with Visual Studio 2008 and MFC. I'm working on a project actually and I'm trying to create an SDI Application. Right, now I want to add a dockable DialogBar. Well, the first time I added it with the resource view, I can create the bar without problems. But... I want to add some controls like a couple of ListBox, buttons and so on. The program doesn't allows me to create member variables for these controls. The option appears in grey.

    Searching a little, I found that it's mandatory to create a new class derived from CDialogBar and "enhance" it with the Message handler and so on. I've been reading some tutorials and it didn't work for me.

    Can you please help me? I'm starting to get nervous, the use of dialogbars is mandatory in my design and I've never implemented them before.

    Thank you very much, and sorry for my poor english.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Creating DialogBar with my own controls (MFC)

    Victor Nijegorodov

  3. #3
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Creating DialogBar with my own controls (MFC)

    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.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  4. #4
    Join Date
    May 2013
    Posts
    4

    Re: Creating DialogBar with my own controls (MFC)

    To the one who gave me the link:

    At the end of the 2nd step, it says that I have to override the OnInitDialog function. I went to the Class View->right click->properties->overrides and the function doesn't appear on list.

    I'm trying to code it manually, hope it works...


    ________________________
    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(...)

    Now I want to do that, but I'm creating the listbox inside a DialogBar. So I can't use ClassWizard to attach the dialog to a CDialogBar class, and due to this I can't use the wizard to create de m_list variable.

    Sorry if the problem is totally obvious.. This is why I hate learning to use a program with wizards... ¬¬''

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Creating DialogBar with my own controls (MFC)

    Quote Originally Posted by JazzDev View Post
    To the one who gave me the link:

    At the end of the 2nd step, it says that I have to override the OnInitDialog function.
    No, it doesn't.
    What it says is:
    The key to the transformation is the conversion of the virtual OnInitDialog() member function to the WM_INITDIALOG message mapped method by changing the OnInitDialog method and by adding the ON_MESSAGE() handler. You may not have an override of OnInitDialog(). If not, add one before proceeding.
    Then in the pp. 3...5 it describes how to do it.
    Victor Nijegorodov

  6. #6
    Join Date
    May 2013
    Posts
    4

    Re: Creating DialogBar with my own controls (MFC)

    Ooooh holy ****! It worked!

    Thanks for all! I followed the steps of VictorN and now my ****in' listboxes show what I want.

    I owe you one guys

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

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

    Glad you solved your problem!
    But there were not steps of mine but of old good MSDN! (Unfortunately modern MSDN in not so good... )
    Last edited by VictorN; May 28th, 2013 at 12:31 PM.
    Victor Nijegorodov

  8. #8
    Join Date
    May 2013
    Posts
    4

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

    Just one more doubt.
    Now I have the DialogBar working. It has two ListBoxes.
    The DialogBar is defined and created in MainFrame, and I want to add strings to listboxes in the dialogbar when a button is pressed in the default toolbar.
    So I caught the event in the button and..

    MainFrm.h:
    Code:
    ...
    DECLARE_MESSAGE_MAP()
    public:
    
    	afx_msg void OnStart();
    };
    MainFrm.cpp:
    Code:
    ...
    ON_COMMAND(ID_START, &CMainFrame::OnStart)
    ...
    void CMainFrame::OnStart()
    {
    	m_dlgbar.m_list.AddString("Simulation started..");
    }
    And it doesn't works.

    However, if I try to add some text to the listbox in the OnCreate function of the DialogBar, the text is added without any problem..

    Thanks!

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

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

    Quote Originally Posted by JazzDev View Post
    And it doesn't works.

    However, if I try to add some text to the listbox in the OnCreate function of the DialogBar, the text is added without any problem..
    Then there's something wrong in your code.

    Now seriously, how can you expect us to answer that when you provide just a tiny snippet, but not a real code that we can compile and see what happens there?
    Best regards,
    Igor

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

    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/

  11. #11
    Join Date
    Sep 2013
    Posts
    5

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

    Dear All, (JazzDev, VictorN, ovidiucucu,....everyone)

    Could you help me solve my problem as a continuation of this topic. My problem as follows:
    1) I followed the link : How to Initialize Child Controls in a Derived CDialogBar
    To insert an object of CDialogBar in my app which is written in VC+2005.
    However I gets an error, I could not know why eventhough I have double checked the instruction.
    Details:
    I derived a "CPane1" class from CDialogBar

    Code: Pane1.h

    [/CODE]#pragma once


    // CPane1 dialog

    class CPane1 : public CDialogBar
    {
    DECLARE_DYNAMIC(CPane1)

    public:
    //CPane1(CWnd* pParent = NULL); // standard constructor
    CPane1();

    virtual ~CPane1();

    // Dialog Data
    enum { IDD = IDD_PANE_DIALOG};

    protected:
    virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
    afx_msg LONG OnInitDialog ( UINT, LONG ); // <-Add this line.

    DECLARE_MESSAGE_MAP()
    };


    Code : Pane1.cpp

    // Pane1.cpp : implementation file
    //

    #include "stdafx.h"
    #include "Sketcher.h"
    #include "Pane1.h"


    IMPLEMENT_DYNAMIC(CPane1, CDialog)


    CPane1::CPane1()
    {

    }

    CPane1::~CPane1()
    {
    }

    void CPane1:oDataExchange(CDataExchange* pDX)
    {
    CDialogBar:oDataExchange(pDX);
    }


    BEGIN_MESSAGE_MAP(CPane1, CDialogBar)

    ON_MESSAGE(WM_INITDIALOG, OnInitDialog )
    END_MESSAGE_MAP()


    // CPane1 message handlers

    LONG CPane1::OnInitDialog ( UINT wParam, LONG lParam)
    {
    //CDialog::OnInitDialog();

    // TODO: Add extra initialization here
    BOOL bRet = HandleInitDialog(wParam, lParam);

    if (!UpdateData(FALSE))
    {
    AfxMessageBox(_T("Warning: UpdateData failed during dialog init.\n"));
    }

    return bRet;

    //return TRUE; // return TRUE unless you set the focus to a control
    // EXCEPTION: OCX Property Pages should return FALSE
    }

    Finally, I create it in CMainFrame:

    CPane1 aPane_Dlg;

    if (!aPane_Dlg.Create(this, IDD_PANE_DIALOG, CBRS_LEFT,
    IDD_PANE_DIALOG))
    {
    TRACE0("Failed to create dialog bar\n");
    return -1; // fail to create
    }



    Thank you so much.
    Attached Files Attached Files

  12. #12
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

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

    Please, use correct Code tags!
    What error do you get and where?
    Victor Nijegorodov

  13. #13
    Join Date
    Sep 2013
    Posts
    5

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

    Dear All, (JazzDev, VictorN, ovidiucucu,....everyone)

    Could you help me solve my problem as a continuation of this topic. My problem as follows:
    1) I followed the link : How to Initialize Child Controls in a Derived CDialogBar
    To insert an object of CDialogBar in my app which is written in VC+2005.
    However I gets an error, I could not know why eventhough I have double checked the instruction.
    Details:
    I derived a "CPane1" class from CDialogBar

    Code: Pane1.h


    Code:
    #pragma once
    
    
    // CPane1 dialog
    
    class CPane1 : public CDialogBar
    {
    DECLARE_DYNAMIC(CPane1)
    
    public:
    //CPane1(CWnd* pParent = NULL); // standard constructor
    CPane1();
    
    virtual ~CPane1();
    
    // Dialog Data
    enum { IDD = IDD_PANE_DIALOG};
    
    protected:
    virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
    afx_msg LONG OnInitDialog ( UINT, LONG ); // <-Add this line.
    
    DECLARE_MESSAGE_MAP()
    };
    ]


    Code : Pane1.cpp

    [/
    Code:
    // Pane1.cpp : implementation file
    //
    
    #include "stdafx.h"
    #include "Sketcher.h"
    #include "Pane1.h"
    
    
    IMPLEMENT_DYNAMIC(CPane1, CDialog)
    
    
    CPane1::CPane1()
    {
    
    }
    
    CPane1::~CPane1()
    {
    }
    
    void CPane1:oDataExchange(CDataExchange* pDX)
    {
    CDialogBar:oDataExchange(pDX);
    }
    
    
    BEGIN_MESSAGE_MAP(CPane1, CDialogBar)
    
    ON_MESSAGE(WM_INITDIALOG, OnInitDialog )
    END_MESSAGE_MAP()
    
    
    // CPane1 message handlers
    
    LONG CPane1::OnInitDialog ( UINT wParam, LONG lParam)
    {
    //CDialog::OnInitDialog();
    
    // TODO: Add extra initialization here
    BOOL bRet = HandleInitDialog(wParam, lParam);
    
    if (!UpdateData(FALSE))
    {
    AfxMessageBox(_T("Warning: UpdateData failed during dialog init.\n"));
    }
    
    return bRet;
    
    //return TRUE; // return TRUE unless you set the focus to a control
    // EXCEPTION: OCX Property Pages should return FALSE
    }]
    Finally, I create it in CMainFrame:


    Code:
    CPane1 aPane_Dlg;
    
    if (!aPane_Dlg.Create(this, IDD_PANE_DIALOG, CBRS_LEFT,
    IDD_PANE_DIALOG))
    {
    TRACE0("Failed to create dialog bar\n");
    return -1; // fail to create
    }


    Thank you so much.

    Dear VictorN and so on...
    I've attached the errors.
    Please help me, Thank you so much.

    Name:  Bug2.jpg
Views: 1741
Size:  17.6 KBName:  Bug1.jpg
Views: 2282
Size:  12.4 KB

  14. #14
    Join Date
    Sep 2013
    Posts
    5

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

    Dear, VictorN

    The error I get in CMainFrame . It seems that the program could not create the object. However, I do not get the trapping message
    CPane1 aPane_Dlg;

    Code:
    if (!aPane_Dlg.Create(this, IDD_PANE_DIALOG, CBRS_LEFT,
    IDD_PANE_DIALOG))
    {
    TRACE0("Failed to create dialog bar\n");
    return -1; // fail to create
    }
    Thank you.

  15. #15
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

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

    Well, didn't you read the text of this messagebox?
    Or did you just ignore the "Press Retry to debug the application"?
    You have indded to press the Retry to step in the code th see where (line 881 in "barcore.cpp") and why the assertion failed!
    Victor Nijegorodov

Page 1 of 2 12 LastLast

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