DOCX

Hands-On Lab:Taskbar with Visual C++ and MFC

By David Sullivan,2014-09-23 10:17
7 views 0
Hands-On Lab:Taskbar with Visual C++ and MFC

    Hands-On Lab Taskbar with Visual C++ and MFC

Lab version: 1.0.0

    Last updated: 9/23/2011

CONTENTS

    OVERVIEW ............................................................................................................................................. 3

    EXERCISE 1: EXPERIMENT WITH THE NEW WINDOWS 7 TASKBAR FEATURES ............................. 5

    Task 1Using Taskbar Overlay Icons ............................................................................................... 6

    Task 2Using Taskbar Progress Bars ............................................................................................... 8

    Task 3Using Taskbar Jump Lists .................................................................................................. 10 EXERCISE 2: SUPPORTING TASKBAR THUMBNAILS ...................................................................... 15

    Task 1Supporting Taskbar Thumbnails ........................................................................................ 16 SUMMARY ............................................................................................................................................ 17

Overview

    The new Windows 7 Taskbar represents the culmination of many years of Windows launch-surface evolution. The new taskbar streamlines many end-user scenarios including:

    ; Launching applications

    ; Switching between running applications and windows within a single application

    ; Managing recent/frequent user destinations

    ; Accessing common application tasks

    ; Reporting progress and status notifications through the taskbar button

    ; Controlling the application without leaving the taskbar thumbnail

Figure 1

    The new Windows 7 Taskbar and Start Menu

    The new taskbar is a differentiating opportunity that allows applications to shine on the Windows 7 platform. It is the end user’s primary point-of-contact for initiating and managing activities. Thus, the integration of new taskbar features into modern Windows 7 applications is a critically important goal.

    The consolidation of features into the new taskbar user interface is designed to provide a cleaner look to Windows, reduce the number of concepts users must learn in order to interact with the system, ease discoverability of key features, tasks and destinations, and make common end-user scenarios more accessible.

    Objectives

    In this Hands-On Lab, you will learn how to integrate your MFC application with the Windows 7 Taskbar, including how to:

    ; Provide visual progress and status indicators using taskbar progress bars and overlay icons

    ; Quickly access common tasks and frequent destinations using taskbar jump lists, system

    categories and custom categories

    ; Customize the taskbar thumbnail with live previews of MDI views

System Requirements

    You must have the following items to complete this lab:

    ; Microsoft Visual Studio 2010

    ; Windows 7

    ; Windows 7 SDK

    Exercise 1: Experiment with the New Windows 7 Taskbar Features

    In this exercise, you will experiment with the new Windows 7 taskbar features. You will extend a showcase application that demonstrates the use of the new taskbar functionality to provide a taskbar overlay icon, progress bar, jump list, and thumbnail previews. Most of the application’s user interface is

    already implemented; you will need to fill in only the missing parts that interact with the Windows 7 taskbar using Visual C++ MFC (Microsoft Foundation Class Library).

    To begin this exercise, open the TaskbarDemo_Starter\TaskbarDemo.sln solution (under the Source

    folder) using Visual Studio 2010.

Figure 2

    Taskbar Concepts solution structure in Visual Studio 2010

    Spend a minute or two exploring the header, source and resource files. Take a look at the various dialogs and main form. Take a few moments to build and run the application as well. Task 1Using Taskbar Overlay Icons

    In this task, you will toggle an overlay icon on the application’s taskbar button when a user clicks an icon

    button.

    Note that in MFC, you can set an overlay icon by using CFrameWnd::SetTaskbarOverlayIcon(). To clear the Taskbar overlay icon, pass NULL (0) to SetTaskbarOverlayIcon().

    1. In the TaskbarDemo\SourceFiles sub tree, open OverlayIconTab.cpp and look for the message

    handler for the Info overlay icon, COverlayIconTab::OnOverlayIconInfo(). We’ll use this

    message handler to add the code necessary to set the same icon in the button as a Taskbar

    overlay icon.

    2. Add the following code to COverlayIconTab::OnOverlayIconInfo():

    C++

    CMainFrame* mainFrm = dynamic_cast(AfxGetApp()->GetMainWnd());

    if (mainFrm)

     mainFrm->SetTaskbarOverlayIcon(IDI_ICON_INFO,L"Info");

    3. Compile and run the application.

    4. Press the information button ().

    5. Notice that the display on the taskbar has changed, from the original (), and now contains an

    overlay of the information button () superimposed on it (see Figure 3 below).

Figure 3

    Taskbar overlay icon shown

    6. Continue adding the necessary code for the rest of the icon overlay message handlers:

    C++

    void COverlayIconTab::OnOverlayIconQuestion()

    {

     // TODO: Add code to set question overlay icon

     CMainFrame* mainFrm = dynamic_cast(AfxGetApp()->GetMainWnd());

     if (mainFrm)

     mainFrm->SetTaskbarOverlayIcon(IDI_ICON_QUESTION, L"Question"); }

void COverlayIconTab::OnOverlayIconNoVolume()

    {

     // TODO: Add code to set no-volume overlay icon

     CMainFrame* mainFrm = dynamic_cast(AfxGetApp()->GetMainWnd());

     if (mainFrm)

     mainFrm->SetTaskbarOverlayIcon(IDI_ICON_NO_VOLUME,L"No Volume"); }

    void COverlayIconTab::OnOverlayIconNone()

    {

     // TODO: Add code to clear overlay icon

     CMainFrame* mainFrm = dynamic_cast(AfxGetApp()->GetMainWnd());

     if (mainFrm)

     mainFrm->SetTaskbarOverlayIcon((HICON)0, L"");

    }

    7. Build and run the demo to test the remaining icon overlays.

    Task 2Using Taskbar Progress Bars

    In this task, you will set the state and value of the application’s taskbar progress bar when the user

    selects the progress state from a combo box or changes the value by using a slider. MFC provides a

    number of methods to update the Taskbar progress bar’s value and status:

    ; CFrameWnd::SetProgressBarRange()

    ; CFrameWnd::SetProgressBarPosition()

    ; CFrameWnd::SetProgressBarState()

1. In the TaskbarDemo\SourceFiles sub tree:

    a. Open the file ProgressBarTab.cpp, and

    b. Look for the slider control’s message handler, CProgressBarTab::OnHScroll()

    c. Add the following code to update the Taskbar’s progress bar after the comment to add

    code to handle the slide bar change:

    C++

    UpdateData();

    CMainFrame* mainFrm = dynamic_cast(AfxGetApp()->GetMainWnd());

    mainFrm->SetProgressBarPosition(m_ProgressSliderValue);

2. Build and run the application, and then:

    a. Go to the Progress Bar tab.

    b. Move the slider, and notice the movement on the application’s Taskbar button.

    You should see an output similar to Figure 4 below:

Figure 4

    Slider bar and taskbar button progress indicators

    3. Now, we want to update the status of the progress bar whenever the relevant combo box

    option is updated, so we add the necessary code to the CProgressBarTab::OnCbnSelchangeProgressStatusCombo() event handler: C++

    CMainFrame* mainFrm = dynamic_cast(AfxGetApp()->GetMainWnd());

switch (m_ProgrssStatusCombo.GetCurSel())

    {

    case 0 :

     mainFrm->SetProgressBarState(TBPF_NORMAL);

     break;

    case 1 :

     mainFrm->SetProgressBarState(TBPF_ERROR);

     break;

    case 2 :

     mainFrm->SetProgressBarState(TBPF_PAUSED);

     break;

    case 3 :

     mainFrm->SetProgressBarState(TBPF_INDETERMINATE);

     break;

    case 4 :

     mainFrm->SetProgressBarState(TBPF_NOPROGRESS);

     break;

    default:

     mainFrm->SetProgressBarState(TBPF_NORMAL); }

4. Build and run the application, and then:

    a. Navigate to the Progress Bar tab, and

    b. Select a different style from the Progress Status Combo and then

    c. Move the progress slider. The progress bar state will be changed to the appropriate

    style; namely, the taskbar’s progress bar is red when the Progress Status is set to Error,

    the taskbar’s progress bar is green when the Progress Status is set to Normal (check out

    the other styles too):

    Figure 5

    Red progress bar indicating error

    5. Finally, this last step is optional, but you might want to reset the status combo to ‘Normal’

    whenever the progress is updated from No Progress or Intermediate.

    To fix this, add the following at the end of CProgressBarTab::OnHScroll():

    C++

    if (m_ProgressSliderValue > 0 && m_ProgrssStatusCombo.GetCurSel() >= 3) // No

    progress or intermediate

    {

     // Set to something else other than no progress

     m_ProgrssStatusCombo.SetCurSel(0); // Normal

    }

    Task 3Using Taskbar Jump Lists

    In this task, you will add the functionality to support adding known categories, custom categories,

    destinations, and tasks to the application’s jump list. To support this, MFC provides a new class,

    CJumpList, that allows manipulating the categories and entries on the Taskbar application’s JumpList.

    1. In the TaskbarDemo\SourceFiles sub tree, open the file JumpListTab.cpp and look for the

    implementation of CJumpListTab::UpdateRegistration(). This method executes another process

    to register needed file associations. The file association registration does not need to be in

    another process; however it was placed in another process to keep this tutorial application

    simple and focused.

    Note: We need to register the file associations because an application must be a registered file

    type handler for an item of that type to appear in its Jump List. It does not, however, need to

    be the default handler for that file type.

Report this document

For any questions or suggestions please email
cust-service@docsford.com