15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


How to let views in a splitter window have their own titleba

Randy Taylor -- randy_taylor@ebt.com
Saturday, December 07, 1996

[Mini-digest: 3 responses]

CDocument::SetTitle
CWnd::SetWindowText
CView::OnActivateView

Eh?

This technique works reliably well in Visual C++:
 =20
Help Search Index titlebar. Begin reading.

randy_taylor@ebt.com


----------
> From: Chuck Yan 
> To: mfc-l@netcom.com
> Subject: How to let views in a splitter window have their own titlebars=
?
> Date: Wednesday, December 04, 1996 12:51 PM
>=20
>=20
> Environment: MSVC++ 4.0, Win NT 3.51
>=20
> Problem statement:
>=20
> A splitter window holds two form views created from dialog box template=
s.
> I'd like to let each view have its own title bar to indicate which view
is
> the active view and display some info on the title bar. However,
> even if I check the "Title Bar" box for the dialog box template style,=20
> the title bar still does not show up.  How to make this work?
>=20
> Please give me some hints on this. Thanks in advance.
>=20
> Chuck Yan
> The Ohio State University
>=20
-----From: Lin Sebastian Kayser 

Well Chuck I am afraid you have to do it all yourself since Windows does =
=3D
not handle the WS_CAPTION style for child windows and the views are =3D
child windows of the SplitterWnd. I read an article about it in the =3D
Windows Development Journal which dealt with the topic and actually used =
=3D
some code of it. However I had to modify some stuff to get the right =3D
behaviour. I give the implementation of my view class in the following =3D
it is part of a shipping application and has not caused any problems. I =3D
always use this view and when I don't need the captions I just set it to =
=3D
zero. BTW you MFC-Developers here on the list: wouldn't it be a good =3D
idea to add that pseudo-caption  to CView. It is always a little bit =3D
confusing when using splitters to determine which view actually has =3D
focus, so many people will end up writing code like this.

But here is the code:

All Stuff overridden with Class Wizard exept the following 4 methods:

.H-File

void SetCaptionHeight(UINT nHeight) 				{m_nCaptionHeight =3D3D nHeight;}
UINT nCaptionHeight() const					{return m_nCaptionHeight;}

void SetCaption(const CString& strCaption);
CString strCaption() const					{return m_strCaption;}



.CPP-File

void CABCCView::OnActivateView(BOOL bActivate, CView* pActivateView, =3D
CView* pDeactiveView)=3D20
{
	m_bActive =3D3D bActivate;
	if ((m_nCaptionHeight > 0) && (::IsWindow(m_hWnd)))
		SendMessage(WM_NCPAINT);

	CScrollView::OnActivateView(bActivate, pActivateView, pDeactiveView);
}

void CABCCView::OnActivateFrame(UINT nState, CFrameWnd* pFrameWnd)=3D20
{
	m_bActive =3D3D (nState !=3D3D WA_INACTIVE);

	if (nState =3D3D=3D3D WA_INACTIVE)
	{
		if ((m_nCaptionHeight > 0) && (::IsWindow(m_hWnd)))
		{
			SendMessage(WM_NCPAINT);
		}
	}

	CScrollView::OnActivateFrame(nState, pFrameWnd);
}


LRESULT CABCCView::OnNcCalcSize(WPARAM, LPARAM lParam)=3D20
{
	LRESULT lResult =3D3D Default();

	if (m_nCaptionHeight > 0)
	{
		NCCALCSIZE_PARAMS FAR* lpncsp =3D3D
			(NCCALCSIZE_PARAMS FAR*) lParam;

		lpncsp->rgrc[0].top +=3D3D m_nCaptionHeight + 1;
	}

	return lResult;
}

void CABCCView::OnNcPaint()
{
	Default();
	if (m_nCaptionHeight =3D3D=3D3D 0) return;

	CRect rectWindow;
	GetWindowRect(&rectWindow);

	CRect rect(0,0, rectWindow.right - rectWindow.left, m_nCaptionHeight + =3D
1);
	CDC* pDC =3D3D GetWindowDC();

	if (pDC !=3D3D NULL)
	{
		DoDrawCaption(pDC, rect);
		ReleaseDC(pDC);
	}
	else
	{
		XBREAK("Could not obtain a Window DC");
	}
}


UINT CABCCView::OnNcHitTest(CPoint /*point*/)
{
	LRESULT lResult =3D3D Default();
	if ((m_nCaptionHeight > 0) && (lResult =3D3D=3D3D HTNOWHERE))
		lResult =3D3D HTBORDER;

	return LOWORD(lResult);
}

void CABCCView::DoDrawCaption(CDC* pDC, CRect rect)
{
	CFrameWnd* poFrame =3D3D (CFrameWnd*) GetParentFrame();
	ASSERT_KINDOF(CFrameWnd, poFrame);


	COLORREF cfText =3D3D (m_bActive) ?
		GetSysColor(COLOR_CAPTIONTEXT) :
		GetSysColor(COLOR_INACTIVECAPTIONTEXT);

	COLORREF cfBack =3D3D (m_bActive) ?
		GetSysColor(COLOR_ACTIVECAPTION) :
		GetSysColor(COLOR_INACTIVECAPTION);

	CBrush brush(cfBack);

	CPen* poOldPen 		=3D3D (CPen*) pDC->SelectStockObject(NULL_PEN);
	CBrush* poOldBrush	=3D3D pDC->SelectObject(&brush);

	pDC->DrawEdge(&rect, EDGE_RAISED, BF_RECT);
	rect.DeflateRect(1,1);

	pDC->Rectangle(&rect);
=3D09
	pDC->SetBkMode(TRANSPARENT);
	pDC->SetTextColor(cfText);

	CRect rectText =3D3D rect;
	rectText.left +=3D3D ABCCVIEW_CAPTIONTEXT_BORDER;
	CFont oFont;

	if (oFont.CreateFont(	m_nCaptionHeight - 2,			// Height
					0,						// Width adapt
					0,						// Escapement
					0,						// Orientation
				(m_bActive) ? FW_BOLD : FW_THIN,	// Weight
					FALSE,						// Italics
					0,						// Underline
					0,						// Strikeout
					0,						// Ansi Char set
					OUT_DEFAULT_PRECIS,			// Precision
					CLIP_DEFAULT_PRECIS,			// Precision
					DEFAULT_QUALITY,				// Quality
					FF_SWISS,					// Family
					NULL))						// Name
	{
		CFont* poOldFont =3D3D pDC->SelectObject(&oFont);
		pDC->DrawText(m_strCaption, m_strCaption.GetLength(),
			&rectText, DT_SINGLELINE | DT_VCENTER);

		pDC->SelectObject(poOldFont);
	}

	pDC->SelectObject(poOldBrush);
	pDC->SelectObject(poOldPen);
}

void CABCCView::SetCaption(const CString& str)
{
	m_strCaption =3D3D str;

	if ((m_nCaptionHeight > 0) && (::IsWindow(m_hWnd)))
	{
		SendMessage(WM_NCPAINT);
	}
}


Best regards,
Lin

 ...........................................................
.        Lin Sebastian Kayser, Kayser & Nass, Munich        .
. Lin.Kayser@Munich.Netsurf.de / 100532.2621@CompuServe.com .
.    http://ourworld.compuserve.com/homepages/Lin_Kayser    .
.        Writing from Munich at 08.12.96 02:45 GMT+1:00     .
 ...........................................................


-----Original Message-----
From:	Chuck Yan [SMTP:yan@CMML2.eng.ohio-state.edu]
Sent:	Wednesday, December 04, 1996 5:52 PM
To:	mfc-l@netcom.com
Subject:	How to let views in a splitter window have their own titlebars?


Environment: MSVC++ 4.0, Win NT 3.51

Problem statement:

A splitter window holds two form views created from dialog box =3D
templates.
I'd like to let each view have its own title bar to indicate which view =3D
is
the active view and display some info on the title bar. However,
even if I check the "Title Bar" box for the dialog box template style,=3D=
20
the title bar still does not show up.  How to make this work?

Please give me some hints on this. Thanks in advance.

Chuck Yan
The Ohio State University
-----From: kevin@earth.pln.com.tw (Kevin Tarn)

To make your title bar showing, try this :

void CTitleView::OnInitialUpdate()=20
{
	CFormView::OnInitialUpdate();
=09
	// TODO: Add your specialized code here and/or call the base class
	DWORD dwStyle =3D ::GetWindowLong(m_hWnd, GWL_STYLE);

	dwStyle |=3D WS_CAPTION;

	::SetWindowLong(m_hWnd, GWL_STYLE, dwStyle);
}


Kevin Tarn
kevin@pln.com.tw

----------
=B1H=A5=F3=AA=CC:  Chuck Yan[SMTP:yan@CMML2.eng.ohio-state.edu]
=B6=C7=B0e=A4=E9=B4=C1:  1996=A6~12=A4=EB5=A4=E9 AM 1:52
=A6=AC=A5=F3=AA=CC:  mfc-l@netcom.com
=A5D=A6=AE:  How to let views in a splitter window have their own titleba=
rs?


Environment: MSVC++ 4.0, Win NT 3.51

Problem statement:

A splitter window holds two form views created from dialog box templates.
I'd like to let each view have its own title bar to indicate which view i=
s
the active view and display some info on the title bar. However,
even if I check the "Title Bar" box for the dialog box template style,=20
the title bar still does not show up.  How to make this work?

Please give me some hints on this. Thanks in advance.

Chuck Yan
The Ohio State University





Eric Marc Loebenberg -- loebenbe@qc.bell.ca
Tuesday, December 10, 1996

[Mini-digest: 3 responses]

I believe Chuck is asking a question that cannot be found in the VC++
help.  When you create a splitter with two views in Win32, the only title
bar that appears is the one on the frame window.  Even if you set the
titlebar style for the view window, a title bar does not appear.  In VC++
1.5 (16 bit) the second title bar appeared.  Chuck's question is a valid
one and I wait to see the response.

----------
From: Randy Taylor 
To: mfc-l@netcom.com
Subject: Re: How to let views in a splitter window have their own
titlebars?
Date: Saturday, December 07, 1996 2:49 PM

[Mini-digest: 3 responses]

CDocument::SetTitle
CWnd::SetWindowText
CView::OnActivateView

Eh?

This technique works reliably well in Visual C++:
 =20
Help Search Index titlebar. Begin reading.

randy_taylor@ebt.com


----------
> From: Chuck Yan 
> To: mfc-l@netcom.com
> Subject: How to let views in a splitter window have their own titlebars=
?
> Date: Wednesday, December 04, 1996 12:51 PM
>=20
>=20
> Environment: MSVC++ 4.0, Win NT 3.51
>=20
> Problem statement:
>=20
> A splitter window holds two form views created from dialog box
templates.
> I'd like to let each view have its own title bar to indicate which view
is
> the active view and display some info on the title bar. However,
> even if I check the "Title Bar" box for the dialog box template style,=20
> the title bar still does not show up.  How to make this work?
>=20
> Please give me some hints on this. Thanks in advance.
>=20
> Chuck Yan
> The Ohio State University
>=20
-----From: Lin Sebastian Kayser 

Well Chuck I am afraid you have to do it all yourself since Windows does =
=3D
not handle the WS_CAPTION style for child windows and the views are =3D
child windows of the SplitterWnd. I read an article about it in the =3D
Windows Development Journal which dealt with the topic and actually used =
=3D
some code of it. However I had to modify some stuff to get the right =3D
behaviour. I give the implementation of my view class in the following =3D
it is part of a shipping application and has not caused any problems. I =3D
always use this view and when I don't need the captions I just set it to =
=3D
zero. BTW you MFC-Developers here on the list: wouldn't it be a good =3D
idea to add that pseudo-caption  to CView. It is always a little bit =3D
confusing when using splitters to determine which view actually has =3D
focus, so many people will end up writing code like this.

But here is the code:

All Stuff overridden with Class Wizard exept the following 4 methods:

.H-File

void SetCaptionHeight(UINT nHeight) 				{m_nCaptionHeight =3D3D nHeight;}
UINT nCaptionHeight() const					{return m_nCaptionHeight;}

void SetCaption(const CString& strCaption);
CString strCaption() const					{return m_strCaption;}



.CPP-File

void CABCCView::OnActivateView(BOOL bActivate, CView* pActivateView, =3D
CView* pDeactiveView)=3D20
{
	m_bActive =3D3D bActivate;
	if ((m_nCaptionHeight > 0) && (::IsWindow(m_hWnd)))
		SendMessage(WM_NCPAINT);

	CScrollView::OnActivateView(bActivate, pActivateView, pDeactiveView);
}

void CABCCView::OnActivateFrame(UINT nState, CFrameWnd* pFrameWnd)=3D20
{
	m_bActive =3D3D (nState !=3D3D WA_INACTIVE);

	if (nState =3D3D=3D3D WA_INACTIVE)
	{
		if ((m_nCaptionHeight > 0) && (::IsWindow(m_hWnd)))
		{
			SendMessage(WM_NCPAINT);
		}
	}

	CScrollView::OnActivateFrame(nState, pFrameWnd);
}


LRESULT CABCCView::OnNcCalcSize(WPARAM, LPARAM lParam)=3D20
{
	LRESULT lResult =3D3D Default();

	if (m_nCaptionHeight > 0)
	{
		NCCALCSIZE_PARAMS FAR* lpncsp =3D3D
			(NCCALCSIZE_PARAMS FAR*) lParam;

		lpncsp->rgrc[0].top +=3D3D m_nCaptionHeight + 1;
	}

	return lResult;
}

void CABCCView::OnNcPaint()
{
	Default();
	if (m_nCaptionHeight =3D3D=3D3D 0) return;

	CRect rectWindow;
	GetWindowRect(&rectWindow);

	CRect rect(0,0, rectWindow.right - rectWindow.left, m_nCaptionHeight + =3D
1);
	CDC* pDC =3D3D GetWindowDC();

	if (pDC !=3D3D NULL)
	{
		DoDrawCaption(pDC, rect);
		ReleaseDC(pDC);
	}
	else
	{
		XBREAK("Could not obtain a Window DC");
	}
}


UINT CABCCView::OnNcHitTest(CPoint /*point*/)
{
	LRESULT lResult =3D3D Default();
	if ((m_nCaptionHeight > 0) && (lResult =3D3D=3D3D HTNOWHERE))
		lResult =3D3D HTBORDER;

	return LOWORD(lResult);
}

void CABCCView::DoDrawCaption(CDC* pDC, CRect rect)
{
	CFrameWnd* poFrame =3D3D (CFrameWnd*) GetParentFrame();
	ASSERT_KINDOF(CFrameWnd, poFrame);


	COLORREF cfText =3D3D (m_bActive) ?
		GetSysColor(COLOR_CAPTIONTEXT) :
		GetSysColor(COLOR_INACTIVECAPTIONTEXT);

	COLORREF cfBack =3D3D (m_bActive) ?
		GetSysColor(COLOR_ACTIVECAPTION) :
		GetSysColor(COLOR_INACTIVECAPTION);

	CBrush brush(cfBack);

	CPen* poOldPen 		=3D3D (CPen*) pDC->SelectStockObject(NULL_PEN);
	CBrush* poOldBrush	=3D3D pDC->SelectObject(&brush);

	pDC->DrawEdge(&rect, EDGE_RAISED, BF_RECT);
	rect.DeflateRect(1,1);

	pDC->Rectangle(&rect);
=3D09
	pDC->SetBkMode(TRANSPARENT);
	pDC->SetTextColor(cfText);

	CRect rectText =3D3D rect;
	rectText.left +=3D3D ABCCVIEW_CAPTIONTEXT_BORDER;
	CFont oFont;

	if (oFont.CreateFont(	m_nCaptionHeight - 2,			// Height
					0,						// Width adapt
					0,						// Escapement
					0,						// Orientation
				(m_bActive) ? FW_BOLD : FW_THIN,	// Weight
					FALSE,						// Italics
					0,						// Underline
					0,						// Strikeout
					0,						// Ansi Char set
					OUT_DEFAULT_PRECIS,			// Precision
					CLIP_DEFAULT_PRECIS,			// Precision
					DEFAULT_QUALITY,				// Quality
					FF_SWISS,					// Family
					NULL))						// Name
	{
		CFont* poOldFont =3D3D pDC->SelectObject(&oFont);
		pDC->DrawText(m_strCaption, m_strCaption.GetLength(),
			&rectText, DT_SINGLELINE | DT_VCENTER);

		pDC->SelectObject(poOldFont);
	}

	pDC->SelectObject(poOldBrush);
	pDC->SelectObject(poOldPen);
}

void CABCCView::SetCaption(const CString& str)
{
	m_strCaption =3D3D str;

	if ((m_nCaptionHeight > 0) && (::IsWindow(m_hWnd)))
	{
		SendMessage(WM_NCPAINT);
	}
}


Best regards,
Lin

 ...........................................................
.        Lin Sebastian Kayser, Kayser & Nass, Munich        .
. Lin.Kayser@Munich.Netsurf.de / 100532.2621@CompuServe.com .
.    http://ourworld.compuserve.com/homepages/Lin_Kayser    .
.        Writing from Munich at 08.12.96 02:45 GMT+1:00     .
 ...........................................................


-----Original Message-----
From:	Chuck Yan [SMTP:yan@CMML2.eng.ohio-state.edu]
Sent:	Wednesday, December 04, 1996 5:52 PM
To:	mfc-l@netcom.com
Subject:	How to let views in a splitter window have their own titlebars?


Environment: MSVC++ 4.0, Win NT 3.51

Problem statement:

A splitter window holds two form views created from dialog box =3D
templates.
I'd like to let each view have its own title bar to indicate which view =3D
is
the active view and display some info on the title bar. However,
even if I check the "Title Bar" box for the dialog box template style,=3D=
20
the title bar still does not show up.  How to make this work?

Please give me some hints on this. Thanks in advance.

Chuck Yan
The Ohio State University
-----From: kevin@earth.pln.com.tw (Kevin Tarn)

To make your title bar showing, try this :

void CTitleView::OnInitialUpdate()=20
{
	CFormView::OnInitialUpdate();
=09
	// TODO: Add your specialized code here and/or call the base class
	DWORD dwStyle =3D ::GetWindowLong(m_hWnd, GWL_STYLE);

	dwStyle |=3D WS_CAPTION;

	::SetWindowLong(m_hWnd, GWL_STYLE, dwStyle);
}


Kevin Tarn
kevin@pln.com.tw

----------
=B1H=A5=F3=AA=CC:  Chuck Yan[SMTP:yan@CMML2.eng.ohio-state.edu]
=B6=C7=B0e=A4=E9=B4=C1:  1996=A6~12=A4=EB5=A4=E9 AM 1:52
=A6=AC=A5=F3=AA=CC:  mfc-l@netcom.com
=A5D=A6=AE:  How to let views in a splitter window have their own titleba=
rs?


Environment: MSVC++ 4.0, Win NT 3.51

Problem statement:

A splitter window holds two form views created from dialog box templates.
I'd like to let each view have its own title bar to indicate which view i=
s
the active view and display some info on the title bar. However,
even if I check the "Title Bar" box for the dialog box template style,=20
the title bar still does not show up.  How to make this work?

Please give me some hints on this. Thanks in advance.

Chuck Yan
The Ohio State University

----------

-----From: Lin Sebastian Kayser 

Kevin,

that would be an easy way - however your suggested solution has at least two undesired side-effects:

1. You cannot activate your view by clicking on the caption - at least the caption is not shown in highlighted state.
2. You can actually *move* the view to a different location by dragging the caption!

Sorry to say but the only working way of having a caption on a view is to draw it yourself.

Best regards,
Lin
 ...........................................................
.        Lin Sebastian Kayser, Kayser & Nass, Munich        .
. Lin.Kayser@Munich.Netsurf.de / 100532.2621@CompuServe.com .
.    http://ourworld.compuserve.com/homepages/Lin_Kayser    .
.        Writing from Munich at 10.12.96 23:20 GMT+1:00     .
 ...........................................................



--------------Reply separator
-----From: kevin@earth.pln.com.tw (Kevin Tarn)

To make your title bar showing, try this :
void CTitleView::OnInitialUpdate() 
{
CFormView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class DWORD dwStyle = ::GetWindowLong(m_hWnd, GWL_STYLE);
dwStyle |= WS_CAPTION;
	::SetWindowLong(m_hWnd, GWL_STYLE, dwStyle);
}


-----From: bshamsian@ccvhs.vhsla.com

     Environment: MSVC++ 4.1, Win NT 4.0
     
     Chuck Mr. V. Ramachandran wrote an article in the July 1996 Windows 
     Developer Journal which showed exactly how to do this.  The articel is 
     called "Tiny Captions for MFC Splitter Views".  You can find the 
     article online at http://www.wdj.com/notgone/707art.htm.  This page 
     also points to the location af the source file and sample application 
     he wrote (ftp://ftp.mfi.com/pub/windev/1996/jul96.zip). 
     
     The files you need are zCapform.cpp and zCapform.h.  
     
     If you do not have access to web page here is the code:

File zCapform.cpp
================================================================================
/*
        File: ZCapForm.cpp
        Author: V.Ramachandran
        Date: 07 March, 1996

        ZCaptionFormView is a form view with a caption, which can be used
        anywhere, but most useful when used with a splitter window.
*/

#include "stdafx.h"
#include "zcapform.h"

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

IMPLEMENT_DYNAMIC (ZCaptionFormView, CFormView)

#define new DEBUG_NEW

#define LEFT_MARGIN 2

BEGIN_MESSAGE_MAP(ZCaptionFormView, CFormView)
        //{{AFX_MSG_MAP(ZCaptionFormView)
        ON_MESSAGE (WM_NCCALCSIZE, OnNcCalcSize)
        ON_WM_NCPAINT()
        ON_WM_NCHITTEST()
        //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/************************************************************************/
// OnActivateView - MFC virtual function called whenever the view loses
// or gains activation.
// Parameters: Read MFC documentation, not used in this implementation.
// Force repaint of caption, and then call base class.
/************************************************************************/

void ZCaptionFormView::OnActivateView (BOOL bActivate, CView *pActivateView,
                                        CView *pDeactivateView)
{
        m_bActive = bActivate;
        if ((m_nCaptionHeight > 0) && (::IsWindow (m_hWnd))) 
                SendMessage (WM_NCPAINT);
        CFormView::OnActivateView (bActivate, pActivateView, pDeactivateView);
}

/************************************************************************/
// OnActivateFrame - MFC virtual function called whenever the MDI child
// frame activation state changes.
// Parameter: nState indicates the state.
// If state is inactive, send a WM_NCPAINT after setting m_bActive.
// If state is active, don't do anything, because you will get a
// OnActivateView as well for the active view.
/************************************************************************/

void ZCaptionFormView::OnActivateFrame (UINT nState, CFrameWnd* pFrameWnd)
{
        m_bActive = (nState == WA_INACTIVE) ? FALSE : TRUE;
        if (nState == WA_INACTIVE) {
                if ((m_nCaptionHeight > 0) && (::IsWindow (m_hWnd))) 
                        SendMessage (WM_NCPAINT);
        }
        CFormView::OnActivateFrame(nState, pFrameWnd);
}

/************************************************************************/
// OnNcCalcSize - MFC handler for WM_NCCALCSIZE (read documentation on
// message).
// This message is used to calculate the client area of a window. Call
// Default first to get the actual client area, and then return an area
// minus the caption area.
/************************************************************************/

LRESULT ZCaptionFormView::OnNcCalcSize(WPARAM wParam, LPARAM lParam)
{
        LRESULT lResult = Default ();
        if (m_nCaptionHeight > 0) {
                NCCALCSIZE_PARAMS FAR* lpncsp = (NCCALCSIZE_PARAMS FAR *)lParam;
                lpncsp->rgrc [0].top += m_nCaptionHeight;
        }
        return lResult;
}

/************************************************************************/
// OnNcPaint - MFC handler for WM_NCPAINT
// No parameters
// Draw the caption in the non-client area. The color depends on whether is
// active or not. Some points to be noted in this function: Use of
// GetWindowDC and using a coordinate system wrt top-left of non-client 
// area rather than client coordinates. 
// Calls out to virtual function DoDrawCaption to do the actual drawing.
/************************************************************************/

void ZCaptionFormView::OnNcPaint() 
{
        Default ();                     // Draw other parts as normal.
        if (m_nCaptionHeight <= 0)              // Optimize if Caption is off
                return;
        
// Get the rectangle to paint. We use GetWindowRect and then shift to 
// window based (non-client) coordinates. Effectively, we are only using
// the width of the window, other values are set explicitly.

        RECT rect, windowRect;
        GetWindowRect (&windowRect); 
        SetRect (&rect, 0, 0, windowRect.right - windowRect.left,
                                             m_nCaptionHeight + 1);

// Get the window dc. Remember we are painting the non client area.
        CDC *pDC = GetWindowDC ();
        if (pDC) {
             DoDrawCaption (pDC, rect);              // Call out to virtual fn.
             ReleaseDC (pDC);
        }
        else {                  // Could not get DC!
                TRACE0 ("ZCaptionFormView::OnNcPaint - GetWindowDC failed!.\n");
                ASSERT (FALSE);
        }
}

/************************************************************************/
// OnNcHitTest - MFC handler for WM_NCHITTEST
// Parameters: mouse at screen coordinates (read message documentation).
// Call Default first. If mouse is HTNOWHERE, return HTCLIENT. If this
// is not done, clicking on the pseudo-caption will not activate this
// view.
/************************************************************************/

UINT ZCaptionFormView::OnNcHitTest (CPoint point)
{
        LRESULT lResult = Default ();
        // Optimize if Caption is off
        if ((m_nCaptionHeight > 0) && (lResult == HTNOWHERE))
                lResult = HTCLIENT;
        return LOWORD (lResult);
}

/************************************************************************/
// DoDrawCaption - virtual function override to draw caption
// Parameters: Pointer to DC to draw on, rectangle to indicate area. 
// Point is wrt to 0,0 of window (not client).  
// This version draws the caption in the active caption color, and 
// prints out a caption as well. 
// Override this function to extend draw capabilities.
// m_bActive tells you whether to draw in active colors, or inactive
// colors.
/************************************************************************/

void ZCaptionFormView::DoDrawCaption (CDC *pDC, const RECT& rect)
{
        CFrameWnd *pFrame = (CFrameWnd *)GetParentFrame ();
        ASSERT (pFrame->IsKindOf (RUNTIME_CLASS (CFrameWnd)));

// Decide color on whether the view is currently active.        
        COLORREF color = (m_bActive) ?
                        GetSysColor (COLOR_ACTIVECAPTION) : 
                        GetSysColor (COLOR_INACTIVECAPTION);

        CPen blackPen (PS_SOLID, 1, RGB (0, 0, 0));
        CBrush blueBrush (color);

// Select pen and brush and draw a rectangle.

        CPen *pOldPen = pDC->SelectObject (&blackPen);
        CBrush *pOldBrush = pDC->SelectObject (&blueBrush);
        pDC->Rectangle (&rect);

// Now, write the caption.

        pDC->SetTextColor ((m_bActive) ?
                           GetSysColor (COLOR_CAPTIONTEXT) : 
                           GetSysColor (COLOR_INACTIVECAPTIONTEXT));
        pDC->SetBkMode (TRANSPARENT);
        
        RECT rectText = rect;
        rectText.left += LEFT_MARGIN;    // Leave some margin, looks better.

        CFont *pFont = GetFont ();              
        if (pFont)
                pDC->SelectObject (pFont);
        pDC->DrawText (m_sCaption, m_sCaption.GetLength (), &rectText, 
                       DT_SINGLELINE | DT_VCENTER);
        pDC->SelectObject (pOldBrush);
        pDC->SelectObject (pOldPen);
}

// End.
================================================================================

File zCapForm.h
================================================================================
/*
        File: ZCapForm.h
        Author: V.Ramachandran
        Date: 07 March, 1996

        ZCaptionFormView is a form view with a caption, which can be used
        anywhere, but most useful when used with a splitter window.
*/

#ifndef __ZCaptionFormView__
#define __ZCaptionFormView__

#define DEFAULT_CAPTION_HEIGHT 15               // 15 arbitrary!

class ZCaptionFormView : public CFormView
{
public:
        ZCaptionFormView (UINT nID, int nHeight = DEFAULT_CAPTION_HEIGHT) 
                                : CFormView (nID), m_nCaptionHeight (nHeight), 
                                  m_bActive (FALSE)
                                { ASSERT (m_nCaptionHeight >= 0); }
        ZCaptionFormView (LPCSTR lpszTemplateName, 
                                int nHeight = DEFAULT_CAPTION_HEIGHT) 
                                : CFormView (lpszTemplateName), 
                                  m_nCaptionHeight (nHeight), m_bActive (FALSE)
                                { ASSERT (m_nCaptionHeight >= 0); }

public:
        int GetCaptionHeight () const { return m_nCaptionHeight; }
        void SetCaptionHeight (int nHeight) 
                                { 
                                        ASSERT (m_nCaptionHeight >= 0);
                                        m_nCaptionHeight = nHeight; 
                                }
        void GetCaption (CString& sCaption) { sCaption = m_sCaption; }
        void SetCaption (LPCSTR szCaption) { m_sCaption = szCaption; }

protected:
        virtual void OnActivateView (BOOL bActivate, CView *pActivateView,
                                        CView *pDeactivateView);
        virtual void OnActivateFrame(UINT nState, CFrameWnd* pFrameWnd);
        virtual void DoDrawCaption (CDC *pDC, const RECT& rect);

        // Generated message map functions
        //{{AFX_MSG(CInfoView)
        afx_msg LRESULT OnNcCalcSize(WPARAM wParam, LPARAM lParam);
        afx_msg void OnNcPaint();
        afx_msg UINT OnNcHitTest (CPoint point);
        //}}AFX_MSG
        DECLARE_MESSAGE_MAP()
        DECLARE_DYNAMIC (ZCaptionFormView)

protected:
        int m_nCaptionHeight;
        CString m_sCaption;
        BOOL m_bActive;         // TRUE if active, FALSE otherwise
};

#endif // __ZCaptionFormView__

================================================================================

Hope This Helps.....

______________________________ Reply Separator _________________________________
Subject: How to let views in a splitter window have their own title  
Author:  mfc-l@netcom.com at vhsunix
Date:    12/7/96 9:30 PM


     
Environment: MSVC++ 4.0, Win NT 3.51
     
Problem statement:
     
A splitter window holds two form views created from dialog box templates. 
I'd like to let each view have its own title bar to indicate which view is 
the active view and display some info on the title bar. However,
even if I check the "Title Bar" box for the dialog box template style, 
the title bar still does not show up.  How to make this work?
     
Please give me some hints on this. Thanks in advance.
     
Chuck Yan
The Ohio State University
     
     





| Вернуться в корень Архива |