Powering Your Gadgets With ActiveX (part 1)
A Pictorial Tutorial
After you’ve written your first couple of sidebar gadgets, you may find yourself wanting to access system functionality that isn’t exposed by the gadget APIs. If you know how to do what you want using C++, then all you need is an ActiveX control in order to expose that functionality to your gadget. This blog post will show you how.
A Simple Gadget
Let's create a simple gadget we can use to demonstrate the technique. First, create a gadget folder:
cd %LOCALAPPDATA%\Microsoft\Windows Sidebar\Gadgets
md test.gadget
cd test.gadget
Create a file named ‘gadget.xml’ with the following contents:
<?xml version="1.0" encoding="utf-8" ?>
<gadget>
<name>ActiveX </name>
<namespace>test</namespace>
<version>1.0.0.0</version>
<author name="Your Name">
<info url="www.your-url.com" />
</author>
<copyright>2006</copyright>
<description>Call an ActiveX control</description>
<hosts>
<host name="sidebar">
<base type="HTML" apiVersion="1.0.0" src="gadget.html" />
<permissions>Full</permissions>
<platform minPlatformVersion="0.3" />
</host>
</hosts>
<version value="1.0.0.0" MinPlatformVersion="0.1"/>
</gadget>
And create a file named ‘gadget.html’ with these contents:
<HTML>
<SCRIPT>
function main()
{
try
{
out("Hello, world!");
}
catch (e)
{
content.innerText = "ERROR: " + e + " : " + e.name + " : " + e.message;
}
}
function out(text)
{
content.innerHTML += "<DIV style='height:20;overflow:hidden'>" + text + "</DIV>";
}
</SCRIPT>
</HEAD>
<BODY STYLE="height:300;width:600;background-color:gray" onload="javascript:main();">
<DIV id="content" style="height:270;width:580;border-style:solid;overflow:auto">
</DIV>
</BODY>
</HTML>
Now we have a simple but functional gadget. If you like, you can open up your gadget gallery pane and add this gadget to your sidebar; it doesn’t yet create or call an ActiveX control, but it will.
...unfortunately, the MSN Spaces blog engine won't let me publish this tutorial in one go (although it did let me type it all in...), so I'm going to have to break it up into chunks. Tune in for part 2...
Conclusion
I hope you enjoyed this tutorial, and found it useful. I welcome any feedback and corrections – send them to Bruce.Williams@microsoft.com.