[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.
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.
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...
________________________
Originally Posted by ovidiucucu
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... ¬¬''
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.
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..
Re: [RESOLVED] Creating DialogBar with my own controls (MFC)
Originally Posted by JazzDev
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?
Re: [RESOLVED] Creating DialogBar with my own controls (MFC)
Originally Posted by JazzDev
Originally Posted by ovidiucucu
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.
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.
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.
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!
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.