# DirectUI Window as in XP system folders(xp样式任务列表控件)

## 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.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 高色彩bmp图像的资源id
• 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

new CDirectUIItemStatic(
_T(
"This is a multiline test text")));
new CDirectUIItemEdit(
_T(
"Edit this!"), &m_wndDirectUI));
"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.

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
• 本文已收录于以下专栏：

举报原因： 您举报文章：DirectUI Window as in XP system folders(xp样式任务列表控件) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)