<html>
<head>
<script>
function Init() {
document.body.style.width = 100;
document.body.style.height = 100;
}
</script>
</head>
<body onload="Init();">
<!-- whiteBackground.png is any image that's 100x100 -->
<g:background
id="imgBackground"
src="whiteBackground.png"
style="position:absolute;top:0;left:0;"
opacity="0">
<span>Hello World!</span>
</body>
</html>
Drag the gadget from the Gallery and drop it on the desktop and you'll see something like:
function Init() {
document.body.style.width = 100;
document.body.style.height = 100;
// imgBackground is the id of the <g:background> body element
var oText = imgBackground.addTextObject("","",0,"White", 10,50);
oText.value = "Hello World!";
oText.font = "Arial";
oText.fontSize = "14";
oText.addGlow( "Blue", 20, 100);
}
<script>
function Init() {
document.body.style.width = 100;
document.body.style.height = 100;
// imgBackground is the id of the <g:background> body element
// bullseye.png is transparent around the outside
imgBackground.addImageObject( "bullseye.PNG", 0, 0 ); // <<--- added
}
The result is a perfectly round gadget:
<body onload="Init();"
ondragenter="event.returnValue= false"
ondragover="event.returnValue= false"
ondrop="ProcessItemsFromDrop()">
...
</body>
In the following code, which is the final version of the DropTarget.HTML file, the ProcessItemsFromDrop() function uses the System.Shell object (supplied by the Gadget infrastructure) to get the Drop event and read and process each item.
<html><head>
<script>
function Init() {
document.body.style.width = 100;
document.body.style.height = 100;
// bullseye.png is transparent around the outside
imgBackground.addImageObject("bullseye.png", 0, 0);
}
var gsDestFolder = "c:\\temp\\bullseye\\";
//--------------------------------
// for this example, we'll move a file to a specific folder
// prefixing its name with today's date
//
function ProcessOneFile( sSrcFile ) {
var oFSO = new ActiveXObject("Scripting.FileSystemObject");
var oDt = new Date();
var sRenPrefix = oDt.getFullYear()
+ ("0" + (oDt.getMonth() + 1)).slice(-2) // 01 to 12
+ ("0" + oDt.getDate()).slice(-2) // 01 to 31
+ ("_");
var sFilename = oFSO.GetFileName(sSrcFile); // e.g., filename.ext
var sDestFile = gsDestFolder;
sDestFile += sRenPrefix; // e.g., 20090714_
sDestFile += sFilename; // e.g., SomeFile.txt
oFSO.MoveFile( sSrcFile, sDestFile ); // move & rename to target folder
}
//-----------------------------------------
function ProcessItemsFromDrop()
{
var intIndex = 0;
var oItem;
while (oItem = System.Shell.itemFromFileDrop(event.dataTransfer, intIndex)) {
ProcessOneFile( oItem.path );
intIndex++;
}
var sHtml = "<br><center><b>" + intIndex + "</b> file(s)<br>processed</center>";
document.all.resultsMsg.innerHTML = sHtml;
// clear the message after 5 seconds
window.setTimeout("document.all.resultsMsg.innerHTML='';", 5000);
}
</script>
</head>
<body onload="Init();"
ondragenter="event.returnValue = false"
ondragover="event.returnValue = false"
ondrop="ProcessItemsFromDrop()">
<!-- whiteBackground.png is any image that's 100x100 -->
<g:background
id="imgBackground"
src="whiteBackground.png"
style="position:absolute;top:0;left:0;"
opacity="0">
<span id="resultsMsg"></span>
</body>
</html>
As you can see, in line 33 we used the System.Shell object to handle the drop, but there is also an instance of the FileSystemObject object, which we use to move/rename files that are dropped (lines 15-26).
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)