Programming Windows Sidebar Gadgets

by Milad 2. May 2010 22:38

It’s been a long time since Windows Vista is introduced to the IT World(And how much we are thankful for Windows 7 being introduced!)

One of the remarkable things about Post-XP Windows, is the cute Gadgets floating around our desktops!

This posts tells you how you can make your own Gadgets for Windows Sidebar*

What is a .gadget file?

If you are a curious Mac user(or possibly Yahoo Widget Engine user.) you may already know the answer to this question.
A .gadget file is just an archive file(.ZIP file.) wrapping a bunch of stuff together and making them ready for running under Sidebar.exe.

What is in a .gadget file?

A simple gadget, must have 2 files:

  • gadget.xml
  • An HTML Startup Page

A more complicated one, can have these files too:

  • CSS Files for Styling
  • Javascript Codes (Interacting with Sidebar APIs)
  • A bunch of Images

Gadget.xml:

The manifest of your Gadget. Everything that sidebar should know about your gadget is stored in this file.
This is a general form of a gadget.xml file:


<?xml version="1.0" encoding="utf-8" ?>
<gadget>
        <name>Sample Gadget</name>
        <namespace>windows.sdk</namespace>
        <version>1.0.0.0</version>
        <author name="Microsoft">
                <info url="msdn.microsoft.com" />
                <logo src="logo.png" />
        </author>
        <copyright>&amp;#169; Microsoft Corporation.</copyright>
        <description>Sidebar gadget sample.</description>
        <icons>
                <icon height="48" width="48" src="icon.png" />
        </icons>
        <hosts>
                <host name="sidebar">                         <!-- New autoscaleDPI node -->
                        <autoscaleDPI>true</autoscaleDPI>                         <base type="HTML" apiVersion="1.0.0" src="sample.html" />
                        <permissions>Full</permissions>
                        <platform minPlatformVersion="1.0" />
                        <defaultImage src="icon.png" />
                </host>
        </hosts>
</gadget>

HTML Startup page:

A really simple page containing your gadget.

Sidebar.exe APIs

Well nobody like a static gadget!
Fortunately, you can spice up your gadgets with some APIs provided by Sidebar.exe. For example you can access Power Information of the machine your gadget is running on. Or the information about machine’s Total Memory and Available Memory or available networks in wireless range etc.

Here is a sample Javascript:
(this code uses Sidebar.exe APIs to get Machine Power Status)


function OnPageLoad() {
    System.Machine.PowerStatus.powerLineStatusChanged = GetPowerStatus;

    GetPowerStatus();
}

function GetPowerStatus() {  
    var sMachineInfo;
    // Get the machine details.
    if (System.Machine.PowerStatus.isPowerLineConnected == true) {
        sMachineInfo += "Connected to External Power." + "<br/>";       
    } else {
        var percent = System.Machine.PowerStatus.batteryPercentRemaining;       
        sMachineInfo += "Power Percent Remaining: " + percent.toString() + "%<br/>";
    }

    // Initialize the gadget content.
    var s = document.getElementById("gadgetContent");
    s.InnerHTML = sMachineInfo;
}

Deploying your gadgets:

In order to deploy your gadgets, you should select it’s files and put them into a .ZIP archive. Then change the extension of the file from .ZIP to .Gadget in order to make it visible to Sidebar.exe

Where to now?

http://msdn.microsoft.com/en-us/library/dd370867(VS.85).aspx#development
http://www.codeproject.com/KB/gadgets/InsideBar.aspx

And I really recommend this book (for a quick review, check the book’s source codes.):
Cover image for product 047017661X

And you know the most accurate reference for Microsoft APIs, is MSDN.

* You can use all of these resources for Windows Sideshow Gadget development.

Tags: ,

Computer | Internet | Programming