人生是一场不能存盘的RPG,我只能尽量多搞几个Screenshot

December 4, 2006

Gadget开发入门

Filed under: Windows platform

http://microsoftgadgets.com/Sidebar/DevelopmentOverview.aspx

–Where is the Side bar
C:\Programe Files\Windows SideBar\
SideBar的主程序以及公用的Gadget

“%userprofile%\AppData\Local\Microsoft\Windows Sidebar\Gadgets”
此处保存的是某个特定用户的Gadget.

–Run the Sider bar
Start -> All Programs -> Accessories

在vista的help中,查找 Windows Sidebar: frequently asked questions
How do I open Sidebar?
Click to open Windows Sidebar.对应的link为:
<a class=”shellExecuteLink” href=”shortcut:&quot;%25programfiles%25\windows sidebar\sidebar.exe&quot;”
title=”Click to open Windows Sidebar”>
<img src=”mshelp://help/?id=Microsoft.Windows.Resources.ShellExecuteTopicIcon”>
Click to open <span class=”notLocalizable”>Windows</span> Sidebar.
</a>
href=”shortcut:”%programfiles%\… “”这种写法我还是首次注意到.

–Mechanism of Sidebar and Gadget
Gadget由HTML, JavaScript,CSS,图片,资源组成, 通过调用Sidebar提供的API和系统中的ActiveX对象来完成操作.
常用到的ActiveX:
JScripting.FileSystemObject访问文件系统,
Microsoft.XmlDom分析XML文件,
Microsoft.XMLHTTP调用Web Service

Gadget本身是zip或cab格式,如果使用cab格式,需要进行数字签名, VS2005提供了数字签名工具Signtool.exe.

Gadget在其安全配置上,与普通的Web网页有很多不同点。
1. Gadget可以创建任何已安装的ActiveX对象的实例,因为其被配置为”可以初始化并且脚本访问未被标记为安全的ActiveX”。
2. Gadget也可以访问跨域的数据源,因为只有将不同位置的数据进行相应的集成,Gadget对于开发人员和最终用户才有意义。
3. 重要!!一个在Microsoft Gadget Gallery中发布的Gadget只能使用系统已有的ActiveX,禁止下载任何新的ActiveX控件,
无论这个ActiveX控件是签名或者未签名的。

注意:一个Gadget可以有多个实例同时运行.

–文件组成:
Gadget 所在folder的名称必须以.gadget结尾. 比如HelloWorld.gadget
* 我写了一个gadget,目录命名为为Mailsnatcher.gadget,无法从Gadget Gallery中看到,
改为eMailsnatcher.gadget,就可以看到了,原因不明.

以Clock为例, Clock位于%programfiles%\Windows\Gadgets\Clock.Gadget
主程序位于en-US下,可见各个语言对应的主程序可以不同.
en-US\中的gadget.xml是最关键的部分,定义了整个Gadget的信息以及用到的资源.
.Html用于定义gadget的UI,行为

gadget.xml gadget 的manifest, 只能取gadget.xml这个名字

常用的资源有:
icon, gadget.xml中<icons>中指定,用于在Gadget Gallery中显示.
defaultImage , gadget.xml中<defaultImage> 中指定,用于在拖动时显示.
logo, gadget.xml中<logo >中指定,用于在拖动时显示.用于在Gadget Gallery中显示详细信息.
这3个资源的路径都是相对于.gadget目录的.

–开发要点:
无IDE,不能所见即所得; 无法进行Debug,只能写Log来Trace;

–Sample
在”%userprofile%\AppData\Local\Microsoft\Windows Sidebar\Gadgets”下
生成目录: HelloWorld.gadget

在HelloWorld.gadget下生成文件gadget.xml
<?xml version=”1.0″ encoding=”utf-8″ ?>
<gadget>
<name>First Gadget!</name>
<namespace>Example.You</namespace>
<version>1.0</version>
<author name=”Aloneplayer”>
<info url=”" />
</author>
<copyright>2006</copyright>
<description>My first gadget</description>
<hosts>
<host name=”sidebar”>
<base type=”HTML” apiVersion=”1.0.0″ src=”HelloWorld.html” />
<permissions>full</permissions>
<platform minPlatformVersion=”0.3″ />
</host>
</hosts>
</gadget>

在HelloWorld.gadget下生成文件HelloWorld.html
<html>
<head>
<title>Hello, World!</title>
<style>
body {
width:130;
height:50;
}
</style>
</head>
<body>
<span id=”gadgetContent” style=”font-family: Tahoma; font-size: 10pt;”>Hello, World!</span>
</body>
</html>

此时,就可以点击Sider bar上的”+”呼出Gadget Gallery,可以看到一个以gadget.xml中<name>为命名的
gadget.

修改HelloWorld.html,使用背景:
<style>
body {
width:130;
height:50;
background: url(’Background.png’);
padding-top: 5px;
}
</style>

使用API:
<html>
<head>
<title>Hello, World!</title>
<style>
body{
width:130;
height:50;
background: url(’Background.png’); }
padding-top: 5px;
</style>
<script>
var variableName = “ProgramFiles”;
function setContentText() {
gadgetContent.innerText = System.Environment.getEnvironmentVariable(variableName);
}
</script>
</head>
<body onload=”setContentText()”>
<span id=”gadgetContent” style=”font-family: Tahoma; font-size: 10pt;”>Hello, World!</span>
</body>
</html>

添加Setting,此刻可以看到Gadget上会出现一个扳手状的buttong,同时context menu上也会出现”options”
用于进行setting
<html>
<head>
<style>
body{
width:250;
height:75;
}
</style>
<script>
function init()
{
var currentSetting = System.Gadget.Settings.read(”variableName”);
if (currentSetting != “”)
envVar.innerText = currentSetting;
}

System.Gadget.onSettingsClosing = SettingsClosing;
function SettingsClosing(event)
{
if (event.closeAction == event.Action.commit)
{
variableName = envVar.value;
System.Gadget.Settings.write(”variableName”, variableName);
}

event.cancel = false;
}
</script>
</head>
<body onload=”init()”>
<span id=”mySpan” style=”font-family: Tahoma; font-size: 10pt;”>
Environment Variable:<br>
<input type=”text” id=”envVar” length=”40″>
</span>
</body>
</html>

–发布:
把所有的文件做成一个zip包或cab包,把包的后缀改成.gadget,双击.gadget,就可以安装这个
gadget.

Comments »

The URI to TrackBack this entry is: http://recordsome.blogsome.com/2006/12/04/p201/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.






















Get free blog up and running in minutes with Blogsome
Theme designed by Hadley Wickham