📄 flatbar.shtml
字号:
// call "RepositionControls()" if you have added one or more controls
// and want to add more buttons/controls.
// This function will automatically be called for controls when calling
// CtrlReplace() and CtrlInsert().
void RepositionControls();
// Recalculate the size of the toolbar and recalculate the
// layout of its parent.
// There should be no need to call this method from outside
// the toolbar.
void RecalcLayout();</FONT></TT></PRE>
There is no must to add controls with either CtrlInsert() or CtrlReplace().
You can do it in a more traditional fashion too and the toolbar still works
(i.e. it takes care of all its children, regardless of the way of insertion).
So there is no need to change your existing code. You should call RepositionControls()
by yourself, if you add more controls in another way than the prefered
one.
<P>You might add a combo-box in the following way (assuming your toolbar
resource includes a button with the id IDC_COMBOBOX, that is to be replaced
by the control):
<PRE><TT><FONT COLOR="#990000">// ...
// replace a button by a CComboBox-control on the toolbar
DWORD dwComboStyle = WS_VSCROLL|CBS_AUTOHSCROLL|CBS_DROPDOWN|CBS_HASSTRINGS;
CComboBox * pBox = (CComboBox*) m_wndToolBar.CtrlReplace(
RUNTIME_CLASS(CComboBox), // insert control of type CComboBox
IDC_COMBOBOX, // id of the button that is to replace with the control
dwComboStyle // style bits
);
if( pBox ) {
pBox->AddString(TEXT("Line 1"));
pBox->AddString(TEXT("Line 2"));
pBox->AddString(TEXT("Line 3"));
}
// ...</FONT></TT></PRE>
As you can see, adding a control is an easy task.
<P>If you plan to use other types of controls (other than CComboBox or
CEdit), you have to derive a class from this type and to implement the
DECLARE_DYNCREATE macro and to overload the following methods:
<BR><FONT COLOR="#990000">virtual BOOL Create( LPCTSTR lpszClassName, LPCTSTR
lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd,
UINT nID, CCreateContext* pContext = NULL);</FONT>
<BR>and
<BR><FONT COLOR="#990000">virtual void PostNcDestroy();</FONT>
<P>Lets say you plan to insert a static text into the toolbar:
<BR>Create a new class using class-wizard to create a class derived from
CStatic.
<BR>Use class-wizard or class-view to overload the 2 virtual methods described
above.
<BR>The result should look like this (assuming you have named the class
"CText"):
<PRE><TT><FONT COLOR="#990000">/////////////////////////////////////////////////////////////////////////////
// CText window
class CText : public CStatic
{
DECLARE_DYNCREATE(CText); // <<== YOU HAVE TO INSERT THIS
// Construction
public:
CText();
// Attributes
public:
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CText)
public:
virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext = NULL);
protected:
virtual void PostNcDestroy();
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CText();
// Generated message map functions
protected:
//{{AFX_MSG(CText)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};</FONT></TT></PRE>
In the implementation file you have to fill out the 2 overloaded methods:
<PRE><TT><FONT COLOR="#990000">IMPLEMENT_DYNCREATE(CText, CStatic); // <<== DO NOT FORGET THIS
BOOL CText::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCrea
{
return CWnd::Create(TEXT("STATIC"), TEXT("text control"), dwStyle|SS_LEFT, rect, pParentWnd, nID);
}
void CText::PostNcDestroy()
{
delete this;
}</FONT></TT></PRE>
That's all you have to do.
<BR>The proceeding with other types of controls is similar.
<P>Now you can insert such a control like follows:
<PRE><TT><FONT COLOR="#990000">int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
// ...
m_wndToolBar.CtrlInsert(
RUNTIME_CLASS(CText), // the control's runtime class
CRect(-100, -22, 0, 0), // width is 100, height is 22
IDC_TEXT, // control's ID
2 // insert before button with index 2
);
// ...
}</FONT></TT></PRE>
<H4>
<A NAME="rev2"></A>Changes in Revision 2</H4>
Many thanks to <A HREF="mailto:W.Loch@intershop.de">Wolfgang Loch</A>,
<A HREF="mailto:jarmstrong@runge.com.au">John Armstrong</A> and <A HREF="mailto:aivasyuk@clark.net">Anatoly
Ivasyuk</A> who helped me to release that version.
<UL>
<LI>
buttons that are checked <B>and</B> disabled are now looking ok</LI>
<LI>
don't draw a gripper if the bar is not dockable</LI>
<LI>
do not adjust space for gripper in "classic" mode</LI>
<LI>
give the bar itself a real 3D look (as in Office or DevStudio) (see images
below)</LI>
<LI>
some minor code improvements</LI>
</UL>
<IMG SRC="OldStyleBorderLook.jpg" NOSAVE HEIGHT=180 WIDTH=472>
<BR>Old style 3D look of a toolbar.
<P><IMG SRC="NewStyleBorderLook.jpg" NOSAVE HEIGHT=186 WIDTH=472>
<BR>New style (as in Office or DevStudio).
<P>To enable the new 3D style you have to exchange the call to EnableDocking()
in your CMainFrame's OnCreate() method with a call to FrameEnableDocking():
<P><TT><FONT COLOR="#990000">// original code:</FONT></TT>
<BR><TT><FONT COLOR="#990000">int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)</FONT></TT>
<BR><TT><FONT COLOR="#990000"> // ...</FONT></TT>
<BR><TT><FONT COLOR="#990000"> EnableDocking(CBRS_ALIGN_ANY);</FONT></TT>
<BR><TT><FONT COLOR="#990000"> DockControlBar(&m_wndToolBar);</FONT></TT>
<BR><TT><FONT COLOR="#990000"> // ...</FONT></TT>
<BR><TT><FONT COLOR="#990000"> return 0;</FONT></TT>
<BR><TT><FONT COLOR="#990000">}</FONT></TT>
<P><TT><FONT COLOR="#990000">// changed code to get "real" 3D style:</FONT></TT>
<BR><TT><FONT COLOR="#990000">int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)</FONT></TT>
<BR><TT><FONT COLOR="#990000"> // ...</FONT></TT>
<BR><TT><FONT COLOR="#990000"> FrameEnableDocking(this,
CBRS_ALIGN_ANY);</FONT></TT>
<BR><TT><FONT COLOR="#990000"> DockControlBar(&m_wndToolBar);</FONT></TT>
<BR><TT><FONT COLOR="#990000"> // ...</FONT></TT>
<BR><TT><FONT COLOR="#990000"> return 0;</FONT></TT>
<BR><TT><FONT COLOR="#990000">}</FONT></TT>
<H4>
<A NAME="help"></A>Help !</H4>
Since my commercial projects have a higher priority than my hobby projects,
I don't have enough time to complete the persistence and customization
of the toolbar in a reasonable time. I'm looking for someone, who wants
to help me completing this. I have a somewhat more advanced version of
the class, that implements some aspects of these issues (send me mail to
get the source and see the images below). Both these problems got more
complex than I expected ...
<P><IMG SRC="ContextMenu.jpg" ALT="Context Menu" NOSAVE HEIGHT=146 WIDTH=212>
<BR>context menu of the toolbar
<P><IMG SRC="ToolBarPage-Resource.jpg" ALT="Toolbar Dialog Resource" NOSAVE HEIGHT=262 WIDTH=406>
<BR>"Toolbar" customization dialog in the resource
<P><IMG SRC="Customize-Dlg.jpg" ALT="Toolbar Dialog" NOSAVE HEIGHT=292 WIDTH=386>
<BR>"Toolbar" customization dialog in action
<P><A NAME="download"></A>CToolBarEx still consists of two files:
<BR>CToolBarEx.h and
<BR>CToolBarEx.cpp
<BR><A HREF="enh_flatbar_source.zip">Download enhanced source</A> 25K
<BR><A HREF="enh_flatbar_sample.zip">Download enhanced sample</A> 56K
<P>
<HR>
<TABLE BORDER=0 WIDTH="100%" >
<TR>
<TD WIDTH="33%"><FONT SIZE=-1><A HREF="http://www.codeguru.com">Goto HomePage</A></FONT></TD>
<TD WIDTH="33%">
<CENTER><FONT SIZE=-2>© 1997 Zafir Anjum</FONT> </CENTER>
</TD>
<TD WIDTH="34%">
<DIV ALIGN=right><FONT SIZE=-1>Contact me: <A HREF="mailto:zafir@dsp.net">zafir@dsp.com</A> </FONT></DIV>
</TD>
</TR>
</TABLE>
<CENTER><FONT SIZE=-2>404</FONT></CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -