Tải bản đầy đủ - 0 (trang)
14-8. Create a Shortcut on the Desktop or Start Menu

14-8. Create a Shortcut on the Desktop or Start Menu

Tải bản đầy đủ - 0trang

CHAPTER 14 ■ WINDOWS INTEGRATION



How It Works

The .NET Framework class library does not include the functionality to create desktop or Start menu

shortcuts; however, this is relatively easy to do using the Windows Script Host component accessed

through COM Interop. Chapter 15 describes how to create an Interop assembly that provides access to a

COM component. If you are using Visual Studio, add a reference to the Windows Script Host object

model, listed on the COM tab of the Add Reference dialog box. If you don’t have Visual Studio .NET, use

the Type Library Importer (Tlbimp.exe) to create an Interop assembly for the wshom.ocx file, which is

usually located in the Windows\System32 folder. (You can obtain the latest version of the Windows Script

Host from http://msdn.microsoft.com/scripting.)

Once you have generated the Interop assembly and imported it into your project, follow these steps

to create a desktop or Start menu shortcut.

1.



Instantiate a WshShell object, which provides access to the Windows shell.



2.



Use the SpecialFolders property of the WshShell object to determine the

correct path of the folder where you want to put the shortcut. You must specify

the name of the folder you want as an index to the SpecialFolders property.

For example, to create a desktop shortcut, specify the value Desktop, and to

create a Start menu shortcut, specify StartMenu. Using the SpecialFolders

property, you can obtain the path to any of the special system folders. If the

specified folder does not exist on the platform you are running on,

SpecialFolders returns an empty string. Other commonly used values include

AllUsersDesktop and AllUsersStartMenu; you can find the full list of special

folder names in the section on the SpecialFolders property in the Windows

Script Host documentation.



3.



Call the CreateShortcut method of the WshShell object, and provide the fully

qualified file name of the shortcut file you want to create. The file should have

the extension .lnk. CreateShortcut will return an IWshShortcut instance.



4.



Use the properties of the IWshShortcut instance to configure the shortcut. You

can configure properties such as the executable that the shortcut references, a

description for the shortcut, a hotkey sequence, and the icon displayed for the

shortcut.



5.



Call the Save method of the IWshShortcut instance to write the shortcut to disk.

The shortcut will appear either on the desktop or in the Start menu (or

elsewhere), depending on the path specified when the IWshShortcut instance

was created.



The Code

The following example class creates a shortcut to Notepad.exe on both the desktop and Start menu of the

current user. The example creates both shortcuts by calling the CreateShortcut method and specifying a

different destination folder for each shortcut file. This approach makes it possible to create the shortcut

file in any of the special folders returned by the WshShell.SpecialFolders property.



707



www.it-ebooks.info



CHAPTER 14 ■ WINDOWS INTEGRATION



using System;

using System.IO;

using IWshRuntimeLibrary;

namespace Apress.VisualCSharpRecipes.Chapter14

{

class Recipe14_08

{

public static void CreateShortcut(string destination)

{

// Create a WshShell instance through which to access the

// functionality of the Windows shell.

WshShell wshShell = new WshShell();

// Assemble a fully qualified name that places the Notepad.lnk

// file in the specified destination folder. You could use the

// System.Environment.GetFolderPath method to obtain a path, but

// the WshShell.SpecialFolders method provides access to a wider

// range of folders. You need to create a temporary object reference

// to the destination string to satisfy the requirements of the

// Item method signature.

object destFolder = (object)destination;

string fileName = Path.Combine(

(string)wshShell.SpecialFolders.Item(ref destFolder),

"Notepad.lnk"

);

// Create the shortcut object. Nothing is created in the

// destination folder until the shortcut is saved.

IWshShortcut shortcut =

(IWshShortcut)wshShell.CreateShortcut(fileName);

// Configure the fully qualified name to the executable.

// Use the Environment class for simplicity.

shortcut.TargetPath = Path.Combine(

Environment.GetFolderPath(Environment.SpecialFolder.System),

"notepad.exe"

);

// Set the working directory to the Personal (My Documents) folder.

shortcut.WorkingDirectory =

Environment.GetFolderPath(Environment.SpecialFolder.Personal);

// Provide a description for the shortcut.

shortcut.Description = "Notepad Text Editor";

// Assign a hotkey to the shortcut.

shortcut.Hotkey = "CTRL+ALT+N";

// Configure Notepad to always start maximized.

shortcut.WindowStyle = 3;



708



www.it-ebooks.info



CHAPTER 14 ■ WINDOWS INTEGRATION



// Configure the shortcut to display the first icon in Notepad.exe.

shortcut.IconLocation = "notepad.exe, 0";

// Save the configured shortcut file.

shortcut.Save();

}

public static void Main()

{

// Create the Notepad shortcut on the desktop.

CreateShortcut("Desktop");

// Create the Notepad shortcut on the Windows Start menu of

// the current user.

CreateShortcut("StartMenu");

// Wait to continue.

Console.WriteLine(Environment.NewLine);

Console.WriteLine("Main method complete. Press Enter.");

Console.ReadLine();

}

}

}



14-9. Create a Windows 7 Jump List

Problem

You need to create a Windows 7 Jump List for your application.



Solution

Use the features of Windows API CodePack for Microsoft .NET Framework. Create and configure a

JumpList instance for your application. The items that you add to the JumpList define the tasks,

documents, and categories that appear in the Windows taskbar.



How It Works

Windows API CodePack for Microsoft .NET Framework is a source code library published by Microsoft to

simplify integration with Windows using managed code. One feature of the CodePack is support for the

Windows 7 taskbar. There is nothing in the CodePack that you could not write yourself, but using the

CodePack is simpler and quicker, and provides a consistent API that Microsoft will support across

Windows versions. (You can download the CodePack from

http://code.msdn.microsoft.com/WindowsAPICodePack).



709



www.it-ebooks.info



CHAPTER 14 ■ WINDOWS INTEGRATION



Once you have compiled the library, reference the Microsoft.WindowsAPICodePack.dll and

Microsoft.WindowsAPICodePack.Shell.dll assemblies (these files will be in Shell\bin\debug folder

within the CodePack directory) and follow these steps to create and populate a Jump List:

1.



Import the Microsoft.WindowsAPICodePack.Shell and

Microsoft.WindowsAPICodePack.Taskbar namespaces with the using directive.



2.



Add an event handler to the Shown member of your application class.



3.



Within the event handler, call the static method JumpList.CreateJumpList to

create a new JumpList instance for your application.



4.



Create instances of JumpListLink to represent tasks and documents and add

them to the JumpList instance using the AddUserTasks method.



■ Note The JumpList class can also be used to display recently used documents, but only if your application is

registered as a handler for the document types you display. See the Windows API CodePack for details and

example code to handle document type registration.



The Code

The following example uses the Microsoft.WindowsAPICodePack.Taskbar.JumpList class to populate the

Windows 7 Jump List with three items. The first item opens a command prompt. The second item opens

Notepad and displays the Notepad icon in the Jump List. The third item shows how to open another

application’s file using a Jump List. To compile the example, you must build the Windows API CodePack

for .NET Framework and reference the Microsoft.WindowsAPICodePack.dll and

Microsoft.WindowsAPICodePack.Shell.dll assemblies.

using

using

using

using

using

using

using

using

using



System;

System.Collections.Generic;

System.ComponentModel;

System.Data;

System.Drawing;

System.Linq;

System.Text;

System.IO;

System.Windows.Forms;



using Microsoft.WindowsAPICodePack.Shell;

using Microsoft.WindowsAPICodePack.Taskbar;

namespace Recipe14_09

{

public partial class Form1 : Form

{

public Form1()

{



710



www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

14-8. Create a Shortcut on the Desktop or Start Menu

Tải bản đầy đủ ngay(0 tr)

×