|
|||||||||||||||||||||
|
|||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Visual Studio 2005 UpdatePlease note that I have added a downloadable demo project for Visual Studio 2005 users. Everything in the original article applies to Visual Studio 2005 usage as well, except for the bit about using ClassWizard to create your class/object associations. I'm assuming that if you are using Visual Studio 2005, you already know how to define associations, add event handlers, etc. So, just ignore the ClassWizard comments. The important thing is that if you derive your own class, you must make certain that the resource ID of your dialog bar matches the resource ID defined in the class header. If you use the derived classes that I have provided, then you simply need to name your dialog bar resource in the resource editor IntroductionI've seen a lot of posts lately in forums and article comments from readers wanting to know how to use owner-drawn or custom controls in a dialog bar. Even though this information is available in various articles from Microsoft, I decided to post this article as a sort of tutorial bringing all the information together into one article.... and of course, providing working examples to look at. In order to achieve this, you have to base your dialog bar on a class derived from HOWTO: How to Initialize Child Controls in a Derived CDialogBar. There is also a companion article: INFO: Using CBitmapButton in CDialogBar and CFormView. The derived classes in this example were created following the instructions from this article, primarily. Using the Sample ClassesTo use the example classes in your app, simply follow these steps. I'm assuming that you are using Visual Studio, in my case, 6. First, from the Visual Studio main menu, click INSERT/RESOURCE and then expand DIALOG. Click At this point, you will want to link your dialog bar to your derived class. Go to your project folder and delete (or rename) the *.clw file. Then select your dialog bar in the resource editor and press CTRL_W. ClassWizard will prompt to rebuild the class wizard database. Make sure the MyDlgBar.h and MyDlgBar.cpp files are in the list and let it rebuild. It will then prompt you to add a new class or select an existing class for your dialog bar resource. Choose SELECT EXISTING CLASS and then select the . . . . . . . . . . . .
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "MyDlgBar.h" <--- ADD THIS LINE
class CMainFrame : public CFrameWnd
{
. . . . . . . . . . . . . . .
Then, inside the class declaration of your MainFrm.h file, create an instance of the dialog bar like this: . . . . . . . . . . . . . . .
// Implementation
public:
virtual ~CMainFrame();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected: // control bar embedded members
CMyDlgBar m_myDlgBar; <----- ADD THIS LINE
CStatusBar m_wndStatusBar;
CToolBar m_wndToolBar;
. . . . . . . . . . . . . . .
Finally, to the if (!m_myDlgBar.Create(this, IDD_DIALOGBAR,
CBRS_TOP | CBRS_GRIPPER |CBRS_TOOLTIPS |
CBRS_FLYBY | CBRS_HIDE_INPLACE,
IDD_VIEW_DIALOGBAR))
{
TRACE0("Failed to create dialog bar m_wndDlgBar\n");
return -1; // fail to create
}
m_myDlgBar.EnableDocking(CBRS_ALIGN_TOP | CBRS_ALIGN_BOTTOM);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_myDlgBar);
You can modify this if you like. This creates a standard dockable dialog bar with a gripper, so that it can be undocked or moved. By default, it is created docked at the top of the app. Note BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
// AND THE LINE BELOW
ON_COMMAND_EX(IDD_VIEW_DIALOGBAR, OnBarCheck)
// AND THE LINE BELOW
ON_UPDATE_COMMAND_UI(IDD_VIEW_DIALOGBAR, OnUpdateControlBarMenu)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
Now you can compile and run. You will have a working dialog bar, except that you have nothing on it. So let's add an owner-drawn Adding an Owner-Drawn CBitmapButtonJust add a button to the dialog bar as normal. In Properties, check caption Owner-drawn. Give it an ID of Now in the ClassView tab, right-click on if(bReturn)
m_bmButton.AutoLoad(IDC_BUTTON_BITMAP, this);
Again, if you gave your button a different ID, you will need to change the ID here. That's it... now you have an owner-drawn Adding the Message HandlerJust like any dialog bar, the messages must be handled in either the MainFrame, Document or View class. We will handle this one in the View class. You must add your message handlers manually. In your View class header file, add a function declaration in the message map section, like this: // Generated message map functions
protected:
//{{AFX_MSG(CDerivedDBView)
afx_msg void OnButtonBitmap(); <---- ADD THIS LINE
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
Then in the message map of your View class *.cpp file, edit the code as follows: BEGIN_MESSAGE_MAP(CDerivedDBView, CFormView)
//{{AFX_MSG_MAP(CDerivedDBView)
//ADD THE LINE BELOW
ON_BN_CLICKED(IDC_BUTTON_BITMAP, OnButtonBitmap)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
Now you add your function implementation to the bottom of your View class *.cpp file: void CDerivedDBView::OnButtonBitmap()
{
CString msg = "You Clicked a CBitmapButton on a Dialog Bar \n";
msg += "in a CDialogBar derived class! ";
AfxMessageBox(msg);
}
One last thing: at the top of your View class *.cpp file, add the line: #include "MyDlgBar.h"
That's all there is to it. Compile, run and voila!, you have a working Example: The Subclassed Button ControlThe demo project includes a subclassed button control in the dialog bar. Chris Maunder has an excellent article on subclassing controls. You can see his article for instructions on deriving your class. To subclass a button in the dialog bar, use his instructions for subclassing existing items. Look at the MyDlgBar.h and MyDlgBar.cpp files in the sample project to see how this is implemented. To use the sample class files, just add NewButton.h and NewButton.cpp to your project. Now drag a button onto your dialog bar and give it an ID of m_subButton.SubclassDlgItem(IDC_DBSUB_BUTTON, this);
Since you have more than one instruction, be sure to place brackets around your instructions under the History
| ||||||||||||||||||||