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.

当你点开像控制栏,我的图片等特殊的文件夹时,你有没有看过这些出自xp的小窗口呢?它们为文件夹或是选中的项目展现了一组可行而且有明显标注的任务。有了这前十的东西,windows考虑到了你想要做的事情,这比让一个新手在工具栏或菜单中寻找正确的命令来快多了。我认为这种小窍门也适用于我的程序中,所以我写了这个类模仿了一个新的控件。

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.
这个类现在可以使用主题了。如果想使用主题可以在StdAfx.h里加入#define USE_THEMES,不加则。。。

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.
为你的工程加入这两个文件并确保你的工程有CMemDC类,要不就直接用示例中提供的。像普通控件一样在任意处插入窗口CWndDirectUI 。在编译前确保你的资源里有名为IDC_MYHAND的hand cursor 。

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.
控件默认使用的是style "Themed"。你可以手动改变其风格,仅需调用SetStyle() using CWndDirectUI::styleOffice, CWndDirectUI::styleXPclassic, CWndDirectUI::styleXP or CWndDirectUI::styleThemed. For retrieving the current style use GetStyle().不要乱改风格,以免没有伸缩按钮的风格丢失该按钮。

For manual expand/collapse use the functions ExpandGroup(groupNr, expand).
手动伸缩使用函数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.
填充控件的最佳方式为使用函数InitFromMenu(UINT id). 传递一个包含2d menu的menu id(无子菜单)。如果你想的话可以用SetToolbarImages(UINT uiToolbar)通过简单的传递一个toolbar id来插入toolbar icons 。请把所以项目插入一个toolbar中,因为目前你只能用一个toolbar来实现这功能。顺便说一句,如果控件中有太多项目要显示,scrollbars 会自动显示 。

 

{
  ...

  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:
超过16色请使用带以下参数的**函数。

 

  • 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.
要手动增加组和项目请看类定义和函数AddGroup(), AddItemCommand() 因此用户每一步只需简单设定一个新列表,这就是全部所需。当用户选择列表中的一个项目时,你的应用程序的操作就像普通菜单操作一样。

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 a multiline test 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值