Click here to Skip to main content
15,126,367 members
Articles / Desktop Programming / MFC
Posted 3 May 2005


94 bookmarked

AppBar - How to implement a sliding desktop bar with a simple MFC class and a DLL

Rate me:
Please Sign up or sign in to vote.
4.58/5 (25 votes)
10 Jul 2008CPOL4 min read
An alternative to standard Windows AppBars with minimal changes to your application.

Image 1


I searched many times on the Internet for an easy way to implement an AppBar (desktop application toolbar) but without success. Microsoft provides some WinAPI shell functions like SHAppBarMessage (read here about it) and an old sample application (AppBar.exe). I tried it out but that was not what I expected because Shell AppBars reorganize the desktop to fit the toolbar, while I wanted a sliding bar that doesn't disturb other application windows or the desktop icons.

So, I have developed a single MFC object, CAppBarMngr, to allow almost any application to become a sliding AppBar, with minimal changes to the application's source code. Since it is necessary to respond to mouse movements outside the application window, I had to implement a global mouse hook, which requires to generate a DLL, but you don't have to deal with the DLL internals, just to distribute with your software.

Overall Design

There is just one class: CAppBarMngr, which will be responsible for sliding the application's main frame from the left or right edge of the desktop screen. As I mentioned earlier, a global hook is needed to respond appropriately to the mouse cursor position (i.e. if the side edge has been reached).

Traditionally, a global hook is implemented as a DLL that sends messages to an application's window through its HWND handler (hook DLLs can't be MFC enabled), which requires several modifications in the application's code for receiving and managing the hook messages. In order to avoid that, however, I have tried a different approach: to send messages to a secondary thread, as explained below.

CAppBarMngr Class

This class is derived from CWinThread, so it is capable of receiving messages sent using WinAPI PostThreadMessage() function, by implementing the PreTranslateMessage() event. Also this class has some other responsibilities: to be a wrapper for the hook DLL, and to handle window movements (sliding).

CAppBarMngr has just one public member: the Init() function. It will be used to link the manager with the managed window. It receives three arguments as described in the source code:

// Function:  Init   - Loads DLL functions and initilize mouse hook
// Arguments: _hWnd  - Handler of window to manage
//            _width - Desired width of managed window
//            _left  - True if window is left side docked, false if right sided
// Returns:   APPBARHOOK_DLLERROR - An error has occurred while loading DLL functions
//            APPBARHOOK_ALREADYHOOKED - Another instance has already hooked the mouse
//            APPBARHOOK_SUCCESS - All is OK

int CAppBarMngr::Init(HWND _hWnd, int _width, bool _left)

How to Use It

This is all you have to do to implement AppBar into your application:

  • Insert the AppBarMngr.cpp and AppBarMngr.h files into your MFC project.
  • Put #include <span class="code-string">"AppBarMngr.h"</span> into your main file (where main window is created).
  • Create your main window, usually into yourapp::InitInstance().
  • Create a thread with an MFC ::AfxBeginThread() function as shown below, saving a pointer to it.
  • Call the Init method specifying window's HWND handler, desired width and edge.
  • Verify if Init has returned successfully.

Here is the code portion from the demo application:

BOOL CAppBarDemoApp::InitInstance()
 CMainFrame* pFrame = new CMainFrame;
 m_pMainWnd = pFrame;

 // creates a simple frame, without caption, icon or menu

 pFrame->Create(NULL, "AppBarDemo", WS_POPUP);
 // avoid taskbar button to appear, also removes 3D edge

 // Don't show, AppBar Manager class will do


 // Create AppBar manager thread

 CAppBarMngr *appbar = 
           (CAppBarMngr *)::AfxBeginThread(RUNTIME_CLASS(CAppBarMngr));

 // Init AppBar Manager, right sided

 int result = appbar->Init(pFrame->m_hWnd, 150, false);  // use true for left sided
 // Check if hooking has been successful

  return TRUE;
 else if (result==APPBARHOOK_DLLERROR)
  ::AfxMessageBox("Error loading AppBarHook.dll");
 // else should be APPBARHOOK_ALREADYHOOKED, close application

 return FALSE;

That's all you have to do. Also, don't forget to distribute the hook DLL (AppBarHook.dll) with your executable application; it must reside in the same directory to work properly.

Single Instance Control for Free

As it makes no sense to run two copies of the same AppBar program, the Init() function is a notification that the hook has already been used (by returning APPBARHOOK_ALREADYHOOKED), so, you can use it to avoid a second instance to run. Notice the last lines in the example above, if hook has been already used, then it returns FALSE to close the application.

The Hook Project

I had to develop a project to implement the global mouse hook DLL. It has just one file: AppBarHook.cpp. I don't want to make a hook tutorial here because there are several great articles here at CodeProject. So, I will just give you some of the details.

The usual hook technique saves a windows handler (HWND) of the receiving window for hook messages. I have used a Thread ID instead, that's why CAppBarMngr is a thread object. So, messages are passed using WinAPI ::SendThreadMessage() instead of the ::SendMessage() function.

The hook will detect mouse events of possible interest to CAppBarMngr. I say possible because the hook doesn't know about the window state and position, it knows only about the managed edge and window width. The MFC class will do the rest of the work.

The Hook DLL exports only one function: SetHook(), which creates the global mouse hook and saves the desired width and edge. It is called by CAppBarMngr.

About the Demo Application

I have created a simple demo application project for testing purpose. It has a simple CFrameWnd derived object without frame, caption, menu or border. I have tested this with other standard MFC windows without any problem.

The demo source is a Visual C++ 6.0 project, but you will be able to open and automatically convert to a newer Visual C++ version.

If you find any bug in this application, please don't care about it. It is just for demo purposes and it is not the intention of this article. I will not describe its internals here for the same reason.

.NET Version

I have been requested several times for a .NET version of this control. I have started working on it. I will leave a message in the forum below when it is ready.


  • May 3, 2005 - First edition
  • July 9, 2008 - Added multi-monitor support


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


About the Author

Jaime Olivares
Peru Peru

Computer Electronics professional, Software Architect and senior Windows C++ and C# developer with experience in many other programming languages, platforms and application areas including communications, simulation systems, PACS/DICOM (radiology), GIS, 3D graphics and HTML5-based web applications.
Currently intensively working with Visual Studio and TFS.

Comments and Discussions

QuestionSame code in VS 2008 C# or Pin
sshparmar10-Oct-12 3:18
Membersshparmar10-Oct-12 3:18 
AnswerRe: Same code in VS 2008 C# or Pin
Jaime Olivares14-Nov-12 11:18
MemberJaime Olivares14-Nov-12 11:18 
QuestionConvertion to VS 2010 Pin
amilkar16-Dec-11 4:49
Memberamilkar16-Dec-11 4:49 
AnswerRe: Convertion to VS 2010 Pin
Jaime Olivares16-Dec-11 5:49
MemberJaime Olivares16-Dec-11 5:49 
GeneralRe: Convertion to VS 2010 Pin
amilkar16-Dec-11 13:34
Memberamilkar16-Dec-11 13:34 
Generalneed to know: All this stuff can be acheeved by WinApi (without hook dll) Pin
VaKa4-Mar-11 1:06
MemberVaKa4-Mar-11 1:06 
GeneralMy vote of 5 Pin
jpiquemal28-Aug-10 0:00
Memberjpiquemal28-Aug-10 0:00 
GeneralMy vote of 5 Pin
Kim Sung-kyu2-Aug-10 6:21
MemberKim Sung-kyu2-Aug-10 6:21 
QuestionHow to implement the same functionality in C#.Net Pin
Shakti.00125-Dec-07 23:14
MemberShakti.00125-Dec-07 23:14 
AnswerRe: How to implement the same functionality in C#.Net Pin
Jaime Olivares7-Dec-07 5:19
MemberJaime Olivares7-Dec-07 5:19 
GeneralRe: How to implement the same functionality in C#.Net Pin
alexv197728-Nov-08 4:35
Memberalexv197728-Nov-08 4:35 
QuestionCan I use this class in my dialog-based app? Pin
Member 200639830-May-06 4:42
MemberMember 200639830-May-06 4:42 
AnswerRe: Can I use this class in my dialog-based app? Pin
Sandro7815-Feb-08 6:27
MemberSandro7815-Feb-08 6:27 
Generaluse in .NET Pin
duneth10-Mar-06 4:52
Memberduneth10-Mar-06 4:52 
GeneralDrawing glitch Pin
_Stilgar_15-Aug-05 6:14
Member_Stilgar_15-Aug-05 6:14 
Generaldual monitor Pin
Djohnnie17-Jun-05 4:33
MemberDjohnnie17-Jun-05 4:33 
GeneralRe: dual monitor Pin
Jaime Olivares17-Jun-05 4:38
MemberJaime Olivares17-Jun-05 4:38 
GeneralRe: dual monitor Pin
ShanGill25-Jun-08 3:24
MemberShanGill25-Jun-08 3:24 
GeneralRe: dual monitor Pin
Jaime Olivares25-Jun-08 4:43
MemberJaime Olivares25-Jun-08 4:43 
GeneralRe: dual monitor Pin
Jaime Olivares11-Jul-08 7:00
MemberJaime Olivares11-Jul-08 7:00 
GeneralRe: dual monitor Pin
ShanGill19-Jul-08 10:42
MemberShanGill19-Jul-08 10:42 
GeneralRe: dual monitor Pin
Jaime Olivares19-Jul-08 12:02
MemberJaime Olivares19-Jul-08 12:02 
Generalvb6 Pin
alejrgr13-May-05 4:26
Memberalejrgr13-May-05 4:26 
GeneralRe: vb6 Pin
Jaime Olivares13-May-05 6:30
MemberJaime Olivares13-May-05 6:30 
GeneralRe: vb6 Pin
alejrgr16-May-05 4:59
Memberalejrgr16-May-05 4:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.