Click here to Skip to main content
15,860,972 members
Articles / Desktop Programming / MFC
Article

DirectUI Window as in XP system folders

Rate me:
Please Sign up or sign in to vote.
4.45/5 (43 votes)
1 Aug 2004CPOL6 min read 260.8K   7.6K   170   77
A control to show a list of possible tasks just as in XP.

Image 1

Introduction

Have you seen these nice little windows that come up in Windows XP when you go to special folders like the Control Panel, My Pictures etc. ? They show up a list of possible and popular tasks for the folder and/or selected items. With this "Top ten" list of things, Windows thinks you would like to do, it's much faster for a beginner than searching for the right command in the toolbar or menu. I thought that this user help would also fit in my own applications, so I wrote this class to simulate this new control.

Background

It's not to hard to write your own control with this behavior. But as I tried to do this I found out that Windows uses a lot of different colors (here: different kinds of blue) - and I'm quite unsure how to get these colors without depending on the theme-thing. So I tried to get almost the same colors, no matter if the way was right. Be sure that there will be theme support in the near future.

As this does not seem to be an official control there's no wording for this. So I used the Spy++ to see the class names decided to name it DirectUI and call the boxes as "groups" and the clickable items as simply "item".

Using the code

This class can now use Themes as a new style. If you'd like to activate them just include a #define USE_THEMES in your StdAfx.h. Otherwise the self-made XP-style will be used.

Add the two files to your project and make sure to have a CMemDC class in your project, or use the one provided in the sample. Insert the window CWndDirectUI anywhere you want, just like a normal control. Before compiling make sure that you have a hand cursor named IDC_MYHAND in your resource.

By default the control is drawn using the style "Themed" (see screenshot above). You can change the style manually any time by simply calling SetStyle() using CWndDirectUI::styleOffice, CWndDirectUI::styleXPclassic, CWndDirectUI::styleXP or CWndDirectUI::styleThemed. For retrieving the current style use GetStyle(). Please be carefull when switching to the office style after the user was able to use the control, as in this style there's no expand/collapse button, so previously collapsed groups can't be expanded by the user withg this style.

For manual expand/collapse use the functions ExpandGroup(groupNr, expand).

To fill the control the best way is to use the function InitFromMenu(UINT id). Pass a menu id containing a 2D menu (no further submenus). It will automatically create the groups from the first level and add the submenu items below. If you'd like you can insert toolbar icons using SetToolbarImages(UINT uiToolbar) by simply passing a toolbar id. Please insert all items into 1 toolbar, as you can use this function only with 1 toolbar at the moment. By the way: scrollbars will automatically show if there are too many items for displaying in the control.

{
  ...

  m_wndDirectUI.InitFromMenu(IDR_DIRECT1);
  m_wndDirectUI.SetToolbarImages(IDR_MAINFRAME);

  ...
}

To use images with more than 16 colors use the SetToolbarImages(uiToolbar, uiAlternateImages, nFlagColor, clBackground, nWidth) function with the following parameters:

  • uiAlternateImages: resource id of the hi-color bitmap
  • nFlagColor: See CImageList::Create() for more info. Default is ILC_COLOR8 for 256 color images
  • clBackground: Background color used for maps (default is purple)
  • nWidth: Button width (default is 16)

For manually adding groups and items please see the class definition and the functions AddGroup(),

AddItemCommand()
etc. So for every action the user does simply set a new list - that's all! When the user selects an item in the list the commands come to your application like normal menu commands, so process them like you process all the other commands. Please check this site if you'd like to use it in e.g. a dockable window. There are lots of classes for this so I see no reason to re-invent the wheel again.

Using static text and edit controls

By now two new items are include with CWndUI: static text and an edit control. If you'd like to use these items you have to insert them manually to the list by using AddItemCommand(). So to add a static text and an edit control take a look at the following piece of code from the sample:

{
  ...
  int nGroup;
  nGroup = m_wndDirectUI.AddGroup("Test group");

  m_wndDirectUI.AddItem(nGroup, new CDirectUIItemStatic(
    _T("This is\na multiline\ntest text")));
  m_wndDirectUI.AddItem(nGroup, new CDirectUIItemEdit(
    _T("Edit this!"), &m_wndDirectUI));
  m_wndDirectUI.AddItemCommand(nGroup, "item 1", ID_NUMBER1);
  ...
}

These new items have caused a few changes to the existing code, so if you used this class before and created own controls based on this class please check the code. The CDirectUIItemEdit uses the popular CInPlaceEdit class created by Zafir Anjum (and modified a little for my class) - thank you for your code! In case you are interested in how to use inplace controls take a look at this class, perhaps you find some idea how to do it. As I have no screenshot on how CDirectUIItemStatic and CDirectUIItemEdit are "officially" painted I just used it the way I think it could be. If you have any detailed information please send me an image or modified code.

In case you'd like to extend the control the next step would be a combined class that holds an edit control and a button beside (like "Find" or "Go"), but that's your homework :-)

For the future... what's missing?

There are a few things not implemented in this first version, e.g.:

  • Keyboard support - only the mouse can access this control
  • Focus rect when button is down, and all the behavior that comes with handling the focus

And ... this is my first code using themes. Please tell me if I have made some mistakes!

Compatibility and Testing

The code was tested using Windows XP Home and Visual C++ 6.0 . It should run on all versions starting with Windows 98 and Visual C++ 5. I haven't tested the code too well, so please tell me if there's a bug. I'm sorry, but in the demo (only there) the resources are mixed up in English and German.

Copyright and Credits

Feel free to use this code in any application (freeware, shareware, commercial, ...) as long you keep the copyright in the code and documentation, as well as credits.

  • Thanks a lot to Keith Rule for his CMemDC class!
  • Thanks to David Yuheng Zhao for his CVisualStylesXP class.
  • Thanks to all of you who found a bug (see discussions below), you are mentioned inside the code!
  • Thanks to Zafir Anjum for his CInPlaceEdit class I used!
  • Thanks to Mathieu Dubaele for his help with the edit item.

History

  • July 27, 2004: Two new items added, lot of code changed
  • May 8, 2004: Now supports Themes as a new style
  • May 2, 2004: Support for hi-color icons, thanks to Dany Cantin for his code for hi-color toolbars
  • December 1, 2003: Bugfix (GDI resources), thanks to Michel Wassink and the others who helped me finding the bug.
  • March 1, 2003:
    • BugFix: When a group is collapsed you can still select an item (OnHittest)
    • BugFix: When scrolled the OnHittest returns wrong results
    • BugFix: Warning for IDC_HAND is fixed
    • Example: How to use in a dialog (select the menu item DIRECTUI_WINDOW, Show dialog for an example)
  • January 5, 2003: Scrolling, expand+collapse, new styles, minor changes. Class and files renamed for CJobWnd to CDirectUI
  • November 19, 2002: First version

License

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


Written By
Web Developer
Germany Germany
Ok, a few words about me:

I started programming on the C64 by the "trial and error" method. Years later my parents got their first PC (Atari PC4, AT 8 MHz) where I started with Turbo Pascal. The next steps led to Turbo Pascal for Windows, Visual C++ 1.52c, and finally Visual C++ 6.

I had several chances to code larger projects, e.g.
* stand-alone disc copy station software (Win 3.1) with automatic reboot, disc encryption, ...
* government-used strategic decision system
* MLM marketing tool (www.upline.de)
* maintenance for show planning system and other TV software (www.hse24.de)

A lot of code, tipps and help came from this site for my later projects. Thanks again to all who helped me, even if they don't know it. I'm trying to share code (which is worth sharing) so others can get the help I got (and still need) for everyday problems.

Well, I think that's enough.

Comments and Discussions

 
GeneralRe: TruColor toolbars now supported Pin
kanetheterrible13-May-04 21:47
kanetheterrible13-May-04 21:47 
GeneralOffice XP style task pane Pin
Seshagiri5-Apr-04 2:42
Seshagiri5-Apr-04 2:42 
GeneralRe: Office XP style task pane Pin
Alexander Bischofberger5-Apr-04 7:41
Alexander Bischofberger5-Apr-04 7:41 
GeneralRe: Office XP style task pane Pin
Seshagiri5-Apr-04 23:34
Seshagiri5-Apr-04 23:34 
GeneralRe: Office XP style task pane Pin
Alexander Bischofberger25-Apr-04 0:44
Alexander Bischofberger25-Apr-04 0:44 
GeneralRe: Office XP style task pane Pin
endar9819-May-04 10:00
endar9819-May-04 10:00 
GeneralRe: Office XP style task pane Pin
Alexander Bischofberger24-May-04 3:51
Alexander Bischofberger24-May-04 3:51 
GeneralDynamic Group Items Pin
Pereraq18-Jan-04 18:00
Pereraq18-Jan-04 18:00 
GeneralRe: Dynamic Group Items Pin
Alexander Bischofberger18-Jan-04 20:59
Alexander Bischofberger18-Jan-04 20:59 
GeneralRe: Dynamic Group Items Pin
Rolando Cruz16-Feb-05 4:39
Rolando Cruz16-Feb-05 4:39 
GeneralRe: Dynamic Group Items Pin
Alexander Bischofberger16-Feb-05 7:53
Alexander Bischofberger16-Feb-05 7:53 
GeneralRe: Dynamic Group Items Pin
Rolando Cruz16-Feb-05 14:09
Rolando Cruz16-Feb-05 14:09 
GeneralRe: Dynamic Group Items Pin
Alexander Bischofberger17-Feb-05 7:47
Alexander Bischofberger17-Feb-05 7:47 
GeneralDialog Use Pin
esmithx19-Dec-03 8:27
esmithx19-Dec-03 8:27 
GeneralRe: Dialog Use Pin
Alexander Bischofberger10-Jan-04 9:56
Alexander Bischofberger10-Jan-04 9:56 
GeneralProble When i use this control with a dialog box Pin
CaptainIgloo23-Nov-03 2:50
CaptainIgloo23-Nov-03 2:50 
GeneralI found the resource leak! Pin
Crog18-Oct-03 7:17
Crog18-Oct-03 7:17 
GeneralRe: I found the resource leak! Pin
Michel Wassink19-Oct-03 20:55
Michel Wassink19-Oct-03 20:55 
GeneralRe: I found the resource leak! Pin
Alexander Bischofberger20-Oct-03 7:58
Alexander Bischofberger20-Oct-03 7:58 
GeneralQuick Fix for Assertion Failure Under VC++ 7.0 Pin
Darcy L. Watkins13-Oct-03 11:56
Darcy L. Watkins13-Oct-03 11:56 
GeneralA quick fix for the resource leaks Pin
Renault the Duck28-Jul-03 15:48
Renault the Duck28-Jul-03 15:48 
GeneralMemory Overflow Pin
DaRpH14-Jul-03 11:29
DaRpH14-Jul-03 11:29 
Generallittle hint Pin
ROK_RShadow16-Jun-03 0:11
ROK_RShadow16-Jun-03 0:11 
GeneralAnother small bug Pin
Zebrex11-Jun-03 3:51
Zebrex11-Jun-03 3:51 
Generalgreat work Pin
xp++xp10-May-03 17:32
xp++xp10-May-03 17:32 

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.