{"id":279,"date":"2018-10-16T15:35:41","date_gmt":"2018-10-16T07:35:41","guid":{"rendered":"http:\/\/www.yinyubo.cn\/?p=279"},"modified":"2022-05-17T09:45:34","modified_gmt":"2022-05-17T01:45:34","slug":"electron-2","status":"publish","type":"post","link":"https:\/\/www.yinyubo.com\/?p=279","title":{"rendered":"electron\u5b9e\u4f8b\uff08\u4e00\u4e2a\u4f8b\u5b50\u6559\u4f60\u5165\u95e8\uff09"},"content":{"rendered":"<h1><strong>\u603b\u89c8<\/strong><\/h1>\n<p>electron\u662f\u76ee\u524d\u6700\u6d41\u884c\u7684\u684c\u9762\u7ea7\u5f00\u53d1\u6846\u67b6\u3002\u4f5c\u4e3a\u4e00\u4e2a\u8001\u6d4b\u5f00\uff0c\u4e0d\u80fd\u505c\u4e0b\u5b66\u4e60\u7684\u811a\u672c\u3002\u4e0b\u9762\u6211\u7528\u4e00\u4e2aelectron\u5236\u4f5c\u4e86\u4e00\u4e2a\u767b\u9646\u7684\u4f8b\u5b50\uff0c\u7528\u5230\u7684\u529f\u80fd\u5305\u542b\u7b80\u5355\u7684ipc\u901a\u4fe1\uff0c\u9875\u9762\u8df3\u8f6c\uff0c\u5f15\u5165jquery,bootstrap\u3002\u4e0b\u9762\u5f00\u59cb\u6211\u4eec\u7684\u5206\u89e3\u52a8\u4f5c\u3002<\/p>\n<h2><strong>\u6587\u4ef6\u76ee\u5f55<\/strong><\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-280 size-full\" src=\"https:\/\/yinyubo-1257235934.cos.ap-nanjing.myqcloud.com\/content\/electron\u6587\u4ef6\u76ee\u5f55.png\" alt=\"\" width=\"297\" height=\"350\" \/><br \/>\n\u8be5\u5de5\u7a0b\u662f\u6211\u4e0b\u8f7d\u4e86github\u4e0a\u7684quick-start\u7684\u5de5\u7a0b\uff0c\u81ea\u5df1\u4fee\u6539\u4e86main.js\u548cindex.html\u3002\u6dfb\u52a0\u4e86\u4e00\u4e2aweb-content\/welcome.html\u9875\u9762\u3002<\/p>\n<ul>\n<li>main.js\u662f\u540e\u7aef\u4e3b\u8fdb\u7a0b\u6587\u4ef6<\/li>\n<li>index.html\u662f\u9996\u9875<\/li>\n<li>welcome.html\u662f\u767b\u5f55\u6210\u529f\u4e4b\u540e\u7684\u9875\u9762<\/li>\n<\/ul>\n<h2><strong>main.js(\u4e3b\u8fdb\u7a0b)<\/strong><\/h2>\n<p>main.js\u4e5f\u5c31\u662f\u4e3b\u8fdb\u7a0b\u3002\u8fd9\u91cc\u6211\u8d34\u51fa\u7528\u5904\u6700\u5927\u7684ipc\u524d\u540e\u7aef\u901a\u4fe1\u7684\u4ee3\u7801\u3002<br \/>\n\u8bb2\u89e3\uff1a\u540e\u7aef\u7528\u7684\u662fipcMain\u3002\u524d\u7aef\u7528\u7684\u662fipcRender\uff0c\u8fd9\u4e2a\u5f00\u53d1\u6a21\u5f0f\u662f\u53d1\u5e03\/\u8ba2\u9605\u6a21\u5f0f\u3002ipc.on()\u53d1\u5e03\u4e00\u4e2a\u2018login\u2019\u7684\u76d1\u542c\u6d88\u606f\u3002\u5f53\u524d\u7aef\u53d1\u8d77\u2018login\u2019\u7684\u8bf7\u6c42\u65f6\uff0c\u89e6\u53d1\u540e\u53f0\u7684funtion()\u3002mainWindow\u63a7\u5236\u9875\u9762\u8df3\u8f6c\u3002<span style=\"font-size: 1rem;\">mainWindow.loadFile(&#8216;.\/web-content\/welcome.html&#8217;)\u7528\u5f53\u524d\u7a97\u53e3\u6253\u5f00\u4e00\u4e2awelcome\u9875\u9762\u3002<\/span><\/p>\n<pre class=\"lang:js decode:true\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">let mainWindow\nconst ipc = require('electron').ipcMain\nipc.on('login', function(event, user_name,user_password) {\n  result='\u767b\u5f55\u5931\u8d25';\n  console.log(user_name,user_password);  \/\/ \u6253\u5370 \"\u7528\u6237\u540d\u5bc6\u7801\"\n  if(user_name==\"1\" &amp;&amp; user_password==\"2\"){\n     result='\u767b\u5f55\u6210\u529f';\n     console.log(result);\n     \/\/\u767b\u5f55\u6210\u529f\u4e4b\u540e\u6253\u5f00welcome\u9875\u9762\n     mainWindow.loadFile('.\/web-content\/welcome.html')\n  }else{\n    console.log(result);\n  }\n  \/\/\u56e0\u4e3a\u662f\u540c\u6b65\u7b56\u7565\uff0c\u6240\u4ee5\u7528event.returnValue\n  event.returnValue = result;\n  console.log(\"end\");\n});<\/code><\/pre>\n<h2><strong>index.html\uff08\u9996\u9875\uff09<\/strong><\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-283 size-full\" src=\"https:\/\/yinyubo-1257235934.cos.ap-nanjing.myqcloud.com\/content\/electron\u6587\u4ef6-1.png\" alt=\"\" width=\"497\" height=\"354\" \/><br \/>\n\u9996\u9875\u6548\u679c\u5982\u4e0a\u56fe\uff0c\u7528\u6237\u540d\u8f93\u51651\uff0c\u5bc6\u7801\u8f93\u51652\uff0c\u70b9\u51fbsubmit\u6309\u94ae\u5f00\u59cb\u767b\u9646\u3002<br \/>\n\u4e3a\u4e86\u5f15\u7528bootstrap\uff0c\u6211\u4eec\u5728\u6700\u524d\u9762\u7684&lt;head&gt;\u4e0b\u52a0\u5165bootstrap\u548cjquery<\/p>\n<pre class=\"lang:python decode:true\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">&lt;head&gt;\n    &lt;title&gt;\u6d4b\u8bd5\u5f00\u53d1\u7b14\u8bb0&lt;\/title&gt;\n    &lt;meta charset=\"utf-8\"&gt;\n    &lt;link rel=\"stylesheet\" type=\"text\/css\" href=\"node_modules\/bootstrap\/dist\/css\/bootstrap.min.css\"&gt;\n    &lt;script&gt;if (typeof module === 'object') {window.module = module; module = undefined;}&lt;\/script&gt;\n    &lt;script&gt;if (window.module) module = window.module;&lt;\/script&gt;\n    &lt;script src=\"https:\/\/cdn.bootcss.com\/jquery\/1.12.4\/jquery.min.js\"&gt;&lt;\/script&gt;\n    &lt;script src=\"https:\/\/cdn.bootcss.com\/bootstrap\/4.1.3\/js\/bootstrap.min.js\"&gt;&lt;\/script&gt;\n&lt;\/head&gt;<\/code><\/pre>\n<div>\n<div>ipcRenderer<span style=\"font-size: 1rem;\">\u8fde\u63a5\u540e\u7aef\uff0c\u901a\u8fc7<\/span><span style=\"font-size: 1rem;\">sendSync\u6765\u540c\u6b65\u53d1\u9001\u6d88\u606f\uff0c<\/span><span style=\"font-size: 1rem;\">\u53d1\u9001\u7528\u6237\u540d\u548c\u5bc6\u7801\u5230\u540e\u7aef\uff0c\u5982\u679c\u4e3a\u771f\uff0c\u65b0\u7a97\u53e3\u6253\u5f00github\u9996\u9875\u3002\u7528\u4e86jquery\u7684$\u5b9a\u4f4d\u5143\u7d20\u3002\u7528\u4e86.val()\u83b7\u5f97\u5143\u7d20\u91cc\u586b\u5199\u7684\u503c\uff0c.afrer()\u5728\u6307\u5b9a\u4f4d\u7f6e\u540e\u6dfb\u52a0\u5143\u7d20\u3002<\/span><\/div>\n<\/div>\n<pre class=\"lang:python decode:true\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">&lt;script&gt;\n  function connectMain() {\n    const ipcRenderer = require('electron').ipcRenderer;\n    var user_name=$(\"#input_username\").val()\n    var user_password=$(\"#input_userpassword\").val()\n    result=ipcRenderer.sendSync('login',user_name,user_password)\n    if(result==\"\u767b\u5f55\u6210\u529f\"){\n      const remote = require('electron').remote;\n      const BrowserWindow = remote.BrowserWindow;\n      var win = new BrowserWindow({ width: 800, height: 600 });\n      win.loadURL('https:\/\/github.com');\n      \/\/ BrowserWindow.loadURL('https:\/\/github.com');\n    }\n    else{\n      console.log($(\"#input_username\"))\n      var txt1=\"&lt;div&gt;\u5bc6\u7801\u9519\u8bef&lt;\/div&gt;\";\n      $(\"#input_userpassword\").after(txt1)\n    }\n    console.log(result)\n  }\n&lt;\/script&gt;<\/code><\/pre>\n<h2><strong>\u5b8c\u6574\u4ee3\u7801<\/strong><\/h2>\n<h6>main.js<\/h6>\n<pre class=\"lang:js decode:true\" title=\"main.js\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">\/\/ Modules to control application life and create native browser window\nconst {app, BrowserWindow,Menu,shell} = require('electron')\n\/\/ Keep a global reference of the window object, if you don't, the window will\n\/\/ be closed automatically when the JavaScript object is garbage collected.\nlet mainWindow\nconst ipc = require('electron').ipcMain\nipc.on('login', function(event, user_name,user_password) {\n  result='\u767b\u5f55\u5931\u8d25';\n  console.log(user_name,user_password);  \/\/ \u6253\u5370 \"\u7528\u6237\u540d\u5bc6\u7801\"\n  if(user_name==\"1\" &amp;&amp; user_password==\"2\"){\n     result='\u767b\u5f55\u6210\u529f';\n     console.log(result);\n     \/\/\u767b\u5f55\u6210\u529f\u4e4b\u540e\u6253\u5f00welcome\u9875\u9762\n     mainWindow.loadFile('.\/web-content\/welcome.html')\n  }else{\n    console.log(result);\n  }\n  \/\/\u56e0\u4e3a\u662f\u540c\u6b65\u7b56\u7565\uff0c\u6240\u4ee5\u7528event.returnValue\n  event.returnValue = result;\n  console.log(\"end\");\n});\nfunction createWindow () {\n  \/\/ Create the browser window.\n  mainWindow = new BrowserWindow({width: 800, height: 600})\n  \/\/ and load the index.html of the app.\n  mainWindow.loadFile('index.html')\n  \/\/ Open the DevTools.\n  \/\/ mainWindow.webContents.openDevTools()\n  \/\/ Emitted when the window is closed.\n  mainWindow.on('closed', function () {\n    \/\/ Dereference the window object, usually you would store windows\n    \/\/ in an array if your app supports multi windows, this is the time\n    \/\/ when you should delete the corresponding element.\n    mainWindow = null\n  })\n}\n\/\/ This method will be called when Electron has finished\n\/\/ initialization and is ready to create browser windows.\n\/\/ Some APIs can only be used after this event occurs.\napp.on('ready', () =&gt; {\n  createWindow()\n})\n\/\/ Quit when all windows are closed.\napp.on('window-all-closed', function () {\n  \/\/ On OS X it is common for applications and their menu bar\n  \/\/ to stay active until the user quits explicitly with Cmd + Q\n  if (process.platform !== 'darwin') {\n    app.quit()\n  }\n})\napp.on('activate', function () {\n  \/\/ On OS X it's common to re-create a window in the app when the\n  \/\/ dock icon is clicked and there are no other windows open.\n  if (mainWindow === null) {\n    createWindow()\n  }\n})\n\/\/ In this file you can include the rest of your app's specific main process\n\/\/ code. You can also put them in separate files and require them here.\n<\/code><\/pre>\n<h6>index.html<\/h6>\n<pre class=\"lang:js decode:true\" title=\"index.html\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n    &lt;title&gt;\u6d4b\u8bd5\u5f00\u53d1\u7b14\u8bb0&lt;\/title&gt;\n    &lt;meta charset=\"utf-8\"&gt;\n    &lt;link rel=\"stylesheet\" type=\"text\/css\" href=\"node_modules\/bootstrap\/dist\/css\/bootstrap.min.css\"&gt;\n    &lt;script&gt;if (typeof module === 'object') {window.module = module; module = undefined;}&lt;\/script&gt;\n    &lt;script&gt;if (window.module) module = window.module;&lt;\/script&gt;\n    &lt;script src=\"https:\/\/cdn.bootcss.com\/jquery\/1.12.4\/jquery.min.js\"&gt;&lt;\/script&gt;\n    &lt;script src=\"https:\/\/cdn.bootcss.com\/bootstrap\/4.1.3\/js\/bootstrap.min.js\"&gt;&lt;\/script&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n  &lt;div class=\"container-fluid\"&gt;\n    &lt;h1 class=\"page-header\"&gt;electron-bootstrap\u5b9e\u4f8b&lt;\/h1&gt;\n    &lt;div class=\"row\"&gt;\n      &lt;div class=\"col-md-offset-8 col-sm-offset-3 col-md-4 col-sm-6\"&gt;\n        &lt;div class=\"panel panel-default\"&gt;\n          &lt;div class=\"panel-body\"&gt;\n            &lt;form&gt;\n              &lt;div class=\"form-group\"&gt;\n                &lt;label for=\"input_username\"&gt;\u7528\u6237\u540d&lt;\/label&gt;\n                &lt;input type=\"email\" class=\"form-control\" id=\"input_username\" placeholder=\"Email\"&gt;\n              &lt;\/div&gt;\n              &lt;div class=\"form-group\"&gt;\n                &lt;label for=\"input_userpassword\"&gt;\u5bc6\u7801&lt;\/label&gt;\n                &lt;input type=\"password\" class=\"form-control\" id=\"input_userpassword\" placeholder=\"Password\"&gt;\n              &lt;\/div&gt;\n              &lt;div class=\"checkbox\"&gt;\n                &lt;label&gt;\n                  &lt;input type=\"checkbox\"&gt; \u8bb0\u4f4f\u5bc6\u7801\n                &lt;\/label&gt;\n              &lt;\/div&gt;\n              &lt;button type=\"submit\" id=\"abc\" class=\"btn btn-default\" onclick=\"connectMain()\"&gt;\u5f00\u59cb\u767b\u9646&lt;\/button&gt;\n            &lt;\/form&gt;\n          &lt;\/div&gt;\n        &lt;\/div&gt;\n      &lt;\/div&gt;\n    &lt;\/div&gt;\n  &lt;\/div&gt;\n&lt;script&gt;\n  function connectMain() {\n    const ipcRenderer = require('electron').ipcRenderer;\n    var user_name=$(\"#input_username\").val()\n    var user_password=$(\"#input_userpassword\").val()\n    result=ipcRenderer.sendSync('login',user_name,user_password)\n    if(result==\"\u767b\u5f55\u6210\u529f\"){\n      const remote = require('electron').remote;\n      const BrowserWindow = remote.BrowserWindow;\n      var win = new BrowserWindow({ width: 800, height: 600 });\n      win.loadURL('https:\/\/github.com');\n      \/\/ BrowserWindow.loadURL('https:\/\/github.com');\n    }\n    else{\n      console.log($(\"#input_username\"))\n      var txt1=\"&lt;div&gt;\u5bc6\u7801\u9519\u8bef&lt;\/div&gt;\";\n      $(\"#input_userpassword\").after(txt1)\n    }\n    console.log(result)\n  }\n&lt;\/script&gt;\n&lt;\/body&gt;\n    &lt;script src=\".\/renderer.js\"&gt;&lt;\/script&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n<h6>welcome.html<\/h6>\n<pre class=\"lang:python decode:true\" title=\"welcome.html\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n    &lt;title&gt;es2105\u7684\u5199\u6cd5&lt;\/title&gt;\n    &lt;meta charset=\"utf-8\"&gt;\n&lt;\/head&gt;\n&lt;body&gt;\nhello,world\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<h6>renderer.js<\/h6>\n<pre class=\"lang:python decode:true\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">function connectMain() {\n    const ipcRenderer = require('electron').ipcRenderer;\n    var user_name=$(\"#input_username\").val()\n    var user_password=$(\"#input_userpassword\").val()\n    result=ipcRenderer.sendSync('login',user_name,user_password)\n    if(result==\"\u767b\u5f55\u6210\u529f\"){\n      const remote = require('electron').remote;\n      const BrowserWindow = remote.BrowserWindow;\n      var win = new BrowserWindow({ width: 800, height: 600 });\n      win.loadURL('https:\/\/github.com');\n      \/\/ BrowserWindow.loadURL('https:\/\/github.com');\n    }\n    else{\n      console.log($(\"#input_username\"))\n      var txt1=\"&lt;div&gt;\u5bc6\u7801\u9519\u8bef&lt;\/div&gt;\";\n      $(\"#input_userpassword\").after(txt1)\n    }\n    console.log(result)\n  }<\/code><\/pre>\n<h6>package.json<\/h6>\n<pre class=\"lang:python decode:true \"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">{\n  \"name\": \"electron-quick-start\",\n  \"version\": \"1.0.0\",\n  \"description\": \"A minimal Electron application\",\n  \"main\": \"main.js\",\n  \"scripts\": {\n    \"start\": \"electron .\"\n  },\n  \"repository\": \"https:\/\/github.com\/electron\/electron-quick-start\",\n  \"keywords\": [\n    \"Electron\",\n    \"quick\",\n    \"start\",\n    \"tutorial\",\n    \"demo\"\n  ],\n  \"author\": \"GitHub\",\n  \"license\": \"CC0-1.0\",\n  \"devDependencies\": {\n    \"electron\": \"^2.0.0\"\n  },\n  \"dependencies\": {\n    \"bootstrap\": \"^4.1.3\",\n    \"electron-json-storage\": \"^4.1.4\",\n    \"jquery\": \"^1.12.4\",\n    \"popper.js\": \"^1.14.4\"\n  }\n}\n<\/code><\/pre>\n<h2>\u53c2\u8003<\/h2>\n<p>https:\/\/wizardforcel.gitbooks.io\/electron-doc\/content\/api\/ipc-main.html<br \/>\nhttps:\/\/electronjs.org\/docs\/tutorial\/first-app<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u603b\u89c8 electron\u662f\u76ee\u524d\u6700\u6d41\u884c\u7684\u684c\u9762\u7ea7\u5f00\u53d1\u6846\u67b6\u3002\u4f5c\u4e3a\u4e00\u4e2a\u8001\u6d4b\u5f00\uff0c\u4e0d\u80fd\u505c\u4e0b\u5b66\u4e60\u7684\u811a\u672c\u3002\u4e0b\u9762\u6211\u7528\u4e00\u4e2aelect [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":283,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-279","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-7"],"_links":{"self":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/279","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=279"}],"version-history":[{"count":1,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/279\/revisions"}],"predecessor-version":[{"id":947,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/279\/revisions\/947"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/media\/283"}],"wp:attachment":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}