Creating a Windows Gadget

Introduction

 
In this article, I have described the steps to create a Windows Gadget. I have demonstrated the article with an example of a digital clock gadget.
 

Gadgets

 
Gadgets are small applications that are hosted on the Windows Sidebar. Windows Gadgets are simply made up of HTML and JavaScript code. User-created gadgets are stored in the following folder:
 
C:\Users\<current user>\AppData\Local\Microsoft\Windows Sidebar\Gadgets
 
Every gadget is stored in its own folder under the above-mentioned folder. The folder name should be the same as the gadget name followed by the  .gadget extension. Every gadget has two files associated with it. One is an HTML file with embedded JavaScript code to provide the functionality of the gadget. The other file is an XML file called gadget.xml and is also known as the gadget manifest.
 
Example Code
 
In the HTML file, the style element is used to specify the layout and format of the gadget. The script element is used to specify the content and working of the gadget. Finally, a span element is used to display the gadget.
 
Following is the HTML/JavaScript code for our digital clock gadget (DigitalClock.html)
  1. <html>  
  2.    <head>  
  3.       <title>Digital Clock</title>  
  4.       <style>  
  5.          body  
  6.          {  
  7.          margin: 0;  
  8.          width: 130px;  
  9.          height: 65px;  
  10.          }  
  11.          #gadgetContent  
  12.          {  
  13.          width: 130px;  
  14.          top: 24px;  
  15.          text-align: center;  
  16.          position: absolute;  
  17.          font-family: Verdana;  
  18.          font-size: 10pt;  
  19.          }  
  20.       </style>  
  21.       <script language="javascript">  
  22.          function showtime()  
  23.          {  
  24.              var now=new Date();       // Find current date  
  25.              var h=now.getHours();  // Find current hour  
  26.              var m=now.getMinutes();    // Find current minute  
  27.              var s=now.getSeconds();    // Find current second  
  28.              h=(h<10)?"0"+h:h;       // Convert hour to 2 digits  
  29.              m=(m<10)?"0"+m:m;       // Convert minute to 2 digits  
  30.              s=(s<10)?"0"+s:s;       // Convert second to 2 digits  
  31.              gadgetContent.innerHTML="<h2><font color='red'>"+h+":</font>  
  32.          <font color='lime'>"+m+":</font><font color='cyan'>"+s+"</font></h2>";  
  33.                      // Set time on the span element  
  34.              setTimeout("showtime()",1000); // Display time after every one second  
  35.          }  
  36.       </script>  
  37.    </head>  
  38.    <body onload="showtime()" bgcolor="black">  
  39.       <span id="gadgetContent"></span>  
  40.    </body>  
  41. </html> 
The gadget manifest file is an XML file which describes the properties of the gadget.
 
Following is the code of the gadget.xml file:
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <gadget>  
  3.    <name>DigitalClock</name>  
  4.    <namespace>Example.Azim</namespace>  
  5.    <version>1.0.0.0</version>  
  6.    <author name="Azim">  
  7.       <info url="www.example.com" />  
  8.    </author>  
  9.    <copyright>Azim</copyright>  
  10.    <description>Digital Clock</description>  
  11.    <hosts>  
  12.       <host name="sidebar">  
  13.          <base type="HTML" apiVersion="1.0.0" src="DigitalClock.html" />  
  14.          <platform minPlatformVersion="1.0" />  
  15.          <permissions>Full</permissions>  
  16.       </host>  
  17.    </hosts>  
  18. </gadget> 
Perform the following steps to add the gadget to the sidebar.
  • Create a folder called DigitalClock. gadget in the following folder:
C:\Users\<current user>\AppData\Local\Microsoft\Windows Sidebar\Gadgets
  • Copy the files DigitalClock.html and gadget.xml into the DigitalClock.gadget folder.
  • Right-click on the sidebar and select Add Gadgets option.
  • Select DigitalClockgadget.
Note
 
I have attached the DigitalClock.html and gadget.xml files along with this project. The following figures show how to add the gadget to the sidebar.
 
image1.jpg
 

Summary

 
In this article, we learned about creating window gadgets.


Similar Articles