📄 graded_caption.shtml.htm
字号:
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Author" CONTENT="Zafir Anjum">
<TITLE>Miscellaneous - Gradient color caption bar</TITLE>
</HEAD>
<body background="../fancyhome/back.gif" tppabs="http://www.codeguru.com/fancyhome/back.gif" bgcolor="#FFFFFF" link="#B50029" vlink="#8E2323" alink="#FF0000" bgproperties="fixed">
<table WIDTH="100%">
<tr WIDTH="100%">
<td><td>
</tr>
</table>
<CENTER>
<H3>
<FONT COLOR="#AOAO99">Gradient color caption bar</FONT></H3></CENTER>
<HR>
This article was contributed by <A HREF="mailto:corkum@rocscience.com">Brent Corkum</A>.
You can also visit Brent's site at <A HREF="http://www.rocscience.com/%7ecorkum/caption3.html">http://www.rocscience.com/~corkum/caption3.html</A>
for the latest update of this article.
<P><IMG SRC="graded_caption.gif" tppabs="http://www.codeguru.com/misc/graded_caption.gif"></P>
<P>This class overides the default caption painting in MFC and replaces it with a graded caption bar. The source code for the class along with a sample application can be found at <A HREF="ftp://ftp.rocscience.com/caption3.zip" tppabs="ftp://ftp.rocscience.com/caption3.zip">ftp://ftp.rocscience.com/caption3.zip</A> . The code originates from the MSJ article in January 1997 and then again in June 1997. I've made a few modifications, cleaned it up and have simplified the installation process. Some of the new features include support for the modified state and the synchronization of MainFrame and ChildFrame caption text. I also explain how to easily update ChildFrame caption text through the view class and add modified state support to the ChildFrame captions.
<P> </P>
<FONT ><P>Steps for adding color graded caption bars to your MFC MDI application:</P>
<P> </P>
<OL>
<LI>Add the PaintCap.c , PaintCap.h , SubClass.cpp, and SubClass.h files to your project.</LI>
<LI>Add the following lines to the protected section of the CMainFrame class definition (MainFrm.h)</LI>
</FONT><FONT COLOR="#990000"><PRE>	CCaptionPainter m_capp;
	virtual void OnUpdateFrameTitle(BOOL bAddToTitle);
	LRESULT OnPaintMyCaption(WPARAM wp,LPARAM lp);</PRE>
</FONT><FONT ><LI>Add the following include to the top of the MainFrm.h file</LI>
</FONT><FONT COLOR="#990000"><PRE>#include "PaintCap.h"</PRE>
</FONT><FONT ><LI>Add the following to the CMainFrame::OnCreate member function just after the toolbar stuff (MainFrm.cpp):</LI>
</FONT><FONT COLOR="#990000"><PRE>m_capp.Install(this,WM_PAINTMYCAPTION);</PRE>
</FONT><FONT ><LI>Add the following to the top of the MainFrm.cpp file:</LI>
</FONT><FONT COLOR="#990000"><PRE>const UINT WM_PAINTMYCAPTION = WM_USER+5;</PRE>
</FONT><FONT ><LI>Place the following message map definition to the CMainFrame class (MainFrm.cpp). Make sure it's after the grayed out AFX_MSG_MAP block;</LI>
</FONT><FONT COLOR="#990000"><PRE>ON_MESSAGE(WM_PAINTMYCAPTION,OnPaintMyCaption)</PRE>
</FONT><FONT ><LI>Add the following two member functions to the CMainFrame class:</LI>
</FONT><FONT COLOR="#990000"><PRE>void CMainFrame::OnUpdateFrameTitle(BOOL bAddToTitle)
{
	m_capp.UpdateFrameTitle(m_hWnd,m_strTitle);
}
LRESULT CMainFrame::OnPaintMyCaption(WPARAM bActive, LPARAM lParam)
{
m_capp.PaintMyCaption(bActive,lParam,m_strTitle);
	return 0;
}</PRE>
</FONT><FONT ><P> </P>
<P>Programs like VC++ modify the document name in the caption bar if the document has been modified (using SetModifiedFlag()) by appending an asterix (*) to the document name. To add this functionality requires a few modifications:</P>
<P> </P>
<LI>In the CMainFrame::OnCreate member function</FONT><FONT > </FONT><FONT >change the m_capp.Install call added in step 2 above to:</LI>
</FONT><FONT COLOR="#990000"><PRE>m_capp.Install(this,WM_PAINTMYCAPTION,TRUE);</PRE>
</FONT><FONT ><LI>Add a public virtual function override for the SetModifiedFlag do your CDocument derived class:</LI>
</FONT><FONT COLOR="#990000"><P>virtual void SetModifiedFlag(BOOL);</P>
</FONT><FONT ><LI>Add the member function definition to your CDocument derived cpp file:</LI>
</FONT><FONT COLOR="#990000"><PRE>void CYourDoc::SetModifiedFlag(BOOL flag)
{
CDocument::SetModifiedFlag(flag);
((CMainFrame *)AfxGetMainWnd())->RedrawCaption();
}</PRE>
</FONT><FONT ><P>IMPORTANT: Make sure you change the above CYourDoc class name to the name of your CDocument derived class name.</P>
<LI>Add the MainFrm.h include to the top of your CDocument derived cpp file:</LI>
</FONT><FONT FACE="Courier New" COLOR="#990000"><P>#include "MainFrm.h"</P>
</FONT><FONT ><LI>Add a public member function called RedrawCaption to your CMainFrame class in MainFrm.h:</LI>
</FONT><FONT COLOR="#990000"><PRE>void RedrawCaption(void);</PRE>
</FONT><FONT ><LI>Add the member function definition to the MainFrm.cpp file:</LI>
</FONT><FONT COLOR="#990000"><PRE>void CMainFrame::RedrawCaption(void)
{
m_capp.Invalidate();
m_capp.PaintCaption();
}</PRE>
</FONT><FONT ><P> </P>
<P>Programs like VC++ also modify the view name in the caption bar of all the views associated with the modified document by appending an asterix (*) to the view name. To add this functionality requires a few modifications:</P>
<P> </P>
<LI>Let's first add an update handler for the caption text so that we can modify it and save it with the ChildFrame. Add the following public virtual function definition to the CChildFrame class in the CChildFrm.h file:</LI>
</FONT><FONT FACE="Courier New" COLOR="#990000"><P>virtual void OnUpdateFrameTitle(BOOL bAddToTitle);</P>
</FONT><FONT ><LI>Add the following OnUpdateFrameTitle virtual member function to your CChildFrm.cpp file:</LI>
</FONT><FONT COLOR="#990000"><PRE>void CChildFrame::OnUpdateFrameTitle(BOOL bAddToTitle)
{
	CDocument* pDoc = GetActiveDocument();
	if (bAddToTitle && pDoc != NULL)
	{
		CString strCurCaption, strWindowText, strNewCaption;
		// Get the current child window caption text
		GetWindowText(strCurCaption);
		// Get the special view name through the view's window text
		GetActiveView()->GetWindowText(strWindowText);
		// Get the doc name attached to this window
		const CString& strDocTitle = pDoc->GetTitle();
		// generate our new window caption
		strNewCaption = strDocTitle;
		if(m_nWindow > 0){
			strNewCaption += ":";
			CString temp;
			temp.Format("%d",m_nWindow);
			strNewCaption += temp;
		}
		if(!strWindowText.IsEmpty()){
		 	strNewCaption += " - ";
			strNewCaption += strWindowText;
		}
		if(pDoc->IsModified())strNewCaption += "*";
		// Only switch to the new caption if it differs from the old
		// (this reduces flicker--MFC uses AfxSetCaption)
		if (strNewCaption != strCurCaption)
			SetWindowText(strNewCaption);
	}
	// give the MDI frame window a chance to update its title
	GetMDIFrame()->OnUpdateFrameTitle(bAddToTitle);
}</PRE>
</FONT><FONT ><P>*Note that a call to SetWindowText from within the view class will now update the caption on the views frame. Refer to the OnSettext() View member function in the sample program for more info. If you wish to change how the caption text is displayed in the child frames edit the above function.</P>
<LI>Modify the SetModifiedFlag function you added in step 10 above to:</LI>
</FONT><FONT COLOR="#990000"><PRE>void CCaptionDoc::SetModifiedFlag(BOOL flag)
{
CDocument::SetModifiedFlag(flag);
((CMainFrame *)AfxGetMainWnd())->RedrawCaption();
POSITION pos = GetFirstViewPosition();
CView *pView;
CChildFrame *pChild;
while(pos!=NULL){
pView = GetNextView(pos);
if(pView){
pChild = (CChildFrame *)pView->GetParent();
if(pChild)pChild->OnUpdateFrameTitle(TRUE);
}
}
}</PRE>
</FONT><LI>Add the include file ChildFrm.h to the top of the file containing the SetModifiedFlag code in step 16:</LI></OL>
<DIR>
<DIR>
<FONT COLOR="#990000"><PRE>#include "ChildFrm.h"</PRE>
</FONT><P> </P>
<P> </P></DIR>
</DIR>
<P><A HREF="graded_caption.zip" tppabs="http://www.codeguru.com/misc/graded_caption.zip">Download project</A> 52KB
<P>
<HR>
<TABLE BORDER=0 WIDTH="100%" >
<TR>
<TD WIDTH="33%"><FONT SIZE=-1><A HREF="../index.htm" tppabs="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@home.com">zafir@home.com</A> </FONT></DIV>
</TD>
</TR>
</TABLE>
<CENTER><FONT SIZE=-2>6686</FONT></CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -