Tải bản đầy đủ - 0 (trang)
14-9. Create a Windows 7 Jump List

14-9. Create a Windows 7 Jump List

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

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



CHAPTER 14 ■ WINDOWS INTEGRATION



// Call the default intializer.

InitializeComponent();

// Register an event handler for when the windows is shown.

this.Shown += new EventHandler(onWindowShown);

}

void onWindowShown(object sender, EventArgs e)

{

// Create a new Jump List.

JumpList jumpList = JumpList.CreateJumpList();

// Create a user task.

jumpList.AddUserTasks(

new JumpListLink("cmd.exe", "Open Command Prompt"));

// Create a user task with an icon.

jumpList.AddUserTasks(new JumpListLink("notepad.exe", "Open Notepad")

{

IconReference = new IconReference("notepad.exe", 0)

});

// Create a user task with a document.

jumpList.AddUserTasks(

new JumpListLink(@"C:\Users\Adam\Desktop\test.txt",

"Open Text File"));

}

}

}



14-10. Use Windows Search

Problem

You need to search the Windows file system.



Solution

Use Microsoft Windows API CodePack for .NET Framework to access the Windows Search feature.

Create one or more Microsoft.WindowsAPICodePack.Shell.SearchConditions to define the constraints for

your search and pass them to an instance of Microsoft.WindowsAPICodePack.Shell.ShellSearchFolder.

The ShellSearchFolder acts as a collection whose members represent the results of your search. The

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

integration with Windows using managed code. (You can download the CodePack from

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



711



www.it-ebooks.info



CHAPTER 14 ■ WINDOWS INTEGRATION



How It Works

The search model is built around conditions and locations. Conditions are the individual aspects of a file

that match your search—for example, if you needed to find files text files that have “windows” in the file

name, the constrains would be:

1.



Files that have the .txt extension



2.



Files that have the word windows in the file name.



Locations are where you want the search to be conducted and can include Windows 7 libraries.

When using the CodePack, searches are created and performed as follows:

1.



Create one or more instances of SearchCondition using the

SearchConditionFactory.CreateLeafCondition method. The CreateLeafCondition method

accepts three parameters:





A property of the SystemProperties.System class. The System class contains

properties for all of the attributes you can search for—some of the most

commonly used are described in Table 14-6.







The data value to match when searching. This must match the data type of

the preceding attribute.







A value from the SearchConditionOperation enumeration. This value

specifies the way in which the data value is matched (equals, greater than,

less than, etc.). Commonly used members are described in Table 14-7.



2.



If you have created more than one SearchCondition, you must combine them

with the SearchConditionFactory.CreateAndOrCondition method. This method

takes a value from the SearchConditionType enumeration (And, Or, Not) that

specifies how the individual conditions are combined.



3.



Create a new instance of the ShellSearchFolder class, using the

SearchCondition you previously created and one or more locations as

constructor parameters. The CodePack includes the KnownFolders

enumeration, whose members reference useful directories and libraries.



4.



Treat the ShellSearchFolder as a collection. Enumerating the contents will

return the results of your search, with each file represented by an instance of

ShellObject.



■ Tip Your search is not performed until you try to access the members of the ShellSearchFolder collection—at

which point the current thread is blocked until the search has completed. Perform the search in a background

thread to avoid an unresponsive application.



712



www.it-ebooks.info



CHAPTER 14 ■ WINDOWS INTEGRATION



Table 14-6. Commonly Used Members of the SystemProperties.System Class



Property



Data Type



Description



DateModified



DateTime



The last time that the file was changed



DateCreated



DateTime



The time the file was created



FileExtention



String



The extension for the file, including the period



FileName



String



The name of the file, including the file extension



FileOwner



String



The owner of the file



Table 14-7. Commonly Used Members of the SearchConditionOperation Class



Member



Description



Equal



The file attribute and the target value are the same.



NotEqual



The file attribute and the target value are different.



ValueContains



The file attribute contains the target value (for example, “windows” contains “win”).



ValueStartsWith



The file attribute starts with the target value (for example, “windows” starts with

“win”).



ValueEndsWith



The file attribute ends with the target value (for example, “Microsoft” ends with

“soft”).



ValueNotContains



The file attribute does not contains the target value (for example, “Microsoft” does

not contain “win”).



LessThan



The file attribute is less than the target value.



GreaterThan



The file attribute is greater than the target value.



The Code

The following example is a Windows Forms application that uses the FileExtension and FileName

attributes to search for files in the current user’s directories. The user interface, built using the Visual

Studio designer, is shown in Figure 14-1. To compile the example, you must build Windows API

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

Microsoft.WindowsAPICodePack.Shell.dll assemblies.



713



www.it-ebooks.info



CHAPTER 14 ■ WINDOWS INTEGRATION



Figure 14-1. Example search application

The button1_Click method is called when the Search button is pressed. A SearchCondition is

created using the values that the user has entered for the file name and extension, and combined using

the SearchConditionType.And enumeration value. The search results are read from the

ShellSearchFolder and name of each file found is added to the large text box.

using

using

using

using

using

using

using

using

using

using



System;

System.Collections.Generic;

System.ComponentModel;

System.Data;

System.Drawing;

System.Linq;

System.Text;

System.Windows.Forms;

Microsoft.WindowsAPICodePack.Shell;

Microsoft.WindowsAPICodePack.Shell.PropertySystem;



namespace Recipe14_10

{

public partial class Recipe14_10 : Form

{



714



www.it-ebooks.info



CHAPTER 14 ■ WINDOWS INTEGRATION



public Recipe14_10()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

// Create the leaf condition for the file extension.

SearchCondition fileExtCondition =

SearchConditionFactory.CreateLeafCondition(

SystemProperties.System.FileExtension, textBox1.Text,

SearchConditionOperation.Equal);

// Create the leaf condition for the file name.

SearchCondition fileNameCondition =

SearchConditionFactory.CreateLeafCondition(

SystemProperties.System.FileName, textBox2.Text,

SearchConditionOperation.ValueContains);

// Combine the two leaf conditions.

SearchCondition comboCondition =

SearchConditionFactory.CreateAndOrCondition(

SearchConditionType.And,

false, fileExtCondition, fileNameCondition);

// Create the search folder.

ShellSearchFolder searchFolder = new ShellSearchFolder(

comboCondition, (ShellContainer)KnownFolders.UsersFiles);

// Clear the result text box.

textBox3.Clear();

textBox3.AppendText("Processing search results...\n");

// Run through each search result.

foreach (ShellObject shellObject in searchFolder)

{

textBox3.AppendText("Result: "

+ shellObject.ParsingName + "\n");

}

// Display a final message to the user.

textBox3.AppendText("All results processed\n");

}

}

}



715



www.it-ebooks.info



CHAPTER 14 ■ WINDOWS INTEGRATION



14-11. Check Internet Connectivity

Problem

You need to check that the computer has Internet access.



Solution

Use the Windows API CodePack for Microsoft .NET Framework to access to enumerate the available

network connections and determine which, if any, are connected to the Internet. The Windows API

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

integration with Windows using managed code. (You can download the CodePack from http://code.

msdn.microsoft.com/WindowsAPICodePack).



How It Works

The Microsoft.WindowsAPICodePack.Net.NetworkListManager class contains the IsConnectedToInternet

property. If this returns true, the GetNetworks method can be used to obtain a collection of connected

networks, each of which is represented by the Microsoft.WindowsAPICodePack.Net.Network class.



The Code

The following example uses the IsConnectedToInternet property of the Microsoft.WindowsAPICodePack.

Net.NetworkListManager class, and if the result is positive, gets the list of network connections and

writes out the name of those that are connected. To compile the example, you must build Windows API

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

assembly.

using System;

using Microsoft.WindowsAPICodePack.Net;

namespace Recipe14_11

{

class Recipe14_11

{

static void Main(string[] args)

{

// Check the internet connection state.

bool isInternetConnected =

NetworkListManager.IsConnectedToInternet;

Console.WriteLine("Machine connected to Internet: {0}",

isInternetConnected);

if (isInternetConnected)

{



716



www.it-ebooks.info



CHAPTER 14 ■ WINDOWS INTEGRATION



// Get the list of all network connections.

NetworkCollection netCollection =

NetworkListManager.GetNetworks(

NetworkConnectivityLevels.Connected);

// Work through the set of connections and write out the

// name of those that are connected to the internet.

foreach (Network network in netCollection)

{

if (network.IsConnectedToInternet)

{

Console.WriteLine(

"Connection {0} is connected to the internet",

network.Name);

}

}

}

Console.WriteLine("\nMain method complete. Press Enter.");

Console.ReadLine();

}

}

}



14-12. Display a Task Dialog

Problem

You need to display a task dialog—a standard Windows-specific dialog box, such as an elevated task

request.



Solution

Use Windows API CodePack for Microsoft .NET Framework to create and display task dialogs. Windows

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

integration with Windows using managed code—it contains extensive support for Windows-specific

dialog boxes, which allow your application to better integrate with the platform. (You can download the

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



How It Works

Create an instance of the Microsoft.WindowsAPICodePack.Dialogs.TaskDialog class and use the class

properties to configure the task dialog. Add event handlers so that you are notified when buttons are

pressed and then call the Show method to display the dialog. Some of the most useful properties of the

TaskDialog class are shown in Table 14-8.



717



www.it-ebooks.info



CHAPTER 14 ■ WINDOWS INTEGRATION



Table 14-8. Selected Properties of the TaskDialog Class



Property



Description



Cancelable



Determines if the user can dismiss the dialog.



Controls



The set of controls embedded within the task dialog. See the recipe code for an

example.



InstructionText



The summary text displayed in the dialog.



StandardButtons



The standard buttons for the dialog. Set with the values of the

TaskDialogStandardButtons enumeration. See the recipe code for an example.



Text



The detailed text, displayed below the InstructionText.



The Code

The following example is a Windows Forms application comprising a button and a text area. To compile

the example, you must build the Windows API CodePack for Microsoft .NET Framework and reference

the Microsoft.WindowsAPICodePack.dll assembly. The user interface is shown in Figure 14-2. We have

added an event handler such that the showElevatedTaskRequest method is called when the button is

clicked.



Figure 14-2. Windows Forms interface



718



www.it-ebooks.info



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

14-9. Create a Windows 7 Jump List

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

×