Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

CKCSideBannerWnd: An MFC Banner Control that Can Add a Professional-looking Feel to Most Windows...

, 6 Nov 2003
A CWnd-derived control that can attach itself to any window, without the programmer making provisions for it
Prize winner in Competition "MFC/C++ Sep 2003"
kcsidebannerwnd_demo.zip
SideBanner
Release
SideBanner.exe
res
ico00001.ico
icon1.ico
texture.bmp
SideBanner.clw
SideBanner.dsp
SideBanner.dsw
// SideBannerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SideBanner.h"
#include "SideBannerDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:

	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSideBannerDlg dialog

CSideBannerDlg::CSideBannerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSideBannerDlg::IDD, pParent)
	, m_uFlags(0)
	, m_hBrush(NULL)
{
	//{{AFX_DATA_INIT(CSideBannerDlg)
	m_bFillType = 1;
	m_strCaption = _T("");
	m_strTitle = _T("");
	m_nCapOffsetX = 0;
	m_nEdgeOffsetX = 0;
	m_nCapOffsetY = 0;
	m_nEdgeOffsetY = 0;
	m_nBannerSize = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

CSideBannerDlg::~CSideBannerDlg()
{
	if ( m_fontCaption.m_hObject )
		m_fontCaption.DeleteObject();
	if ( m_fontTitle.m_hObject )
		m_fontTitle.DeleteObject();
}

void CSideBannerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSideBannerDlg)
	DDX_Control(pDX, IDC_COMBO_VERT, m_ctlVert);
	DDX_Control(pDX, IDC_COMBO_HORZ, m_ctlHorz);
	DDX_Radio(pDX, IDC_RADIO_FLAT, m_bFillType);
	DDX_Text(pDX, IDC_EDIT_CAPTION, m_strCaption);
	DDX_Text(pDX, IDC_EDIT_TITLE, m_strTitle);
	DDX_Text(pDX, IDC_EDIT_CAPOFFSET, m_nCapOffsetX);
	DDX_Text(pDX, IDC_EDIT_EDGEOFFSET, m_nEdgeOffsetX);
	DDX_Text(pDX, IDC_EDIT_CAPOFFSET2, m_nCapOffsetY);
	DDX_Text(pDX, IDC_EDIT_EDGEOFFSET2, m_nEdgeOffsetY);
	DDX_Text(pDX, IDC_EDIT_BANNERSIZE, m_nBannerSize);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSideBannerDlg, CDialog)
	//{{AFX_MSG_MAP(CSideBannerDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_TOP, OnButtonTop)
	ON_BN_CLICKED(IDC_BUTTON_LEFT, OnButtonLeft)
	ON_BN_CLICKED(IDC_BUTTON_BOTTOM, OnButtonBottom)
	ON_BN_CLICKED(IDC_BUTTON_RIGHT, OnButtonRight)
	ON_EN_CHANGE(IDC_EDIT_CAPTION, OnChangeEditCaption)
	ON_EN_CHANGE(IDC_EDIT_TITLE, OnChangeEditTitle)
	ON_BN_CLICKED(IDC_RADIO_FLAT, OnRadioFlat)
	ON_BN_CLICKED(IDC_RADIO_GRADIENT, OnRadioGradient)
	ON_EN_CHANGE(IDC_EDIT_CAPOFFSET, OnChangeEditCapoffset)
	ON_EN_CHANGE(IDC_EDIT_EDGEOFFSET, OnChangeEditEdgeoffset)
	ON_EN_CHANGE(IDC_EDIT_BANNERSIZE, OnChangeEditBannersize)
	ON_WM_CTLCOLOR()
	ON_BN_CLICKED(IDC_STATIC_COL1, OnStaticCol1)
	ON_BN_CLICKED(IDC_STATIC_COL2, OnStaticCol2)
	ON_WM_SIZE()
	ON_CBN_SELCHANGE(IDC_COMBO_HORZ, OnSelchangeAlignment)
	ON_EN_CHANGE(IDC_EDIT_EDGEOFFSET2, OnChangeEditEdgeoffset)
	ON_EN_CHANGE(IDC_EDIT_CAPOFFSET2, OnChangeEditCapoffset)
	ON_CBN_SELCHANGE(IDC_COMBO_VERT, OnSelchangeAlignment)
	ON_BN_CLICKED(IDC_RADIO_TEXTURE, OnRadioTexture)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSideBannerDlg message handlers

BOOL CSideBannerDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	m_banner.Attach(this, KCSB_ATTACH_LEFT);

	m_banner.SetColBkg( GetSysColor( COLOR_3DFACE ) );
	m_banner.SetColBkg2( RGB(101,141,190) );
	m_banner.SetColEdge( RGB(0,0,0) );
	m_banner.SetIcon( AfxGetApp()->LoadIcon(IDI_KCLOGO), KCSB_ICON_LEFT | KCSB_ICON_VCENTER );
	m_ctlHorz.SelectString(-1, _T("Left"));
	m_ctlVert.SelectString(-1, _T("Center"));

	
	//
	// Example code to set a texture
	CBitmap				bmp;

	bmp.LoadBitmap(IDB_TEXTURE);
	m_banner.SetTexture((HBITMAP)bmp.Detach());

/*
	// 
	// Example code to set fonts for the control
	m_fontTitle.CreatePointFont(120, "Comic Sans MS");
	m_banner.SetTitleFont(&m_fontTitle);
	m_fontCaption.CreatePointFont(95, "Courier New");
	m_banner.SetCaptionFont(&m_fontCaption);
*/

	m_strTitle = m_banner.GetTitle();
	m_strCaption = m_banner.GetCaption();
	m_nEdgeOffsetX = m_banner.GetEdgeOffset().cx;
	m_nCapOffsetX = m_banner.GetCaptionOffset().cx;
	m_nEdgeOffsetY = m_banner.GetEdgeOffset().cy;
	m_nCapOffsetY = m_banner.GetCaptionOffset().cy;
	m_nBannerSize = m_banner.GetSize();

	UpdateData(FALSE);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

/////////////////////////////////////////////////////////////////////////////

void CSideBannerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CSideBannerDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CSideBannerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

/////////////////////////////////////////////////////////////////////////////

void CSideBannerDlg::OnButtonTop() 
{
	unsigned int			uFlag = m_banner.GetPosFlag();

	if ( (uFlag & KCSB_ATTACH_LEFT) || (uFlag & KCSB_ATTACH_RIGHT) )
		SwapOffsetValues();
	m_banner.SetPosFlag( KCSB_ATTACH_TOP | m_uFlags);	
}

/////////////////////////////////////////////////////////////////////////////

void CSideBannerDlg::OnButtonLeft() 
{
	unsigned int			uFlag = m_banner.GetPosFlag();

	if ( (uFlag & KCSB_ATTACH_TOP) || (uFlag & KCSB_ATTACH_BOTTOM) )
		SwapOffsetValues();
	m_banner.SetPosFlag( KCSB_ATTACH_LEFT | m_uFlags);	
}

/////////////////////////////////////////////////////////////////////////////

void CSideBannerDlg::OnButtonBottom() 
{
	unsigned int			uFlag = m_banner.GetPosFlag();

	if ( (uFlag & KCSB_ATTACH_LEFT) || (uFlag & KCSB_ATTACH_RIGHT) )
		SwapOffsetValues();
	m_banner.SetPosFlag( KCSB_ATTACH_BOTTOM | m_uFlags);	
}

/////////////////////////////////////////////////////////////////////////////

void CSideBannerDlg::OnButtonRight() 
{
	unsigned int			uFlag = m_banner.GetPosFlag();

	if ( (uFlag & KCSB_ATTACH_TOP) || (uFlag & KCSB_ATTACH_BOTTOM) )
		SwapOffsetValues();
	m_banner.SetPosFlag( KCSB_ATTACH_RIGHT | m_uFlags);	
}

/////////////////////////////////////////////////////////////////////////////

void CSideBannerDlg::OnChangeEditCaption() 
{
	CString			str(_T(""));
	
	GetDlgItem(IDC_EDIT_CAPTION)->GetWindowText(str);
	m_banner.SetCaption(str);
}

/////////////////////////////////////////////////////////////////////////////

void CSideBannerDlg::OnChangeEditTitle() 
{
	CString			str(_T(""));
	
	GetDlgItem(IDC_EDIT_TITLE)->GetWindowText(str);
	m_banner.SetTitle(str);
}

/////////////////////////////////////////////////////////////////////////////

void CSideBannerDlg::OnRadioFlat() 
{
	m_banner.SetFillFlag( KCSB_FILL_FLAT );
}

/////////////////////////////////////////////////////////////////////////////

void CSideBannerDlg::OnRadioGradient() 
{
	m_banner.SetFillFlag( KCSB_FILL_GRADIENT );
}

/////////////////////////////////////////////////////////////////////////////

void CSideBannerDlg::OnRadioTexture() 
{
	m_banner.SetFillFlag( KCSB_FILL_TEXTURE );	
}

/////////////////////////////////////////////////////////////////////////////

void CSideBannerDlg::OnChangeEditCapoffset() 
{
	UpdateData();
	m_banner.SetCaptionOffset( CSize(m_nCapOffsetX, m_nCapOffsetY) );
	m_banner.Invalidate();
}

/////////////////////////////////////////////////////////////////////////////

void CSideBannerDlg::OnChangeEditEdgeoffset() 
{
	UpdateData();
	m_banner.SetEdgeOffset( CSize(m_nEdgeOffsetX, m_nEdgeOffsetY) );
	m_banner.Invalidate();
}

/////////////////////////////////////////////////////////////////////////////

void CSideBannerDlg::SwapOffsetValues()
{
	UpdateData();

	// if you're lazy to declare a temp variable, a little XOR trick will swap the values aswell :)
	m_nCapOffsetX ^= m_nCapOffsetY;
	m_nCapOffsetY ^= m_nCapOffsetX;
	m_nCapOffsetX ^= m_nCapOffsetY;

	m_nEdgeOffsetX ^= m_nEdgeOffsetY;
	m_nEdgeOffsetY ^= m_nEdgeOffsetX;
	m_nEdgeOffsetX ^= m_nEdgeOffsetY;

	UpdateData(FALSE);

	OnChangeEditCapoffset();
	OnChangeEditEdgeoffset();
}

/////////////////////////////////////////////////////////////////////////////

void CSideBannerDlg::OnChangeEditBannersize() 
{
	UpdateData();
	m_banner.SetSize(m_nBannerSize);
}

/////////////////////////////////////////////////////////////////////////////

HBRUSH CSideBannerDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr; // = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	if ( m_hBrush )
	{
		::DeleteObject(m_hBrush);
		m_hBrush = 0;
	}

	switch ( pWnd->GetDlgCtrlID() )
	{
	case	IDC_STATIC_COL1:
		hbr = m_hBrush = ::CreateSolidBrush( m_banner.GetColBkg() );
		pDC->SetBkMode(TRANSPARENT);
		break;

	case	IDC_STATIC_COL2:
		hbr = m_hBrush = ::CreateSolidBrush( m_banner.GetColBkg2() );
		pDC->SetBkMode(TRANSPARENT);
		break;

	default:
		hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	}

	// TODO: Return a different brush if the default is not desired
	return hbr;
}

/////////////////////////////////////////////////////////////////////////////

void CSideBannerDlg::OnStaticCol1() 
{
	CColorDialog			dlg( m_banner.GetColBkg(), CC_FULLOPEN );
	
	if ( dlg.DoModal() == IDOK )
	{
		m_banner.SetColBkg( dlg.GetColor() );
		m_banner.Invalidate();
		GetDlgItem(IDC_STATIC_COL1)->Invalidate();
	}
}

/////////////////////////////////////////////////////////////////////////////

void CSideBannerDlg::OnStaticCol2() 
{
	CColorDialog			dlg( m_banner.GetColBkg2(), CC_FULLOPEN );	
	
	if ( dlg.DoModal() == IDOK )
	{
		m_banner.SetColBkg2( dlg.GetColor() );
		m_banner.Invalidate();
		GetDlgItem(IDC_STATIC_COL2)->Invalidate();
	}
}

/////////////////////////////////////////////////////////////////////////////

void CSideBannerDlg::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	
	if ( m_banner.m_hWnd )
		m_banner.UpdateSize();
}

/////////////////////////////////////////////////////////////////////////////

void CSideBannerDlg::OnSelchangeAlignment() 
{
	UpdateData();

	UINT			uiFlag = 0;
	int				nHSel = m_ctlHorz.GetCurSel();
	int				nVSel = m_ctlVert.GetCurSel();
	CString			strHorz = _T(""), strVert = _T("");

	m_ctlHorz.GetLBText( nHSel, strHorz );
	m_ctlVert.GetLBText( nVSel, strVert );

	if ( strHorz == "Left" )
		uiFlag = KCSB_ICON_LEFT;
	else if ( strHorz == "Right" )
		uiFlag = KCSB_ICON_RIGHT;

	if ( strVert == "Top" )
		uiFlag |= KCSB_ICON_TOP;
	else if ( strVert == "Center" )
		uiFlag |= KCSB_ICON_VCENTER;
	else if ( strVert == "Bottom" )
		uiFlag |= KCSB_ICON_BOTTOM;

	m_banner.SetIconPos(uiFlag);
}

/////////////////////////////////////////////////////////////////////////////

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Peter Mares
Architect
Ireland Ireland
Peter Mares has no comment on himself. I'll let the objective humanoids do the damage Wink | ;)
He is currently developing a hobby MMO to prove that he can do it Wink | ;)

My Blog

All good things were meant to be improved
Follow on   Twitter

| Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 7 Nov 2003
Article Copyright 2003 by Peter Mares
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid