Developing for the Windows 7 Taskbar – Application ID

This is the first post in a new Windows 7 Taskbar series. One of the first Windows 7 changes that developers should pay attention to is the new Windows Taskbar. We all need to understand the functionality this feature introduces so that we can ensure that our applications work well with the Taskbar, resulting in an enhanced experience for our end users.

I assume that by now you are familiar with the basic functionality that Windows 7 Taskbar offers and the reasons behind the change we made from previous taskbar versions. If you are not familiar with and haven’t seen any demonstrations of the Windows 7 Taskbar, please watch the Windows 7 Taskbar Overview webcast on Channel 9. There are also great posts on the E7 blog like The Windows 7 Taskbar about some of the reasons we introduced the new Taskbar and desktop experience in Windows 7. I do encourage you to read these posts and watch the video so that you have some context for the technical material we are going to cover here.

The new Taskbar is probably the most noticeable change to Windows 7 when you first log on. The Windows 7 Taskbar is an application-launching and window-switching mechanism that consolidates the functions of previous Windows Desktop mechanisms, such as Quick Launch, Recent Documents, Notification Area icons, desktop shortcuts, and running application windows. Windows 7 Taskbar offers features like Jump Lists, Preview Handler, and Overlay Icons. But before we start diving into the various Windows 7 Taskbar features, let’s lay the basic foundation to our discussion and define some Windows 7 taskbar terminology.

The basic component is the Taskbar button. The Taskbar button is represented as an icon displayed on the Taskbar. As you can see in the following image, the Taskbar contains several buttons. You can tell what their status is by how they are displayed.Taskbar_buttons1

For example, the Media Player button has no frame or border, but simply sits directly on the Taskbar. This indicates that Media Player is not running. But it is pinned to the Taskbar, and will stay on the Taskbar unless we unpin it. A transparent frame that lets most of the color of the underlying Taskbar through the Windows Explorer button. This indicates that the app is running but is not the active application. The Visual Studio icon has a more opaque rectangular frame underlying its icon indicating that the user is actively using this app. You will also notice that Word has a “stack” of icons representing that multiple instances of Word or multiple Windows are grouped under the same Taskbar button. It is very important to understand the logic that underlies the creation, assignment, and grouping of Taskbar buttons.

A very large number of applications that run on Windows 7 (for example, Office Word 2007 and Visual Studio 2008), were not designed to work with the Windows 7 Taskbar, so how is it that they can play ball with the Taskbar, group multiple instances, and even take advantage of Word Jump Lists? Basically, a behind-the-scenes Application ID (AppID) is automatically computed and assigned to an application once you launch it. Every running application has an AppID assigned to it, either automatically computed for the app by Windows, or set by the app itself. Guess what? It is not a GUID; it is just a string (with a maximum of 128 characters), that either you provide or is being computed by the OS. All windows and applications, including Jump Lists, which have the same AppID are grouped under the same Taskbar button. Therefore, it is important to understand that every component (process, shortcut, window, Taskbar button, and document type – that is, registered file type handler) of your application has the AppID associated with it.

You may ask, "Where do AppIDs come from?" As mentioned before, the OS generates Application IDs for your application using a very simple, yet important to understand heuristic. Since in Windows 7 you can assign AppIDs to individual windows, the OS tries to extrapolate the AppID from a window. Applications usually display at least one window that the OS can query for its AppID. However, most existing apps do not have an AppID attached to each window (or, for that matter, any AppID at all), and therefore the OS falls back to the process to which the window belongs for the AppID. Each process has several properties that the OS can check, like the executable of the process. But even then, the process may not provide a granular enough separation. Different shortcuts may provide different start up command line parameters to the same executable and launch different applications (imagine a “launcher” application) that will be grouped under the same Taskbar button. In such cases, the OS has the ability to look into the specific shortcut that launched the application, to find the executable, the command line parameters, and so on. Note that if you have a register file, this registration points to an application that gets launched once you double click that file. Again, this is another way to calculate the AppID. The following image illustrates this automatic computation process. image

If you are interested in really understanding the internals of the Windows 7 taskbar, a great Channel 9 video describes its underlying architecture. Watch the Rob Jarrett and Ben Bets Windows 7 Taskbar - Behind the Scenes video and, specifically for the Application ID overview, watch Jump into the Windows 7 Taskbar Jump Lists (between 29:30 and 34:40).

While the OS can compute AppIDs for you, you may want to have greater control over the AppID for a given application or even an individual window in your application. Assume that you have an application that hosts (runs) another application (like what happens when you debug an application using Visual Studio). Or you have several different applications or processes that you wish to group under the same Taskbar button. The Taskbar API offers you ways to control the Application ID per application or per window. IN ANY CASE, if you are writing a new application that targets Windows 7, we highly recommend that you provide your own application ID, as we will describe below.

OK, let’s examine the API that allows you to control the AppID associations of your application.

If you want to have a separate Taskbar button for each process (including all windows owned by that process), you can set an explicit AppID for the entire process that affects all windows within the process that do not have their own explicit AppID. Setting the explicit process AppID is very easy. All it takes is a single call to the SetCurrentProcessExplicitAppUserModelID function as shown in the following code snippet:

SetCurrentProcessExplicitAppUserModelID(c_rgszAppID[0]);
Where c_rgszAppID[0] is a pointer to a string. You should note that according to the SDK documentation, “This method must be called during an application's initial startup routine before the application presents any user interface (UI) or makes any manipulation of its Jump Lists.”

In managed code, from the latest Windows API Code Pack Library, you can use the AppID property that is part of the Taskbar object, which you can find in the Microsoft.WindowsAPICodePack.Shell.Taskbar namespace. Using that property you can set and get the application ID of a given application.

Setting the AppID for a window is a bit more complicated (but only a bit). It requires calling the SHGetPropertyStoreForWindow function and then manipulating the resulting IPropertyStore object to retrieve the requested property as shown in the following code snippet:

void SetAppID(HWND hWnd, int iAppID)
{
IPropertyStore *pps;
HRESULT hr = SHGetPropertyStoreForWindow(hWnd, IID_PPV_ARGS(&pps));
if (SUCCEEDED(hr))
{
PROPVARIANT pv;
if (iAppID >= 0)
{
hr = InitPropVariantFromString(c_rgszAppID[iAppID], &pv);
}
else
{
PropVariantInit(&pv);
}
if (SUCCEEDED(hr))
{
hr = pps->SetValue(PKEY_AppUserModel_ID, pv);
PropVariantClear(&pv);
}
pps->Release();
}
}

Here you can see how we extract the current windows property store by calling SHGetPropertyStoreForWindow, passing hWnd as refrence to the window. Next we initiate, InitPropVariantFromString(c_rgszAppID[iAppID], &pv), a property variant with a string that represents the AppID for that window. Finally, we set the value of the new property store to the window.

Unfortunately, the current Windows API Code pack doesn’t support setting specific application IDs per window, although all you need to do is add the following function to the Taskbar.cs file:

private static void SetWindowAppId(string appId)
{
Microsoft.WindowsAPICodePack.Shell.ShellNativeMethods.SetWindowAppId
(OwnerHandle, "name here");
}

Since the Windows API Code Pack provides the source code, you can actually check the specific implementation of SetWindowAppId function and see for yourself that it is very similar to the SetAppID example above. Note you don’t have to use the full qualified assembly name “Microsoft.WindowsAPICodePack.Shell’, but I did to help you navigate the Windows API Code Pack hierarchy.

By the way, the window AppID is dynamic, so it’s entirely possible for a window to show up as part of one Taskbar button and then change its AppID so that it appears on an entirely different Taskbar button. This has interesting effects. For example, the Jump List is attached to a Taskbar button (with a specific AppID), so the same window might show a different Jump List when it is reattached to an entirely different Taskbar button. This potentially can confuse users, so the recommended practice is to set the window AppID and stick to it, using the same process for determining the AppID every time the window displays.

You can find the managed code Taskbar object in the Windows API Code pack project.

[Sasha Goldshtein helped write this post, and you can find his original posting here].


Comments

  1. Posted on: June 18, 2009 at 2:32PM  

    The new taskbar has been successful in accomodating more programs compared to the old one which got filled up far too quickly. However, I am not happy about 2 things with the new taskbar.

    1.  You can't separate two windows from the same AppID and put another app window in between the two.

    2.  Jump lists don't contain minimize and maximize and Shift+right click is not very convenient.

  2. Posted on: June 19, 2009 at 12:24PM  

    to 7flavor.

    Yes you can. See the second post on AppID.

  3. Posted on: June 21, 2009 at 12:57PM  

    You can have the same result with  managed code using the Windows API Code pack project Taskbar object, and then call the SetWindowAppId function that i showed above.

  4. Posted on: August 03, 2009 at 3:35AM  

    Hi,

    First of all I must tell that Windows7 has great toolbar - I was trying to create something like this for 3 years (but had no time). Now everythign (and even more) is there. Good job! (If only the price of Win7 could be lower...)

    Then some feedback about the toolbar (I'm using Win7 RC: bld 7100):

    1. I'm using 2 graphic cards at the same time: one is nVidia, second is AMD. And I have located some issue when playing with the display settings. The toolbar has a frame drawed bellow it and under some circumstances (moving it across the screen) it can stuck (so I can end with background frame around my desktop). How would I report such a problem?

    You should provide some way of problem reporting for end-clients where they could write what is wrong with screenshots. Not only for the situation when app stopped unexpected.

    2. I have IE, Opera, Firefox and Chrome sticket to my taskbar. IE is able to display the windows/tabs but I cannot tell which tab belongs to which window. Maybe you could make the preview thumbnails showcase a grid?

    3. Something that I am missing on MSDN web is a tutorial for very beginners. People who do not know how to write code. They have started they adventure with C++ or C# and... writing things with "wrong approach". You have high chance that they will work on Windows code some day :) Why not give them a tutorial with very very basic guide to programming using WinAPI? Except that I love MSDN :)

Trackbacks

  1. Posted by: Developing for the Windows 7 Taskbar ??? Application ID « News.ProgramsDB.Net on June 18, 2009 at 3:00PM

    Pingback from  Developing for the Windows 7 Taskbar ??? Application ID « News.ProgramsDB.Net

  2. Posted by: RSS Digest #87 – Twitter Chess, Win7 Starter no OEM Branding, and other great stuff… on June 18, 2009 at 11:02PM

    Pingback from  RSS Digest #87 – Twitter Chess, Win7 Starter no OEM Branding, and other great stuff…

  3. Posted by: Developing for the Windows 7 Taskbar ??? Application ID - Windows 7 … on June 18, 2009 at 11:50PM

    Pingback from  Developing for the Windows 7 Taskbar ??? Application ID - Windows 7 …

  4. Posted by: Developing for the Windows 7 Taskbar ??? Application ID - Windows 7 … | All about windows 7 on June 19, 2009 at 12:31AM

    Pingback from  Developing for the Windows 7 Taskbar ??? Application ID - Windows 7 … | All about windows 7

  5. Posted by: Developing for the Windows 7 Taskbar ??? Application ID - Windows 7 … | Current Technology Updates daily on June 19, 2009 at 2:35AM

    Pingback from  Developing for the Windows 7 Taskbar ??? Application ID - Windows 7 … | Current Technology Updates daily

  6. Posted by: Developing for Windows 7 Taskbar – Application ID (AppID) on June 19, 2009 at 5:26AM

    Pingback from  Developing for Windows 7 Taskbar – Application ID (AppID)

  7. Posted by: Dew Dump – June 19, 2009 | Alvin Ashcraft's Morning Dew on June 19, 2009 at 8:29PM

    Pingback from  Dew Dump – June 19, 2009 | Alvin Ashcraft's Morning Dew

  8. Posted by: Developing for the Windows 7 Taskbar ??? Application ID|Join Our Story!|AngNetwork Blog on June 21, 2009 at 1:49PM

    Pingback from  Developing for the Windows 7 Taskbar ??? Application ID|Join Our Story!|AngNetwork Blog

  9. Posted by: Windows 7 for Developers on June 22, 2009 at 7:07PM

    Welcome to yet another Windows 7 Taskbar post. In the previous post, Developing for the Windows 7 Taskbar

  10. Posted by: Developing for the Windows 7 Taskbar ??? Jump into Jump Lists ??? Part 1 | Windows Seven 7 on June 22, 2009 at 8:02PM

    Pingback from  Developing for the Windows 7 Taskbar ??? Jump into Jump Lists ??? Part 1 | Windows Seven 7

  11. Posted by: Developing for the Windows 7 Taskbar ??? Application ID - Windows 7 … | Tech Talk on June 23, 2009 at 6:28AM

    Pingback from  Developing for the Windows 7 Taskbar ??? Application ID - Windows 7 … | Tech Talk

  12. Posted by: Developing for the Windows 7 Taskbar ??? Jump into Jump Lists ??? Part 2 | Windows Seven 7 on June 25, 2009 at 9:06PM

    Pingback from  Developing for the Windows 7 Taskbar ??? Jump into Jump Lists ??? Part 2 | Windows Seven 7

  13. Posted by: Developing for the Windows 7 Taskbar ??? Jump into Jump Lists ??? Part 2 | Windows 7 Information, News, Downloads, Support Forums on June 26, 2009 at 10:29AM

    Pingback from  Developing for the Windows 7 Taskbar ??? Jump into Jump Lists ??? Part 2 | Windows 7 Information, News, Downloads, Support Forums

  14. Posted by: Developing for the Windows 7 Taskbar ??? Jump into Jump Lists ??? Part 1 | Everything Microsoft on July 02, 2009 at 1:19AM

    Pingback from  Developing for the Windows 7 Taskbar ??? Jump into Jump Lists ??? Part 1 | Everything Microsoft

  15. Posted by: TechFriday: Jumping into Jumplists | Aimee Gurl... on July 09, 2009 at 12:48PM

    Pingback from  TechFriday: Jumping into Jumplists | Aimee Gurl...

  16. Posted by: Developing for the Windows 7 Taskbar – Jump into Jump Lists – Part 1 | Futurecat - Michael Karek on July 10, 2009 at 9:03PM

    Pingback from  Developing for the Windows 7 Taskbar – Jump into Jump Lists – Part 1 | Futurecat - Michael Karek

  17. Posted by: Jocelyn's Blog on July 14, 2009 at 9:58PM

    Everybody’s excited about Windows 7 and there might be a few of you guys out there wanting to take advantage

  18. Posted by: Windows 7 for Developers on July 23, 2009 at 3:29AM

    Yesterday Windows 7 completed a major milestone-- release to manufacturing (RTM)! And in three months

  19. Posted by: Is Your Application Ready for Windows 7 RTM? | Windows Seven 7 on July 23, 2009 at 3:44AM

    Pingback from  Is Your Application Ready for Windows 7 RTM? | Windows Seven 7

  20. Posted by: lzhdim on July 23, 2009 at 7:19PM
  21. Posted by: Windows 7 Applications experience and performance on July 23, 2009 at 8:26PM

    Pingback from  Windows 7 Applications experience and performance

  22. Posted by: Windows 7 RTM?????? ?????????????????????????????? - ????????????????????? on July 23, 2009 at 9:31PM

    Pingback from  Windows 7 RTM?????? ?????????????????????????????? - ?????????????????????

  23. Posted by: Windows 7 RTM?????? ?????????????????????????????? at HI7 on July 23, 2009 at 11:01PM

    Pingback from  Windows 7 RTM?????? ?????????????????????????????? at HI7

  24. Posted by: Windows 7 RTM?????? ?????????????????????????????? - ???windows 7????????? on July 24, 2009 at 7:16AM

    Pingback from  Windows 7 RTM?????? ??????????????????????????????  - ???windows 7?????????

  25. Posted by: LCD-TV???STB?????????????????? | tobe123.com on July 27, 2009 at 8:16AM

    Pingback from  LCD-TV???STB?????????????????? | tobe123.com

  26. Posted by: Windows 7 for Developers on July 28, 2009 at 12:31PM

    We covered the basics of the Windows 7 Taskbar in Developing for the Windows 7 Taskbar – Application

  27. Posted by: Windows 7 Taskbar Dynamic Overlay Icons and Progress Bars | Windows Seven 7 on July 28, 2009 at 1:33PM

    Pingback from  Windows 7 Taskbar Dynamic Overlay Icons and Progress Bars | Windows Seven 7

  28. Posted by: Windows 7 Taskbar Dynamic Overlay Icons and Progress Bars | Windows 7 Information, News, Downloads, Support Forums on July 29, 2009 at 1:58AM

    Pingback from  Windows 7 Taskbar Dynamic Overlay Icons and Progress Bars | Windows 7 Information, News, Downloads, Support Forums

  29. Posted by: KoBeBoy » Blog Archive » ????????????????????? on July 29, 2009 at 3:15AM

    Pingback from  KoBeBoy  » Blog Archive   » ?????????????????????

  30. Posted by: Windows7????????????,??????pc???????????????? | WWW.211B.COM?????????DIY????????? on August 03, 2009 at 7:35AM

    Pingback from  Windows7????????????,??????pc???????????????? | WWW.211B.COM?????????DIY?????????

  31. Posted by: Developing for the Windows 7 Taskbar ??? Jump into Jump Lists ??? Part 1 | Tom Richardson on August 09, 2009 at 4:49PM

    Pingback from  Developing for the Windows 7 Taskbar ??? Jump into Jump Lists ??? Part 1 | Tom Richardson