Click here to Skip to main content
15,891,976 members
Articles / Desktop Programming / WTL

CCaptionButton - Add Buttons on the Caption Bar

Rate me:
Please Sign up or sign in to vote.
4.96/5 (24 votes)
19 Sep 20054 min read 135.8K   5.3K   69  
A reusable WTL base class to add buttons on the caption bar.
// MainDlg.h : interface of the CMainDlg class
//
/////////////////////////////////////////////////////////////////////////////

#pragma once

#include "CaptionButton.h"
#include "AppBar.h"

class CMainDlg : public CDialogImpl<CMainDlg>, public CUpdateUI<CMainDlg>,
		public CMessageFilter, public CIdleHandler,
		public CCaptionButton<CMainDlg>,
		public CAppBar<CMainDlg>
{
public:
	enum { IDD = IDD_MAINDLG };
	enum { 
		ID_PUSH_PIN = WM_USER + 100,
		ID_UP_PIN	= WM_USER + 101,
		ID_DOWN_PIN = WM_USER + 102,
		ID_LEFT_PIN	= WM_USER + 103,
		ID_RIGHT_PIN= WM_USER + 104
	};

	virtual BOOL PreTranslateMessage(MSG* pMsg)
	{
		return CWindow::IsDialogMessage(pMsg);
	}

	virtual BOOL OnIdle()
	{
		return FALSE;
	}

	BEGIN_UPDATE_UI_MAP(CMainDlg)
	END_UPDATE_UI_MAP()

	BEGIN_MSG_MAP(CMainDlg)
		CHAIN_MSG_MAP(CCaptionButton<CMainDlg>)
		CHAIN_MSG_MAP(CAppBar<CMainDlg>)
		MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
		COMMAND_ID_HANDLER(ID_APP_ABOUT, OnAppAbout)
		COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
		COMMAND_ID_HANDLER(ID_PUSH_PIN, OnPushPin)
		COMMAND_ID_HANDLER(ID_LEFT_PIN, OnLeftPin)
		COMMAND_ID_HANDLER(ID_RIGHT_PIN, OnRightPin)
		COMMAND_ID_HANDLER(ID_UP_PIN, OnUpPin)
		COMMAND_ID_HANDLER(ID_DOWN_PIN, OnDownPin)
	END_MSG_MAP()

	LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		// center the dialog on the screen
		CenterWindow();

		// set icons
		HICON hIcon = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), 
			IMAGE_ICON, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR);
		SetIcon(hIcon, TRUE);
		HICON hIconSmall = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), 
			IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
		SetIcon(hIconSmall, FALSE);

		// register object for message filtering and idle updates
		CMessageLoop* pLoop = _Module.GetMessageLoop();
		ATLASSERT(pLoop != NULL);
		pLoop->AddMessageFilter(this);
		pLoop->AddIdleHandler(this);

		UIAddChildWindowContainer(m_hWnd);

		//add caption buttons
		CImageList	il;
		il.Create(IDB_PIN, 16, 5, RGB(255, 255, 255));
		AddButton(ID_PUSH_PIN, 16, 16, il, "Stay visible");

		CImageList	il2;
		il2.Create(IDB_UP, 16, 5, RGB(255, 255, 255));
		AddButton(ID_UP_PIN, 16, 8, il2, "Dock top");

		CImageList	il3;
		il3.Create(IDB_DOWN, 16, 5, RGB(255, 255, 255));
		AddButton(ID_DOWN_PIN, 16, 8, il3, "Dock bottom");

		CImageList	il4;
		il4.Create(IDB_LEFT, 8, 5, RGB(255, 255, 255));
		AddButton(ID_LEFT_PIN, 8, 16, il4, "Dock left");

		CImageList	il5;
		il5.Create(IDB_RIGHT, 8, 5, RGB(255, 255, 255));
		AddButton(ID_RIGHT_PIN, 8, 16, il5, "Dock right");

		//disable a caption button
		EnableButton(2, false);

		//initialize appbar
		InitAppBar(APPBAR_DOCKING_ALL, true, false);


		return TRUE;
	}

	POINT GetButtonPos(int index)
	{
		CRect	rcWnd;
		GetWindowRect(&rcWnd);
		rcWnd.OffsetRect(-rcWnd.TopLeft());

		rcWnd.DeflateRect(2, 2);

		//locate the top right base point
		CPoint	pt(rcWnd.right, rcWnd.top);
		pt.x -= 20;
		pt.y += 2;
		switch (index)
		{
		case 0://autohide pin
			pt.x -= 16;
			break;
		case 1://up
			pt.x -= 18+16+10;
			break;
		case 2://down
			pt.x -= 18+16+10;
			pt.y += 8;
			break;
		case 3://left
			pt.x -= 16+18+10+10;
			break;
		case 4://right
			pt.x -= 16+10;
			break;
		}

		return pt;
	}

	LRESULT OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		CAboutDlg dlg;
		dlg.DoModal();
		return 0;
	}

	LRESULT OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		CloseDialog(wID);
		return 0;
	}

	void CloseDialog(int nVal)
	{
		DestroyWindow();
		::PostQuitMessage(nVal);
	}

	LRESULT OnPushPin(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		static bool bChecked = false;

		//change check status
		bChecked = !bChecked;
		CheckButton(0, bChecked);
		SetAutoHide(!bChecked);

		return 0;
	}
	LRESULT OnLeftPin(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		DockAppBar(APPBAR_DOCKING_LEFT);
		return 0;
	}
	LRESULT OnRightPin(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		DockAppBar(APPBAR_DOCKING_RIGHT);
		return 0;
	}
	LRESULT OnUpPin(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		DockAppBar(APPBAR_DOCKING_TOP);
		return 0;
	}
	LRESULT OnDownPin(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		DockAppBar(APPBAR_DOCKING_BOTTOM);
		return 0;
	}
};

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
China China
I'm a chinese programer living in Shanghai, currently working for a software company whose main business is to deliver computer based testing. Software simulation for computer based testing and certifications is my main responsibility in this company. Execpt for software development, I like out-door activities and photography. I am willing to make friends in China and all over the world, so contact me if you have anything in common with meSmile | :)

Comments and Discussions