Windows 7 Program Development Series (taskbar)

Source: Internet
Author: User

Windows 7 introduces many new features, the most intuitive of which is the changes on the user interface. Many people return to XP because they cannot adapt to this change. However, in my opinion, these new features are a kind of progress. After using them for a while, they also have the impulse to do some development. Therefore, the previous power management software was rewritten once (Click here to download), and Windows 7 taskbar features and jumplist were used.

There are few Chinese documents about Windows 7 development. Microsoft official tutorials are still rich, but they are all in English. It may be difficult for some friends who are not very good at English to learn. I will give a short tutorial on the task bar and jumplist, hoping to help later.

The task bar is a little simpler than jumplist. Let's start with the task bar. The taskbar of Windows 7 includes several new features: progress bar (progress bar), overlay icon (overlay icon), Thumbnail (thumbnail), and thumbnail toolbar (toolbar located below the thumbnail) tooltip, tooltip, and aero peek Preview (display window preview when the mouse is parked on the thumbnail ). Below is a foobar2000 runtime:

In foobar2000, use the progress bar to display the playing progress of the current song, and there is a white triangle (overlay icon) in the lower right corner to show whether the current playing is paused, and use thumbnail to display the album cover, the thumbnail toolbar has three buttons: Previous, paused, and next. The tooltip on the top shows the current Playing Track. When you place the cursor over the thumbnail, the aero peek function hides all windows, only the preview of the current window is displayed. The above progress bar and overlay icon are not clear, and the following are clear:

This articleArticle.

It is easy to find the SDK on msdn, so I will not post it. The SDK has 1.44 GB, which takes a little time to download. There is nothing to say about the installation process. After installation, find Microsoft Windows SDK v7.0> Visual Studio registration> Windows SDK Configuration tool in the Start menu and set v7.0 to the current version, in this way, the Windows SDK in Vs will use v7.0. All functions related to the taskbar buttons are included in this interface, such as progress bar and overlay icon. Create a Win32 project and create a simple window.Code. Register a "taskbarbuttoncreated" message at the beginning of the winmain function,

// Register User message

Wm_taskbarbuttoncreated =: registerwindowmessage (text ("taskbarbuttoncreated "));

In this way, we can receive the registered wm_taskbarbuttoncreated message in wndproc. In this message, create an itaskbarlist4 interface object and call the initialization method. The com-related content is beyond the scope of this tutorial. If you are interested, refer to relevant materials.

// Create the interface object itaskbarlist

If(MSG = wm_taskbarbuttoncreated)

{

: Cocreateinstance (clsid_taskbarlist, null, clsctx_inproc_server, iid_ppv_args (& g_ptaskbar ));

G_ptaskbar-> hrinit ();

} The setprogressstate method sets the progress bar status, which is actually the color of the progress bar. It accepts a window handle and a status mark as a parameter. Tbpf_noprogress indicates the progress bar to be canceled, while tbpf_indeterminate indicates a continuously rolling progress bar without a definite value.

The setprogressvalue method sets the progress bar value, that is, the current progress. Its parameters are window handle, current value, and total value. If you use this method to set the progress bar, the previously set tbpf_indeterminate will be invalid.

Setoverlayicon sets the overwrite icon. Its parameters are window handle, icon handle, and description. The icons must be 16x16 in size. If the icon handle is null, The overwrite icon is canceled.

After the g_ptaskbar object is initialized, add the following code:

G_ptaskbar-> setprogressstate (g_hwnd, tbpf_error );

G_ptaskbar-> setprogressvalue (g_hwnd, 50,100 );

G_ptaskbar-> setoverlayicon (g_hwnd, g_hred, text ("error "));

This Code sets the progress bar status to error (red), progress to 50%, and a red icon in the lower right corner (the icon has been loaded in the winmain function ). The effect after running is as follows:

Toolbar is a little more troublesome. You need to set a thumbbutton structure. The dwmask parameter is a combination of some tags. Here we set it to thb_icon | thb_tooltip, that is, we will use the icon and tooltip fields (note that the tooltip here is the tooltip of the button, do not confuse with the preceding tooltip ). IID is related to the Event Response and can be numbered sequentially. Hicon is the icon handle used by this button. Set the prompt information in sztip. Use the following code to add three buttons:

Thumbbuttonmaskdwmask = thb_icon | thb_tooltip;

Thumbbuttonthbbuttons [3];

Thbbuttons [0]. dwmask = dwmask;

Thbbuttons [0]. IID = 0;

Thbbuttons [0]. hicon = g_hred;

Stringcbcopy (thbbuttons [0]. sztip,Sizeof(Thbbuttons [0]. sztip), text ("red "));

Thbbuttons [1]. dwmask = dwmask;

Thbbuttons [1]. IID = 1;

Thbbuttons [1]. hicon = g_hgreen;

Stringcbcopy (thbbuttons [1]. sztip,Sizeof(Thbbuttons [1]. sztip), text ("green "));

Thbbuttons [2]. dwmask = dwmask;

Thbbuttons [2]. IID = 2;

Thbbuttons [2]. hicon = g_hblue;

Stringcbcopy (thbbuttons [2]. sztip,Sizeof(Thbbuttons [2]. sztip), text ("blue "));

G_ptaskbar-> thumbbaraddbuttons (g_hwnd, arraysize (thbbuttons), thbbuttons );

The effect is as follows:

The button is ready, and the message that responds to these buttons is displayed. After you click these buttons, Windows will send a wm_command message, where the wparam high is thbn_clicked, and the low is the button ID (set by the IID parameter ).

CaseWm_command:

If(Hiword (wparam) = thbn_clicked)

{

Onthumbnailbuttonclicked (loword (wparam ));

}

Break;

VoidOnthumbnailbuttonclicked (IntID)

{

Switch(ID)

{

Case0:

Setforegroundwindow (g_hwnd );

MessageBox (g_hwnd, text ("red button clicked! "), Null, mb_ OK );

Break;

Case1:

Setforegroundwindow (g_hwnd );

MessageBox (g_hwnd, text ("green button clicked! "), Null, mb_ OK );

Break;

Case2:

Setforegroundwindow (g_hwnd );

MessageBox (g_hwnd, text ("blue button clicked! "), Null, mb_ OK );

Break;

}

}

Click the button on the toolbar to display the corresponding message box.

Tooltip is relatively simple. Setthumbnailtooltip can pass in the text of the window handle and description. Add the following code to wm_taskbarbuttoncreated message processing:

G_ptaskbar-> setthumbnailtooltip (g_hwnd, text ("some information "));

The information prompt is displayed on the preview window:

So far, thisProgramThe thumbnails and aero peek previews are still controlled by the system. For most programs, the thumbnails and previews automatically generated by the system are sufficient. But sometimes we want to control it by ourselves. For example, foobar2000 needs to display the album cover in the thumbnail (see the first figure ).

To achieve manual control, you need to set window properties after the window is created, telling windows that the thumbnails and preview charts of the window need to be provided by the program. Add the following code after the window is created:

// Set window properties to open the custom thumbnail and aeropeek Preview

Booltruth = true;

Dwmsetwindowattribute (g_hwnd, dwmwa_has_iconic_bitmap, & truth,Sizeof(Truth ));

Dwmsetaskwattribute (g_hwnd, dwmwa_force_iconic_representation, & truth,Sizeof(Truth ));

After this setting, you can receive the messages wm_dwmsendiconicthumbnail and wm_dwmsendiconiclivepreviewbitmap in wndproc. They are messages sent by windows to the program for thumbnail and aero peek preview. For the first message, the high and low positions of the parameter wparam indicate the maximum width and height of the requested thumbnail, respectively. In message processing, a bitmap handle (hbitmap) is sent to the system through dwmseticonicthumbnail. The bitmap size required by the system cannot exceed the maximum width and height passed in the parameter, and must be a 32-Bit Bitmap, but the general software can only save 24-bit bitmaps, this bitmap does not meet the requirements and cannot be displayed. Therefore, I used GDI + to write two auxiliary functions for loading resources in PNG format into hbitmap and scaling the size.

The first function is to load resources in PNG format. The parameters are: module handle (if this module is used, getmodulehandle (null) can be used to obtain the handle), Resource ID, and resource type. This code loads resources into the memory and creates a bitmap object from this memory.

// Load resources in PNG or other formats as gdiplus: bitmap

Gdiplus: bitmap * loadbitmapfromresource (hmodulehmodule, uintresid, lpctstrrestype)

{

// Allocate a memory and copy the loaded resources to the memory.

// Obtain the resource handle

Hrsrchrsrc = http://blog.soso.com/qz.q/FindResource (hmodule, makeintresource (resid), restype );

If(Hrsrc = http://blog.soso.com/qz.q/=NULL)

{

ReturnNULL;

}

// Resource size

IntSize = sizeofresource (getmodulehandle (null), hrsrc );

// Load Resources

Hglobalhglobal = loadresource (getmodulehandle (null), hrsrc );

If(Hglobal = NULL)

{

ReturnNULL;

}

// Allocate a memory of the same size

Hglobalhglobal2 = globalalloc (gmem_moveable, size );

If(Hglobal2 = NULL)

{

Freeresource (hglobal );

ReturnNULL;

}

// Lock Resources

Lpvoidlpvoid = lockresource (hglobal );

If(Lpvoid = NULL)

{

Freeresource (hglobal );

Globalfree (hglobal2 );

ReturnNULL;

}

// Lock memory

Lpvoidlpvoid2 = globallock (hglobal2 );

If(Lpvoid2 = NULL)

{

Unlockresource (hglobal );

Freeresource (hglobal );

Globalfree (hglobal2 );

ReturnNULL;

}

// Copy the resource to the memory

Memcpy (lpvoid2, lpvoid, size );

// Unlock

Globalunlock (hglobal2 );

Unlockresource (hglobal );

// Create a stream

Lpstreamlpstream;

Hresulthr = createstreamonhglobal (hglobal2, true, & lpstream );

If(HR! = S_ OK)

{

Freeresource (hglobal );

Globalfree (hglobal2 );

ReturnNULL;

}

// Create gdiplus Based on the stream: bitmap

Gdiplus: bitmap * BMP = gdiplus: bitmap: fromstream (lpstream );

// Release resources and memory

Freeresource (hglobal );

Globalfree (hglobal2 );

ReturnBMP;

}

The second function is to scale the bitmap object. The last parameter can be used to select whether to maintain the aspect ratio.

// Scale gdiplua: bitmap

Gdiplus: bitmap * resizebitmap (gdiplus: bitmap * bmp src,IntDestwidth,IntDestheight,BoolKeepaspect =True)

{

// Obtain the width and height of the source Image

IntSrcwidth = BMP Src-> getwidth ();

IntSrcheight = BMP Src-> getheight ();

// Calculate the horizontal and vertical scaling ratios

FloatScaleh = (Float) Destwidth/srcwidth;

FloatScalev = (Float) Destheight/srcheight;

// If you need to maintain the aspect ratio, a smaller zoom ratio is adopted in both the horizontal and vertical directions.

If(Keepaspect)

{

If(Scaleh> scalev)

{

Scaleh = scalev;

}

Else

{

Scalev = scaleh;

}

}

// Calculate the target width and height

Destwidth = (Int) (Srcwidth * scaleh );

Destheight = (Int) (Srcheight * scalev );

// Create the target bitmap

Gdiplus: bitmap * bmp dest =NewGdiplus: Bitmap (destwidth, destheight, pixelformat32bppargb );

Gdiplus: graphicsgraphic (bmp dest );

// Set InterpolationAlgorithm

Graphic. setinterpolationmode (gdiplus: interpolationmodehighqualitybicubic );

// Draw the source image to the object image

Graphic. drawimage (bmp src, gdiplus: rect (0, 0, destwidth, destheight ),

0, 0, srcwidth, srcheight, gdiplus: unitpixel );

Graphic. Flush ();

ReturnBmp dest;

}

Before calling these two functions, you have to do some work. That is to initialize the GDI +. Add the following initialization code at the beginning of the winmain function:

// Initialize GDI +

Ulong_ptrtoken;

Gdiplus: gdiplusstartupinputinput;

Gdiplus: gdiplusstartup (& token, & input, null );

When the winmain function ends, do not forget to close the GDI +:

// Close the GDI +

Gdiplus: gdiplusshutdown (token );

After the preparation is complete, you can process the thumbnail. Add the following message processing code to the message processing function:

CaseWm_dwmsendiconicthumbnail:

Onsendthumbnail (hwnd, hiword (lparam), loword (lparam ));

Break;

Here we get the width and height required by the thumbnail, and go to the onsendthumbnail function for processing.

VoidOnsendthumbnail (hwndhwnd,IntWidth,IntHeight)

{

// Load the resource as bitmap

Gdiplus: bitmap * BMP = loadbitmapfromresource (getmodulehandle (null), idb_thumb, text ("PNG "));

// Scale bitmap

Gdiplus: bitmap * BMP resized = resizebitmap (BMP, width, height,False);

// Obtain the bitmap handle

Hbitmaphbmp;

BMP resized-> gethbitmap (uint (gdiplus: Color: alphamask), & hbmp );

// Send a bitmap handle

Dwmseticonicthumbnail (hwnd, hbmp, 0 );

// Release the object

Deleteobject (hbmp );

DeleteBMP resized;

DeleteBMP;

}

Because the aspect ratio is not maintained, the following results are obtained:

If the aspect ratio is maintained, the following results are displayed:

Next, process the aero peek preview. The corresponding Windows message is wm_dwmsendiconiclivepreviewbitmap. However, the width and height of the Message Parameter are not provided. However, if the width and height of the preview image we provide exceed the size of the customer area of the window, it cannot be displayed. Therefore, we need to manually obtain the size of the customer area and then scale the image to the size of the customer area. Add the following message processing:

CaseWm_dwmsendiconiclivepreviewbitmap:

Onsendpreview (hwnd );

Break;

VoidOnsendpreview (hwndhwnd)

{

// Obtain the customer region of the window

Rectrcclient;

Getclientrect (hwnd, & rcclient );

// Load the resource as bitmap

Gdiplus: bitmap * BMP = loadbitmapfromresource (getmodulehandle (null), idb_preview, text ("PNG "));

Gdiplus: bitmap * BMP resized = resizebitmap (BMP, rcclient. right-rcClient.left,

Bottom-rcClient.top,False);

// Obtain the bitmap handle

Hbitmaphbmp;

BMP resized-> gethbitmap (uint (gdiplus: Color: alphamask), & hbmp );

// Send a bitmap handle

Dwmseticoniclivepreviewbitmap (hwnd, hbmp, null, 0 );

// Release the object

Deleteobject (hbmp );
DeleteBMP resized;

DeleteBMP;

}

The following result is displayed. the transformer image is a preview image we sent to windows, which is not displayed on the actual program interface.

One thing to note is that Windows will cache thumbnails and will not request thumbnails from the program every time. To update the thumbnails, you need to call the dwminvalidateiconicbitmaps API, in this way, when Windows needs to display the thumbnail again, wm_dwmsendiconicthumbnail will be sent to request the thumbnail to the application.

So far, all the functions mentioned above have been implemented. There are several header files that need to be included in the program:

# Include <shobjidl. h>

# Include <dwmapi. h>

# Include <tchar. h>

# Include <strsafe. h>

# Include <gdiplus. h>

The libraries to be linked during compilation are: dwmapi. Lib gdiplus. Lib

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.