Click here to Skip to main content
15,860,844 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.7K   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

 
GeneralMy vote of 5 Pin
borlittle23-Aug-11 2:14
borlittle23-Aug-11 2:14 
GeneralAbout the font Pin
husoso14-Oct-07 16:38
husoso14-Oct-07 16:38 
QuestionAdding your source code on MFC DLL Pin
Daniel Mayer27-Jul-06 4:52
Daniel Mayer27-Jul-06 4:52 
QuestionCan Not be Used in Visual Studio 2005 Pin
_DESOLATED_8-Jan-06 15:49
_DESOLATED_8-Jan-06 15:49 
AnswerRe: Can Not be Used in Visual Studio 2005 Pin
XiaoQing Li9-Apr-06 22:41
XiaoQing Li9-Apr-06 22:41 
GeneralYou can improve mouse selection Pin
Alex Dovosheya22-Aug-05 3:38
Alex Dovosheya22-Aug-05 3:38 
QuestionWhy Invalidate()? Pin
Alex Dovosheya17-Aug-05 0:02
Alex Dovosheya17-Aug-05 0:02 
GeneralCreate ActiveX Pin
Crack1229-Jul-05 0:58
Crack1229-Jul-05 0:58 
I was trying to create an activeX out of this control. I got it to work with MFC Apps while I am unable to add the activeX to an ATL Composite Control. May be problem with the way I wrote the activeX.
Does anyone have an ActiveX Version of this control or point me as to how to go about writing an ActiveX out of this control. ?

Any help would be appreciated.

Thank you


GeneralFixes Pin
Squidge27-Jul-05 8:57
Squidge27-Jul-05 8:57 
Generaladding icons Pin
Sanatforu20-Feb-05 3:40
Sanatforu20-Feb-05 3:40 
GeneralError while compiling Pin
Diablo_m5-Feb-05 8:14
Diablo_m5-Feb-05 8:14 
Generalsilver look Pin
Michel Wassink25-Nov-04 20:26
Michel Wassink25-Nov-04 20:26 
GeneralRe: silver look Pin
Alexander Bischofberger26-Nov-04 7:30
Alexander Bischofberger26-Nov-04 7:30 
Generaldon't work with othere themes for me Pin
wb5-Aug-04 23:01
wb5-Aug-04 23:01 
GeneralRe: don't work with othere themes for me Pin
Alexander Bischofberger6-Aug-04 9:19
Alexander Bischofberger6-Aug-04 9:19 
GeneralCant able to find uxtheme.h and tmschema.h files Pin
auur31-Jul-04 20:40
auur31-Jul-04 20:40 
GeneralRe: Cant able to find uxtheme.h and tmschema.h files Pin
Alexander Bischofberger1-Aug-04 4:25
Alexander Bischofberger1-Aug-04 4:25 
GeneralAdding controls Pin
Atlence23-Jul-04 11:54
Atlence23-Jul-04 11:54 
GeneralRe: Adding controls Pin
Alexander Bischofberger23-Jul-04 12:03
Alexander Bischofberger23-Jul-04 12:03 
Questionanimation? Pin
Huisheng Chen4-May-04 2:26
Huisheng Chen4-May-04 2:26 
AnswerRe: animation? Pin
Alexander Bischofberger4-May-04 9:58
Alexander Bischofberger4-May-04 9:58 
Generaldialog use problem Pin
kanetheterrible121-Apr-04 2:59
kanetheterrible121-Apr-04 2:59 
GeneralRe: dialog use problem Pin
Alexander Bischofberger25-Apr-04 0:34
Alexander Bischofberger25-Apr-04 0:34 
GeneralRe: dialog use problem Pin
kanetheterrible130-Apr-04 0:48
kanetheterrible130-Apr-04 0:48 
GeneralRe: TruColor toolbars now supported Pin
Alexander Bischofberger2-May-04 2:32
Alexander Bischofberger2-May-04 2: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.