【问题标题】:Can a Windows Gadget save data?Windows 小工具可以保存数据吗?
【发布时间】:2015-01-05 16:45:57
【问题描述】:

我正在创建一个小的 Windows 边栏小工具,用于在简单的 textarea 中记笔记。

像往常一样,我有一个 gadget.xml 清单文件和一个 .html 文件,见下文。

如何在小工具中读取一些数据/保存一些数据?

我知道这通常只使用 JavaScript 是不可能的(注意:使用 localstorage 是不可能的,因为我想要数据的持久性),所以如何在 @ 中保存/读取数据987654326@?


<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=Unicode" />
        <title>NeverForget</title>
        <style type="text/css">
        body
        {
            margin: 0;
            width: 300px;
            height: 200px;            
            background-color: transparent;
        }
        #gadgetContent 
        {
            width: 100%;
            height: 100%;
            overflow: hidden;
            border: none;
            background-color: transparent;
        }
        </style>
        <script type="text/jscript" language="jscript">
            function init() {
                // how to load notes from a file here on startup?
            }
            window.onkeydown = function () {
                // how to save data to file?
            }
        </script>
    </head>

    <body onload="init()">
            <textarea id="gadgetContent">Bonjour</textarea>
    </body>
</html>

【问题讨论】:

    标签: windows-desktop-gadgets


    【解决方案1】:

    尝试以下方法之一:

    1. 有内置的 methods of the System.Gadget.Settings object 可用于从 Settings.ini 文件(存储在 C:\Users\[user]\AppData\Local\Microsoft\Windows Sidebar 中)读取/写入,但是如果小工具关闭或卸载,此信息将丢失。

    2. 使用FileSystemObject 在任何地方创建或读取或写入文件夹/文件。限制:文件只能保存为 unicode 或 ascii。

    3. 使用ADO Stream object 在任何地方创建或读取或写入文件。限制:不能创建文件夹 - 必须与 FileSystemObject 一起使用。优点:可以使用您计算机上存在的任何代码页。

    由于我喜欢使用 utf-8 保存文本文件,下面的示例使用第三种方法,但您可能会决定放弃一些错误处理。 (注意 - 此示例基于 script published by Andrew Urquhart

    幸运的是,侧边栏可以使用knownfoldersknownfolderpaths,因此查找文档文件夹的路径就像

    var docs = System.Shell.knownFolderPath("Documents");
    

    请记住,反斜杠是 javascript 中的转义字符,因此字符串中的路径必须将其反斜杠加倍。

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>NeverForget</title>
    <style type="text/css">
    <!--
    body {margin:0;width:300px;height:200px;background-color:transparent;padding:10px;}
    #gadgetContent {width:280px;height:144px;overflow:auto;border:1px solid black;background-color:#eee;}
    button {margin-left:66px;margin-top:10px;}
    #message {display:none;width:280px;height:180px;position:absolute;top:10px;left:10px;background-color:#eee;border:1px solid red;}
    #messageContent {width:278px;height:144px;word-wrap:break-word;overflow-y:auto;}
    #newButton {position:absolute;bottom:10px;left:54px;}
    -->
    </style>
    <script type="text/jscript">
    //globals
    var myFolderPath=System.Shell.knownFolderPath("Documents")+"\\myFiles";
    //end globals
    function showMessage(msg){
     message.style.display="block";
     messageContent.innerText=msg;
    }
    function closeMessage(){
     message.style.display="none";
     messageContent.innerText="";
    }
    function loadFile(strAbsoluteFilePath, strCharSet){
     var adReadAll=-1, adReadLine=-2, strFileContents="", objStream=new ActiveXObject("ADODB.Stream"), fso=new ActiveXObject("Scripting.FileSystemObject");
     try{
      if(!strAbsoluteFilePath){
       throw new Error(1, "Required parameter \"strAbsoluteFilePath\" was not defined");
      }
      if(!strCharSet){
       throw new Error(2, "Required parameter \"strCharSet\" was not defined");
      }
      if(!fso.FolderExists(myFolderPath)){
       throw new Error(3, "Folder \""+myFolderPath+"\" does not exist");
      }
      objStream.Open();
      try{
       objStream.CharSet=strCharSet;
       objStream.LoadFromFile(strAbsoluteFilePath);
       strFileContents=objStream.ReadText(adReadAll);
       gadgetContent.innerText=strFileContents;
      }
      catch(err){
       throw new Error(err.number, "Loading failed:\r\n" + err.description);
      }
      finally{
       objStream.Close(); // Always close the stream regardless of what happens
       objStream=null;
       fso=null;
      }
     }
     catch(err){
      showMessage("Function loadFile() failed with parameters strAbsoluteFilePath=\"" + strAbsoluteFilePath + "\", strCharSet=\"" + strCharSet + "\". Message=\r\n" + err.description+"\r\nError Number: "+err.number);
     }
    }
    function saveFile(strAbsoluteFilePath, strCharSet, strFileContents, blnOverwrite){
     var adSaveCreateNotExist=1, adSaveCreateOverWrite=2, objStream = new ActiveXObject("ADODB.Stream"), fso=new ActiveXObject("Scripting.FileSystemObject");
     try{
      if(!strAbsoluteFilePath){
       throw new Error(1, "Required parameter \"strAbsoluteFilePath\" was not defined");
      }
      if(!strCharSet){
       throw new Error(2, "Required parameter \"strCharSet\" was not defined");
      }
      if(typeof strFileContents != "string"){
       throw new Error(3, "Required parameter \"strFileContents\" was not a string");
      }
      if(!fso.FolderExists(myFolderPath)){
       fso.CreateFolder(myFolderPath);
      }
      objStream.Open();
      try{
       objStream.CharSet=strCharSet;
       objStream.WriteText(strFileContents);
       objStream.SaveToFile(strAbsoluteFilePath, (blnOverwrite ? adSaveCreateOverWrite : adSaveCreateNotExist));
       return true;
      }
      catch(err){
       throw new Error(err.number, "SaveToFile failed:\r\n" + err.description);
      }
      finally{
       objStream.Close(); // Always close the stream regardless of what happens
       objStream=null;
       fso=null;
      }
      return false;
     }
     catch(err){
      showMessage("Function saveFile() failed with parameters strAbsoluteFilePath=\"" + strAbsoluteFilePath + "\", strCharSet=\"" + strCharSet + "\", strFileContents=\"" + strFileContents + "\", blnOverwrite=\"" + blnOverwrite + "\". Message=\r\n" + err.description+"\r\nError Number: "+err.number);
     }
    }
    function init(){
     loadButton.onclick=function(){loadFile(myFolderPath+"\\myFile.txt","utf-8");};
     saveButton.onclick=function(){saveFile(myFolderPath+"\\myFile.txt", "utf-8", gadgetContent.innerText, true);};
     closeButton.onclick=closeMessage;
    }
    </script>
    </head>
    <body onload="init()">
     <textarea id="gadgetContent">Bonjour</textarea>
     <div id="message">
      <div id="messageContent"></div>
      <div id="newButton"><button id="closeButton">Close</button></div>
     </div>
     <button id="loadButton">Load</button><button id="saveButton">Save</button>
    </body>
    </html>

    【讨论】:

    • 哇非常感谢@mystifeid!这真是太棒了!最后一件事:如何使文本框和正文完全透明?即我们在文本下看到桌面,没有白色/灰色背景。
    • 尝试使用 1px x 1px 透明 png 作为 g:background(请参阅备注here 了解如何在 html 中声明),然后将其调整为 100% 的宽度和高度(主体大小) onload 函数。(即:imgBackground.style.width="100%"; imgBackground.style.height="100%";)
    • 通过 html 或普通 javascript 方法添加的黑色文本在使用透明 g:background 时可能会显示为洋红色。您可以通过添加/删除文本为g:text objects 来解决此问题。顺便说一句,这超出了您最初问题的范围。请尝试搜索、阅读并自己进行体面的尝试。如果几天后您无法取得更多进展,请提出另一个问题并提供您的 css/script/html。
    猜你喜欢
    • 2010-09-15
    • 2015-06-01
    • 1970-01-01
    • 2023-03-22
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    • 2011-02-04
    相关资源
    最近更新 更多