Click here to Skip to main content
15,867,453 members
Articles / Desktop Programming / MFC
Article

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 153.6K   6.4K   95   29
An alternative to standard Windows AppBars with minimal changes to your application.

Image 1

Introduction

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:

C++
//------------------------------------------------------
// 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 "AppBarMngr.h" 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:

C++
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

 pFrame->ModifyStyleEx(WS_EX_APPWINDOW|WS_EX_CLIENTEDGE, WS_EX_TOOLWINDOW);
 // Don't show, AppBar Manager class will do

 pFrame->ShowWindow(SW_HIDE);
 pFrame->UpdateWindow();

 // 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

 if (result==APPBARHOOK_SUCCESS)
  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.

History

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

License

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


Written By
Architect
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 VB.net Pin
sshparmar10-Oct-12 2:18
sshparmar10-Oct-12 2:18 
AnswerRe: Same code in VS 2008 C# or VB.net Pin
Jaime Olivares14-Nov-12 10:18
Jaime Olivares14-Nov-12 10:18 
QuestionConvertion to VS 2010 Pin
amilkar16-Dec-11 3:49
amilkar16-Dec-11 3:49 
AnswerRe: Convertion to VS 2010 Pin
Jaime Olivares16-Dec-11 4:49
Jaime Olivares16-Dec-11 4:49 
GeneralRe: Convertion to VS 2010 Pin
amilkar16-Dec-11 12:34
amilkar16-Dec-11 12:34 
Generalneed to know: All this stuff can be acheeved by WinApi (without hook dll) Pin
VaKa4-Mar-11 0:06
VaKa4-Mar-11 0:06 
GeneralMy vote of 5 Pin
jpiquemal27-Aug-10 23:00
jpiquemal27-Aug-10 23:00 
simple and effective
GeneralMy vote of 5 Pin
Kim Sung-kyu2-Aug-10 5:21
Kim Sung-kyu2-Aug-10 5:21 
QuestionHow to implement the same functionality in C#.Net Pin
Shakti.00125-Dec-07 22:14
Shakti.00125-Dec-07 22:14 
AnswerRe: How to implement the same functionality in C#.Net Pin
Jaime Olivares7-Dec-07 4:19
Jaime Olivares7-Dec-07 4:19 
GeneralRe: How to implement the same functionality in C#.Net Pin
alexv197728-Nov-08 3:35
alexv197728-Nov-08 3:35 
QuestionCan I use this class in my dialog-based app? Pin
Member 200639830-May-06 3:42
Member 200639830-May-06 3:42 
AnswerRe: Can I use this class in my dialog-based app? Pin
Sandro7815-Feb-08 5:27
Sandro7815-Feb-08 5:27 
Generaluse in .NET Pin
duneth10-Mar-06 3:52
duneth10-Mar-06 3:52 
GeneralDrawing glitch Pin
_Stilgar_15-Aug-05 5:14
_Stilgar_15-Aug-05 5:14 
Generaldual monitor Pin
Djohnnie17-Jun-05 3:33
Djohnnie17-Jun-05 3:33 
GeneralRe: dual monitor Pin
Jaime Olivares17-Jun-05 3:38
Jaime Olivares17-Jun-05 3:38 
GeneralRe: dual monitor Pin
ShanGill25-Jun-08 2:24
ShanGill25-Jun-08 2:24 
GeneralRe: dual monitor Pin
Jaime Olivares25-Jun-08 3:43
Jaime Olivares25-Jun-08 3:43 
GeneralRe: dual monitor Pin
Jaime Olivares11-Jul-08 6:00
Jaime Olivares11-Jul-08 6:00 
GeneralRe: dual monitor Pin
ShanGill19-Jul-08 9:42
ShanGill19-Jul-08 9:42 
GeneralRe: dual monitor Pin
Jaime Olivares19-Jul-08 11:02
Jaime Olivares19-Jul-08 11:02 
Generalvb6 Pin
alejrgr13-May-05 3:26
alejrgr13-May-05 3:26 
GeneralRe: vb6 Pin
Jaime Olivares13-May-05 5:30
Jaime Olivares13-May-05 5:30 
GeneralRe: vb6 Pin
alejrgr16-May-05 3:59
alejrgr16-May-05 3: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.