Windows 7 Program Development Series II (jumplist Article 2-destination)

Source: Internet
Author: User

In addition to user tasks, jumplist also has destination. Unlike user task, destination is a link to the file associated with the program. Destination can also be categorized. Windows has automatically managed "Recent" and "frequently used" categories for us. For example, the Notepad program uses the "Recent" category:

This is sufficient for most programs. However, windows also provides an interface for us to manage our own categories. Programs can add their own categories as needed. This section describes how to add your category to jumplist.

1. Forward
Add a custom category to jumplist.

First, define some files to be put into jumplist:

Lpctstr szfiles [] ={< br/> text ("testfile1.txt"), <br/> text ("testfile2.txt"), <br/> text ("testfile3.txt ") <br/> };

Respond to the key message. When you press "J", create a jumplist.

Case wm_char: <br/> switch (wparam) <br/>{< br/> case 'J': <br/> createjumplist (); <br/> break; <br/>}< br/> break;

// Create destination list <br/> void createjumplist () <br/>{< br/> // create a file <br/> for (INT I = 0; I <arraysize (szfiles); ++ I) <br/>{< br/> createfile (szfiles [I], generic_write, 0, null, create_always, 0, null ); <br/>}< br/> // jumplist <br/> icustomdestinationlist * pcdl = NULL; <br/> hresult hR = cocreateinstance (clsid_destinationlist, null, <br/> clsctx_inproc_server, iid_ppv_args (& pcdl); <br/> If (succeeded (HR )) <br/>{< br/> // beginlist <br/> uint umaxslots; <br/> iobjectarray * poaremoved = NULL; <br/> hR = pcdl-> beginlist (& umaxslots, iid_ppv_args (& poaremoved); <br/> If (succeeded (HR )) <br/> {<br/> // objectcollection <br/> iobjectcollection * POC = NULL; <br/> hR = cocreateinstance (clsid_enumerableobjectcollection, null, <br/> clsctx_inproc_server, iid_ppv_args (& POC); <br/> If (succeeded (HR )) <br/>{< br/> // create shellitem for each file <br/> for (INT I = 0; I <arraysize (szfiles); ++ I) <br/> {<br/> // path of the spliced file <br/> wchar pszpath [max_path]; <br/> wchar pszcurdir [max_path]; <br/> getcurrentdirectory (arraysize (pszcurdir), pszcurdir); <br/> pathcombine (pszpath, pszcurdir, szfiles [I]); <br/> // create a shellitem Based on the file path <br/> ishellitem * psi = NULL; <br/> hR = shcreateitemfromparsingname (pszpath, null, iid_ppv_args (& psi )); <br/> If (succeeded (HR) <br/>{< br/> POC-> addobject (PSI); <br/> psi-> release (); <br/>}< br/> iobjectarray * POA = NULL; <br/> hR = POC-> QueryInterface (iid_ppv_args (& PoA )); <br/> If (succeeded (HR )) <br/>{< br/> // Add the custom category to jumplist <br/> pcdl-> appendcategory (text ("My custom category"), POA ); <br/> PoA-> release (); <br/>}< br/> hR = pcdl-> commitlist (); <br/> POC-> release (); <br/>}< br/> poaremoved-> release (); <br/>}< br/> pcdl-> release (); <br/>}< br/>}

The above code first creates the files defined above in the current folder. The other steps are similar to creating a user task. HoweverIobejctcollection

Which of the following is added to the interface?Ishellitem

Interface insteadIshelllink

Interface. CreateIshellitem

Interface, first obtain the complete path of the file, and then use the API FunctionShcreateitemfromparsingname

Create. ObtainIobjectarray

Call after InterfaceAppendcategory

Add a custom category to jumplist.

Execute the above program and press "J" in the window ". No effect. As mentioned above, the files in destination are the files associated with our applications. We have added three TXT files, but our application is not currently associated with the TXT file. There are still some work to be done below.

II. Application ID

Appid is a string used by windows to identify a program. By default, appid does not need to be set for our program. Windows automatically generates and manages appid for us. However, in some cases, it is better to manage appid by ourselves, such as the file association mentioned above. Only files associated with our program are displayed in destination. In Windows 7, the taskbar button group is also based on appid. In windows with the same appid, their taskbar buttons are divided into groups, even if they are created by different programs. Conversely, windows with different appid, even if they are created by the same program, their taskbar buttons will not be grouped into a group. Next we will do this experiment:

1. Set appid for the application. API functionsSetcurrentprocessexplicitappusermodelid

Appid used to set the application (the function name is a little long ).

2. Set appid for the window. The default appid of the window is the same as the application that created it. Therefore, the two windows created by the same program are grouped into one group in the taskbar (provided that both windows have taskbar buttons ). If we set different appid for the window, these two windows will not be grouped into one group. The appid of the Setting window does not have a direct API function. And settingsIshelllink

The same as the title of the interface, which must be usedIpropertystore

This interface. Write the following function to set the appid of the window.

Void setwndappid (hwnd, lpctstr szappid) <br/>{< br/> ipropertystore * PPS = NULL; <br/> hresult hR = shgetpropertystoreforwindow (hwnd, iid_ppv_args (& PPS); <br/> If (succeeded (HR) <br/>{< br/> propvariant PV; <br/> If (szappid! = NULL) <br/>{< br/> hR = initpropvariantfromstring (szappid, & PV ); <br/>}< br/> else <br/>{< br/> propvariantinit (& PV ); <br/>}< br/> If (succeeded (HR) <br/>{< br/> hR = PPS-> setvalue (pkey_appusermodel_id, PV ); <br/> If (succeeded (HR) <br/>{< br/> PPS-> commit (); <br/>}< br/> propvariantclear (& PV); <br/>}< br/> PPS-> release (); <br/>}< br/>}

This function accepts a window handle and appid (string ). API functionsShgetpropertystoreforwindow

ObtainIpropertystore

Interface. InitializePropvariant

And then set it to appid.

Create a Windows program and create two windows in it. It is recommended that all windows are overlappped. Such windows have buttons in the taskbar. If no appid is set for the window, the taskbar buttons of the two windows are merged into a group (provided that the taskbar buttons of Windows 7 are opened for merging ).

The taskbar button is merged:

If we set different appid for the window:

Lpctstr app_id [] = {<br/> text ("Wilford. testappid. ID "), <br/> text (" Wilford. testappid. id1 "), <br/> text (" Wilford. testappid. id2 ") <br/> };

Setwndappid (g_hwnd1, app_id [1]); <br/> setwndappid (g_hwnd2, app_id [2]);

The taskbar button will not be grouped:

3. Associate the file type with appid

File Association is implemented through the registry. There are many items to be set. I used some auxiliary functions (these codes are from Microsoft tutorials) to complete these tasks. How to operate the registry is not a topic of this article, so I will not explain it. The auxiliary code will be provided in the Code download at the end of the article. Next I will only explain what key values need to be set.

1. Register appid in the registry. Use the following registry to export the file content. We can see the Registry structure required to register an appid.

Windows Registry Editor Version 5.00

[Hkey_classes_root/Wilford. jumplist2]

"Friendlytypename" = "Custom jump list document"

[Hkey_classes_root/Wilford. jumplist2/curver]

@ = "Wilford. jumplist2"

[Hkey_classes_root/Wilford. jumplist2/defaulticon]

@ = "C: // users // Wilford // documents // Visual Studio 2008 // projects // course // jumplist2 // debug // jumplist2.exe, 0"

[Hkey_classes_root/Wilford. jumplist2/Shell]

@ = "Open"

[Hkey_classes_root/Wilford. jumplist2/Shell/Open]

[Hkey_classes_root/Wilford. jumplist2/Shell/Open/command]

@ = "C: // users // Wilford // documents // Visual Studio 2008 // projects // course // jumplist2 // debug // jumplist2.exe % 1"

2. Associate the file type with appid. In this example, the. txt file is associated with the appid created above.

Windows Registry Editor Version 5.00

[Hkey_classes_root/. txt/openwithprogids]

"Wilford. jumplist2" = hex (0 ):

Add the code for registering appid to the program. When you press the "r" Key, register appid. Of course, if you do not do it in the program, but directly import the above registry file, it is also possible, the corresponding program path needs to be changed.

Case 'r': <br/> If (! Fileregistration: arefileextensionsregistered (app_id) <br/>{< br/> If (e_accessdenied = fileregistration: registerfileextensions (<br/> app_id, szext, arraysize (szext) <br/>{< br/> MessageBox (g_hwnd, text ("Access Denied! "), Text (" error "), mb_ OK | mb_iconerror); <br/>}< br/> break;

Create a new jumplist and you can see the effect.

 

The Windows 7 program development series is now available. If there are new studies in the future, a new tutorial will be released.

 

Source code

Application id demo code

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.