Click here to Skip to main content
15,896,201 members
Articles / Desktop Programming / WTL

CAppBar, a WTL implementation of AppBar

Rate me:
Please Sign up or sign in to vote.
4.82/5 (26 votes)
14 Sep 2005CPOL3 min read 87.4K   2.6K   50  
A reusable WTL base class to support AppBar
// MainDlg.h : interface of the CMainDlg class
//
/////////////////////////////////////////////////////////////////////////////

#pragma once

#include "appbar.h"
#include "resource.h"
#include "aboutdlg.h"

class CMainDlg : public CDialogImpl<CMainDlg>, public CUpdateUI<CMainDlg>,
		public CMessageFilter, public CIdleHandler,public CAppBar<CMainDlg>
{
public:
	enum { IDD = IDD_MAINDLG };

	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)
		COMMAND_HANDLER(IDC_CHECK_KEEPSIZE, BN_CLICKED, OnClickedKeepsize)
		CHAIN_MSG_MAP(CAppBar<CMainDlg>)
		MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
		COMMAND_ID_HANDLER(ID_APP_ABOUT, OnAppAbout)
		COMMAND_ID_HANDLER(IDC_DOCK_LEFT, OnLeft)
		COMMAND_ID_HANDLER(IDC_DOCK_RIGHT, OnRight)
		COMMAND_ID_HANDLER(IDC_DOCK_TOP, OnTop)
		COMMAND_ID_HANDLER(IDC_DOCK_BOTTOM, OnBottom)
		COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
		COMMAND_HANDLER(IDC_BTN_UNDOCK, BN_CLICKED, OnClickedUndock)
		COMMAND_HANDLER(IDC_CHECK_AUTOHIDE, BN_CLICKED, OnClickedAutohide)
	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);
		
		InitAppBar(APPBAR_DOCKING_ALL, false);

		return TRUE;
	}

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

		return 0;
	}

	LRESULT OnLeft(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		DockAppBar(APPBAR_DOCKING_LEFT);
		return 0;
	}

	LRESULT OnRight(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		DockAppBar(APPBAR_DOCKING_RIGHT);
		return 0;
	}

	LRESULT OnTop(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		DockAppBar(APPBAR_DOCKING_TOP);
		return 0;
	}

	LRESULT OnBottom(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		DockAppBar(APPBAR_DOCKING_BOTTOM);
		return 0;
	}

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

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


	LRESULT OnClickedUndock(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		DockAppBar(APPBAR_DOCKING_NONE);
		return 0;
	}
	LRESULT OnClickedAutohide(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		int nChecked = IsDlgButtonChecked(IDC_CHECK_AUTOHIDE);
		SetAutoHide(nChecked==BST_CHECKED);

		return 0;
	}
	LRESULT OnClickedKeepsize(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		int nChecked = IsDlgButtonChecked(IDC_CHECK_KEEPSIZE);
		SetKeepSize(nChecked==BST_CHECKED);

		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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


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