Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Apply the New Vista APIs to Sidebar Gadgets, Part 2 : Page 2

Explore the various Windows Vista Sidebar gadget objects and how they can be used to enhance the functionality of your gadget.


advertisement
Network
To obtain detailed information about your network connection, use the System.Network.Wireless object. This object returns detailed networking information about the computer hosting the gadget.

The gadget in Listing 2 shows the detailed network status of your computer. When the page is loaded, you first print out the current status of your wireless network. Then, you set the connectionChanged and signalStrengthChanged events to the WirelessChanged event handler. This event handler will be called whenever there is a change in the wireless connection and signal strength.

System Shell
To manipulate files and folders on Windows from within your Sidebar gadget, use the following objects:

  • System.Shell
  • System.Shell.Folder
  • System.Shell.Item
  • System.Shell.RecycleBin
  • System.Shell.Drive
System.Shell
The System.Shell object enables you to use the command shell of Windows to perform file operations.



This code shows you how to allow users to choose a file from within a gadget:

//---choose a file--- var item = System.Shell.chooseFile( true, "Text File:*.txt:Png File:*.png", ".", ""); //---if the user clicks Open--- if (item!=null) { result.innerHTML = "File selected: " + item.name + "<br/>"; }

The chooseFile() method invokes the Open dialog, and the user selects a file for opening. The user then selects clicks the Open button, and the file name is displayed.

The chooseFolder() method allows users to select a folder to open, as the following code illustrates:

//---choose a folder--- item = System.Shell.chooseFolder("Please select a folder", 0); if (item!=null) { result.innerHTML += "Folder selected: " + item.name + "<br/>"; }

The drive() method retrieves detailed information about a specific drive. It returns a System.Shell.Drive object:

//---choose a drive--- var drive = System.Shell.drive("c:");

Author's Note: See the section "System.Shell.Drive" for more information about the System.Shell.Drive object.

The execute() method specifies a command to be executed by the Windows shell. For example, you can launch Notepad from within your Sidebar gadget, like this:

System.Shell.execute ("notepad.exe");

You can also allow users to drag and drop files onto your Sidebar gadgets. After files are dropped onto your gadget, you can ask the user to specify a folder in which to copy the dropped files, as shown in Listing 3. Notice that you need to add the ondragenter, ondragover, and ondrop attributes to the <body> element so that users can drag and drop files onto your gadget. When a file is dropped onto your gadget, it calls the onDropped function.

You can call the chooseFolder() method to allow the user to choose a folder in which to copy the dropped files. The itemFromFileDrop() method enables you to obtain the files that are dropped (as a collection of System.Shell.Item objects). The files are then copied to the destination folder using the copyHere() method. If you want to move the files instead of copying them, use the moveHere() method:

folder.moveHere(System.Shell.itemFromFileDrop(event.dataTransfer, i));

The itemFromPath() method returns a collection of System.Shell.Item objects associated with a specified path. For example, the following code copies all the files stored in C:\folder1 into C:\folder2:

var destFolder = System.Shell.itemFromPath("C:\\folder2"); var sourceFolder = System.Shell.itemFromPath("C:\\folder1"); //---copies all the files in folder1 into folder2--- for (var i = 0; i<sourceFolder.SHFolder.Items.count; i++ ){ var item = sourceFolder.SHFolder.Items.item(i); destFolder.SHFolder.copyHere( item, 16 + 256 + 512 ); }

The knownFolder() method takes in a string representing a well-known folder and returns a System.Shell.Folder object. The following code displays the total number of items on the Desktop:

var result = document.getElementById("result"); var folder = System.Shell.knownFolder("Desktop") result.innerHTML = folder.Items.count;

Some well-known folders are:
  • Desktop
  • Startup
  • StartMenu
  • Documents
  • Programs
  • CommonPrograms
  • PublicDesktop
  • PublicFavorites
  • PublicDocuments
  • System
  • Windows
  • Videos
  • ProgramFiles
  • Downloads
  • RecycleBinFolder
If you want to know the path of a well-known folder, use the knownFolderPath() method:

var result = document.getElementById("result"); result.innerHTML = System.Shell.knownFolderPath("Desktop")

The refreshDesktop() method refreshes the desktop after you have added or removed some files:

System.Shell.refreshDesktop();

The saveFileDialog() method shows the standard Save As dialog to allow a user to specify a file for saving:

var result = document.getElementById("result"); var strPath = System.Shell.saveFileDialog( "C:\\", "Text File\0*.txt\0Png File\0*.png"); result.innerHTML = "File name and path: " + strPath;

Author's Note: There is a bug in the current version of the saveFileDialog() method. Instead of specifying the file filter in the format of Description:Extension:Description:Extension, it should be specified as Description\0Extension\0Description\0Extension.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap