{"id":47400,"date":"2025-07-30T07:41:57","date_gmt":"2025-07-29T23:41:57","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/47400.html"},"modified":"2025-07-30T07:41:57","modified_gmt":"2025-07-29T23:41:57","slug":"electron-%e5%b0%8f%e7%99%bd%e5%85%a5%e9%97%a8%ef%bc%9a%e4%bb%8e%e9%9b%b6%e5%bc%80%e5%a7%8b%e6%9e%84%e5%bb%ba%e8%b7%a8%e5%b9%b3%e5%8f%b0%e6%a1%8c%e9%9d%a2%e5%ba%94%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/47400.html","title":{"rendered":"Electron \u5c0f\u767d\u5165\u95e8\uff1a\u4ece\u96f6\u5f00\u59cb\u6784\u5efa\u8de8\u5e73\u53f0\u684c\u9762\u5e94\u7528"},"content":{"rendered":"<h2>\u4e00\u3001Electron \u6982\u8ff0\u4e0e\u73af\u5883\u642d\u5efa<\/h2>\n<h4>1.1 \u4ec0\u4e48\u662f Electron<\/h4>\n<p>Electron \u662f\u4e00\u4e2a\u4f7f\u7528 JavaScript\u3001HTML \u548c CSS \u6784\u5efa\u8de8\u5e73\u53f0\u684c\u9762\u5e94\u7528\u7684\u5f00\u6e90\u6846\u67b6\u3002\u5b83\u901a\u8fc7\u5c06 Chromium \u6d4f\u89c8\u5668\u5f15\u64ce\u548c Node.js \u8fd0\u884c\u65f6\u5d4c\u5165\u5230\u4e8c\u8fdb\u5236\u6587\u4ef6\u4e2d&#xff0c;\u5141\u8bb8\u5f00\u53d1\u8005\u4f7f\u7528\u5355\u4e00\u7684 JavaScript \u4ee3\u7801\u5e93\u521b\u5efa\u80fd\u5728 Windows\u3001macOS \u548c Linux \u4e0a\u8fd0\u884c\u7684\u5e94\u7528\u7a0b\u5e8f&#xff0c;\u65e0\u9700\u539f\u751f\u5f00\u53d1\u7ecf\u9a8c\u3002\u7b80\u5355\u6765\u8bf4&#xff0c;Electron \u8ba9 Web \u5f00\u53d1\u8005\u80fd\u591f\u5229\u7528\u4ed6\u4eec\u5df2\u6709\u7684\u524d\u7aef\u6280\u80fd\u6765\u5f00\u53d1\u684c\u9762\u5e94\u7528\u3002<\/p>\n<p>Electron \u7531 GitHub \u5f00\u53d1&#xff0c;\u6700\u521d\u540d\u4e3a Atom Shell&#xff0c;\u73b0\u5728\u662f OpenJS \u57fa\u91d1\u4f1a\u7684\u4e00\u4e2a\u9879\u76ee&#xff0c;\u7531\u6d3b\u8dc3\u7684\u793e\u533a\u8d21\u732e\u8005\u7ef4\u62a4\u3002\u5b83\u7684\u6838\u5fc3\u4f18\u52bf\u5728\u4e8e\u4e00\u6b21\u7f16\u5199&#xff0c;\u5904\u5904\u8fd0\u884c\u7684\u8de8\u5e73\u53f0\u7279\u6027&#xff0c;\u8fd9\u4f7f\u5f97\u5f00\u53d1\u8005\u53ef\u4ee5\u7528\u719f\u6089\u7684\u6280\u672f\u6808\u521b\u5efa\u684c\u9762\u5e94\u7528&#xff0c;\u800c\u65e0\u9700\u4e3a\u6bcf\u4e2a\u64cd\u4f5c\u7cfb\u7edf\u5355\u72ec\u7f16\u5199\u4ee3\u7801\u3002<\/p>\n<h4>1.2 \u4e3a\u4ec0\u4e48\u9009\u62e9 Electron<\/h4>\n<p>\u4f5c\u4e3a\u9762\u5411\u5c0f\u767d\u7684 Electron \u6559\u7a0b&#xff0c;\u6211\u4eec\u9700\u8981\u660e\u786e\u4e3a\u4ec0\u4e48 Electron \u662f\u4e00\u4e2a\u503c\u5f97\u5b66\u4e60\u7684\u6846\u67b6&#xff1a;<\/p>\n<li>\u6280\u672f\u6808\u53cb\u597d&#xff1a;\u5982\u679c\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86 JavaScript\u3001HTML \u548c CSS&#xff0c;\u90a3\u4e48\u5b66\u4e60 Electron \u51e0\u4e4e\u6ca1\u6709\u989d\u5916\u7684\u5b66\u4e60\u6210\u672c\u3002<\/li>\n<li>\u8de8\u5e73\u53f0\u652f\u6301&#xff1a;Electron \u5e94\u7528\u53ef\u4ee5\u5728 Windows\u3001macOS \u548c Linux \u4e09\u5927\u4e3b\u6d41\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8fd0\u884c&#xff0c;\u5927\u5927\u63d0\u9ad8\u4e86\u5f00\u53d1\u6548\u7387\u3002<\/li>\n<li>\u5f3a\u5927\u7684\u751f\u6001\u7cfb\u7edf&#xff1a;Electron \u62e5\u6709\u4e30\u5bcc\u7684\u7b2c\u4e09\u65b9\u5e93\u548c\u5de5\u5177&#xff0c;\u53ef\u4ee5\u8f7b\u677e\u6269\u5c55\u5e94\u7528\u529f\u80fd&#xff0c;\u5982\u6587\u4ef6\u64cd\u4f5c\u3001\u7f51\u7edc\u8bf7\u6c42\u7b49\u3002<\/li>\n<li>\u5f00\u6e90\u4e14\u6d3b\u8dc3\u7684\u793e\u533a&#xff1a;\u4f5c\u4e3a\u5f00\u6e90\u9879\u76ee&#xff0c;Electron \u6709\u4e00\u4e2a\u5e9e\u5927\u4e14\u6d3b\u8dc3\u7684\u793e\u533a\u652f\u6301&#xff0c;\u8fd9\u610f\u5473\u7740\u4f60\u53ef\u4ee5\u8f7b\u677e\u627e\u5230\u8d44\u6e90\u3001\u6559\u7a0b\u548c\u89e3\u51b3\u95ee\u9898\u7684\u65b9\u6cd5\u3002<\/li>\n<li>\u4f01\u4e1a\u7ea7\u5e94\u7528\u6848\u4f8b&#xff1a;\u8bb8\u591a\u77e5\u540d\u7684\u684c\u9762\u5e94\u7528&#xff0c;\u5982 VS Code\u3001Atom \u7f16\u8f91\u5668\u3001Discord \u7b49&#xff0c;\u90fd\u662f\u4f7f\u7528 Electron \u6784\u5efa\u7684&#xff0c;\u8bc1\u660e\u4e86\u5176\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u7684\u53ef\u9760\u6027\u3002<\/li>\n<h4>1.3 \u73af\u5883\u642d\u5efa<\/h4>\n<p>\u5728\u5f00\u59cb\u7f16\u5199\u7b2c\u4e00\u4e2a Electron \u5e94\u7528\u4e4b\u524d&#xff0c;\u6211\u4eec\u9700\u8981\u642d\u5efa\u5fc5\u8981\u7684\u5f00\u53d1\u73af\u5883\u3002<\/p>\n<h5>1.3.1 \u5b89\u88c5 Node.js<\/h5>\n<p>Electron \u57fa\u4e8e Node.js&#xff0c;\u56e0\u6b64\u9996\u5148\u9700\u8981\u5b89\u88c5 Node.js\u3002Node.js \u7684\u5b89\u88c5\u7a0b\u5e8f\u4f1a\u81ea\u52a8\u5b89\u88c5 npm&#xff08;Node.js \u5305\u7ba1\u7406\u5668&#xff09;&#xff0c;\u8fd9\u662f\u6211\u4eec\u5b89\u88c5 Electron \u548c\u5176\u4ed6\u4f9d\u8d56\u7684\u4e3b\u8981\u5de5\u5177\u3002<\/p>\n<p>\u5b89\u88c5\u6b65\u9aa4&#xff1a;<\/p>\n<li>\u8bbf\u95eeNode.js\u00a0\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u9002\u5408\u4f60\u64cd\u4f5c\u7cfb\u7edf\u7684\u5b89\u88c5\u7a0b\u5e8f\u3002<\/li>\n<li>\u8fd0\u884c\u5b89\u88c5\u7a0b\u5e8f&#xff0c;\u6309\u7167\u63d0\u793a\u5b8c\u6210\u5b89\u88c5\u3002<\/li>\n<li>\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f&#xff1a;\u6253\u5f00\u7ec8\u7aef&#xff08;\u547d\u4ee4\u63d0\u793a\u7b26\u6216 PowerShell&#xff09;&#xff0c;\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4&#xff1a;<\/li>\n<p>node -v<\/p>\n<p>npm -v<\/p>\n<p>\u5982\u679c\u5b89\u88c5\u6210\u529f&#xff0c;\u8fd9\u4e24\u4e2a\u547d\u4ee4\u4f1a\u5206\u522b\u8f93\u51fa\u7248\u672c\u53f7\u3002\u672c\u4e66\u5047\u8bbe\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86 Node.js 20 \u6216\u66f4\u9ad8\u7248\u672c&#xff0c;\u56e0\u4e3a Electron \u5728 2025 \u5e74\u521d\u5df2\u7ecf\u5c06\u5176 npm \u751f\u6001\u7cfb\u7edf\u8fc1\u79fb\u5230 Node.js 22 \u4f5c\u4e3a\u6700\u4f4e\u652f\u6301\u7248\u672c\u3002<\/p>\n<h5>1.3.2 \u5b89\u88c5 Electron<\/h5>\n<p>\u5b89\u88c5 Electron \u6709\u4e24\u79cd\u4e3b\u8981\u65b9\u5f0f&#xff1a;\u5168\u5c40\u5b89\u88c5\u548c\u672c\u5730\u5b89\u88c5\u3002\u5bf9\u4e8e\u5f00\u53d1\u5e94\u7528&#xff0c;\u5efa\u8bae\u672c\u5730\u5b89\u88c5&#xff0c;\u8fd9\u6837\u53ef\u4ee5\u66f4\u597d\u5730\u63a7\u5236\u7248\u672c\u5e76\u907f\u514d\u6743\u9650\u95ee\u9898\u3002<\/p>\n<p>\u672c\u5730\u5b89\u88c5\u6b65\u9aa4&#xff1a;<\/p>\n<li>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u9879\u76ee\u76ee\u5f55&#xff1a;<\/li>\n<p>mkdir my-electron-app<\/p>\n<p>cd my-electron-app<\/p>\n<li>\u521d\u59cb\u5316 npm \u9879\u76ee&#xff1a;<\/li>\n<p>npm init -y<\/p>\n<p>\u8fd9\u4f1a\u5728\u9879\u76ee\u76ee\u5f55\u4e2d\u751f\u6210\u4e00\u4e2apackage.json\u6587\u4ef6&#xff0c;\u7528\u4e8e\u7ba1\u7406\u9879\u76ee\u4f9d\u8d56\u548c\u914d\u7f6e\u3002<\/p>\n<li>\u5b89\u88c5 Electron \u4f5c\u4e3a\u5f00\u53d1\u4f9d\u8d56&#xff1a;<\/li>\n<p>npm install electron &#8211;save-dev<\/p>\n<p>\u5168\u5c40\u5b89\u88c5&#xff08;\u53ef\u9009&#xff09;&#xff1a;<\/p>\n<p>\u5982\u679c\u4f60\u60f3\u5728\u547d\u4ee4\u884c\u4e2d\u76f4\u63a5\u4f7f\u7528 Electron \u547d\u4ee4&#xff0c;\u53ef\u4ee5\u5168\u5c40\u5b89\u88c5 Electron&#xff1a;<\/p>\n<p>npm install electron -g<\/p>\n<h5>1.3.3 \u9a8c\u8bc1 Electron \u5b89\u88c5<\/h5>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e&#xff0c;\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u9a8c\u8bc1 Electron \u662f\u5426\u6b63\u786e\u5b89\u88c5&#xff1a;<\/p>\n<li>\u5728\u9879\u76ee\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3amain.js\u7684\u6587\u4ef6&#xff0c;\u8fd9\u662f Electron \u5e94\u7528\u7684\u4e3b\u8fdb\u7a0b\u6587\u4ef6\u3002<\/li>\n<li>\u5728main.js\u4e2d\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801&#xff1a;<\/li>\n<p>const { app, BrowserWindow } &#061; require(&#039;electron&#039;);<\/p>\n<p>function createWindow() {<\/p>\n<p>const win &#061; new BrowserWindow({<\/p>\n<p>width: 800,<\/p>\n<p>height: 600,<\/p>\n<p>webPreferences: {<\/p>\n<p>nodeIntegration: true<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>win.loadFile(&#039;index.html&#039;);<\/p>\n<p>}<\/p>\n<p>app.whenReady().then(createWindow);<\/p>\n<p>app.on(&#039;window-all-closed&#039;, () &#061;&gt; {<\/p>\n<p>if (process.platform !&#061;&#061; &#039;darwin&#039;) {<\/p>\n<p>app.quit();<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>app.on(&#039;activate&#039;, () &#061;&gt; {<\/p>\n<p>if (BrowserWindow.getAllWindows().length &#061;&#061;&#061; 0) {<\/p>\n<p>createWindow();<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<li>\u521b\u5efa\u4e00\u4e2aindex.html\u6587\u4ef6&#xff0c;\u4f5c\u4e3a\u5e94\u7528\u7684\u4e3b\u754c\u9762&#xff1a;<\/li>\n<p>&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>&lt;meta charset&#061;&#034;UTF-8&#034;&gt;<\/p>\n<p>&lt;title&gt;Hello Electron!&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&lt;h1&gt;Hello Electron!&lt;\/h1&gt;<\/p>\n<p>&lt;p&gt;\u8fd9\u662f\u4f60\u7684\u7b2c\u4e00\u4e2aElectron\u5e94\u7528\u3002&lt;\/p&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<li>\u5728package.json\u7684scripts\u90e8\u5206\u6dfb\u52a0\u542f\u52a8\u547d\u4ee4&#xff1a;<\/li>\n<p>&#034;scripts&#034;: {<\/p>\n<p>&#034;start&#034;: &#034;electron .&#034;<\/p>\n<p>}<\/p>\n<li>\u542f\u52a8\u5e94\u7528&#xff1a;<\/li>\n<p>npm start<\/p>\n<p>\u5982\u679c\u4e00\u5207\u987a\u5229&#xff0c;\u4f60\u5e94\u8be5\u4f1a\u770b\u5230\u4e00\u4e2a\u663e\u793a &#034;Hello Electron!&#034; \u7684\u7a97\u53e3&#xff0c;\u8fd9\u8868\u793a\u4f60\u7684 Electron \u73af\u5883\u5df2\u7ecf\u642d\u5efa\u6210\u529f\u3002<\/p>\n<h4>1.4 Electron \u5e94\u7528\u7684\u57fa\u672c\u7ed3\u6784<\/h4>\n<p>\u5728\u6df1\u5165\u5b66\u4e60 Electron \u7684 API \u4e4b\u524d&#xff0c;\u4e86\u89e3 Electron \u5e94\u7528\u7684\u57fa\u672c\u7ed3\u6784\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002Electron \u5e94\u7528\u4e3b\u8981\u7531\u4e24\u90e8\u5206\u7ec4\u6210&#xff1a;\u4e3b\u8fdb\u7a0b\u548c\u6e32\u67d3\u8fdb\u7a0b\u3002<\/p>\n<h5>1.4.1 \u4e3b\u8fdb\u7a0b<\/h5>\n<p>\u4e3b\u8fdb\u7a0b\u662f Electron \u5e94\u7528\u7684\u5165\u53e3\u70b9&#xff0c;\u5b83\u8d1f\u8d23\u521b\u5efa\u548c\u7ba1\u7406\u5e94\u7528\u7a97\u53e3\u3002\u5728\u4e3b\u8fdb\u7a0b\u4e2d&#xff0c;\u4f60\u53ef\u4ee5\u4f7f\u7528 Electron \u63d0\u4f9b\u7684\u5404\u79cd\u6a21\u5757\u6765\u63a7\u5236\u5e94\u7528\u7684\u751f\u547d\u5468\u671f\u3001\u521b\u5efa\u7a97\u53e3\u3001\u5904\u7406\u7cfb\u7edf\u4e8b\u4ef6\u7b49\u3002<\/p>\n<p>\u4e3b\u8fdb\u7a0b\u7684\u7279\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u4e3b\u8fdb\u7a0b\u6587\u4ef6&#xff08;\u901a\u5e38\u662fmain.js&#xff09;\u5728\u5e94\u7528\u542f\u52a8\u65f6\u88ab\u6267\u884c\u3002<\/li>\n<\/ul>\n<ul>\n<li>\u4f7f\u7528app\u6a21\u5757\u7ba1\u7406\u5e94\u7528\u7684\u751f\u547d\u5468\u671f\u3002<\/li>\n<\/ul>\n<ul>\n<li>\u4f7f\u7528BrowserWindow\u6a21\u5757\u521b\u5efa\u548c\u7ba1\u7406\u5e94\u7528\u7a97\u53e3\u3002<\/li>\n<\/ul>\n<ul>\n<li>\u53ef\u4ee5\u8bbf\u95ee Node.js \u7684\u6240\u6709 API\u3002<\/li>\n<\/ul>\n<ul>\n<li>\u4e3b\u8fdb\u7a0b\u4e2d\u7684\u6bcf\u4e2aBrowserWindow\u5b9e\u4f8b\u90fd\u4f1a\u5728\u6e32\u67d3\u8fdb\u7a0b\u4e2d\u521b\u5efa\u4e00\u4e2a\u5bf9\u5e94\u7684\u7f51\u9875\u3002<\/li>\n<\/ul>\n<h5>1.4.2 \u6e32\u67d3\u8fdb\u7a0b<\/h5>\n<p>\u6e32\u67d3\u8fdb\u7a0b\u8d1f\u8d23\u663e\u793a\u7528\u6237\u754c\u9762&#xff0c;\u6bcf\u4e2aBrowserWindow\u5b9e\u4f8b\u90fd\u4f1a\u8fd0\u884c\u5728\u81ea\u5df1\u7684\u6e32\u67d3\u8fdb\u7a0b\u4e2d\u3002\u6e32\u67d3\u8fdb\u7a0b\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u8fd0\u884c\u5728 Chromium \u6d4f\u89c8\u5668\u4e2d\u7684\u7f51\u9875&#xff0c;\u53ef\u4ee5\u4f7f\u7528\u6807\u51c6\u7684 Web \u6280\u672f&#xff08;HTML\u3001CSS\u3001JavaScript&#xff09;\u6765\u6784\u5efa\u7528\u6237\u754c\u9762\u3002<\/p>\n<p>\u6e32\u67d3\u8fdb\u7a0b\u7684\u7279\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u6bcf\u4e2a\u6e32\u67d3\u8fdb\u7a0b\u90fd\u662f\u72ec\u7acb\u7684&#xff0c;\u5b83\u4eec\u4e4b\u95f4\u4e0d\u80fd\u76f4\u63a5\u901a\u4fe1\u3002<\/li>\n<\/ul>\n<ul>\n<li>\u9ed8\u8ba4\u60c5\u51b5\u4e0b&#xff0c;\u6e32\u67d3\u8fdb\u7a0b\u4e2d\u7684 JavaScript \u65e0\u6cd5\u76f4\u63a5\u8bbf\u95ee Node.js \u7684 API\u3002<\/li>\n<\/ul>\n<ul>\n<li>\u53ef\u4ee5\u901a\u8fc7ipcRenderer\u548cipcMain\u6a21\u5757\u4e0e\u4e3b\u8fdb\u7a0b\u8fdb\u884c\u901a\u4fe1\u3002<\/li>\n<\/ul>\n<ul>\n<li>\u53ef\u4ee5\u4f7f\u7528\u6807\u51c6\u7684 Web APIs&#xff0c;\u5982 DOM \u64cd\u4f5c\u3001fetch \u7b49\u3002<\/li>\n<\/ul>\n<h5>1.4.3 \u8fdb\u7a0b\u95f4\u901a\u4fe1<\/h5>\n<p>\u7531\u4e8e\u4e3b\u8fdb\u7a0b\u548c\u6e32\u67d3\u8fdb\u7a0b\u662f\u76f8\u4e92\u72ec\u7acb\u7684&#xff0c;\u5b83\u4eec\u4e4b\u95f4\u7684\u901a\u4fe1\u9700\u8981\u901a\u8fc7 Electron \u63d0\u4f9b\u7684 IPC&#xff08;Inter-Process Communication&#xff09;\u673a\u5236\u3002<\/p>\n<p>\u4e3b\u8981\u7684 IPC \u6a21\u5757\u6709&#xff1a;<\/p>\n<ul>\n<li>ipcMain&#xff1a;\u7528\u4e8e\u5728\u4e3b\u8fdb\u7a0b\u4e2d\u76d1\u542c\u548c\u53d1\u9001\u6d88\u606f\u3002<\/li>\n<\/ul>\n<ul>\n<li>ipcRenderer&#xff1a;\u7528\u4e8e\u5728\u6e32\u67d3\u8fdb\u7a0b\u4e2d\u76d1\u542c\u548c\u53d1\u9001\u6d88\u606f\u3002<\/li>\n<\/ul>\n<ul>\n<li>remote&#xff1a;\u63d0\u4f9b\u4e86\u4e00\u79cd\u5728\u6e32\u67d3\u8fdb\u7a0b\u4e2d\u76f4\u63a5\u8c03\u7528\u4e3b\u8fdb\u7a0b\u6a21\u5757\u7684\u65b9\u6cd5&#xff0c;\u4f46\u4e0d\u63a8\u8350\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u4f7f\u7528&#xff0c;\u56e0\u4e3a\u5b83\u53ef\u80fd\u5bfc\u81f4\u5b89\u5168\u95ee\u9898\u3002<\/li>\n<\/ul>\n<h3>\u4e8c\u3001\u4e3b\u8fdb\u7a0b API \u8be6\u89e3<\/h3>\n<h4>2.1 app \u6a21\u5757&#xff1a;\u5e94\u7528\u751f\u547d\u5468\u671f\u7ba1\u7406<\/h4>\n<p>app\u6a21\u5757\u662f Electron \u4e2d\u6700\u91cd\u8981\u7684\u6a21\u5757\u4e4b\u4e00&#xff0c;\u5b83\u8d1f\u8d23\u7ba1\u7406\u5e94\u7528\u7684\u751f\u547d\u5468\u671f\u548c\u5168\u5c40\u8bbe\u7f6e\u3002\u5728\u4e3b\u8fdb\u7a0b\u4e2d&#xff0c;\u4f60\u53ef\u4ee5\u901a\u8fc7const { app } &#061; require(&#039;electron&#039;)\u6765\u83b7\u53d6app\u6a21\u5757\u7684\u5b9e\u4f8b\u3002<\/p>\n<h5>2.1.1 \u5e94\u7528\u751f\u547d\u5468\u671f\u4e8b\u4ef6<\/h5>\n<p>app\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u4e8b\u4ef6&#xff0c;\u7528\u4e8e\u76d1\u542c\u5e94\u7528\u7684\u4e0d\u540c\u751f\u547d\u5468\u671f\u9636\u6bb5&#xff1a;<\/p>\n<p>app.whenReady()\u65b9\u6cd5<\/p>\n<p>app.whenReady()\u8fd4\u56de\u4e00\u4e2a Promise&#xff0c;\u5f53 Electron \u5b8c\u6210\u521d\u59cb\u5316\u5e76\u51c6\u5907\u597d\u521b\u5efa\u6d4f\u89c8\u5668\u7a97\u53e3\u65f6&#xff0c;\u8fd9\u4e2a Promise \u4f1a\u88ab resolve\u3002\u8fd9\u662f\u5728\u4e3b\u8fdb\u7a0b\u4e2d\u521b\u5efa\u7a97\u53e3\u7684\u6700\u4f73\u65f6\u673a\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>const { app, BrowserWindow } &#061; require(&#039;electron&#039;);<\/p>\n<p>app.whenReady().then(() &#061;&gt; {<\/p>\n<p>createWindow();<\/p>\n<p>});<\/p>\n<p>app.on(&#039;window-all-closed&#039;, callback)\u4e8b\u4ef6<\/p>\n<p>\u5f53\u6240\u6709\u7a97\u53e3\u90fd\u88ab\u5173\u95ed\u65f6\u89e6\u53d1\u3002\u5728\u8fd9\u4e2a\u4e8b\u4ef6\u4e2d&#xff0c;\u4f60\u53ef\u4ee5\u51b3\u5b9a\u662f\u5426\u9000\u51fa\u5e94\u7528\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>app.on(&#039;window-all-closed&#039;, () &#061;&gt; {<\/p>\n<p>if (process.platform !&#061;&#061; &#039;darwin&#039;) { \/\/ \u5728macOS\u4e0a&#xff0c;\u7528\u6237\u901a\u5e38\u901a\u8fc7Cmd&#043;Q\u663e\u5f0f\u9000\u51fa<\/p>\n<p>app.quit();<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>app.on(&#039;activate&#039;, callback)\u4e8b\u4ef6<\/p>\n<p>\u5f53\u5e94\u7528\u88ab\u6fc0\u6d3b\u65f6\u89e6\u53d1&#xff0c;\u901a\u5e38\u53d1\u751f\u5728\u70b9\u51fb\u5e94\u7528\u56fe\u6807\u6216\u4ece dock \u4e2d\u9009\u62e9\u5e94\u7528\u65f6\u3002\u5728 macOS \u4e0a&#xff0c;\u5373\u4f7f\u6240\u6709\u7a97\u53e3\u90fd\u5173\u95ed\u4e86&#xff0c;\u5e94\u7528\u4ecd\u7136\u5904\u4e8e\u6fc0\u6d3b\u72b6\u6001&#xff0c;\u8fd9\u65f6\u9700\u8981\u91cd\u65b0\u521b\u5efa\u7a97\u53e3\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>app.on(&#039;activate&#039;, () &#061;&gt; {<\/p>\n<p>if (BrowserWindow.getAllWindows().length &#061;&#061;&#061; 0) {<\/p>\n<p>createWindow();<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>app.on(&#039;before-quit&#039;, callback)\u4e8b\u4ef6<\/p>\n<p>\u5728\u5e94\u7528\u5373\u5c06\u9000\u51fa\u524d\u89e6\u53d1\u3002\u4f60\u53ef\u4ee5\u5728\u8fd9\u4e2a\u4e8b\u4ef6\u4e2d\u6267\u884c\u4e00\u4e9b\u6e05\u7406\u5de5\u4f5c&#xff0c;\u5982\u4fdd\u5b58\u6570\u636e\u6216\u5173\u95ed\u6587\u4ef6\u53e5\u67c4\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>app.on(&#039;before-quit&#039;, (event) &#061;&gt; {<\/p>\n<p>\/\/ \u6267\u884c\u6e05\u7406\u5de5\u4f5c<\/p>\n<p>console.log(&#039;\u5e94\u7528\u5373\u5c06\u9000\u51fa&#039;);<\/p>\n<p>});<\/p>\n<h5>2.1.2 \u5e94\u7528\u4fe1\u606f\u76f8\u5173\u65b9\u6cd5<\/h5>\n<p>app\u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u83b7\u53d6\u5e94\u7528\u4fe1\u606f\u7684\u65b9\u6cd5&#xff1a;<\/p>\n<p>app.getVersion()\u65b9\u6cd5<\/p>\n<p>\u8fd4\u56de\u5e94\u7528\u7684\u7248\u672c\u53f7\u3002\u5982\u679c\u5728package.json\u4e2d\u627e\u4e0d\u5230\u7248\u672c\u53f7&#xff0c;\u5c31\u8fd4\u56de\u5f53\u524d\u5305\u6216\u53ef\u6267\u884c\u6587\u4ef6\u7684\u7248\u672c\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>console.log(&#096;\u5e94\u7528\u7248\u672c&#xff1a;${app.getVersion()}&#096;);<\/p>\n<p>app.getName()\u65b9\u6cd5<\/p>\n<p>\u8fd4\u56de\u5e94\u7528\u7684\u540d\u79f0&#xff0c;\u9ed8\u8ba4\u4e3apackage.json\u4e2d\u7684productName\u5b57\u6bb5\u7684\u503c&#xff0c;\u5982\u679c\u6ca1\u6709\u8bbe\u7f6e&#xff0c;\u5219\u4e3apackage.json\u4e2d\u7684name\u5b57\u6bb5\u7684\u503c\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>console.log(&#096;\u5e94\u7528\u540d\u79f0&#xff1a;${app.getName()}&#096;);<\/p>\n<p>app.getPath(name)\u65b9\u6cd5<\/p>\n<p>\u8fd4\u56de\u5e94\u7528\u76f8\u5173\u7684\u7279\u6b8a\u8def\u5f84&#xff0c;\u5982\u7528\u6237\u6570\u636e\u76ee\u5f55\u3001\u7f13\u5b58\u76ee\u5f55\u7b49\u3002name\u53c2\u6570\u53ef\u4ee5\u662f\u4ee5\u4e0b\u503c\u4e4b\u4e00&#xff1a;&#039;userData&#039;, &#039;appData&#039;, &#039;cache&#039;, &#039;temp&#039;, &#039;exe&#039;, &#039;home&#039;, &#039;desktop&#039;, &#039;documents&#039;, &#039;downloads&#039;, &#039;music&#039;, &#039;pictures&#039;, &#039;videos&#039;\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>console.log(&#096;\u7528\u6237\u6570\u636e\u76ee\u5f55&#xff1a;${app.getPath(&#039;userData&#039;)}&#096;);<\/p>\n<p>console.log(&#096;\u7f13\u5b58\u76ee\u5f55&#xff1a;${app.getPath(&#039;cache&#039;)}&#096;);<\/p>\n<h5>2.1.3 \u5e94\u7528\u8bbe\u7f6e\u65b9\u6cd5<\/h5>\n<p>app\u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u8bbe\u7f6e\u5e94\u7528\u884c\u4e3a\u7684\u65b9\u6cd5&#xff1a;<\/p>\n<p>app.setAppUserModelId(id)\u65b9\u6cd5<\/p>\n<p>\u5728 Windows \u4e0a\u8bbe\u7f6e\u5e94\u7528\u7684\u7528\u6237\u6a21\u578b ID&#xff0c;\u8fd9\u4f1a\u5f71\u54cd\u4efb\u52a1\u680f\u56fe\u6807\u548c\u7528\u6237\u4f53\u9a8c\u3002\u8fd9\u4e2a ID \u5e94\u8be5\u662f\u4e00\u4e2a\u552f\u4e00\u7684\u5b57\u7b26\u4e32&#xff0c;\u901a\u5e38\u91c7\u7528\u53cd\u5411\u57df\u540d\u683c\u5f0f\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>app.setAppUserModelId(&#039;com.example.myapp&#039;);<\/p>\n<p>app.setLoginItemSettings(settings)\u65b9\u6cd5<\/p>\n<p>\u8bbe\u7f6e\u5e94\u7528\u662f\u5426\u5728\u7cfb\u7edf\u542f\u52a8\u65f6\u81ea\u52a8\u8fd0\u884c\u3002settings\u662f\u4e00\u4e2a\u5bf9\u8c61&#xff0c;\u53ef\u4ee5\u5305\u542b\u4ee5\u4e0b\u5c5e\u6027&#xff1a;openAtLogin&#xff08;\u662f\u5426\u5728\u767b\u5f55\u65f6\u6253\u5f00&#xff09;\u3001openAsHidden&#xff08;\u662f\u5426\u4ee5\u9690\u85cf\u65b9\u5f0f\u6253\u5f00&#xff09;\u3001path&#xff08;\u8981\u8fd0\u884c\u7684\u8def\u5f84&#xff0c;\u9ed8\u8ba4\u4e3a\u5f53\u524d\u5e94\u7528&#xff09;\u3001args&#xff08;\u8981\u4f20\u9012\u7684\u53c2\u6570\u6570\u7ec4&#xff09;\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>app.setLoginItemSettings({<\/p>\n<p>openAtLogin: true,<\/p>\n<p>openAsHidden: false<\/p>\n<p>});<\/p>\n<h4>2.2 BrowserWindow \u6a21\u5757&#xff1a;\u521b\u5efa\u548c\u7ba1\u7406\u7a97\u53e3<\/h4>\n<p>BrowserWindow\u6a21\u5757\u7528\u4e8e\u521b\u5efa\u548c\u7ba1\u7406\u5e94\u7528\u7a97\u53e3&#xff0c;\u662f\u4e3b\u8fdb\u7a0b\u4e2d\u53e6\u4e00\u4e2a\u6838\u5fc3\u6a21\u5757\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7const { BrowserWindow } &#061; require(&#039;electron&#039;)\u6765\u83b7\u53d6BrowserWindow\u7c7b\u3002<\/p>\n<h5>2.2.1 \u521b\u5efa\u7a97\u53e3<\/h5>\n<p>\u8981\u521b\u5efa\u4e00\u4e2a\u7a97\u53e3&#xff0c;\u9996\u5148\u9700\u8981\u5b9e\u4f8b\u5316BrowserWindow\u7c7b&#xff1a;<\/p>\n<p>new BrowserWindow([options])\u6784\u9020\u51fd\u6570<\/p>\n<p>options\u53c2\u6570\u662f\u4e00\u4e2a\u53ef\u9009\u7684\u5bf9\u8c61&#xff0c;\u7528\u4e8e\u8bbe\u7f6e\u7a97\u53e3\u7684\u5404\u79cd\u5c5e\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u7a97\u53e3\u5c5e\u6027&#xff1a;<\/p>\n<ul>\n<li>width&#xff1a;\u7a97\u53e3\u5bbd\u5ea6&#xff0c;\u9ed8\u8ba4\u503c\u4e3a 800 \u50cf\u7d20\u3002<\/li>\n<\/ul>\n<ul>\n<li>height&#xff1a;\u7a97\u53e3\u9ad8\u5ea6&#xff0c;\u9ed8\u8ba4\u503c\u4e3a 600 \u50cf\u7d20\u3002<\/li>\n<\/ul>\n<ul>\n<li>minWidth&#xff1a;\u7a97\u53e3\u6700\u5c0f\u5bbd\u5ea6\u3002<\/li>\n<\/ul>\n<ul>\n<li>minHeight&#xff1a;\u7a97\u53e3\u6700\u5c0f\u9ad8\u5ea6\u3002<\/li>\n<\/ul>\n<ul>\n<li>maxWidth&#xff1a;\u7a97\u53e3\u6700\u5927\u5bbd\u5ea6\u3002<\/li>\n<\/ul>\n<ul>\n<li>maxHeight&#xff1a;\u7a97\u53e3\u6700\u5927\u9ad8\u5ea6\u3002<\/li>\n<\/ul>\n<ul>\n<li>x&#xff1a;\u7a97\u53e3\u521d\u59cb x \u5750\u6807\u3002<\/li>\n<\/ul>\n<ul>\n<li>y&#xff1a;\u7a97\u53e3\u521d\u59cb y \u5750\u6807\u3002<\/li>\n<\/ul>\n<ul>\n<li>title&#xff1a;\u7a97\u53e3\u6807\u9898\u3002<\/li>\n<\/ul>\n<ul>\n<li>icon&#xff1a;\u7a97\u53e3\u56fe\u6807\u8def\u5f84\u3002<\/li>\n<\/ul>\n<ul>\n<li>webPreferences&#xff1a;\u7f51\u9875\u504f\u597d\u8bbe\u7f6e&#xff0c;\u5982\u662f\u5426\u542f\u7528 Node.js \u96c6\u6210\u7b49\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b&#xff1a;\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7a97\u53e3<\/p>\n<p>const win &#061; new BrowserWindow({<\/p>\n<p>width: 1024,<\/p>\n<p>height: 768,<\/p>\n<p>minWidth: 800,<\/p>\n<p>minHeight: 600,<\/p>\n<p>title: &#039;My Electron App&#039;,<\/p>\n<p>webPreferences: {<\/p>\n<p>nodeIntegration: true \/\/ \u662f\u5426\u5728\u6e32\u67d3\u8fdb\u7a0b\u4e2d\u542f\u7528Node.js\u96c6\u6210<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<h5>2.2.2 \u7a97\u53e3\u52a0\u8f7d\u5185\u5bb9<\/h5>\n<p>\u521b\u5efa\u7a97\u53e3\u540e&#xff0c;\u9700\u8981\u4e3a\u5176\u52a0\u8f7d\u5185\u5bb9\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u52a0\u8f7d\u4e0d\u540c\u7c7b\u578b\u7684\u5185\u5bb9&#xff1a;<\/p>\n<p>win.loadFile(filePath)\u65b9\u6cd5<\/p>\n<p>\u52a0\u8f7d\u672c\u5730 HTML \u6587\u4ef6\u3002filePath\u662f\u76f8\u5bf9\u4e8e\u5e94\u7528\u6839\u76ee\u5f55\u7684\u8def\u5f84\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>win.loadFile(&#039;index.html&#039;);<\/p>\n<p>win.loadURL(url)\u65b9\u6cd5<\/p>\n<p>\u52a0\u8f7d\u8fdc\u7a0b URL \u6216\u672c\u5730\u6587\u4ef6\u3002\u5bf9\u4e8e\u672c\u5730\u6587\u4ef6&#xff0c;\u9700\u8981\u4f7f\u7528file:\/\/\u534f\u8bae\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>\/\/ \u52a0\u8f7d\u8fdc\u7a0bURL<\/p>\n<p>win.loadURL(&#039;https:\/\/www.electronjs.org&#039;);<\/p>\n<p>\/\/ \u52a0\u8f7d\u672c\u5730\u6587\u4ef6<\/p>\n<p>win.loadURL(&#039;file:\/\/&#039; &#043; __dirname &#043; &#039;\/index.html&#039;);<\/p>\n<p>win.loadURL(dataUrl)\u65b9\u6cd5<\/p>\n<p>\u76f4\u63a5\u52a0\u8f7d Data URL \u683c\u5f0f\u7684\u5185\u5bb9\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>win.loadURL(&#039;data:text\/html,&lt;h1&gt;Hello World!&lt;\/h1&gt;&#039;);<\/p>\n<h5>2.2.3 \u7a97\u53e3\u64cd\u4f5c\u65b9\u6cd5<\/h5>\n<p>BrowserWindow\u7c7b\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u65b9\u6cd5\u6765\u64cd\u4f5c\u7a97\u53e3&#xff1a;<\/p>\n<p>\u7a97\u53e3\u72b6\u6001\u63a7\u5236<\/p>\n<ul>\n<li>win.show()&#xff1a;\u663e\u793a\u7a97\u53e3\u3002<\/li>\n<\/ul>\n<ul>\n<li>win.hide()&#xff1a;\u9690\u85cf\u7a97\u53e3\u3002<\/li>\n<\/ul>\n<ul>\n<li>win.toggleVisibility()&#xff1a;\u5207\u6362\u7a97\u53e3\u53ef\u89c1\u6027\u3002<\/li>\n<\/ul>\n<ul>\n<li>win.focus()&#xff1a;\u805a\u7126\u7a97\u53e3\u3002<\/li>\n<\/ul>\n<ul>\n<li>win.blur()&#xff1a;\u4f7f\u7a97\u53e3\u5931\u53bb\u7126\u70b9\u3002<\/li>\n<\/ul>\n<ul>\n<li>win.minimize()&#xff1a;\u6700\u5c0f\u5316\u7a97\u53e3\u3002<\/li>\n<\/ul>\n<ul>\n<li>win.maximize()&#xff1a;\u6700\u5927\u5316\u7a97\u53e3\u3002<\/li>\n<\/ul>\n<ul>\n<li>win.unmaximize()&#xff1a;\u53d6\u6d88\u6700\u5927\u5316\u7a97\u53e3\u3002<\/li>\n<\/ul>\n<ul>\n<li>win.restore()&#xff1a;\u6062\u590d\u7a97\u53e3\u5230\u6b63\u5e38\u5927\u5c0f\u3002<\/li>\n<\/ul>\n<ul>\n<li>win.isMinimized()&#xff1a;\u68c0\u67e5\u7a97\u53e3\u662f\u5426\u6700\u5c0f\u5316\u3002<\/li>\n<\/ul>\n<ul>\n<li>win.isMaximized()&#xff1a;\u68c0\u67e5\u7a97\u53e3\u662f\u5426\u6700\u5927\u5316\u3002<\/li>\n<\/ul>\n<ul>\n<li>win.isFocused()&#xff1a;\u68c0\u67e5\u7a97\u53e3\u662f\u5426\u83b7\u5f97\u7126\u70b9\u3002<\/li>\n<\/ul>\n<p>\u7a97\u53e3\u5c3a\u5bf8\u548c\u4f4d\u7f6e<\/p>\n<ul>\n<li>win.setPosition(x, y[, animate])&#xff1a;\u8bbe\u7f6e\u7a97\u53e3\u4f4d\u7f6e\u3002animate\u53c2\u6570\u8868\u793a\u662f\u5426\u5e73\u6ed1\u8fc7\u6e21\u3002<\/li>\n<\/ul>\n<ul>\n<li>win.getPosition()&#xff1a;\u83b7\u53d6\u7a97\u53e3\u4f4d\u7f6e&#xff0c;\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4[x, y]\u3002<\/li>\n<\/ul>\n<ul>\n<li>win.setSize(width, height[, animate])&#xff1a;\u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f\u3002<\/li>\n<\/ul>\n<ul>\n<li>win.getSize()&#xff1a;\u83b7\u53d6\u7a97\u53e3\u5927\u5c0f&#xff0c;\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4[width, height]\u3002<\/li>\n<\/ul>\n<ul>\n<li>win.setMinimumSize(width, height)&#xff1a;\u8bbe\u7f6e\u7a97\u53e3\u6700\u5c0f\u5c3a\u5bf8\u3002<\/li>\n<\/ul>\n<ul>\n<li>win.setMaximumSize(width, height)&#xff1a;\u8bbe\u7f6e\u7a97\u53e3\u6700\u5927\u5c3a\u5bf8\u3002<\/li>\n<\/ul>\n<ul>\n<li>win.setResizable(resizable)&#xff1a;\u8bbe\u7f6e\u7a97\u53e3\u662f\u5426\u53ef\u8c03\u6574\u5927\u5c0f\u3002<\/li>\n<\/ul>\n<ul>\n<li>win.isResizable()&#xff1a;\u68c0\u67e5\u7a97\u53e3\u662f\u5426\u53ef\u8c03\u6574\u5927\u5c0f\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b&#xff1a;\u7a97\u53e3\u64cd\u4f5c\u7efc\u5408\u793a\u4f8b<\/p>\n<p>\/\/ \u7a97\u53e3\u52a0\u8f7d\u5b8c\u6210\u540e\u6267\u884c\u4e00\u4e9b\u64cd\u4f5c<\/p>\n<p>win.once(&#039;ready-to-show&#039;, () &#061;&gt; {<\/p>\n<p>\/\/ \u663e\u793a\u7a97\u53e3\u5e76\u805a\u7126<\/p>\n<p>win.show();<\/p>\n<p>win.focus();<\/p>\n<p>\/\/ \u8bbe\u7f6e\u7a97\u53e3\u4f4d\u7f6e\u548c\u5927\u5c0f<\/p>\n<p>win.setPosition(100, 100);<\/p>\n<p>win.setSize(1200, 800);<\/p>\n<p>\/\/ \u8bbe\u7f6e\u6700\u5c0f\u5c3a\u5bf8<\/p>\n<p>win.setMinimumSize(800, 600);<\/p>\n<p>\/\/ \u8bbe\u7f6e\u4e0d\u53ef\u8c03\u6574\u5927\u5c0f<\/p>\n<p>win.setResizable(false);<\/p>\n<p>});<\/p>\n<h5>2.2.4 \u7a97\u53e3\u4e8b\u4ef6<\/h5>\n<p>BrowserWindow\u7c7b\u89e6\u53d1\u591a\u4e2a\u4e8b\u4ef6&#xff0c;\u53ef\u4ee5\u901a\u8fc7on\u548conce\u65b9\u6cd5\u6765\u76d1\u542c\u8fd9\u4e9b\u4e8b\u4ef6&#xff1a;<\/p>\n<p>\u7a97\u53e3\u751f\u547d\u5468\u671f\u4e8b\u4ef6<\/p>\n<ul>\n<li>&#039;ready-to-show&#039;&#xff1a;\u5f53\u7a97\u53e3\u5185\u5bb9\u51c6\u5907\u597d\u663e\u793a\u65f6\u89e6\u53d1\u3002<\/li>\n<\/ul>\n<ul>\n<li>&#039;closed&#039;&#xff1a;\u5f53\u7a97\u53e3\u88ab\u5173\u95ed\u65f6\u89e6\u53d1\u3002<\/li>\n<\/ul>\n<ul>\n<li>&#039;close&#039;&#xff1a;\u5f53\u7a97\u53e3\u5373\u5c06\u5173\u95ed\u65f6\u89e6\u53d1\u3002\u53ef\u4ee5\u901a\u8fc7event.preventDefault()\u963b\u6b62\u7a97\u53e3\u5173\u95ed\u3002<\/li>\n<\/ul>\n<ul>\n<li>&#039;destroy&#039;&#xff1a;\u5f53\u7a97\u53e3\u5bf9\u8c61\u88ab\u9500\u6bc1\u65f6\u89e6\u53d1\u3002<\/li>\n<\/ul>\n<p>\u7a97\u53e3\u72b6\u6001\u53d8\u5316\u4e8b\u4ef6<\/p>\n<ul>\n<li>&#039;focus&#039;&#xff1a;\u5f53\u7a97\u53e3\u83b7\u5f97\u7126\u70b9\u65f6\u89e6\u53d1\u3002<\/li>\n<\/ul>\n<ul>\n<li>&#039;blur&#039;&#xff1a;\u5f53\u7a97\u53e3\u5931\u53bb\u7126\u70b9\u65f6\u89e6\u53d1\u3002<\/li>\n<\/ul>\n<ul>\n<li>&#039;maximize&#039;&#xff1a;\u5f53\u7a97\u53e3\u6700\u5927\u5316\u65f6\u89e6\u53d1\u3002<\/li>\n<\/ul>\n<ul>\n<li>&#039;unmaximize&#039;&#xff1a;\u5f53\u7a97\u53e3\u53d6\u6d88\u6700\u5927\u5316\u65f6\u89e6\u53d1\u3002<\/li>\n<\/ul>\n<ul>\n<li>&#039;minimize&#039;&#xff1a;\u5f53\u7a97\u53e3\u6700\u5c0f\u5316\u65f6\u89e6\u53d1\u3002<\/li>\n<\/ul>\n<ul>\n<li>&#039;restore&#039;&#xff1a;\u5f53\u7a97\u53e3\u4ece\u6700\u5c0f\u5316\u6216\u6700\u5927\u5316\u72b6\u6001\u6062\u590d\u65f6\u89e6\u53d1\u3002<\/li>\n<\/ul>\n<ul>\n<li>&#039;move&#039;&#xff1a;\u5f53\u7a97\u53e3\u4f4d\u7f6e\u6539\u53d8\u65f6\u89e6\u53d1\u3002<\/li>\n<\/ul>\n<ul>\n<li>&#039;resize&#039;&#xff1a;\u5f53\u7a97\u53e3\u5927\u5c0f\u6539\u53d8\u65f6\u89e6\u53d1\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b&#xff1a;\u7a97\u53e3\u4e8b\u4ef6\u76d1\u542c\u793a\u4f8b<\/p>\n<p>\/\/ \u76d1\u542c\u7a97\u53e3\u5173\u95ed\u4e8b\u4ef6<\/p>\n<p>win.on(&#039;close&#039;, (event) &#061;&gt; {<\/p>\n<p>\/\/ \u53ef\u4ee5\u5728\u8fd9\u91cc\u8be2\u95ee\u7528\u6237\u662f\u5426\u4fdd\u5b58\u6570\u636e<\/p>\n<p>const answer &#061; confirm(&#039;\u662f\u5426\u8981\u5173\u95ed\u5e94\u7528&#xff1f;&#039;);<\/p>\n<p>if (!answer) {<\/p>\n<p>event.preventDefault(); \/\/ \u963b\u6b62\u7a97\u53e3\u5173\u95ed<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>\/\/ \u76d1\u542c\u7a97\u53e3\u6700\u5927\u5316\u4e8b\u4ef6<\/p>\n<p>win.on(&#039;maximize&#039;, () &#061;&gt; {<\/p>\n<p>console.log(&#039;\u7a97\u53e3\u5df2\u6700\u5927\u5316&#039;);<\/p>\n<p>});<\/p>\n<p>\/\/ \u76d1\u542c\u7a97\u53e3\u6062\u590d\u4e8b\u4ef6<\/p>\n<p>win.on(&#039;restore&#039;, () &#061;&gt; {<\/p>\n<p>console.log(&#039;\u7a97\u53e3\u5df2\u6062\u590d&#039;);<\/p>\n<p>});<\/p>\n<p>\/\/ \u76d1\u542c\u7a97\u53e3\u79fb\u52a8\u4e8b\u4ef6<\/p>\n<p>win.on(&#039;move&#039;, () &#061;&gt; {<\/p>\n<p>console.log(&#096;\u7a97\u53e3\u65b0\u4f4d\u7f6e&#xff1a;${win.getPosition()}&#096;);<\/p>\n<p>});<\/p>\n<h5>2.2.5 \u591a\u7a97\u53e3\u7ba1\u7406<\/h5>\n<p>\u5728 Electron \u5e94\u7528\u4e2d&#xff0c;\u901a\u5e38\u9700\u8981\u7ba1\u7406\u591a\u4e2a\u7a97\u53e3\u3002BrowserWindow\u7c7b\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9759\u6001\u65b9\u6cd5\u6765\u7ba1\u7406\u6240\u6709\u7a97\u53e3&#xff1a;<\/p>\n<p>BrowserWindow.getAllWindows()\u65b9\u6cd5<\/p>\n<p>\u8fd4\u56de\u6240\u6709BrowserWindow\u5b9e\u4f8b\u7684\u6570\u7ec4\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>const windows &#061; BrowserWindow.getAllWindows();<\/p>\n<p>console.log(&#096;\u5f53\u524d\u6253\u5f00\u7684\u7a97\u53e3\u6570\u91cf&#xff1a;${windows.length}&#096;);<\/p>\n<p>BrowserWindow.fromId(id)\u65b9\u6cd5<\/p>\n<p>\u6839\u636e\u7a97\u53e3 ID \u83b7\u53d6BrowserWindow\u5b9e\u4f8b\u3002\u7a97\u53e3 ID \u662f\u4e00\u4e2a\u6574\u6570&#xff0c;\u6bcf\u4e2a\u7a97\u53e3\u90fd\u6709\u552f\u4e00\u7684 ID\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>const win &#061; BrowserWindow.fromId(1);<\/p>\n<p>if (win) {<\/p>\n<p>win.focus();<\/p>\n<p>}<\/p>\n<p>BrowserWindow.getFocusedWindow()\u65b9\u6cd5<\/p>\n<p>\u83b7\u53d6\u5f53\u524d\u83b7\u5f97\u7126\u70b9\u7684\u7a97\u53e3\u5b9e\u4f8b&#xff0c;\u5982\u679c\u6ca1\u6709\u5219\u8fd4\u56denull\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>const focusedWindow &#061; BrowserWindow.getFocusedWindow();<\/p>\n<p>if (focusedWindow) {<\/p>\n<p>console.log(&#096;\u5f53\u524d\u805a\u7126\u7684\u7a97\u53e3\u6807\u9898&#xff1a;${focusedWindow.getTitle()}&#096;);<\/p>\n<p>}<\/p>\n<h4>2.3 Menu \u6a21\u5757&#xff1a;\u521b\u5efa\u5e94\u7528\u83dc\u5355<\/h4>\n<p>\u5728\u684c\u9762\u5e94\u7528\u4e2d&#xff0c;\u83dc\u5355\u662f\u7528\u6237\u4ea4\u4e92\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\u3002Electron \u63d0\u4f9b\u4e86Menu\u6a21\u5757\u6765\u521b\u5efa\u548c\u7ba1\u7406\u5e94\u7528\u83dc\u5355\u3002<\/p>\n<h5>2.3.1 \u521b\u5efa\u83dc\u5355<\/h5>\n<p>\u8981\u521b\u5efa\u83dc\u5355&#xff0c;\u9996\u5148\u9700\u8981\u5b9a\u4e49\u83dc\u5355\u6a21\u677f\u3002\u83dc\u5355\u6a21\u677f\u662f\u4e00\u4e2a\u6570\u7ec4&#xff0c;\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u4ee3\u8868\u4e00\u4e2a\u83dc\u5355\u9879\u6216\u5b50\u83dc\u5355\u3002<\/p>\n<p>\u83dc\u5355\u9879\u5c5e\u6027<\/p>\n<p>\u6bcf\u4e2a\u83dc\u5355\u9879\u53ef\u4ee5\u5305\u542b\u4ee5\u4e0b\u5c5e\u6027&#xff1a;<\/p>\n<ul>\n<li>label&#xff1a;\u83dc\u5355\u9879\u7684\u6587\u672c\u6807\u7b7e\u3002<\/li>\n<\/ul>\n<ul>\n<li>accelerator&#xff1a;\u5feb\u6377\u952e&#xff0c;\u5982CmdOrCtrl&#043;O\u3002<\/li>\n<\/ul>\n<ul>\n<li>icon&#xff1a;\u83dc\u5355\u9879\u7684\u56fe\u6807\u8def\u5f84\u3002<\/li>\n<\/ul>\n<ul>\n<li>type&#xff1a;\u83dc\u5355\u9879\u7684\u7c7b\u578b&#xff0c;\u53ef\u4ee5\u662f&#039;normal&#039;, &#039;separator&#039;, &#039;submenu&#039;, &#039;checkbox&#039;, &#039;radio&#039;\u3002<\/li>\n<\/ul>\n<ul>\n<li>submenu&#xff1a;\u5b50\u83dc\u5355&#xff0c;\u662f\u4e00\u4e2a\u83dc\u5355\u9879\u6570\u7ec4\u3002<\/li>\n<\/ul>\n<ul>\n<li>click&#xff1a;\u70b9\u51fb\u83dc\u5355\u9879\u65f6\u6267\u884c\u7684\u56de\u8c03\u51fd\u6570\u3002<\/li>\n<\/ul>\n<ul>\n<li>enabled&#xff1a;\u662f\u5426\u542f\u7528\u83dc\u5355\u9879&#xff0c;\u9ed8\u8ba4\u503c\u4e3atrue\u3002<\/li>\n<\/ul>\n<ul>\n<li>visible&#xff1a;\u662f\u5426\u663e\u793a\u83dc\u5355\u9879&#xff0c;\u9ed8\u8ba4\u503c\u4e3atrue\u3002<\/li>\n<\/ul>\n<ul>\n<li>checked&#xff1a;\u5bf9\u4e8e\u590d\u9009\u6846\u548c\u5355\u9009\u6309\u94ae\u7c7b\u578b\u7684\u83dc\u5355\u9879&#xff0c;\u662f\u5426\u88ab\u9009\u4e2d\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b&#xff1a;\u57fa\u672c\u83dc\u5355\u6a21\u677f<\/p>\n<p>const template &#061; [<\/p>\n<p>{<\/p>\n<p>label: &#039;\u6587\u4ef6&#039;,<\/p>\n<p>submenu: [<\/p>\n<p>{<\/p>\n<p>label: &#039;\u65b0\u5efa&#039;,<\/p>\n<p>accelerator: &#039;CmdOrCtrl&#043;N&#039;,<\/p>\n<p>click: () &#061;&gt; { \/* \u5904\u7406\u65b0\u5efa\u6587\u4ef6 *\/ }<\/p>\n<p>},<\/p>\n<p>{<\/p>\n<p>label: &#039;\u6253\u5f00&#8230;&#039;,<\/p>\n<p>accelerator: &#039;CmdOrCtrl&#043;O&#039;,<\/p>\n<p>click: () &#061;&gt; { \/* \u5904\u7406\u6253\u5f00\u6587\u4ef6 *\/ }<\/p>\n<p>},<\/p>\n<p>{<\/p>\n<p>type: &#039;separator&#039;<\/p>\n<p>},<\/p>\n<p>{<\/p>\n<p>label: &#039;\u9000\u51fa&#039;,<\/p>\n<p>accelerator: &#039;CmdOrCtrl&#043;Q&#039;,<\/p>\n<p>click: () &#061;&gt; { app.quit(); }<\/p>\n<p>}<\/p>\n<p>]<\/p>\n<p>},<\/p>\n<p>{<\/p>\n<p>label: &#039;\u7f16\u8f91&#039;,<\/p>\n<p>submenu: [<\/p>\n<p>{<\/p>\n<p>label: &#039;\u526a\u5207&#039;,<\/p>\n<p>accelerator: &#039;CmdOrCtrl&#043;X&#039;,<\/p>\n<p>click: () &#061;&gt; { \/* \u5904\u7406\u526a\u5207 *\/ }<\/p>\n<p>},<\/p>\n<p>{<\/p>\n<p>label: &#039;\u590d\u5236&#039;,<\/p>\n<p>accelerator: &#039;CmdOrCtrl&#043;C&#039;,<\/p>\n<p>click: () &#061;&gt; { \/* \u5904\u7406\u590d\u5236 *\/ }<\/p>\n<p>},<\/p>\n<p>{<\/p>\n<p>label: &#039;\u7c98\u8d34&#039;,<\/p>\n<p>accelerator: &#039;CmdOrCtrl&#043;V&#039;,<\/p>\n<p>click: () &#061;&gt; { \/* \u5904\u7406\u7c98\u8d34 *\/ }<\/p>\n<p>}<\/p>\n<p>]<\/p>\n<p>}<\/p>\n<p>];<\/p>\n<p>\u521b\u5efa\u5e76\u5e94\u7528\u83dc\u5355<\/p>\n<p>\u5b9a\u4e49\u597d\u83dc\u5355\u6a21\u677f\u540e&#xff0c;\u53ef\u4ee5\u4f7f\u7528Menu.buildFromTemplate(template)\u65b9\u6cd5\u521b\u5efa\u83dc\u5355\u5bf9\u8c61&#xff0c;\u7136\u540e\u4f7f\u7528Menu.setApplicationMenu(menu)\u65b9\u6cd5\u5c06\u5176\u8bbe\u7f6e\u4e3a\u5e94\u7528\u83dc\u5355\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>const { Menu } &#061; require(&#039;electron&#039;);<\/p>\n<p>const menu &#061; Menu.buildFromTemplate(template);<\/p>\n<p>Menu.setApplicationMenu(menu);<\/p>\n<h5>2.3.2 \u4e0a\u4e0b\u6587\u83dc\u5355<\/h5>\n<p>\u9664\u4e86\u5e94\u7528\u83dc\u5355\u5916&#xff0c;Electron \u8fd8\u652f\u6301\u4e0a\u4e0b\u6587\u83dc\u5355&#xff08;\u53f3\u952e\u83dc\u5355&#xff09;\u3002\u521b\u5efa\u4e0a\u4e0b\u6587\u83dc\u5355\u7684\u65b9\u6cd5\u4e0e\u5e94\u7528\u83dc\u5355\u7c7b\u4f3c&#xff0c;\u4f46\u901a\u5e38\u662f\u5728\u9700\u8981\u65f6\u52a8\u6001\u521b\u5efa\u548c\u663e\u793a\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;\u521b\u5efa\u5e76\u663e\u793a\u4e0a\u4e0b\u6587\u83dc\u5355<\/p>\n<p>const { Menu } &#061; require(&#039;electron&#039;);<\/p>\n<p>\/\/ \u5b9a\u4e49\u4e0a\u4e0b\u6587\u83dc\u5355\u6a21\u677f<\/p>\n<p>const contextMenuTemplate &#061; [<\/p>\n<p>{<\/p>\n<p>label: &#039;\u590d\u5236&#039;,<\/p>\n<p>accelerator: &#039;CmdOrCtrl&#043;C&#039;,<\/p>\n<p>click: () &#061;&gt; { \/* \u5904\u7406\u590d\u5236 *\/ }<\/p>\n<p>},<\/p>\n<p>{<\/p>\n<p>label: &#039;\u7c98\u8d34&#039;,<\/p>\n<p>accelerator: &#039;CmdOrCtrl&#043;V&#039;,<\/p>\n<p>click: () &#061;&gt; { \/* \u5904\u7406\u7c98\u8d34 *\/ }<\/p>\n<p>}<\/p>\n<p>];<\/p>\n<p>\/\/ \u5728\u6e32\u67d3\u8fdb\u7a0b\u4e2d\u76d1\u542c\u53f3\u952e\u70b9\u51fb\u4e8b\u4ef6<\/p>\n<p>const { ipcRenderer } &#061; require(&#039;electron&#039;);<\/p>\n<p>document.addEventListener(&#039;contextmenu&#039;, (event) &#061;&gt; {<\/p>\n<p>event.preventDefault(); \/\/ \u963b\u6b62\u9ed8\u8ba4\u7684\u4e0a\u4e0b\u6587\u83dc\u5355<\/p>\n<p>ipcRenderer.send(&#039;show-context-menu&#039;); \/\/ \u53d1\u9001\u6d88\u606f\u7ed9\u4e3b\u8fdb\u7a0b<\/p>\n<p>});<\/p>\n<p>\/\/ \u5728\u4e3b\u8fdb\u7a0b\u4e2d\u76d1\u542c\u6d88\u606f\u5e76\u663e\u793a\u4e0a\u4e0b\u6587\u83dc\u5355<\/p>\n<p>ipcMain.on(&#039;show-context-menu&#039;, (event) &#061;&gt; {<\/p>\n<p>const menu &#061; Menu.buildFromTemplate(contextMenuTemplate);<\/p>\n<p>menu.popup({ window: BrowserWindow.getFocusedWindow() }); \/\/ \u5728\u5f53\u524d\u805a\u7126\u7a97\u53e3\u4e2d\u663e\u793a\u83dc\u5355<\/p>\n<p>});<\/p>\n<h5>2.3.3 \u7cfb\u7edf\u9ed8\u8ba4\u83dc\u5355<\/h5>\n<p>Electron \u63d0\u4f9b\u4e86\u4e00\u4e9b\u7cfb\u7edf\u9ed8\u8ba4\u7684\u83dc\u5355\u9879\u548c\u884c\u4e3a&#xff0c;\u7279\u522b\u662f\u5728 macOS \u4e0a&#xff0c;\u5e94\u7528\u83dc\u5355\u901a\u5e38\u9075\u5faa\u7cfb\u7edf\u6807\u51c6\u3002<\/p>\n<p>macOS \u7279\u6b8a\u5904\u7406<\/p>\n<p>\u5728 macOS \u4e0a&#xff0c;\u5e94\u7528\u83dc\u5355\u901a\u5e38\u5305\u542b\u5e94\u7528\u540d\u79f0\u3001\u7f16\u8f91\u83dc\u5355\u3001\u7a97\u53e3\u7ba1\u7406\u83dc\u5355\u7b49\u3002Electron \u4f1a\u81ea\u52a8\u4e3a\u8fd9\u4e9b\u6807\u51c6\u83dc\u5355\u6dfb\u52a0\u4e00\u4e9b\u9ed8\u8ba4\u884c\u4e3a&#xff0c;\u4f46\u4f60\u4e5f\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5b83\u4eec\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;macOS \u4e13\u7528\u83dc\u5355\u9879<\/p>\n<p>if (process.platform &#061;&#061;&#061; &#039;darwin&#039;) {<\/p>\n<p>template.unshift({<\/p>\n<p>label: app.getName(),<\/p>\n<p>submenu: [<\/p>\n<p>{<\/p>\n<p>label: &#039;\u5173\u4e8e &#039; &#043; app.getName(),<\/p>\n<p>role: &#039;about&#039;<\/p>\n<p>},<\/p>\n<p>{<\/p>\n<p>type: &#039;separator&#039;<\/p>\n<p>},<\/p>\n<p>{<\/p>\n<p>label: &#039;\u9000\u51fa&#039;,<\/p>\n<p>accelerator: &#039;Cmd&#043;Q&#039;,<\/p>\n<p>click: () &#061;&gt; { app.quit(); }<\/p>\n<p>}<\/p>\n<p>]<\/p>\n<p>});<\/p>\n<p>\/\/ \u7f16\u8f91\u83dc\u5355<\/p>\n<p>const editMenu &#061; template.find(item &#061;&gt; item.label &#061;&#061;&#061; &#039;\u7f16\u8f91&#039;);<\/p>\n<p>editMenu.submenu.push(<\/p>\n<p>{ type: &#039;separator&#039; },<\/p>\n<p>{<\/p>\n<p>label: &#039;\u5168\u9009&#039;,<\/p>\n<p>accelerator: &#039;Cmd&#043;A&#039;,<\/p>\n<p>click: () &#061;&gt; { \/* \u5904\u7406\u5168\u9009 *\/ }<\/p>\n<p>}<\/p>\n<p>);<\/p>\n<p>\/\/ \u7a97\u53e3\u83dc\u5355<\/p>\n<p>template.push({<\/p>\n<p>label: &#039;\u7a97\u53e3&#039;,<\/p>\n<p>role: &#039;window&#039;,<\/p>\n<p>submenu: [<\/p>\n<p>{<\/p>\n<p>label: &#039;\u6700\u5c0f\u5316&#039;,<\/p>\n<p>accelerator: &#039;Cmd&#043;M&#039;,<\/p>\n<p>role: &#039;minimize&#039;<\/p>\n<p>},<\/p>\n<p>{<\/p>\n<p>label: &#039;\u5173\u95ed&#039;,<\/p>\n<p>accelerator: &#039;Cmd&#043;W&#039;,<\/p>\n<p>role: &#039;close&#039;<\/p>\n<p>},<\/p>\n<p>{ type: &#039;separator&#039; },<\/p>\n<p>{<\/p>\n<p>label: &#039;\u5168\u90e8\u663e\u793a&#039;,<\/p>\n<p>role: &#039;front&#039;<\/p>\n<p>}<\/p>\n<p>]<\/p>\n<p>});<\/p>\n<p>}<\/p>\n<h4>2.4 Tray \u6a21\u5757&#xff1a;\u7cfb\u7edf\u6258\u76d8\u56fe\u6807<\/h4>\n<p>\u5728\u8bb8\u591a\u684c\u9762\u5e94\u7528\u4e2d&#xff0c;\u7cfb\u7edf\u6258\u76d8&#xff08;\u4e5f\u79f0\u4e3a\u4efb\u52a1\u680f\u6216 dock&#xff09;\u56fe\u6807\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u529f\u80fd\u3002Electron \u63d0\u4f9b\u4e86Tray\u6a21\u5757\u6765\u521b\u5efa\u548c\u7ba1\u7406\u7cfb\u7edf\u6258\u76d8\u56fe\u6807\u3002<\/p>\n<h5>2.4.1 \u521b\u5efa\u7cfb\u7edf\u6258\u76d8<\/h5>\n<p>\u8981\u521b\u5efa\u7cfb\u7edf\u6258\u76d8&#xff0c;\u9996\u5148\u9700\u8981\u5b9e\u4f8b\u5316Tray\u7c7b&#xff0c;\u5e76\u4f20\u5165\u56fe\u6807\u8def\u5f84\u3002<\/p>\n<p>new Tray(imagePath)\u6784\u9020\u51fd\u6570<\/p>\n<p>imagePath\u53c2\u6570\u662f\u56fe\u6807\u7684\u8def\u5f84&#xff0c;\u53ef\u4ee5\u662f\u76f8\u5bf9\u8def\u5f84\u6216\u7edd\u5bf9\u8def\u5f84\u3002\u5728 macOS \u4e0a&#xff0c;\u56fe\u6807\u5e94\u8be5\u662f\u4e00\u4e2a.icns \u6587\u4ef6&#xff1b;\u5728 Windows \u548c Linux \u4e0a&#xff0c;\u53ef\u4ee5\u662f.ico \u6216.png \u6587\u4ef6\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>const { Tray } &#061; require(&#039;electron&#039;);<\/p>\n<p>const tray &#061; new Tray(path.join(__dirname, &#039;icon.png&#039;));<\/p>\n<h5>2.4.2 \u8bbe\u7f6e\u5de5\u5177\u63d0\u793a\u548c\u83dc\u5355<\/h5>\n<p>\u521b\u5efa\u7cfb\u7edf\u6258\u76d8\u540e&#xff0c;\u53ef\u4ee5\u8bbe\u7f6e\u5de5\u5177\u63d0\u793a\u548c\u4e0a\u4e0b\u6587\u83dc\u5355\u3002<\/p>\n<p>tray.setToolTip(tooltip)\u65b9\u6cd5<\/p>\n<p>\u8bbe\u7f6e\u9f20\u6807\u60ac\u505c\u65f6\u663e\u793a\u7684\u5de5\u5177\u63d0\u793a\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>tray.setToolTip(&#039;\u6211\u7684Electron\u5e94\u7528&#039;);<\/p>\n<p>tray.setContextMenu(menu)\u65b9\u6cd5<\/p>\n<p>\u8bbe\u7f6e\u53f3\u952e\u70b9\u51fb\u6258\u76d8\u56fe\u6807\u65f6\u663e\u793a\u7684\u4e0a\u4e0b\u6587\u83dc\u5355\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>const contextMenu &#061; Menu.buildFromTemplate([<\/p>\n<p>{<\/p>\n<p>label: &#039;\u6253\u5f00\u5e94\u7528&#039;,<\/p>\n<p>click: () &#061;&gt; { mainWindow.show(); }<\/p>\n<p>},<\/p>\n<p>{<\/p>\n<p>label: &#039;\u9000\u51fa&#039;,<\/p>\n<p>click: () &#061;&gt; { app.quit(); }<\/p>\n<p>}<\/p>\n<p>]);<\/p>\n<p>tray.setContextMenu(contextMenu);<\/p>\n<h5>2.4.3 \u6258\u76d8\u4e8b\u4ef6<\/h5>\n<p>Tray\u7c7b\u89e6\u53d1\u591a\u4e2a\u4e8b\u4ef6&#xff0c;\u53ef\u4ee5\u901a\u8fc7on\u548conce\u65b9\u6cd5\u6765\u76d1\u542c\u8fd9\u4e9b\u4e8b\u4ef6\u3002<\/p>\n<p>&#039;click&#039;\u4e8b\u4ef6<\/p>\n<p>\u5f53\u6258\u76d8\u56fe\u6807\u88ab\u70b9\u51fb\u65f6\u89e6\u53d1&#xff0c;\u56de\u8c03\u51fd\u6570\u63a5\u6536event\u548cbounds\u53c2\u6570\u3002bounds\u662f\u4e00\u4e2a\u5bf9\u8c61&#xff0c;\u5305\u542b\u6258\u76d8\u56fe\u6807\u7684\u4f4d\u7f6e\u548c\u5927\u5c0f\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>tray.on(&#039;click&#039;, (event, bounds) &#061;&gt; {<\/p>\n<p>\/\/ \u70b9\u51fb\u6258\u76d8\u56fe\u6807\u65f6\u5207\u6362\u7a97\u53e3\u663e\u793a\u72b6\u6001<\/p>\n<p>if (mainWindow.isVisible()) {<\/p>\n<p>mainWindow.hide();<\/p>\n<p>} else {<\/p>\n<p>mainWindow.show();<\/p>\n<p>mainWindow.focus();<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>&#039;double-click&#039;\u4e8b\u4ef6<\/p>\n<p>\u5f53\u6258\u76d8\u56fe\u6807\u88ab\u53cc\u51fb\u65f6\u89e6\u53d1&#xff0c;\u56de\u8c03\u53c2\u6570\u4e0e&#039;click&#039;\u4e8b\u4ef6\u76f8\u540c\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>tray.on(&#039;double-click&#039;, () &#061;&gt; {<\/p>\n<p>mainWindow.maximize();<\/p>\n<p>});<\/p>\n<p>&#039;right-click&#039;\u4e8b\u4ef6<\/p>\n<p>\u5f53\u6258\u76d8\u56fe\u6807\u88ab\u53f3\u952e\u70b9\u51fb\u65f6\u89e6\u53d1&#xff0c;\u56de\u8c03\u53c2\u6570\u4e0e&#039;click&#039;\u4e8b\u4ef6\u76f8\u540c\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>tray.on(&#039;right-click&#039;, () &#061;&gt; {<\/p>\n<p>\/\/ \u663e\u793a\u4e0a\u4e0b\u6587\u83dc\u5355<\/p>\n<p>contextMenu.popup({ window: mainWindow });<\/p>\n<p>});<\/p>\n<h4>2.5 Dialog \u6a21\u5757&#xff1a;\u7cfb\u7edf\u5bf9\u8bdd\u6846<\/h4>\n<p>\u7cfb\u7edf\u5bf9\u8bdd\u6846\u662f\u684c\u9762\u5e94\u7528\u4e2d\u4e0e\u7528\u6237\u4ea4\u4e92\u7684\u91cd\u8981\u65b9\u5f0f\u3002Electron \u63d0\u4f9b\u4e86Dialog\u6a21\u5757\u6765\u521b\u5efa\u5404\u79cd\u7c7b\u578b\u7684\u7cfb\u7edf\u5bf9\u8bdd\u6846\u3002<\/p>\n<h5>2.5.1 \u6d88\u606f\u5bf9\u8bdd\u6846<\/h5>\n<p>\u6d88\u606f\u5bf9\u8bdd\u6846\u7528\u4e8e\u5411\u7528\u6237\u663e\u793a\u4fe1\u606f\u3001\u8b66\u544a\u6216\u9519\u8bef\u6d88\u606f&#xff0c;\u5e76\u53ef\u4ee5\u83b7\u53d6\u7528\u6237\u7684\u53cd\u9988\u3002<\/p>\n<p>dialog.showMessageBox([browserWindow, ]options)\u65b9\u6cd5<\/p>\n<p>\u663e\u793a\u4e00\u4e2a\u6d88\u606f\u5bf9\u8bdd\u6846\u3002browserWindow\u53c2\u6570\u662f\u53ef\u9009\u7684&#xff0c;\u6307\u5b9a\u5bf9\u8bdd\u6846\u6240\u5c5e\u7684\u7a97\u53e3&#xff1b;options\u53c2\u6570\u662f\u4e00\u4e2a\u5bf9\u8c61&#xff0c;\u5305\u542b\u4ee5\u4e0b\u5c5e\u6027&#xff1a;<\/p>\n<ul>\n<li>type&#xff1a;\u5bf9\u8bdd\u6846\u7c7b\u578b&#xff0c;\u53ef\u4ee5\u662f&#039;none&#039;, &#039;info&#039;, &#039;error&#039;, &#039;question&#039;, &#039;warning&#039;\u3002<\/li>\n<\/ul>\n<ul>\n<li>title&#xff1a;\u5bf9\u8bdd\u6846\u6807\u9898\u3002<\/li>\n<\/ul>\n<ul>\n<li>message&#xff1a;\u5bf9\u8bdd\u6846\u7684\u4e3b\u8981\u6d88\u606f\u3002<\/li>\n<\/ul>\n<ul>\n<li>detail&#xff1a;\u5bf9\u8bdd\u6846\u7684\u8be6\u7ec6\u5185\u5bb9\u3002<\/li>\n<\/ul>\n<ul>\n<li>buttons&#xff1a;\u6309\u94ae\u6587\u672c\u6570\u7ec4&#xff0c;\u9ed8\u8ba4\u4e3a[&#039;\u786e\u5b9a&#039;]\u3002<\/li>\n<\/ul>\n<ul>\n<li>defaultId&#xff1a;\u9ed8\u8ba4\u9009\u4e2d\u7684\u6309\u94ae\u7d22\u5f15\u3002<\/li>\n<\/ul>\n<ul>\n<li>cancelId&#xff1a;\u53d6\u6d88\u6309\u94ae\u7684\u7d22\u5f15\u3002<\/li>\n<\/ul>\n<ul>\n<li>icon&#xff1a;\u5bf9\u8bdd\u6846\u56fe\u6807\u8def\u5f84\u3002<\/li>\n<\/ul>\n<ul>\n<li>noLink&#xff1a;\u662f\u5426\u9690\u85cf\u94fe\u63a5&#xff0c;\u4ec5\u5728 macOS \u4e0a\u6709\u6548\u3002<\/li>\n<\/ul>\n<p>\u8be5\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a Promise&#xff0c;resolve \u7684\u503c\u662f\u7528\u6237\u70b9\u51fb\u7684\u6309\u94ae\u7d22\u5f15\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;\u57fa\u672c\u6d88\u606f\u5bf9\u8bdd\u6846<\/p>\n<p>const { dialog } &#061; require(&#039;electron&#039;);<\/p>\n<p>dialog.showMessageBox({<\/p>\n<p>type: &#039;question&#039;,<\/p>\n<p>title: &#039;\u786e\u8ba4\u9000\u51fa&#039;,<\/p>\n<p>message: &#039;\u4f60\u786e\u5b9a\u8981\u9000\u51fa\u5e94\u7528\u5417&#xff1f;&#039;,<\/p>\n<p>buttons: [&#039;\u53d6\u6d88&#039;, &#039;\u786e\u5b9a&#039;]<\/p>\n<p>}).then(result &#061;&gt; {<\/p>\n<p>if (result.response &#061;&#061;&#061; 1) {<\/p>\n<p>app.quit();<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<h5>2.5.2 \u6587\u4ef6\u9009\u62e9\u5bf9\u8bdd\u6846<\/h5>\n<p>\u6587\u4ef6\u9009\u62e9\u5bf9\u8bdd\u6846\u5141\u8bb8\u7528\u6237\u9009\u62e9\u6587\u4ef6\u6216\u76ee\u5f55\u3002<\/p>\n<p>dialog.showOpenDialog([browserWindow, ]options)\u65b9\u6cd5<\/p>\n<p>\u663e\u793a\u4e00\u4e2a\u6253\u5f00\u6587\u4ef6\u5bf9\u8bdd\u6846\u3002options\u53c2\u6570\u662f\u4e00\u4e2a\u5bf9\u8c61&#xff0c;\u5305\u542b\u4ee5\u4e0b\u5c5e\u6027&#xff1a;<\/p>\n<ul>\n<li>title&#xff1a;\u5bf9\u8bdd\u6846\u6807\u9898\u3002<\/li>\n<\/ul>\n<ul>\n<li>defaultPath&#xff1a;\u9ed8\u8ba4\u8def\u5f84\u3002<\/li>\n<\/ul>\n<ul>\n<li>buttonLabel&#xff1a;\u6309\u94ae\u6807\u7b7e\u3002<\/li>\n<\/ul>\n<ul>\n<li>filters&#xff1a;\u6587\u4ef6\u8fc7\u6ee4\u5668&#xff0c;\u662f\u4e00\u4e2a\u6570\u7ec4&#xff0c;\u6bcf\u4e2a\u5143\u7d20\u5305\u542bname\u548cextensions\u5c5e\u6027\u3002<\/li>\n<\/ul>\n<ul>\n<li>properties&#xff1a;\u5bf9\u8bdd\u6846\u5c5e\u6027\u6570\u7ec4&#xff0c;\u53ef\u4ee5\u5305\u542b&#039;openFile&#039;, &#039;openDirectory&#039;, &#039;multiSelections&#039;, &#039;showHiddenFiles&#039;, &#039;createDirectory&#039;, &#039;promptToCreate&#039;, &#039;noResolveAliases&#039;, &#039;dontAddToRecent&#039;\u3002<\/li>\n<\/ul>\n<p>\u8be5\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a Promise&#xff0c;resolve \u7684\u503c\u662f\u4e00\u4e2a\u5bf9\u8c61&#xff0c;\u5305\u542bfilePaths\u5c5e\u6027&#xff08;\u9009\u4e2d\u7684\u6587\u4ef6\u8def\u5f84\u6570\u7ec4&#xff09;\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;\u6253\u5f00\u6587\u4ef6\u5bf9\u8bdd\u6846<\/p>\n<p>const { dialog } &#061; require(&#039;electron&#039;);<\/p>\n<p>dialog.showOpenDialog({<\/p>\n<p>title: &#039;\u6253\u5f00\u6587\u4ef6&#039;,<\/p>\n<p>defaultPath: app.getPath(&#039;documents&#039;),<\/p>\n<p>filters: [<\/p>\n<p>{ name: &#039;\u6587\u672c\u6587\u4ef6&#039;, extensions: [&#039;txt&#039;, &#039;md&#039;] },<\/p>\n<p>{ name: &#039;\u6240\u6709\u6587\u4ef6&#039;, extensions: [&#039;*&#039;] }<\/p>\n<p>],<\/p>\n<p>properties: [&#039;openFile&#039;, &#039;multiSelections&#039;]<\/p>\n<p>}).then(result &#061;&gt; {<\/p>\n<p>if (!result.canceled) {<\/p>\n<p>console.log(&#039;\u9009\u4e2d\u7684\u6587\u4ef6&#xff1a;&#039;, result.filePaths);<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>dialog.showSaveDialog([browserWindow, ]options)\u65b9\u6cd5<\/p>\n<p>\u663e\u793a\u4e00\u4e2a\u4fdd\u5b58\u6587\u4ef6\u5bf9\u8bdd\u6846\u3002options\u53c2\u6570\u4e0eshowOpenDialog\u7c7b\u4f3c&#xff0c;\u8fd8\u53ef\u4ee5\u5305\u542bdefaultPath\u5c5e\u6027\u6307\u5b9a\u9ed8\u8ba4\u6587\u4ef6\u540d\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;\u4fdd\u5b58\u6587\u4ef6\u5bf9\u8bdd\u6846<\/p>\n<p>dialog.showSaveDialog({<\/p>\n<p>title: &#039;\u4fdd\u5b58\u6587\u4ef6&#039;,<\/p>\n<p>defaultPath: path.join(app.getPath(&#039;documents&#039;), &#039;new-file.txt&#039;),<\/p>\n<p>filters: [<\/p>\n<p>{ name: &#039;\u6587\u672c\u6587\u4ef6&#039;, extensions: [&#039;txt&#039;] }<\/p>\n<p>]<\/p>\n<p>}).then(result &#061;&gt; {<\/p>\n<p>if (!result.canceled) {<\/p>\n<p>console.log(&#039;\u4fdd\u5b58\u8def\u5f84&#xff1a;&#039;, result.filePath);<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<h5>2.5.3 \u5176\u4ed6\u7c7b\u578b\u5bf9\u8bdd\u6846<\/h5>\n<p>\u9664\u4e86\u6d88\u606f\u5bf9\u8bdd\u6846\u548c\u6587\u4ef6\u9009\u62e9\u5bf9\u8bdd\u6846\u5916&#xff0c;Dialog\u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u7c7b\u578b\u7684\u5bf9\u8bdd\u6846&#xff1a;<\/p>\n<p>dialog.showErrorBox(title, content)\u65b9\u6cd5<\/p>\n<p>\u663e\u793a\u4e00\u4e2a\u9519\u8bef\u6d88\u606f\u6846&#xff0c;\u662fshowMessageBox\u7684\u7b80\u5316\u7248&#xff0c;\u4e13\u95e8\u7528\u4e8e\u663e\u793a\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>dialog.showErrorBox(&#039;\u9519\u8bef&#039;, &#039;\u65e0\u6cd5\u6253\u5f00\u6587\u4ef6&#xff0c;\u8bf7\u68c0\u67e5\u6587\u4ef6\u8def\u5f84\u662f\u5426\u6b63\u786e\u3002&#039;);<\/p>\n<p>dialog.showCertificateTrustDialog([browserWindow, ]options)\u65b9\u6cd5<\/p>\n<p>\u663e\u793a\u8bc1\u4e66\u4fe1\u4efb\u5bf9\u8bdd\u6846&#xff0c;\u7528\u4e8e\u5904\u7406 SSL \u8bc1\u4e66\u95ee\u9898\u3002<\/p>\n<p>dialog.showMessageBoxSync([browserWindow, ]options)\u65b9\u6cd5<\/p>\n<p>\u540c\u6b65\u7248\u672c\u7684showMessageBox&#xff0c;\u4f1a\u963b\u585e\u5f53\u524d\u8fdb\u7a0b\u76f4\u5230\u5bf9\u8bdd\u6846\u5173\u95ed\u3002\u4e0d\u5efa\u8bae\u5728\u4e3b\u8fdb\u7a0b\u4e2d\u4f7f\u7528&#xff0c;\u56e0\u4e3a\u4f1a\u963b\u585e\u4e8b\u4ef6\u5faa\u73af\u3002<\/p>\n<h3>\u4e09\u3001\u6e32\u67d3\u8fdb\u7a0b API \u8be6\u89e3<\/h3>\n<h4>3.1 ipcRenderer \u6a21\u5757&#xff1a;\u6e32\u67d3\u8fdb\u7a0b\u901a\u4fe1<\/h4>\n<p>\u5728 Electron \u4e2d&#xff0c;\u6e32\u67d3\u8fdb\u7a0b\u548c\u4e3b\u8fdb\u7a0b\u4e4b\u95f4\u9700\u8981\u8fdb\u884c\u901a\u4fe1&#xff0c;ipcRenderer\u6a21\u5757\u63d0\u4f9b\u4e86\u5728\u6e32\u67d3\u8fdb\u7a0b\u4e2d\u4e0e\u4e3b\u8fdb\u7a0b\u901a\u4fe1\u7684\u80fd\u529b\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7const { ipcRenderer } &#061; require(&#039;electron&#039;)\u6765\u83b7\u53d6ipcRenderer\u6a21\u5757\u7684\u5b9e\u4f8b\u3002<\/p>\n<h5>3.1.1 \u53d1\u9001\u6d88\u606f\u5230\u4e3b\u8fdb\u7a0b<\/h5>\n<p>\u6e32\u67d3\u8fdb\u7a0b\u53ef\u4ee5\u4f7f\u7528ipcRenderer.send(channel[, &#8230;args])\u65b9\u6cd5\u5411\u4e3b\u8fdb\u7a0b\u53d1\u9001\u6d88\u606f\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>\/\/ \u53d1\u9001\u7b80\u5355\u6d88\u606f<\/p>\n<p>ipcRenderer.send(&#039;message-from-renderer&#039;, &#039;\u4f60\u597d&#xff0c;\u4e3b\u8fdb\u7a0b&#xff01;&#039;);<\/p>\n<p>\/\/ \u53d1\u9001\u5305\u542b\u6570\u636e\u7684\u6d88\u606f<\/p>\n<p>ipcRenderer.send(&#039;data-from-renderer&#039;, { key: &#039;value&#039;, array: [1, 2, 3] });<\/p>\n<h5>3.1.2 \u63a5\u6536\u4e3b\u8fdb\u7a0b\u6d88\u606f<\/h5>\n<p>\u6e32\u67d3\u8fdb\u7a0b\u53ef\u4ee5\u4f7f\u7528ipcRenderer.on(channel, listener)\u65b9\u6cd5\u76d1\u542c\u6765\u81ea\u4e3b\u8fdb\u7a0b\u7684\u6d88\u606f\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>ipcRenderer.on(&#039;message-from-main&#039;, (event, message) &#061;&gt; {<\/p>\n<p>console.log(&#039;\u6536\u5230\u4e3b\u8fdb\u7a0b\u6d88\u606f:&#039;, message);<\/p>\n<p>});<\/p>\n<p>ipcRenderer.on(&#039;data-from-main&#039;, (event, data) &#061;&gt; {<\/p>\n<p>console.log(&#039;\u6536\u5230\u4e3b\u8fdb\u7a0b\u6570\u636e:&#039;, data);<\/p>\n<p>});<\/p>\n<h5>3.1.3 \u53d1\u9001\u540c\u6b65\u6d88\u606f<\/h5>\n<p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b&#xff0c;\u6e32\u67d3\u8fdb\u7a0b\u9700\u8981\u53d1\u9001\u540c\u6b65\u6d88\u606f\u5e76\u7acb\u5373\u83b7\u53d6\u54cd\u5e94\u3002\u53ef\u4ee5\u4f7f\u7528ipcRenderer.sendSync(channel[, &#8230;args])\u65b9\u6cd5\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>\/\/ \u53d1\u9001\u540c\u6b65\u6d88\u606f\u5e76\u83b7\u53d6\u54cd\u5e94<\/p>\n<p>const response &#061; ipcRenderer.sendSync(&#039;sync-message&#039;, &#039;\u8bf7\u6c42\u6570\u636e&#039;);<\/p>\n<p>console.log(&#039;\u4e3b\u8fdb\u7a0b\u54cd\u5e94:&#039;, response);<\/p>\n<p>** \u6ce8\u610f&#xff1a;** \u540c\u6b65\u6d88\u606f\u4f1a\u963b\u585e\u6e32\u67d3\u8fdb\u7a0b&#xff0c;\u53ef\u80fd\u5bfc\u81f4\u5e94\u7528\u5361\u987f&#xff0c;\u5e94\u8c28\u614e\u4f7f\u7528\u3002<\/p>\n<h5>3.1.4 \u4e00\u6b21\u6027\u76d1\u542c<\/h5>\n<p>\u5982\u679c\u53ea\u9700\u8981\u76d1\u542c\u4e00\u6b21\u6765\u81ea\u4e3b\u8fdb\u7a0b\u7684\u6d88\u606f&#xff0c;\u53ef\u4ee5\u4f7f\u7528ipcRenderer.once(channel, listener)\u65b9\u6cd5\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>ipcRenderer.once(&#039;one-time-message&#039;, (event, message) &#061;&gt; {<\/p>\n<p>console.log(&#039;\u4e00\u6b21\u6027\u6d88\u606f:&#039;, message);<\/p>\n<p>});<\/p>\n<h4>3.2 remote \u6a21\u5757&#xff1a;\u76f4\u63a5\u8bbf\u95ee\u4e3b\u8fdb\u7a0b\u5bf9\u8c61<\/h4>\n<p>remote\u6a21\u5757\u5141\u8bb8\u6e32\u67d3\u8fdb\u7a0b\u76f4\u63a5\u8bbf\u95ee\u4e3b\u8fdb\u7a0b\u4e2d\u7684\u5bf9\u8c61&#xff0c;\u800c\u65e0\u9700\u901a\u8fc7ipcRenderer\u548cipcMain\u8fdb\u884c\u663e\u5f0f\u901a\u4fe1\u3002\u4e0d\u8fc7&#xff0c;\u7531\u4e8e\u5b89\u5168\u548c\u6027\u80fd\u8003\u8651&#xff0c;\u4e0d\u5efa\u8bae\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u4f7f\u7528remote\u6a21\u5757\u3002<\/p>\n<h5>3.2.1 \u4f7f\u7528 remote \u6a21\u5757<\/h5>\n<p>\u8981\u4f7f\u7528remote\u6a21\u5757&#xff0c;\u53ef\u4ee5\u901a\u8fc7const { remote } &#061; require(&#039;electron&#039;)\u6765\u83b7\u53d6\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;\u83b7\u53d6\u4e3b\u8fdb\u7a0b\u4e2d\u7684 BrowserWindow \u5bf9\u8c61<\/p>\n<p>const { remote } &#061; require(&#039;electron&#039;);<\/p>\n<p>const BrowserWindow &#061; remote.BrowserWindow;<\/p>\n<p>\/\/ \u83b7\u53d6\u5f53\u524d\u7a97\u53e3\u7684\u5f15\u7528<\/p>\n<p>const currentWindow &#061; BrowserWindow.getFocusedWindow();<\/p>\n<p>console.log(&#096;\u5f53\u524d\u7a97\u53e3\u6807\u9898&#xff1a;${currentWindow.getTitle()}&#096;);<\/p>\n<h5>3.2.2 remote \u6a21\u5757\u7684\u66ff\u4ee3\u65b9\u6848<\/h5>\n<p>\u7531\u4e8eremote\u6a21\u5757\u5b58\u5728\u5b89\u5168\u98ce\u9669&#xff0c;\u63a8\u8350\u4f7f\u7528ipcRenderer\u548cipcMain\u8fdb\u884c\u8fdb\u7a0b\u95f4\u901a\u4fe1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u66ff\u4ee3\u65b9\u6848\u7684\u793a\u4f8b&#xff1a;<\/p>\n<p>\u6e32\u67d3\u8fdb\u7a0b\u4ee3\u7801&#xff1a;<\/p>\n<p>const { ipcRenderer } &#061; require(&#039;electron&#039;);<\/p>\n<p>\/\/ \u53d1\u9001\u6d88\u606f\u8bf7\u6c42\u7a97\u53e3\u6807\u9898<\/p>\n<p>ipcRenderer.send(&#039;request-window-title&#039;);<\/p>\n<p>\/\/ \u76d1\u542c\u54cd\u5e94<\/p>\n<p>ipcRenderer.on(&#039;window-title&#039;, (event, title) &#061;&gt; {<\/p>\n<p>console.log(&#039;\u7a97\u53e3\u6807\u9898:&#039;, title);<\/p>\n<p>});<\/p>\n<p>\u4e3b\u8fdb\u7a0b\u4ee3\u7801&#xff1a;<\/p>\n<p>const { ipcMain } &#061; require(&#039;electron&#039;);<\/p>\n<p>ipcMain.on(&#039;request-window-title&#039;, (event) &#061;&gt; {<\/p>\n<p>const window &#061; BrowserWindow.getFocusedWindow();<\/p>\n<p>event.sender.send(&#039;window-title&#039;, window.getTitle());<\/p>\n<p>});<\/p>\n<h4>3.3 webFrame \u6a21\u5757&#xff1a;\u64cd\u4f5c\u7f51\u9875\u5185\u5bb9<\/h4>\n<p>webFrame\u6a21\u5757\u5141\u8bb8\u5728\u6e32\u67d3\u8fdb\u7a0b\u4e2d\u64cd\u4f5c\u5f53\u524d\u7f51\u9875\u7684\u6846\u67b6&#xff08;frame&#xff09;&#xff0c;\u5305\u62ec\u7f29\u653e\u3001\u6267\u884c JavaScript \u4ee3\u7801\u7b49\u3002<\/p>\n<h5>3.3.1 \u7f51\u9875\u7f29\u653e<\/h5>\n<p>webFrame\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u65b9\u6cd5\u6765\u63a7\u5236\u7f51\u9875\u7684\u7f29\u653e\u7ea7\u522b\u3002<\/p>\n<p>webFrame.setZoomFactor(factor)\u65b9\u6cd5<\/p>\n<p>\u8bbe\u7f6e\u7f51\u9875\u7684\u7f29\u653e\u56e0\u5b50\u3002factor\u662f\u4e00\u4e2a\u6570\u503c&#xff0c;1.0 \u8868\u793a\u6b63\u5e38\u5927\u5c0f&#xff0c;2.0 \u8868\u793a\u53cc\u500d\u5927\u5c0f&#xff0c;0.5 \u8868\u793a\u4e00\u534a\u5927\u5c0f\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>const { webFrame } &#061; require(&#039;electron&#039;);<\/p>\n<p>\/\/ \u653e\u592750%<\/p>\n<p>webFrame.setZoomFactor(1.5);<\/p>\n<p>\/\/ \u7f29\u5c0f25%<\/p>\n<p>webFrame.setZoomFactor(0.75);<\/p>\n<p>webFrame.getZoomFactor()\u65b9\u6cd5<\/p>\n<p>\u83b7\u53d6\u5f53\u524d\u7684\u7f29\u653e\u56e0\u5b50\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>console.log(&#096;\u5f53\u524d\u7f29\u653e\u56e0\u5b50&#xff1a;${webFrame.getZoomFactor()}&#096;);<\/p>\n<p>webFrame.setZoomLevel(level)\u65b9\u6cd5<\/p>\n<p>\u8bbe\u7f6e\u7f51\u9875\u7684\u7f29\u653e\u7ea7\u522b\u3002level\u662f\u4e00\u4e2a\u6574\u6570&#xff0c;0 \u8868\u793a\u6b63\u5e38\u5927\u5c0f&#xff0c;\u6b63\u6570\u8868\u793a\u653e\u5927&#xff0c;\u8d1f\u6570\u8868\u793a\u7f29\u5c0f\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>\/\/ \u653e\u5927\u4e00\u7ea7<\/p>\n<p>webFrame.setZoomLevel(1);<\/p>\n<p>\/\/ \u7f29\u5c0f\u4e24\u7ea7<\/p>\n<p>webFrame.setZoomLevel(-2);<\/p>\n<h5>3.3.2 \u6267\u884c JavaScript \u4ee3\u7801<\/h5>\n<p>webFrame\u6a21\u5757\u5141\u8bb8\u5728\u5f53\u524d\u7f51\u9875\u7684\u4e0a\u4e0b\u6587\u4e2d\u6267\u884c JavaScript \u4ee3\u7801\u3002<\/p>\n<p>webFrame.executeJavaScript(code[, userGesture, callback])\u65b9\u6cd5<\/p>\n<p>\u5728\u5f53\u524d\u7f51\u9875\u7684\u4e0a\u4e0b\u6587\u4e2d\u6267\u884c JavaScript \u4ee3\u7801\u3002userGesture\u53c2\u6570\u8868\u793a\u662f\u5426\u7531\u7528\u6237\u624b\u52bf\u89e6\u53d1&#xff08;\u5982\u70b9\u51fb\u4e8b\u4ef6&#xff09;&#xff0c;callback\u662f\u6267\u884c\u5b8c\u6210\u540e\u7684\u56de\u8c03\u51fd\u6570\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>webFrame.executeJavaScript(&#096;<\/p>\n<p>document.body.style.backgroundColor &#061; &#039;lightblue&#039;;<\/p>\n<p>console.log(&#039;\u80cc\u666f\u989c\u8272\u5df2\u6539\u53d8&#039;);<\/p>\n<p>&#096;, true, (result) &#061;&gt; {<\/p>\n<p>console.log(&#039;JavaScript\u6267\u884c\u7ed3\u679c:&#039;, result);<\/p>\n<p>});<\/p>\n<p>webFrame.insertCSS(css[, options])\u65b9\u6cd5<\/p>\n<p>\u5411\u5f53\u524d\u7f51\u9875\u63d2\u5165 CSS \u6837\u5f0f\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>webFrame.insertCSS(&#096;<\/p>\n<p>h1 {<\/p>\n<p>color: red;<\/p>\n<p>text-decoration: underline;<\/p>\n<p>}<\/p>\n<p>&#096;);<\/p>\n<h5>3.3.3 \u8d44\u6e90\u52a0\u8f7d\u63a7\u5236<\/h5>\n<p>webFrame\u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u65b9\u6cd5\u6765\u63a7\u5236\u8d44\u6e90\u52a0\u8f7d\u884c\u4e3a\u3002<\/p>\n<p>webFrame.registerURLSchemeAsSecure(scheme)\u65b9\u6cd5<\/p>\n<p>\u6ce8\u518c\u4e00\u4e2a URL \u534f\u8bae\u4e3a\u5b89\u5168\u534f\u8bae&#xff0c;\u4f7f\u5176\u53ef\u4ee5\u4f7f\u7528https:\u76f8\u5173\u7684 API&#xff0c;\u5982XMLHttpRequest\u548cfetch\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>webFrame.registerURLSchemeAsSecure(&#039;myapp&#039;);<\/p>\n<p>webFrame.registerURLSchemeAsPrivileged(scheme, options)\u65b9\u6cd5<\/p>\n<p>\u6ce8\u518c\u4e00\u4e2a URL \u534f\u8bae\u4e3a\u7279\u6743\u534f\u8bae&#xff0c;\u4f7f\u5176\u5177\u6709\u66f4\u591a\u6743\u9650\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>webFrame.registerURLSchemeAsPrivileged(&#039;myapp&#039;, {<\/p>\n<p>secure: true,<\/p>\n<p>standard: true,<\/p>\n<p>supportFetchAPI: true<\/p>\n<p>});<\/p>\n<h4>3.4 clipboard \u6a21\u5757&#xff1a;\u526a\u8d34\u677f\u64cd\u4f5c<\/h4>\n<p>clipboard\u6a21\u5757\u5141\u8bb8\u5728\u6e32\u67d3\u8fdb\u7a0b\u4e2d\u8bbf\u95ee\u7cfb\u7edf\u526a\u8d34\u677f&#xff0c;\u5b9e\u73b0\u590d\u5236\u3001\u526a\u5207\u548c\u7c98\u8d34\u529f\u80fd\u3002<\/p>\n<h5>3.4.1 \u5199\u5165\u526a\u8d34\u677f<\/h5>\n<p>clipboard\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u5411\u526a\u8d34\u677f\u5199\u5165\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u3002<\/p>\n<p>clipboard.writeText(text)\u65b9\u6cd5<\/p>\n<p>\u5411\u526a\u8d34\u677f\u5199\u5165\u7eaf\u6587\u672c\u6570\u636e\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>const { clipboard } &#061; require(&#039;electron&#039;);<\/p>\n<p>clipboard.writeText(&#039;\u9009\u4e2d\u7684\u6587\u672c&#039;);<\/p>\n<p>clipboard.writeHTML(html)\u65b9\u6cd5<\/p>\n<p>\u5411\u526a\u8d34\u677f\u5199\u5165 HTML \u683c\u5f0f\u7684\u6570\u636e\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>clipboard.writeHTML(&#039;&lt;strong&gt;\u52a0\u7c97\u6587\u672c&lt;\/strong&gt;&#039;);<\/p>\n<p>clipboard.writeImage(image)\u65b9\u6cd5<\/p>\n<p>\u5411\u526a\u8d34\u677f\u5199\u5165\u56fe\u7247\u6570\u636e\u3002image\u53c2\u6570\u662f\u4e00\u4e2aNativeImage\u5bf9\u8c61\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>const { NativeImage } &#061; require(&#039;electron&#039;);<\/p>\n<p>const image &#061; NativeImage.createFromPath(&#039;image.png&#039;);<\/p>\n<p>clipboard.writeImage(image);<\/p>\n<p>clipboard.write(data)\u65b9\u6cd5<\/p>\n<p>\u5411\u526a\u8d34\u677f\u5199\u5165\u591a\u79cd\u683c\u5f0f\u7684\u6570\u636e\u3002data\u53c2\u6570\u662f\u4e00\u4e2a\u5bf9\u8c61&#xff0c;\u952e\u662f MIME \u7c7b\u578b&#xff0c;\u503c\u662f\u5bf9\u5e94\u7684\u6570\u636e\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>clipboard.write({<\/p>\n<p>&#039;text\/plain&#039;: &#039;\u7eaf\u6587\u672c&#039;,<\/p>\n<p>&#039;text\/html&#039;: &#039;&lt;p&gt;HTML\u5185\u5bb9&lt;\/p&gt;&#039;<\/p>\n<p>});<\/p>\n<h5>3.4.2 \u8bfb\u53d6\u526a\u8d34\u677f<\/h5>\n<p>clipboard\u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86\u65b9\u6cd5\u4ece\u526a\u8d34\u677f\u8bfb\u53d6\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u3002<\/p>\n<p>clipboard.readText()\u65b9\u6cd5<\/p>\n<p>\u4ece\u526a\u8d34\u677f\u8bfb\u53d6\u7eaf\u6587\u672c\u6570\u636e\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>const text &#061; clipboard.readText();<\/p>\n<p>console.log(&#039;\u526a\u8d34\u677f\u4e2d\u7684\u6587\u672c:&#039;, text);<\/p>\n<p>clipboard.readHTML()\u65b9\u6cd5<\/p>\n<p>\u4ece\u526a\u8d34\u677f\u8bfb\u53d6 HTML \u683c\u5f0f\u7684\u6570\u636e\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>const html &#061; clipboard.readHTML();<\/p>\n<p>console.log(&#039;\u526a\u8d34\u677f\u4e2d\u7684HTML:&#039;, html);<\/p>\n<p>clipboard.readImage()\u65b9\u6cd5<\/p>\n<p>\u4ece\u526a\u8d34\u677f\u8bfb\u53d6\u56fe\u7247\u6570\u636e&#xff0c;\u8fd4\u56de\u4e00\u4e2aNativeImage\u5bf9\u8c61\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>const image &#061; clipboard.readImage();<\/p>\n<p>if (!image.isEmpty()) {<\/p>\n<p>image.saveToPath(&#039;clipboard-image.png&#039;);<\/p>\n<p>}<\/p>\n<p>clipboard.read(dataTypes)\u65b9\u6cd5<\/p>\n<p>\u4ece\u526a\u8d34\u677f\u8bfb\u53d6\u6307\u5b9a MIME \u7c7b\u578b\u7684\u6570\u636e\u3002dataTypes\u53c2\u6570\u662f\u4e00\u4e2a\u6570\u7ec4&#xff0c;\u5305\u542b\u8981\u8bfb\u53d6\u7684 MIME \u7c7b\u578b\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>const data &#061; clipboard.read([&#039;text\/plain&#039;, &#039;text\/html&#039;]);<\/p>\n<p>console.log(&#039;\u526a\u8d34\u677f\u4e2d\u7684\u6570\u636e:&#039;, data);<\/p>\n<h5>3.4.3 \u526a\u8d34\u677f\u53d8\u5316\u76d1\u542c<\/h5>\n<p>clipboard\u6a21\u5757\u63d0\u4f9b\u4e86on\u65b9\u6cd5\u6765\u76d1\u542c\u526a\u8d34\u677f\u5185\u5bb9\u7684\u53d8\u5316\u3002<\/p>\n<p>clipboard.on(&#039;change&#039;, (event, type) &#061;&gt; { &#8230; })\u4e8b\u4ef6<\/p>\n<p>\u5f53\u526a\u8d34\u677f\u5185\u5bb9\u53d1\u751f\u53d8\u5316\u65f6\u89e6\u53d1\u3002type\u53c2\u6570\u8868\u793a\u53d8\u5316\u7684\u7c7b\u578b&#xff0c;\u53ef\u4ee5\u662f&#039;copy&#039;, &#039;cut&#039;, &#039;paste&#039;\u6216&#039;unknown&#039;\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>clipboard.on(&#039;change&#039;, (event, type) &#061;&gt; {<\/p>\n<p>console.log(&#096;\u526a\u8d34\u677f\u5185\u5bb9\u5df2\u6539\u53d8&#xff08;\u7c7b\u578b&#xff1a;${type}&#xff09;&#096;);<\/p>\n<p>console.log(&#039;\u5f53\u524d\u526a\u8d34\u677f\u6587\u672c&#xff1a;&#039;, clipboard.readText());<\/p>\n<p>});<\/p>\n<h4>3.5 screen \u6a21\u5757&#xff1a;\u5c4f\u5e55\u4fe1\u606f\u548c\u5c3a\u5bf8<\/h4>\n<p>screen\u6a21\u5757\u63d0\u4f9b\u4e86\u5173\u4e8e\u5c4f\u5e55\u7684\u4fe1\u606f&#xff0c;\u5982\u663e\u793a\u5668\u7684\u5c3a\u5bf8\u3001\u5de5\u4f5c\u533a\u57df\u3001\u7f29\u653e\u56e0\u5b50\u7b49\u3002<\/p>\n<h5>3.5.1 \u83b7\u53d6\u5c4f\u5e55\u4fe1\u606f<\/h5>\n<p>screen\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u83b7\u53d6\u5c4f\u5e55\u76f8\u5173\u4fe1\u606f\u3002<\/p>\n<p>screen.getAllDisplays()\u65b9\u6cd5<\/p>\n<p>\u8fd4\u56de\u6240\u6709\u663e\u793a\u5668\u7684\u4fe1\u606f\u6570\u7ec4\u3002\u6bcf\u4e2a\u663e\u793a\u5668\u5bf9\u8c61\u5305\u542b\u4ee5\u4e0b\u5c5e\u6027&#xff1a;<\/p>\n<ul>\n<li>bounds&#xff1a;\u663e\u793a\u5668\u7684\u8fb9\u754c\u77e9\u5f62&#xff0c;\u5305\u542bx, y, width, height\u3002<\/li>\n<\/ul>\n<ul>\n<li>workArea&#xff1a;\u663e\u793a\u5668\u7684\u5de5\u4f5c\u533a\u57df\u77e9\u5f62&#xff08;\u4e0d\u5305\u542b\u4efb\u52a1\u680f\u7b49\u7cfb\u7edf\u5143\u7d20&#xff09;\u3002<\/li>\n<\/ul>\n<ul>\n<li>scaleFactor&#xff1a;\u663e\u793a\u5668\u7684\u7f29\u653e\u56e0\u5b50\u3002<\/li>\n<\/ul>\n<ul>\n<li>id&#xff1a;\u663e\u793a\u5668\u7684\u552f\u4e00\u6807\u8bc6\u7b26\u3002<\/li>\n<\/ul>\n<ul>\n<li>rotation&#xff1a;\u663e\u793a\u5668\u7684\u65cb\u8f6c\u89d2\u5ea6\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>const { screen } &#061; require(&#039;electron&#039;);<\/p>\n<p>const displays &#061; screen.getAllDisplays();<\/p>\n<p>displays.forEach((display, index) &#061;&gt; {<\/p>\n<p>console.log(&#096;\u663e\u793a\u5668 ${index &#043; 1} \u4fe1\u606f:&#096;);<\/p>\n<p>console.log(&#096; \u8fb9\u754c&#xff1a;${display.bounds.x}, ${display.bounds.y}, ${display.bounds.width}x${display.bounds.height}&#096;);<\/p>\n<p>console.log(&#096; \u5de5\u4f5c\u533a\u57df&#xff1a;${display.workArea.x}, ${display.workArea.y}, ${display.workArea.width}x${display.workArea.height}&#096;);<\/p>\n<p>console.log(&#096; \u7f29\u653e\u56e0\u5b50&#xff1a;${display.scaleFactor}&#096;);<\/p>\n<p>});<\/p>\n<p>screen.getPrimaryDisplay()\u65b9\u6cd5<\/p>\n<p>\u8fd4\u56de\u4e3b\u663e\u793a\u5668\u7684\u4fe1\u606f\u5bf9\u8c61\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>const primaryDisplay &#061; screen.getPrimaryDisplay();<\/p>\n<p>console.log(&#039;\u4e3b\u663e\u793a\u5668\u5de5\u4f5c\u533a\u57df&#xff1a;&#039;, primaryDisplay.workArea);<\/p>\n<h5>3.5.2 \u7a97\u53e3\u4f4d\u7f6e\u8ba1\u7b97<\/h5>\n<p>screen\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u65b9\u6cd5\u6765\u5e2e\u52a9\u8ba1\u7b97\u7a97\u53e3\u7684\u4f4d\u7f6e&#xff0c;\u4f7f\u5176\u9002\u5e94\u4e0d\u540c\u7684\u663e\u793a\u5668\u914d\u7f6e\u3002<\/p>\n<p>screen.getCursorScreenPoint()\u65b9\u6cd5<\/p>\n<p>\u8fd4\u56de\u5f53\u524d\u9f20\u6807\u6307\u9488\u5728\u5c4f\u5e55\u5750\u6807\u7cfb\u4e2d\u7684\u4f4d\u7f6e\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>const cursorPosition &#061; screen.getCursorScreenPoint();<\/p>\n<p>console.log(&#096;\u9f20\u6807\u4f4d\u7f6e&#xff1a;${cursorPosition.x}, ${cursorPosition.y}&#096;);<\/p>\n<p>screen.getDisplayNearestPoint(point)\u65b9\u6cd5<\/p>\n<p>\u8fd4\u56de\u79bb\u6307\u5b9a\u70b9\u6700\u8fd1\u7684\u663e\u793a\u5668\u4fe1\u606f\u5bf9\u8c61\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>const point &#061; { x: 100, y: 100 };<\/p>\n<p>const display &#061; screen.getDisplayNearestPoint(point);<\/p>\n<p>console.log(&#096;\u79bb\u70b9 (100, 100) \u6700\u8fd1\u7684\u663e\u793a\u5668\u5de5\u4f5c\u533a\u57df&#xff1a;&#096;, display.workArea);<\/p>\n<p>screen.getWorkingAreaOfCursor()\u65b9\u6cd5<\/p>\n<p>\u8fd4\u56de\u5f53\u524d\u9f20\u6807\u6307\u9488\u6240\u5728\u663e\u793a\u5668\u7684\u5de5\u4f5c\u533a\u57df\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>const workingArea &#061; screen.getWorkingAreaOfCursor();<\/p>\n<p>console.log(&#096;\u9f20\u6807\u6240\u5728\u663e\u793a\u5668\u7684\u5de5\u4f5c\u533a\u57df&#xff1a;${workingArea.x}, ${workingArea.y}, ${workingArea.width}x${workingArea.height}&#096;);<\/p>\n<h3>\u56db\u3001\u8fdb\u7a0b\u95f4\u901a\u4fe1\u4e0e\u9ad8\u7ea7\u4e3b\u9898<\/h3>\n<h4>4.1 ipcMain \u6a21\u5757&#xff1a;\u4e3b\u8fdb\u7a0b\u901a\u4fe1<\/h4>\n<p>ipcMain\u6a21\u5757\u7528\u4e8e\u5728\u4e3b\u8fdb\u7a0b\u4e2d\u76d1\u542c\u548c\u5904\u7406\u6765\u81ea\u6e32\u67d3\u8fdb\u7a0b\u7684\u6d88\u606f&#xff0c;\u662f\u5b9e\u73b0\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684\u5173\u952e\u6a21\u5757\u4e4b\u4e00\u3002<\/p>\n<h5>4.1.1 \u76d1\u542c\u6d88\u606f<\/h5>\n<p>\u4e3b\u8fdb\u7a0b\u53ef\u4ee5\u4f7f\u7528ipcMain.on(channel, listener)\u65b9\u6cd5\u76d1\u542c\u6765\u81ea\u6e32\u67d3\u8fdb\u7a0b\u7684\u6d88\u606f\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>const { ipcMain } &#061; require(&#039;electron&#039;);<\/p>\n<p>ipcMain.on(&#039;message-from-renderer&#039;, (event, message) &#061;&gt; {<\/p>\n<p>console.log(&#039;\u6536\u5230\u6e32\u67d3\u8fdb\u7a0b\u6d88\u606f:&#039;, message);<\/p>\n<p>\/\/ \u53d1\u9001\u54cd\u5e94\u6d88\u606f<\/p>\n<p>event.sender.send(&#039;reply-to-renderer&#039;, &#039;\u4f60\u597d&#xff0c;\u6e32\u67d3\u8fdb\u7a0b&#xff01;&#039;);<\/p>\n<p>});<\/p>\n<p>event\u53c2\u6570\u8bf4\u660e<\/p>\n<p>listener\u56de\u8c03\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662fevent\u5bf9\u8c61&#xff0c;\u5b83\u5305\u542b\u4ee5\u4e0b\u5c5e\u6027\u548c\u65b9\u6cd5&#xff1a;<\/p>\n<ul>\n<li>event.sender&#xff1a;\u53d1\u9001\u6d88\u606f\u7684\u6e32\u67d3\u8fdb\u7a0b\u5bf9\u8c61&#xff08;WebContents\u5b9e\u4f8b&#xff09;\u3002<\/li>\n<\/ul>\n<ul>\n<li>event.preventDefault()&#xff1a;\u963b\u6b62\u9ed8\u8ba4\u884c\u4e3a&#xff08;\u5982\u679c\u6709\u7684\u8bdd&#xff09;\u3002<\/li>\n<\/ul>\n<ul>\n<li>event.returnValue&#xff1a;\u8bbe\u7f6e\u540c\u6b65\u6d88\u606f\u7684\u8fd4\u56de\u503c\u3002<\/li>\n<\/ul>\n<ul>\n<li>event.send(channel[, &#8230;args])&#xff1a;\u5411\u53d1\u9001\u8005\u53d1\u9001\u6d88\u606f\u3002<\/li>\n<\/ul>\n<h5>4.1.2 \u53d1\u9001\u6d88\u606f\u5230\u6e32\u67d3\u8fdb\u7a0b<\/h5>\n<p>\u4e3b\u8fdb\u7a0b\u53ef\u4ee5\u901a\u8fc7event.sender.send(channel[, &#8230;args])\u65b9\u6cd5\u5411\u53d1\u9001\u6d88\u606f\u7684\u6e32\u67d3\u8fdb\u7a0b\u53d1\u9001\u54cd\u5e94\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>ipcMain.on(&#039;request-data&#039;, (event) &#061;&gt; {<\/p>\n<p>const data &#061; fetchData(); \/\/ \u83b7\u53d6\u6570\u636e\u7684\u51fd\u6570<\/p>\n<p>event.sender.send(&#039;data-reply&#039;, data);<\/p>\n<p>});<\/p>\n<p>\u5982\u679c\u9700\u8981\u5411\u6240\u6709\u6e32\u67d3\u8fdb\u7a0b\u53d1\u9001\u6d88\u606f&#xff0c;\u53ef\u4ee5\u4f7f\u7528BrowserWindow.getAllWindows()\u65b9\u6cd5\u83b7\u53d6\u6240\u6709\u7a97\u53e3&#xff0c;\u7136\u540e\u904d\u5386\u53d1\u9001\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>ipcMain.on(&#039;broadcast-message&#039;, (event, message) &#061;&gt; {<\/p>\n<p>BrowserWindow.getAllWindows().forEach(window &#061;&gt; {<\/p>\n<p>window.webContents.send(&#039;global-message&#039;, message);<\/p>\n<p>});<\/p>\n<p>});<\/p>\n<h5>4.1.3 \u5904\u7406\u540c\u6b65\u6d88\u606f<\/h5>\n<p>\u4e3b\u8fdb\u7a0b\u53ef\u4ee5\u4f7f\u7528ipcMain.handle(channel, listener)\u65b9\u6cd5\u5904\u7406\u540c\u6b65\u6d88\u606f\u8bf7\u6c42\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>ipcMain.handle(&#039;sync-message&#039;, (event, arg) &#061;&gt; {<\/p>\n<p>console.log(&#039;\u6536\u5230\u540c\u6b65\u6d88\u606f:&#039;, arg);<\/p>\n<p>return &#039;\u540c\u6b65\u54cd\u5e94&#039;;<\/p>\n<p>});<\/p>\n<p>\u5728\u6e32\u67d3\u8fdb\u7a0b\u4e2d&#xff0c;\u53ef\u4ee5\u4f7f\u7528ipcRenderer.invoke(channel[, &#8230;args])\u65b9\u6cd5\u53d1\u9001\u540c\u6b65\u6d88\u606f\u5e76\u83b7\u53d6\u54cd\u5e94\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>const response &#061; await ipcRenderer.invoke(&#039;sync-message&#039;, &#039;\u540c\u6b65\u8bf7\u6c42&#039;);<\/p>\n<p>console.log(&#039;\u540c\u6b65\u54cd\u5e94:&#039;, response);<\/p>\n<h4>4.2 \u8de8\u8fdb\u7a0b\u6570\u636e\u4f20\u9012<\/h4>\n<p>\u5728 Electron \u5e94\u7528\u4e2d&#xff0c;\u7ecf\u5e38\u9700\u8981\u5728\u4e3b\u8fdb\u7a0b\u548c\u6e32\u67d3\u8fdb\u7a0b\u4e4b\u95f4\u4f20\u9012\u6570\u636e\u3002\u672c\u8282\u5c06\u4ecb\u7ecd\u51e0\u79cd\u5e38\u89c1\u7684\u6570\u636e\u4f20\u9012\u65b9\u5f0f\u53ca\u5176\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<h5>4.2.1 \u57fa\u672c\u6570\u636e\u7c7b\u578b\u4f20\u9012<\/h5>\n<p>\u57fa\u672c\u6570\u636e\u7c7b\u578b&#xff08;\u5982\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5e03\u5c14\u503c&#xff09;\u53ef\u4ee5\u76f4\u63a5\u5728\u8fdb\u7a0b\u95f4\u4f20\u9012&#xff0c;Electron \u4f1a\u81ea\u52a8\u8fdb\u884c\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>\/\/ \u4e3b\u8fdb\u7a0b<\/p>\n<p>ipcMain.on(&#039;send-number&#039;, (event, number) &#061;&gt; {<\/p>\n<p>console.log(&#039;\u6536\u5230\u6570\u5b57:&#039;, number);<\/p>\n<p>event.sender.send(&#039;reply-number&#039;, number * 2);<\/p>\n<p>});<\/p>\n<p>\/\/ \u6e32\u67d3\u8fdb\u7a0b<\/p>\n<p>ipcRenderer.send(&#039;send-number&#039;, 42);<\/p>\n<p>ipcRenderer.on(&#039;reply-number&#039;, (event, result) &#061;&gt; {<\/p>\n<p>console.log(&#039;\u8ba1\u7b97\u7ed3\u679c:&#039;, result); \/\/ \u8f93\u51fa 84<\/p>\n<p>});<\/p>\n<h5>4.2.2 \u5bf9\u8c61\u548c\u6570\u7ec4\u4f20\u9012<\/h5>\n<p>\u590d\u6742\u6570\u636e\u7c7b\u578b&#xff08;\u5982\u5bf9\u8c61\u548c\u6570\u7ec4&#xff09;\u4e5f\u53ef\u4ee5\u5728\u8fdb\u7a0b\u95f4\u4f20\u9012&#xff0c;\u4f46\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9&#xff1a;<\/p>\n<li>\u6570\u636e\u4f1a\u88ab\u6df1\u62f7\u8d1d&#xff0c;\u56e0\u6b64\u4fee\u6539\u63a5\u6536\u65b9\u7684\u6570\u636e\u4e0d\u4f1a\u5f71\u54cd\u53d1\u9001\u65b9\u7684\u6570\u636e\u3002<\/li>\n<li>\u539f\u578b\u94fe\u548c\u51fd\u6570\u4e0d\u4f1a\u88ab\u4fdd\u7559\u3002<\/li>\n<li>\u5faa\u73af\u5f15\u7528\u4f1a\u88ab\u81ea\u52a8\u5904\u7406\u3002<\/li>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>\/\/ \u4e3b\u8fdb\u7a0b<\/p>\n<p>ipcMain.on(&#039;send-object&#039;, (event, obj) &#061;&gt; {<\/p>\n<p>console.log(&#039;\u6536\u5230\u5bf9\u8c61:&#039;, obj);<\/p>\n<p>obj.answer &#061; 42; \/\/ \u4fee\u6539\u5bf9\u8c61\u4e0d\u4f1a\u5f71\u54cd\u53d1\u9001\u65b9<\/p>\n<p>event.sender.send(&#039;reply-object&#039;, obj);<\/p>\n<p>});<\/p>\n<p>\/\/ \u6e32\u67d3\u8fdb\u7a0b<\/p>\n<p>const data &#061; { question: &#039;\u751f\u547d\u3001\u5b87\u5b99\u4ee5\u53ca\u4e00\u5207\u7684\u7ec8\u6781\u7b54\u6848&#039; };<\/p>\n<p>ipcRenderer.send(&#039;send-object&#039;, data);<\/p>\n<p>ipcRenderer.on(&#039;reply-object&#039;, (event, result) &#061;&gt; {<\/p>\n<p>console.log(&#039;\u4fee\u6539\u540e\u7684\u5bf9\u8c61:&#039;, result); \/\/ \u8f93\u51fa { question: &#039;&#8230;&#039;, answer: 42 }<\/p>\n<p>console.log(&#039;\u539f\u59cb\u5bf9\u8c61:&#039;, data); \/\/ \u8f93\u51fa { question: &#039;&#8230;&#039; }<\/p>\n<p>});<\/p>\n<h5>4.2.3 \u7f13\u51b2\u533a\u4f20\u9012<\/h5>\n<p>\u5bf9\u4e8e\u4e8c\u8fdb\u5236\u6570\u636e&#xff0c;\u5982\u6587\u4ef6\u5185\u5bb9\u6216\u56fe\u50cf\u6570\u636e&#xff0c;\u53ef\u4ee5\u4f7f\u7528Buffer\u5bf9\u8c61\u8fdb\u884c\u4f20\u9012\u3002Buffer\u5bf9\u8c61\u5728\u8fdb\u7a0b\u95f4\u4f20\u9012\u65f6\u4e0d\u4f1a\u88ab\u62f7\u8d1d&#xff0c;\u800c\u662f\u901a\u8fc7\u5f15\u7528\u4f20\u9012&#xff0c;\u8fd9\u63d0\u9ad8\u4e86\u6027\u80fd\u5e76\u51cf\u5c11\u4e86\u5185\u5b58\u4f7f\u7528\u3002<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>\/\/ \u4e3b\u8fdb\u7a0b<\/p>\n<p>const fs &#061; require(&#039;fs&#039;);<\/p>\n<p>ipcMain.on(&#039;read-file&#039;, (event, filePath) &#061;&gt; {<\/p>\n<p>try {<\/p>\n<p>const buffer &#061; fs.readFileSync(filePath);<\/p>\n<p>event.sender.send(&#039;file-content&#039;, buffer);<\/p>\n<p>} catch (error) {<\/p>\n<p>event.sender.send(&#039;file-error&#039;, error.message);<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>\/\/ \u6e32\u67d3\u8fdb\u7a0b<\/p>\n<p>ipcRenderer.send(&#039;read-file&#039;, &#039;data.txt&#039;);<\/p>\n<p>ipcRenderer.on(&#039;file-content&#039;, (event, buffer) &#061;&gt; {<\/p>\n<p>const content &#061; buffer.toString();<\/p>\n<p>console.log(&#039;\u6587\u4ef6\u5185\u5bb9:&#039;, content);<\/p>\n<p>});<\/p>\n<h5>4.2.4 \u5927\u578b\u6570\u636e\u4f20\u9012\u6ce8\u610f\u4e8b\u9879<\/h5>\n<p>\u4f20\u9012\u5927\u578b\u6570\u636e\u65f6\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9&#xff1a;<\/p>\n<li>\u907f\u514d\u9891\u7e41\u4f20\u9012\u5927\u578b\u6570\u636e&#xff0c;\u8fd9\u53ef\u80fd\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\u3002<\/li>\n<li>\u5bf9\u4e8e\u975e\u5e38\u5927\u7684\u6587\u4ef6&#xff0c;\u8003\u8651\u4f7f\u7528\u6d41\u5f0f\u4f20\u8f93\u800c\u4e0d\u662f\u4e00\u6b21\u6027\u4f20\u9012\u6574\u4e2aBuffer\u3002<\/li>\n<li>\u5728\u6e32\u67d3\u8fdb\u7a0b\u4e2d\u5904\u7406\u5927\u578b\u6570\u636e\u65f6&#xff0c;\u6ce8\u610f\u4e0d\u8981\u963b\u585e\u4e3b\u7ebf\u7a0b\u3002<\/li>\n<h4>4.3 \u8fdc\u7a0b\u6a21\u5757\u7684\u66ff\u4ee3\u65b9\u6848<\/h4>\n<p>remote\u6a21\u5757\u5141\u8bb8\u6e32\u67d3\u8fdb\u7a0b\u76f4\u63a5\u8bbf\u95ee\u4e3b\u8fdb\u7a0b\u4e2d\u7684\u5bf9\u8c61&#xff0c;\u4f46\u5b83\u5b58\u5728\u4e00\u4e9b\u5b89\u5168\u548c\u6027\u80fd\u95ee\u9898\u3002\u672c\u8282\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528ipcRenderer\u548cipcMain\u5b9e\u73b0\u76f8\u540c\u7684\u529f\u80fd\u3002<\/p>\n<h5>4.3.1 \u7b80\u5355\u65b9\u6cd5\u8c03\u7528<\/h5>\n<p>\u5047\u8bbe\u5728\u4e3b\u8fdb\u7a0b\u4e2d\u6709\u4e00\u4e2acreateWindow()\u51fd\u6570&#xff0c;\u5728\u6e32\u67d3\u8fdb\u7a0b\u4e2d\u9700\u8981\u8c03\u7528\u5b83\u3002\u4f7f\u7528remote\u6a21\u5757\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528&#xff1a;<\/p>\n<p>\u4f7f\u7528 remote \u6a21\u5757&#xff1a;<\/p>\n<p>const { remote } &#061; require(&#039;electron&#039;);<\/p>\n<p>remote.createWindow();<\/p>\n<p>\u66ff\u4ee3\u65b9\u6848&#xff1a;<\/p>\n<p>\u5728\u4e3b\u8fdb\u7a0b\u4e2d&#xff1a;<\/p>\n<p>ipcMain.on(&#039;create-window&#039;, () &#061;&gt; {<\/p>\n<p>createWindow();<\/p>\n<p>});<\/p>\n<p>\u5728\u6e32\u67d3\u8fdb\u7a0b\u4e2d&#xff1a;<\/p>\n<p>const { ipcRenderer } &#061; require(&#039;electron&#039;);<\/p>\n<p>ipcRenderer.send(&#039;create-window&#039;);<\/p>\n<h5>4.3.2 \u5e26\u53c2\u6570\u7684\u65b9\u6cd5\u8c03\u7528<\/h5>\n<p>\u5982\u679c\u65b9\u6cd5\u9700\u8981\u53c2\u6570&#xff0c;\u53ef\u4ee5\u901a\u8fc7\u6d88\u606f\u4f20\u9012\u53c2\u6570&#xff1a;<\/p>\n<p>\u4f7f\u7528 remote \u6a21\u5757&#xff1a;<\/p>\n<p>const { remote } &#061; require(&#039;electron&#039;);<\/p>\n<p>remote.createWindow({ width: 1024, height: 768 });<\/p>\n<p>\u66ff\u4ee3\u65b9\u6848&#xff1a;<\/p>\n<p>\u5728\u4e3b\u8fdb\u7a0b\u4e2d&#xff1a;<\/p>\n<p>ipcMain.on(&#039;create-window&#039;, (event, options) &#061;&gt; {<\/p>\n<p>createWindow(options);<\/p>\n<p>});<\/p>\n<p>\u5728\u6e32\u67d3\u8fdb\u7a0b\u4e2d&#xff1a;<\/p>\n<p>const options &#061; { width: 1024, height: 768 };<\/p>\n<p>ipcRenderer.send(&#039;create-window&#039;, options);<\/p>\n<h5>4.3.3 \u83b7\u53d6\u8fd4\u56de\u503c<\/h5>\n<p>\u5bf9\u4e8e\u6709\u8fd4\u56de\u503c\u7684\u65b9\u6cd5&#xff0c;\u9700\u8981\u4f7f\u7528\u5f02\u6b65\u6d88\u606f\u4f20\u9012&#xff1a;<\/p>\n<p>\u4f7f\u7528 remote \u6a21\u5757&#xff1a;<\/p>\n<p>const { remote } &#061; require(&#039;electron&#039;);<\/p>\n<p>const count &#061; remote.getWindowCount();<\/p>\n<p>console.log(&#096;\u7a97\u53e3\u6570\u91cf&#xff1a;${count}&#096;);<\/p>\n<p>\u66ff\u4ee3\u65b9\u6848&#xff1a;<\/p>\n<p>\u5728\u4e3b\u8fdb\u7a0b\u4e2d&#xff1a;<\/p>\n<p>ipcMain.handle(&#039;get-window-count&#039;, () &#061;&gt; {<\/p>\n<p>return BrowserWindow.getAllWindows().length;<\/p>\n<p>});<\/p>\n<p>\u5728\u6e32\u67d3\u8fdb\u7a0b\u4e2d&#xff1a;<\/p>\n<p>const { ipcRenderer } &#061; require(&#039;electron&#039;);<\/p>\n<p>ipcRenderer.invoke(&#039;get-window-count&#039;).then(count &#061;&gt; {<\/p>\n<p>console.log(&#096;\u7a97\u53e3\u6570\u91cf&#xff1a;${count}&#096;);<\/p>\n<p>});<\/p>\n<h4>4.4 \u5e94\u7528\u6253\u5305\u4e0e\u5206\u53d1<\/h4>\n<p>\u5f00\u53d1\u5b8c\u6210\u540e&#xff0c;\u9700\u8981\u5c06 Electron \u5e94\u7528\u6253\u5305\u6210\u53ef\u6267\u884c\u6587\u4ef6&#xff0c;\u4ee5\u4fbf\u7528\u6237\u5b89\u88c5\u548c\u4f7f\u7528\u3002Electron \u63d0\u4f9b\u4e86\u591a\u79cd\u5de5\u5177\u548c\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<h5>4.4.1 \u4f7f\u7528 Electron Forge \u6253\u5305<\/h5>\n<p>Electron Forge \u662f Electron \u5b98\u65b9\u63a8\u8350\u7684\u6253\u5305\u5de5\u5177&#xff0c;\u5b83\u63d0\u4f9b\u4e86\u4e00\u7ad9\u5f0f\u7684\u89e3\u51b3\u65b9\u6848&#xff0c;\u5305\u62ec\u6253\u5305\u3001\u7b7e\u540d\u3001\u53d1\u5e03\u7b49\u529f\u80fd\u3002<\/p>\n<p>\u5b89\u88c5 Electron Forge&#xff1a;<\/p>\n<p>npm install &#8211;save-dev electron-forge<\/p>\n<p>\u521d\u59cb\u5316 Electron Forge \u9879\u76ee&#xff1a;<\/p>\n<p>npx electron-forge import<\/p>\n<p>\u8fd9\u4f1a\u81ea\u52a8\u68c0\u6d4b\u9879\u76ee\u7c7b\u578b\u5e76\u8fdb\u884c\u914d\u7f6e\u3002<\/p>\n<p>\u914d\u7f6e\u6253\u5305\u9009\u9879&#xff1a;<\/p>\n<p>\u5728package.json\u4e2d&#xff0c;\u53ef\u4ee5\u914d\u7f6e Electron Forge \u7684\u6253\u5305\u9009\u9879&#xff1a;<\/p>\n<p>&#034;config&#034;: {<\/p>\n<p>&#034;forge&#034;: {<\/p>\n<p>&#034;packagerConfig&#034;: {<\/p>\n<p>&#034;asar&#034;: true,<\/p>\n<p>&#034;icon&#034;: &#034;.\/build\/icon&#034;<\/p>\n<p>},<\/p>\n<p>&#034;makers&#034;: [<\/p>\n<p>{<\/p>\n<p>&#034;name&#034;: &#034;&#064;electron-forge\/maker-squirrel&#034;,<\/p>\n<p>&#034;config&#034;: {<\/p>\n<p>&#034;name&#034;: &#034;my-electron-app&#034;<\/p>\n<p>}<\/p>\n<p>},<\/p>\n<p>{<\/p>\n<p>&#034;name&#034;: &#034;&#064;electron-forge\/maker-zip&#034;,<\/p>\n<p>&#034;platforms&#034;: [<\/p>\n<p>&#034;darwin&#034;<\/p>\n<p>]<\/p>\n<p>},<\/p>\n<p>{<\/p>\n<p>&#034;name&#034;: &#034;&#064;electron-forge\/maker-deb&#034;,<\/p>\n<p>&#034;config&#034;: {}<\/p>\n<p>},<\/p>\n<p>{<\/p>\n<p>&#034;name&#034;: &#034;&#064;electron-forge\/maker-rpm&#034;,<\/p>\n<p>&#034;config&#034;: {}<\/p>\n<p>}<\/p>\n<p>]<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\u6253\u5305\u5e94\u7528&#xff1a;<\/p>\n<p>npm run make<\/p>\n<p>\u8fd9\u4f1a\u5728out\u76ee\u5f55\u4e2d\u751f\u6210\u9002\u7528\u4e8e\u4e0d\u540c\u5e73\u53f0\u7684\u5b89\u88c5\u5305\u3002<\/p>\n<h5>4.4.2 \u4f7f\u7528 Electron Packager<\/h5>\n<p>Electron Packager \u662f\u53e6\u4e00\u4e2a\u5e38\u7528\u7684\u6253\u5305\u5de5\u5177&#xff0c;\u5b83\u53ef\u4ee5\u5c06 Electron \u5e94\u7528\u6253\u5305\u6210\u9002\u7528\u4e8e\u4e0d\u540c\u5e73\u53f0\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n<p>\u5b89\u88c5 Electron Packager&#xff1a;<\/p>\n<p>npm install &#8211;save-dev electron-packager<\/p>\n<p>\u914d\u7f6e\u6253\u5305\u811a\u672c&#xff1a;<\/p>\n<p>\u5728package.json\u7684scripts\u90e8\u5206\u6dfb\u52a0\u6253\u5305\u547d\u4ee4&#xff1a;<\/p>\n<p>&#034;scripts&#034;: {<\/p>\n<p>&#034;package:win32&#034;: &#034;electron-packager . my-app &#8211;platform&#061;win32 &#8211;arch&#061;x64 &#8211;icon&#061;build\/icon.ico &#8211;prune&#061;true &#8211;out&#061;release-builds&#034;,<\/p>\n<p>&#034;package:darwin&#034;: &#034;electron-packager . my-app &#8211;platform&#061;darwin &#8211;arch&#061;x64 &#8211;icon&#061;build\/icon.icns &#8211;prune&#061;true &#8211;out&#061;release-builds&#034;,<\/p>\n<p>&#034;package:linux&#034;: &#034;electron-packager . my-app &#8211;platform&#061;linux &#8211;arch&#061;x64 &#8211;icon&#061;build\/icon.png &#8211;prune&#061;true &#8211;out&#061;release-builds&#034;<\/p>\n<p>}<\/p>\n<p>\u6267\u884c\u6253\u5305&#xff1a;<\/p>\n<p>npm run package:win32<\/p>\n<p>npm run package:darwin<\/p>\n<p>npm run package:linux<\/p>\n<p>\u8fd9\u4f1a\u5728release-builds\u76ee\u5f55\u4e2d\u751f\u6210\u9002\u7528\u4e8e\u4e0d\u540c\u5e73\u53f0\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n<h5>4.4.3 \u81ea\u52a8\u66f4\u65b0<\/h5>\n<p>\u4e3a\u4e86\u8ba9\u7528\u6237\u80fd\u591f\u65b9\u4fbf\u5730\u66f4\u65b0\u5e94\u7528&#xff0c;\u53ef\u4ee5\u96c6\u6210\u81ea\u52a8\u66f4\u65b0\u529f\u80fd\u3002Electron \u63d0\u4f9b\u4e86electron-updater\u6a21\u5757\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<p>\u5b89\u88c5 electron-updater&#xff1a;<\/p>\n<p>npm install &#8211;save-dev electron-updater<\/p>\n<p>\u4e3b\u8fdb\u7a0b\u4e2d\u6dfb\u52a0\u81ea\u52a8\u66f4\u65b0\u4ee3\u7801&#xff1a;<\/p>\n<p>const { autoUpdater } &#061; require(&#039;electron-updater&#039;);<\/p>\n<p>autoUpdater.checkForUpdatesAndNotify();<\/p>\n<p>autoUpdater.on(&#039;update-downloaded&#039;, () &#061;&gt; {<\/p>\n<p>dialog.showMessageBox({<\/p>\n<p>type: &#039;info&#039;,<\/p>\n<p>title: &#039;\u66f4\u65b0\u53ef\u7528&#039;,<\/p>\n<p>message: &#039;\u5e94\u7528\u66f4\u65b0\u5df2\u4e0b\u8f7d&#xff0c;\u662f\u5426\u91cd\u542f\u5e94\u7528\u4ee5\u5b89\u88c5\u66f4\u65b0&#xff1f;&#039;<\/p>\n<p>}).then(result &#061;&gt; {<\/p>\n<p>if (result.response &#061;&#061;&#061; 0) {<\/p>\n<p>autoUpdater.quitAndInstall();<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>});<\/p>\n<p>autoUpdater.on(&#039;error&#039;, (error) &#061;&gt; {<\/p>\n<p>dialog.showErrorBox(&#039;\u66f4\u65b0\u9519\u8bef&#039;, error.message);<\/p>\n<p>});<\/p>\n<p>\u914d\u7f6e\u66f4\u65b0\u670d\u52a1\u5668&#xff1a;<\/p>\n<p>\u9700\u8981\u5c06\u5e94\u7528\u7684\u66f4\u65b0\u6587\u4ef6\u4e0a\u4f20\u5230\u4e00\u4e2a HTTP \u670d\u52a1\u5668&#xff0c;\u5e76\u5728package.json\u4e2d\u914d\u7f6e\u66f4\u65b0\u5730\u5740&#xff1a;<\/p>\n<p>&#034;build&#034;: {<\/p>\n<p>&#034;publish&#034;: [<\/p>\n<p>{<\/p>\n<p>&#034;provider&#034;: &#034;generic&#034;,<\/p>\n<p>&#034;url&#034;: &#034;https:\/\/example.com\/update&#034;<\/p>\n<p>}<\/p>\n<p>]<\/p>\n<p>}<\/p>\n<h3>\u4e94\u3001\u5b9e\u6218\u6848\u4f8b&#xff1a;\u6784\u5efa\u7b80\u5355\u7684\u684c\u9762\u5de5\u5177<\/h3>\n<h4>5.1 \u6587\u672c\u7f16\u8f91\u5668\u5e94\u7528<\/h4>\n<p>\u5728\u672c\u8282\u4e2d&#xff0c;\u6211\u4eec\u5c06\u4f7f\u7528 Electron \u6784\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u6587\u672c\u7f16\u8f91\u5668\u5e94\u7528&#xff0c;\u6f14\u793a\u5982\u4f55\u7efc\u5408\u8fd0\u7528\u524d\u9762\u5b66\u4e60\u7684 API\u3002<\/p>\n<h5>5.1.1 \u5e94\u7528\u529f\u80fd\u89c4\u5212<\/h5>\n<p>\u6211\u4eec\u7684\u6587\u672c\u7f16\u8f91\u5668\u5c06\u5177\u5907\u4ee5\u4e0b\u57fa\u672c\u529f\u80fd&#xff1a;<\/p>\n<li>\u65b0\u5efa\u6587\u4ef6<\/li>\n<li>\u6253\u5f00\u6587\u4ef6<\/li>\n<li>\u4fdd\u5b58\u6587\u4ef6<\/li>\n<li>\u53e6\u5b58\u4e3a<\/li>\n<li>\u57fa\u672c\u7684\u7f16\u8f91\u83dc\u5355&#xff08;\u526a\u5207\u3001\u590d\u5236\u3001\u7c98\u8d34&#xff09;<\/li>\n<li>\u7b80\u5355\u7684\u7a97\u53e3\u7ba1\u7406<\/li>\n<h5>5.1.2 \u4e3b\u8fdb\u7a0b\u4ee3\u7801<\/h5>\n<p>\u9996\u5148&#xff0c;\u521b\u5efa\u4e3b\u8fdb\u7a0b\u6587\u4ef6main.js&#xff1a;<\/p>\n<p>const { app, BrowserWindow, Menu, dialog } &#061; require(&#039;electron&#039;);<\/p>\n<p>const path &#061; require(&#039;path&#039;);<\/p>\n<p>let mainWindow;<\/p>\n<p>function createWindow() {<\/p>\n<p>mainWindow &#061; new BrowserWindow({<\/p>\n<p>width: 1024,<\/p>\n<p>height: 768,<\/p>\n<p>webPreferences: {<\/p>\n<p>nodeIntegration: true<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>mainWindow.loadFile(&#039;index.html&#039;);<\/p>\n<p>mainWindow.on(&#039;closed&#039;, () &#061;&gt; {<\/p>\n<p>mainWindow &#061; null;<\/p>\n<p>});<\/p>\n<p>\/\/ \u521b\u5efa\u83dc\u5355<\/p>\n<p>const template &#061; [<\/p>\n<p>{<\/p>\n<p>label: &#039;\u6587\u4ef6&#039;,<\/p>\n<p>submenu: [<\/p>\n<p>{<\/p>\n<p>label: &#039;\u65b0\u5efa&#039;,<\/p>\n<p>accelerator: &#039;CmdOrCtrl&#043;N&#039;,<\/p>\n<p>click: () &#061;&gt; { newFile(); }<\/p>\n<p>},<\/p>\n<p>{<\/p>\n<p>label: &#039;\u6253\u5f00&#8230;&#039;,<\/p>\n<p>accelerator: &#039;CmdOrCtrl&#043;O&#039;,<\/p>\n<p>click: () &#061;&gt; { openFile(); }<\/p>\n<p>},<\/p>\n<p>{<\/p>\n<p>label: &#039;\u4fdd\u5b58&#039;,<\/p>\n<p>accelerator: &#039;CmdOrCtrl&#043;S&#039;,<\/p>\n<p>click: () &#061;&gt; { saveFile(); }<\/p>\n<p>},<\/p>\n<p>{<\/p>\n<p>label: &#039;\u53e6\u5b58\u4e3a&#8230;&#039;,<\/p>\n<p>accelerator: &#039;Shift&#043;CmdOrCtrl&#043;S&#039;,<\/p>\n<p>click: () &#061;&gt; { saveFileAs(); }<\/p>\n<p>},<\/p>\n<p>{ type: &#039;separator&#039; },<\/p>\n<p>{<\/p>\n<p>label: &#039;\u9000\u51fa&#039;,<\/p>\n<p>accelerator: &#039;CmdOrCtrl&#043;Q&#039;,<\/p>\n<p>click: () &#061;&gt; { app.quit(); }<\/p>\n<p>}<\/p>\n<p>]<\/p>\n<p>},<\/p>\n<p>{<\/p>\n<p>label: &#039;\u7f16\u8f91&#039;,<\/p>\n<p>submenu: [<\/p>\n<p>{<\/p>\n<p>label: &#039;\u526a\u5207&#039;,<\/p>\n<p>accelerator: &#039;CmdOrCtrl&#043;X&#039;,<\/p>\n<p>click: () &#061;&gt; { mainWindow.webContents.executeJavaScript(&#039;document.execCommand(&#034;cut&#034;)&#039;); }<\/p>\n<p>},<\/p>\n<p>{<\/p>\n<p>label: &#039;\u590d\u5236&#039;,<\/p>\n<p>accelerator: &#039;CmdOrCtrl&#043;C&#039;,<\/p>\n<p>click: () &#061;&gt; { mainWindow.webContents.executeJavaScript(&#039;document.execCommand(&#034;copy&#034;)&#039;); }<\/p>\n<p>},<\/p>\n<p>{<\/p>\n<p>label: &#039;\u7c98\u8d34&#039;,<\/p>\n<p>accelerator: &#039;CmdOrCtrl&#043;V&#039;,<\/p>\n<p>click: () &#061;&gt; { mainWindow.webContents.executeJavaScript(&#039;document.execCommand(&#034;paste&#034;)&#039;); }<\/p>\n<p>},<\/p>\n<p>{ type: &#039;separator&#039; },<\/p>\n<p>{<\/p>\n<p>label: &#039;\u5168\u9009&#039;,<\/p>\n<p>accelerator: &#039;CmdOrCtrl&#043;A&#039;,<\/p>\n<p>click: () &#061;&gt; { mainWindow.webContents.executeJavaScript(&#039;document.execCommand(&#034;selectAll&#034;)&#039;); }<\/p>\n<p>}<\/p>\n<p>]<\/p>\n<p>}<\/p>\n<p>];<\/p>\n<p>const menu &#061; Menu.buildFromTemplate(template);<\/p>\n<p>Menu.setApplicationMenu(menu);<\/p>\n<p>}<\/p>\n<p>\/\/ \u6587\u4ef6\u64cd\u4f5c\u76f8\u5173\u51fd\u6570<\/p>\n<p>let currentFilePath &#061; null;<\/p>\n<p>function newFile() {<\/p>\n<p>currentFilePath &#061; null;<\/p>\n<p>mainWindow.webContents.send(&#039;new-file&#039;);<\/p>\n<p>}<\/p>\n<p>function openFile() {<\/p>\n<p>dialog.showOpenDialog({<\/p>\n<p>filters: [{ name: &#039;\u6587\u672c\u6587\u4ef6&#039;, extensions: [&#039;txt&#039;] }]<\/p>\n<p>}).then(result &#061;&gt; {<\/p>\n<p>if (!result.canceled &amp;&amp; result.filePaths.length &gt; 0) {<\/p>\n<p>const filePath &#061; result.filePaths[0];<\/p>\n<p>const content &#061; require(&#039;fs&#039;).readFileSync(filePath, &#039;utf8&#039;);<\/p>\n<p>mainWindow.webContents.send(&#039;open-file&#039;, filePath, content);<\/p>\n<p>currentFilePath &#061; filePath;<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>}<\/p>\n<p>function saveFile() {<\/p>\n<p>if (currentFilePath) {<\/p>\n<p>const content &#061; mainWindow.webContents.executeJavaScript(&#039;document.getElementById(&#034;editor&#034;).value&#039;);<\/p>\n<p>require(&#039;fs&#039;).writeFileSync(currentFilePath, content);<\/p>\n<p>} else {<\/p>\n<p>saveFileAs();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>function saveFileAs() {<\/p>\n<p>dialog.showSaveDialog({<\/p>\n<p>filters: [{ name: &#039;\u6587\u672c\u6587\u4ef6&#039;, extensions: [&#039;txt&#039;] }]<\/p>\n<p>}).then(result &#061;&gt; {<\/p>\n<p>if (!result.canceled) {<\/p>\n<p>const filePath &#061; result.filePath;<\/p>\n<p>const content &#061; mainWindow.webContents.executeJavaScript(&#039;document.getElementById(&#034;editor&#034;).value&#039;);<\/p>\n<p>require(&#039;fs&#039;).writeFileSync(filePath, content);<\/p>\n<p>currentFilePath &#061; filePath;<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>}<\/p>\n<p>app.whenReady().then(createWindow);<\/p>\n<p>app.on(&#039;window-all-closed&#039;, () &#061;&gt; {<\/p>\n<p>if (process.platform !&#061;&#061; &#039;darwin&#039;) {<\/p>\n<p>app.quit();<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>app.on(&#039;activate&#039;, () &#061;&gt; {<\/p>\n<p>if (mainWindow &#061;&#061;&#061; null) {<\/p>\n<p>createWindow();<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<h5>5.1.3 \u6e32\u67d3\u8fdb\u7a0b\u4ee3\u7801<\/h5>\n<p>\u521b\u5efa\u6e32\u67d3\u8fdb\u7a0b\u6587\u4ef6index.html\u548c\u5bf9\u5e94\u7684 JavaScript \u6587\u4ef6renderer.js&#xff1a;<\/p>\n<p>index.html&#xff1a;<\/p>\n<p>&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>&lt;meta charset&#061;&#034;UTF-8&#034;&gt;<\/p>\n<p>&lt;title&gt;Electron\u6587\u672c\u7f16\u8f91\u5668&lt;\/title&gt;<\/p>\n<p>&lt;style&gt;<\/p>\n<p>body { margin: 0; padding: 0; }<\/p>\n<p>#editor { width: 100vw; height: 100vh; border: none; padding: 10px; box-sizing: border-box; font-size: 16px; }<\/p>\n<p>&lt;\/style&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&lt;textarea id&#061;&#034;editor&#034;&gt;&lt;\/textarea&gt;<\/p>\n<p>&lt;script src&#061;&#034;renderer.js&#034;&gt;&lt;\/script&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p>renderer.js&#xff1a;<\/p>\n<p>const { ipcRenderer } &#061; require(&#039;electron&#039;);<\/p>\n<p>\/\/ \u76d1\u542c\u65b0\u5efa\u6587\u4ef6\u6d88\u606f<\/p>\n<p>ipcRenderer.on(&#039;new-file&#039;, () &#061;&gt; {<\/p>\n<p>document.getElementById(&#039;editor&#039;).value &#061; &#039;&#039;;<\/p>\n<p>});<\/p>\n<p>\/\/ \u76d1\u542c\u6253\u5f00\u6587\u4ef6\u6d88\u606f<\/p>\n<p>ipcRenderer.on(&#039;open-file&#039;, (event, filePath, content) &#061;&gt; {<\/p>\n<p>document.title &#061; &#096;Electron\u6587\u672c\u7f16\u8f91\u5668 &#8211; ${path.basename(filePath)}&#096;;<\/p>\n<p>document.getElementById(&#039;editor&#039;).value &#061; content;<\/p>\n<p>});<\/p>\n<p>\/\/ \u5904\u7406\u7a97\u53e3\u5173\u95ed\u65f6\u7684\u786e\u8ba4<\/p>\n<p>window.addEventListener(&#039;beforeunload&#039;, (event) &#061;&gt; {<\/p>\n<p>const content &#061; document.getElementById(&#039;editor&#039;).value;<\/p>\n<p>if (content.trim() !&#061;&#061; &#039;&#039;) {<\/p>\n<p>event.returnValue &#061; &#039;\u4f60\u6709\u672a\u4fdd\u5b58\u7684\u66f4\u6539&#xff0c;\u786e\u5b9a\u8981\u9000\u51fa\u5417&#xff1f;&#039;;<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<h5>5.1.4 \u8fd0\u884c\u548c\u6d4b\u8bd5<\/h5>\n<p>\u5728\u9879\u76ee\u76ee\u5f55\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4&#xff1a;<\/p>\n<p>npm install electron &#8211;save-dev<\/p>\n<p>npm start<\/p>\n<p>\u6d4b\u8bd5\u5e94\u7528\u7684\u5404\u9879\u529f\u80fd&#xff1a;<\/p>\n<li>\u65b0\u5efa\u6587\u4ef6&#xff08;Ctrl&#043;N&#xff09;<\/li>\n<li>\u6253\u5f00\u6587\u4ef6&#xff08;Ctrl&#043;O&#xff09;<\/li>\n<li>\u4fdd\u5b58\u6587\u4ef6&#xff08;Ctrl&#043;S&#xff09;<\/li>\n<li>\u53e6\u5b58\u4e3a&#xff08;Shift&#043;Ctrl&#043;S&#xff09;<\/li>\n<li>\u57fa\u672c\u7f16\u8f91\u64cd\u4f5c&#xff08;\u526a\u5207\u3001\u590d\u5236\u3001\u7c98\u8d34\u3001\u5168\u9009&#xff09;<\/li>\n<li>\u9000\u51fa\u5e94\u7528&#xff08;Ctrl&#043;Q&#xff09;<\/li>\n<h4>5.2 \u6587\u4ef6\u7ba1\u7406\u5668\u5e94\u7528<\/h4>\n<p>\u63a5\u4e0b\u6765&#xff0c;\u6211\u4eec\u5c06\u6784\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u6587\u4ef6\u7ba1\u7406\u5668\u5e94\u7528&#xff0c;\u5c55\u793a\u5982\u4f55\u4f7f\u7528 Electron \u7684\u6587\u4ef6\u7cfb\u7edf\u548c\u5bf9\u8bdd\u6846 API\u3002<\/p>\n<h5>5.2.1 \u5e94\u7528\u529f\u80fd\u89c4\u5212<\/h5>\n<p>\u6211\u4eec\u7684\u6587\u4ef6\u7ba1\u7406\u5668\u5c06\u5177\u5907\u4ee5\u4e0b\u57fa\u672c\u529f\u80fd&#xff1a;<\/p>\n<li>\u663e\u793a\u5f53\u524d\u76ee\u5f55\u5185\u5bb9<\/li>\n<li>\u5bfc\u822a\u5230\u4e0a\u7ea7\u76ee\u5f55<\/li>\n<li>\u6253\u5f00\u76ee\u5f55<\/li>\n<li>\u663e\u793a\u6587\u4ef6\u4fe1\u606f<\/li>\n<li>\u57fa\u672c\u6587\u4ef6\u64cd\u4f5c&#xff08;\u65b0\u5efa\u6587\u4ef6\u3001\u5220\u9664\u6587\u4ef6 \/ \u76ee\u5f55&#xff09;<\/li>\n<h5>5.2.2 \u4e3b\u8fdb\u7a0b\u4ee3\u7801<\/h5>\n<p>\u521b\u5efa\u4e3b\u8fdb\u7a0b\u6587\u4ef6main.js&#xff1a;<\/p>\n<p>const { app, BrowserWindow, dialog } &#061; require(&#039;electron&#039;);<\/p>\n<p>const path &#061; require(&#039;path&#039;);<\/p>\n<p>const fs &#061; require(&#039;fs&#039;);<\/p>\n<p>let mainWindow;<\/p>\n<p>function createWindow() {<\/p>\n<p>mainWindow &#061; new BrowserWindow({<\/p>\n<p>width: 1200,<\/p>\n<p>height: 800,<\/p>\n<p>webPreferences: {<\/p>\n<p>nodeIntegration: true<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>mainWindow.loadFile(&#039;index.html&#039;);<\/p>\n<p>mainWindow.on(&#039;closed&#039;, () &#061;&gt; {<\/p>\n<p>mainWindow &#061; null;<\/p>\n<p>});<\/p>\n<p>}<\/p>\n<p>app.whenReady().then(createWindow);<\/p>\n<p>app.on(&#039;window-all-closed&#039;, () &#061;&gt; {<\/p>\n<p>if (process.platform !&#061;&#061; &#039;darwin&#039;) {<\/p>\n<p>app.quit();<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>app.on(&#039;activate&#039;, () &#061;&gt; {<\/p>\n<p>if (mainWindow &#061;&#061;&#061; null) {<\/p>\n<p>createWindow();<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<h5>5.2.3 \u6e32\u67d3\u8fdb\u7a0b\u4ee3\u7801<\/h5>\n<p>\u521b\u5efa\u6e32\u67d3\u8fdb\u7a0b\u6587\u4ef6index.html\u548c\u5bf9\u5e94\u7684 JavaScript \u6587\u4ef6renderer.js&#xff1a;<\/p>\n<p>index.html&#xff1a;<\/p>\n<p>&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>&lt;meta charset&#061;&#034;UTF-8&#034;&gt;<\/p>\n<p>&lt;title&gt;Electron\u6587\u4ef6\u7ba1\u7406\u5668&lt;\/title&gt;<\/p>\n<p>&lt;style&gt;<\/p>\n<p>body { margin: 0; padding: 0; font-family: Arial, sans-serif; }<\/p>\n<p>.header { background-color: #f0f0f0; padding: 10px; }<\/p>\n<p>.breadcrumb { margin: 10px; }<\/p>\n<p>.file-list { list-type: none; padding: 0; margin: 10px; }<\/p>\n<p>.file-item { padding: 5px; border: 1px solid #ddd; margin-bottom: 5px; cursor: pointer; }<\/p>\n<p>.file-item:hover { background-color: #f5f5f5; }<\/p>\n<p>.file-info { margin: 10px; padding: 10px; border: 1px solid #ddd; }<\/p>\n<p>&lt;\/style&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&lt;div class&#061;&#034;header&#034;&gt;<\/p>\n<p>&lt;button id&#061;&#034;back-btn&#034;&gt;\u8fd4\u56de&lt;\/button&gt;<\/p>\n<p>&lt;button id&#061;&#034;new-file-btn&#034;&gt;\u65b0\u5efa\u6587\u4ef6&lt;\/button&gt;<\/p>\n<p>&lt;button id&#061;&#034;delete-btn&#034;&gt;\u5220\u9664&lt;\/button&gt;<\/p>\n<p>&lt;\/div&gt;<\/p>\n<p>&lt;div class&#061;&#034;breadcrumb&#034; id&#061;&#034;breadcrumb&#034;&gt;&lt;\/div&gt;<\/p>\n<p>&lt;ul class&#061;&#034;file-list&#034; id&#061;&#034;file-list&#034;&gt;&lt;\/ul&gt;<\/p>\n<p>&lt;div class&#061;&#034;file-info&#034; id&#061;&#034;file-info&#034;&gt;&lt;\/div&gt;<\/p>\n<p>&lt;script src&#061;&#034;renderer.js&#034;&gt;&lt;\/script&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p>renderer.js&#xff1a;<\/p>\n<p>const { ipcRenderer, remote } &#061; require(&#039;electron&#039;);<\/p>\n<p>const fs &#061; require(&#039;fs&#039;);<\/p>\n<p>const path &#061; require(&#039;path&#039;);<\/p>\n<p>let currentPath &#061; remote.app.getPath(&#039;documents&#039;); \/\/ \u521d\u59cb\u8def\u5f84\u4e3a\u7528\u6237\u6587\u6863\u76ee\u5f55<\/p>\n<p>\/\/ \u521d\u59cb\u5316\u6587\u4ef6\u5217\u8868<\/p>\n<p>updateFileList(currentPath);<\/p>\n<p>\/\/ \u76d1\u542c\u8fd4\u56de\u6309\u94ae\u70b9\u51fb<\/p>\n<p>document.getElementById(&#039;back-btn&#039;).addEventListener(&#039;click&#039;, () &#061;&gt; {<\/p>\n<p>const parentPath &#061; path.dirname(currentPath);<\/p>\n<p>if (parentPath !&#061;&#061; currentPath) {<\/p>\n<p>currentPath &#061; parentPath;<\/p>\n<p>updateFileList(currentPath);<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>\/\/ \u76d1\u542c\u65b0\u5efa\u6587\u4ef6\u6309\u94ae\u70b9\u51fb<\/p>\n<p>document.getElementById(&#039;new-file-btn&#039;).addEventListener(&#039;click&#039;, () &#061;&gt; {<\/p>\n<p>const fileName &#061; prompt(&#039;\u8bf7\u8f93\u5165\u65b0\u6587\u4ef6\u540d&#xff1a;&#039;);<\/p>\n<p>if (fileName) {<\/p>\n<p>const filePath &#061; path.join(currentPath, fileName);<\/p>\n<p>fs.writeFileSync(filePath, &#039;&#039;); \/\/ \u521b\u5efa\u7a7a\u6587\u4ef6<\/p>\n<p>updateFileList(currentPath);<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>\/\/ \u76d1\u542c\u6587\u4ef6\u5217\u8868\u70b9\u51fb\u4e8b\u4ef6<\/p>\n<p>document.getElementById(&#039;file-list&#039;).addEventListener(&#039;click&#039;, (event) &#061;&gt; {<\/p>\n<p>if (event.target.classList.contains(&#039;file-item&#039;)) {<\/p>\n<p>const fileName &#061; event.target.dataset.filename;<\/p>\n<p>const filePath &#061; path.join(currentPath, fileName);<\/p>\n<p>if (fs.statSync(filePath).isDirectory()) {<\/p>\n<p>currentPath &#061; filePath;<\/p>\n<p>updateFileList(currentPath);<\/p>\n<p>} else {<\/p>\n<p>showFileInfo(filePath);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>\/\/ \u66f4\u65b0\u6587\u4ef6\u5217\u8868<\/p>\n<p>function updateFileList(path) {<\/p>\n<p>const fileList &#061; document.getElementById(&#039;file-list&#039;);<\/p>\n<p>fileList.innerHTML &#061; &#039;&#039;;<\/p>\n<p>\/\/ \u66f4\u65b0\u9762\u5305\u5c51\u5bfc\u822a<\/p>\n<p>const breadcrumb &#061; document.getElementById(&#039;breadcrumb&#039;);<\/p>\n<p>breadcrumb.innerHTML &#061; generateBreadcrumb(path);<\/p>\n<p>try {<\/p>\n<p>const files &#061; fs.readdirSync(path, { withFileTypes: true });<\/p>\n<p>files.forEach(file &#061;&gt; {<\/p>\n<p>const fileItem &#061; document.createElement(&#039;li&#039;);<\/p>\n<p>fileItem.className &#061; &#039;file-item&#039;;<\/p>\n<p>fileItem.dataset.filename &#061; file.name;<\/p>\n<p>fileItem.innerHTML &#061; &#096;<\/p>\n<p>&lt;span&gt;${file.name}&lt;\/span&gt;<\/p>\n<p>&lt;span&gt;${file.isDirectory() ? &#039;\u76ee\u5f55&#039; : &#039;\u6587\u4ef6&#039;}&lt;\/span&gt;<\/p>\n<p>&#096;;<\/p>\n<p>fileList.appendChild(fileItem);<\/p>\n<p>});<\/p>\n<p>} catch (error) {<\/p>\n<p>alert(&#039;\u65e0\u6cd5\u8bfb\u53d6\u76ee\u5f55&#xff1a;&#039; &#043; error.message);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\/\/ \u751f\u6210\u9762\u5305\u5c51\u5bfc\u822a<\/p>\n<p>function generateBreadcrumb(path) {<\/p>\n<p>const parts &#061; path.split(path.sep);<\/p>\n<p>return parts.map((part, index) &#061;&gt; {<\/p>\n<p>const href &#061; parts.slice(0, index &#043; 1).join(path.sep);<\/p>\n<p>return &#096;&lt;a href&#061;&#034;#&#034; onclick&#061;&#034;navigateTo(&#039;${href}&#039;)&#034;&gt;${part}&lt;\/a&gt;&#096;;<\/p>\n<p>}).join(&#039; \/ &#039;);<\/p>\n<p>}<\/p>\n<p>\/\/ \u5bfc\u822a\u5230\u6307\u5b9a\u8def\u5f84<\/p>\n<p>window.navigateTo &#061; function(href) {<\/p>\n<p>currentPath &#061; href;<\/p>\n<p>updateFileList(currentPath);<\/p>\n<p>};<\/p>\n<p>\/\/ \u663e\u793a\u6587\u4ef6\u4fe1\u606f<\/p>\n<p>function showFileInfo(filePath) {<\/p>\n<p>const fileInfo &#061; document.getElementById(&#039;file-info&#039;);<\/p>\n<p>try {<\/p>\n<p>const stats &#061; fs.statSync(filePath);<\/p>\n<p>fileInfo.innerHTML &#061; &#096;<\/p>\n<p>&lt;p&gt;&lt;strong&gt;\u6587\u4ef6\u540d&#xff1a;&lt;\/strong&gt; ${path.basename(filePath)}&lt;\/p&gt;<\/p>\n<p>&lt;p&gt;&lt;strong&gt;\u8def\u5f84&#xff1a;&lt;\/strong&gt; ${filePath}&lt;\/p&gt;<\/p>\n<p>&lt;p&gt;&lt;strong&gt;\u7c7b\u578b&#xff1a;&lt;\/strong&gt; ${stats.isDirectory() ? &#039;\u76ee\u5f55&#039; : &#039;\u6587\u4ef6&#039;}&lt;\/p&gt;<\/p>\n<p>&lt;p&gt;&lt;strong&gt;\u5927\u5c0f&#xff1a;&lt;\/strong&gt; ${formatSize(stats.size)}&lt;\/p&gt;<\/p>\n<p>&lt;p&gt;&lt;strong&gt;\u521b\u5efa\u65f6\u95f4&#xff1a;&lt;\/strong&gt; ${stats.birthtime.toLocaleString()}&lt;\/p&gt;<\/p>\n<p>&lt;p&gt;&lt;strong&gt;\u4fee\u6539\u65f6\u95f4&#xff1a;&lt;\/strong&gt; ${stats.mtime.toLocaleString()}&lt;\/p&gt;<\/p>\n<p>&#096;;<\/p>\n<p>} catch (error) {<\/p>\n<p>fileInfo.innerHTML &#061; &#039;\u65e0\u6cd5\u83b7\u53d6\u6587\u4ef6\u4fe1\u606f&#xff1a;&#039; &#043; error.message;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\/\/ \u683c\u5f0f\u5316\u6587\u4ef6\u5927\u5c0f<\/p>\n<p>function formatSize(bytes) {<\/p>\n<p>if (bytes &lt; 1024) return &#096;${bytes} B&#096;;<\/p>\n<p>else if (bytes &lt; 1048576) return &#096;${(bytes \/ 1024).toFixed(1)} KB&#096;;<\/p>\n<p>else if (bytes &lt; 1073741824) return &#096;${(bytes \/ 1048576).toFixed(1)} MB&#096;;<\/p>\n<p>else return &#096;${(bytes \/ 1073741824).toFixed(1)} GB&#096;;<\/p>\n<p>}<\/p>\n<h5>5.2.4 \u8fd0\u884c\u548c\u6d4b\u8bd5<\/h5>\n<p>\u5728\u9879\u76ee\u76ee\u5f55\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4&#xff1a;<\/p>\n<p>npm install electron &#8211;save-dev<\/p>\n<p>npm start<\/p>\n<p>\u6d4b\u8bd5\u5e94\u7528\u7684\u5404\u9879\u529f\u80fd&#xff1a;<\/p>\n<li>\u5bfc\u822a\u76ee\u5f55&#xff08;\u70b9\u51fb\u6587\u4ef6\u6216\u76ee\u5f55&#xff09;<\/li>\n<li>\u8fd4\u56de\u4e0a\u7ea7\u76ee\u5f55&#xff08;\u70b9\u51fb &#034;\u8fd4\u56de&#034; \u6309\u94ae&#xff09;<\/li>\n<li>\u65b0\u5efa\u6587\u4ef6&#xff08;\u70b9\u51fb &#034;\u65b0\u5efa\u6587\u4ef6&#034; \u6309\u94ae&#xff09;<\/li>\n<li>\u67e5\u770b\u6587\u4ef6\u4fe1\u606f&#xff08;\u70b9\u51fb\u6587\u4ef6&#xff09;<\/li>\n<li>\u57fa\u672c\u6587\u4ef6\u64cd\u4f5c&#xff08;\u540e\u7eed\u53ef\u4ee5\u6dfb\u52a0\u66f4\u591a\u529f\u80fd&#xff09;<\/li>\n<h4>5.3 \u56fe\u50cf\u67e5\u770b\u5668\u5e94\u7528<\/h4>\n<p>\u6700\u540e&#xff0c;\u6211\u4eec\u5c06\u6784\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u50cf\u67e5\u770b\u5668\u5e94\u7528&#xff0c;\u5c55\u793a\u5982\u4f55\u4f7f\u7528 Electron \u7684\u56fe\u50cf\u5904\u7406\u548c\u6587\u4ef6\u64cd\u4f5c API\u3002<\/p>\n<h5>5.3.1 \u5e94\u7528\u529f\u80fd\u89c4\u5212<\/h5>\n<p>\u6211\u4eec\u7684\u56fe\u50cf\u67e5\u770b\u5668\u5c06\u5177\u5907\u4ee5\u4e0b\u57fa\u672c\u529f\u80fd&#xff1a;<\/p>\n<li>\u6253\u5f00\u56fe\u50cf\u6587\u4ef6<\/li>\n<li>\u663e\u793a\u56fe\u50cf<\/li>\n<li>\u7f29\u653e\u56fe\u50cf<\/li>\n<li>\u65cb\u8f6c\u56fe\u50cf<\/li>\n<li>\u4fdd\u5b58\u4fee\u6539\u540e\u7684\u56fe\u50cf<\/li>\n<h5>5.3.2 \u4e3b\u8fdb\u7a0b\u4ee3\u7801<\/h5>\n<p>\u521b\u5efa\u4e3b\u8fdb\u7a0b\u6587\u4ef6main.js&#xff1a;<\/p>\n<p>const { app, BrowserWindow, dialog } &#061; require(&#039;electron&#039;);<\/p>\n<p>const path &#061; require(&#039;path&#039;);<\/p>\n<p>let mainWindow;<\/p>\n<p>function createWindow() {<\/p>\n<p>mainWindow &#061; new BrowserWindow({<\/p>\n<p>width: 800,<\/p>\n<p>height: 600,<\/p>\n<p>webPreferences: {<\/p>\n<p>nodeIntegration: true<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>mainWindow.loadFile(&#039;index.html&#039;);<\/p>\n<p>mainWindow.on(&#039;closed&#039;, () &#061;&gt; {<\/p>\n<p>mainWindow &#061; null;<\/p>\n<p>});<\/p>\n<p>}<\/p>\n<p>app.whenReady().then(createWindow);<\/p>\n<p>app.on(&#039;window-all-closed&#039;, () &#061;&gt; {<\/p>\n<p>if (process.platform !&#061;&#061; &#039;darwin&#039;) {<\/p>\n<p>app.quit();<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>app.on(&#039;activate&#039;, () &#061;&gt; {<\/p>\n<p>if (mainWindow &#061;&#061;&#061; null) {<\/p>\n<p>createWindow();<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<h5>5.3.3 \u6e32\u67d3\u8fdb\u7a0b\u4ee3\u7801<\/h5>\n<p>\u521b\u5efa\u6e32\u67d3\u8fdb\u7a0b\u6587\u4ef6index.html\u548c\u5bf9\u5e94\u7684 JavaScript \u6587\u4ef6renderer.js&#xff1a;<\/p>\n<p>index.html&#xff1a;<\/p>\n<p>&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>&lt;meta charset&#061;&#034;UTF-8&#034;&gt;<\/p>\n<p>&lt;title&gt;Electron\u56fe\u50cf\u67e5\u770b\u5668&lt;\/title&gt;<\/p>\n<p>&lt;style&gt;<\/p>\n<p>body { margin: 0; padding: 0; }<\/p>\n<p>.toolbar { background-color: #f0f0f0; padding: 10px; }<\/p>\n<p>#image-container { display: flex; justify-content: center; align-items: center; height: calc(100vh &#8211; 40px); }<\/p>\n<p>#image { max-width: 100%; max-height: 100%; }<\/p>\n<p>&lt;\/style&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&lt;div class&#061;&#034;toolbar&#034;&gt;<\/p>\n<p>&lt;button id&#061;&#034;open-btn&#034;&gt;\u6253\u5f00\u56fe\u50cf&lt;\/button&gt;<\/p>\n<p>&lt;button id&#061;&#034;save-btn&#034;&gt;\u4fdd\u5b58&lt;\/button&gt;<\/p>\n<p>&lt;button id&#061;&#034;zoom-in-btn&#034;&gt;\u653e\u5927&lt;\/button&gt;<\/p>\n<p>&lt;button id&#061;&#034;zoom-out-btn&#034;&gt;\u7f29\u5c0f&lt;\/button&gt;<\/p>\n<p>&lt;button id&#061;&#034;rotate-btn&#034;&gt;\u65cb\u8f6c&lt;\/button&gt;<\/p>\n<p>&lt;\/div&gt;<\/p>\n<p>&lt;div id&#061;&#034;image-container&#034;&gt;<\/p>\n<p>&lt;img id&#061;&#034;image&#034; src&#061;&#034;&#034;&gt;<\/p>\n<p>&lt;\/div&gt;<\/p>\n<p>&lt;script src&#061;&#034;renderer.js&#034;&gt;&lt;\/script&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p>renderer.js&#xff1a;<\/p>\n<p>const { ipcRenderer, remote } &#061; require(&#039;electron&#039;);<\/p>\n<p>const fs &#061; require(&#039;fs&#039;);<\/p>\n<p>const path &#061; require(&#039;path&#039;);<\/p>\n<p>const { nativeImage } &#061; require(&#039;electron&#039;);<\/p>\n<p>let currentImagePath &#061; null;<\/p>\n<p>let image &#061; null;<\/p>\n<p>let zoomFactor &#061; 1;<\/p>\n<p>let rotation &#061; 0;<\/p>\n<p>\/\/ \u521d\u59cb\u5316<\/p>\n<p>document.getElementById(&#039;image&#039;).style.transform &#061; &#096;scale(${zoomFactor}) rotate(${rotation}deg)&#096;;<\/p>\n<p>\/\/ \u76d1\u542c\u6253\u5f00\u56fe\u50cf\u6309\u94ae\u70b9\u51fb<\/p>\n<p>document.getElementById(&#039;open-btn&#039;).addEventListener(&#039;click&#039;, () &#061;&gt; {<\/p>\n<p>ipcRenderer.invoke(&#039;open-image&#039;).then(filePath &#061;&gt; {<\/p>\n<p>if (filePath) {<\/p>\n<p>currentImagePath &#061; filePath;<\/p>\n<p>image &#061; nativeImage.createFromPath(currentImagePath);<\/p>\n<p>document.getElementById(&#039;image&#039;).src &#061; image.toDataURL();<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>});<\/p>\n<p>\/\/ \u76d1\u542c\u4fdd\u5b58\u6309\u94ae\u70b9\u51fb<\/p>\n<p>document.getElementById(&#039;save-btn&#039;).addEventListener(&#039;click&#039;, () &#061;&gt; {<\/p>\n<p>if (image) {<\/p>\n<p>ipcRenderer.invoke(&#039;save-image&#039;, currentImagePath, image).then(success &#061;&gt; {<\/p>\n<p>if (success) {<\/p>\n<p>alert(&#039;\u56fe\u50cf\u5df2\u4fdd\u5b58&#039;);<\/p>\n<p>} else {<\/p>\n<p>alert(&#039;\u4fdd\u5b58\u5931\u8d25&#039;);<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>\/\/ \u76d1\u542c\u653e\u5927\u6309\u94ae\u70b9\u51fb<\/p>\n<p>document.getElementById(&#039;zoom-in-btn&#039;).addEventListener(&#039;click&#039;, () &#061;&gt; {<\/p>\n<p>zoomFactor *&#061; 1.2;<\/p>\n<p>updateImageDisplay();<\/p>\n<p>});<\/p>\n<p>\/\/ \u76d1\u542c\u7f29\u5c0f\u6309\u94ae\u70b9\u51fb<\/p>\n<p>document.getElementById(&#039;zoom-out-btn&#039;).addEventListener(&#039;click&#039;, () &#061;&gt; {<\/p>\n<p>zoomFactor \/&#061; 1.2;<\/p>\n<p>updateImageDisplay();<\/p>\n<p>});<\/p>\n<p>\/\/ \u76d1\u542c\u65cb\u8f6c\u6309\u94ae\u70b9\u51fb<\/p>\n<p>document.getElementById(&#039;rotate-btn&#039;).addEventListener(&#039;click&#039;, () &#061;&gt; {<\/p>\n<p>rotation &#043;&#061; 90;<\/p>\n<p>updateImageDisplay();<\/p>\n<p>});<\/p>\n<p>\/\/ \u66f4\u65b0\u56fe\u50cf\u663e\u793a<\/p>\n<p>function updateImageDisplay() {<\/p>\n<p>const imageElement &#061; document.getElementById(&#039;image&#039;);<\/p>\n<p>imageElement.style.transform &#061; &#096;scale(${zoomFactor}) rotate(${rotation}deg)&#096;;<\/p>\n<p>}<\/p>\n<p>\/\/ \u5904\u7406\u7a97\u53e3\u5173\u95ed\u65f6\u7684\u786e\u8ba4<\/p>\n<p>window.addEventListener(&#039;beforeunload&#039;, (event) &#061;&gt; {<\/p>\n<p>if (image) {<\/p>\n<p>event.returnValue &#061; &#039;\u4f60\u6709\u672a\u4fdd\u5b58\u7684\u66f4\u6539&#xff0c;\u786e\u5b9a\u8981\u9000\u51fa\u5417&#xff1f;&#039;;<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>\/\/ \u4e3b\u8fdb\u7a0b\u901a\u4fe1<\/p>\n<p>ipcRenderer.on(&#039;open-image&#039;, (event, filePath) &#061;&gt; {<\/p>\n<p>if (filePath) {<\/p>\n<p>currentImagePath &#061; filePath;<\/p>\n<p>image &#061; nativeImage.createFromPath(currentImagePath);<\/p>\n<p>document.getElementById(&#039;image&#039;).src &#061; image.toDataURL();<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>ipcRenderer.handle(&#039;open-image&#039;, () &#061;&gt; {<\/p>\n<p>return dialog.showOpenDialog({<\/p>\n<p>filters: [{ name: &#039;\u56fe\u50cf\u6587\u4ef6&#039;, extensions: [&#039;png&#039;, &#039;jpg&#039;, &#039;jpeg&#039;, &#039;gif&#039;, &#039;bmp&#039;] }]<\/p>\n<p>}).then(result &#061;&gt; {<\/p>\n<p>if (!result.canceled &amp;&amp; result.filePaths.length &gt; 0) {<\/p>\n<p>return result.filePaths[0];<\/p>\n<p>} else {<\/p>\n<p>return null;<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>});<\/p>\n<p>ipcRenderer.handle(&#039;save-image&#039;, (event, filePath, image) &#061;&gt; {<\/p>\n<p>return new Promise((resolve) &#061;&gt; {<\/p>\n<p>if (!filePath) {<\/p>\n<p>dialog.showSaveDialog({<\/p>\n<p>filters: [{ name: &#039;\u56fe\u50cf\u6587\u4ef6&#039;, extensions: [&#039;png&#039;] }]<\/p>\n<p>}).then(result &#061;&gt; {<\/p>\n<p>if (!result.canceled) {<\/p>\n<p>filePath &#061; result.filePath;<\/p>\n<p>image.writePNG(filePath, () &#061;&gt; {<\/p>\n<p>resolve(true);<\/p>\n<p>});<\/p>\n<p>} else {<\/p>\n<p>resolve(false);<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>} else {<\/p>\n<p>image.writePNG(filePath, () &#061;&gt; {<\/p>\n<p>resolve(true);<\/p>\n<p>});<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>});<\/p>\n<h5>5.3.4 \u8fd0\u884c\u548c\u6d4b\u8bd5<\/h5>\n<p>\u5728\u9879\u76ee\u76ee\u5f55\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4&#xff1a;<\/p>\n<p>npm install electron &#8211;save-dev<\/p>\n<p>npm start<\/p>\n<p>\u6d4b\u8bd5\u5e94\u7528\u7684\u5404\u9879\u529f\u80fd&#xff1a;<\/p>\n<li>\u6253\u5f00\u56fe\u50cf\u6587\u4ef6&#xff08;\u652f\u6301 PNG\u3001JPG \u7b49\u5e38\u89c1\u683c\u5f0f&#xff09;<\/li>\n<li>\u653e\u5927 \/ \u7f29\u5c0f\u56fe\u50cf&#xff08;\u70b9\u51fb\u76f8\u5e94\u6309\u94ae&#xff09;<\/li>\n<li>\u65cb\u8f6c\u56fe\u50cf&#xff08;\u70b9\u51fb\u65cb\u8f6c\u6309\u94ae&#xff09;<\/li>\n<li>\u4fdd\u5b58\u4fee\u6539\u540e\u7684\u56fe\u50cf&#xff08;\u70b9\u51fb\u4fdd\u5b58\u6309\u94ae&#xff09;<\/li>\n<h3>\u516d\u3001\u603b\u7ed3\u4e0e\u8fdb\u9636\u5b66\u4e60<\/h3>\n<h4>6.1 \u672c\u4e66\u5185\u5bb9\u56de\u987e<\/h4>\n<p>\u5728\u672c\u4e66\u4e2d&#xff0c;\u6211\u4eec\u4ece\u96f6\u5f00\u59cb\u5b66\u4e60\u4e86 Electron \u6846\u67b6\u7684\u57fa\u7840\u77e5\u8bc6\u548c\u6838\u5fc3 API&#xff0c;\u5305\u62ec&#xff1a;<\/p>\n<li>\u73af\u5883\u642d\u5efa&#xff1a;\u5b89\u88c5 Node.js \u548c Electron&#xff0c;\u521b\u5efa\u7b2c\u4e00\u4e2a Electron \u5e94\u7528\u3002<\/li>\n<li>\u4e3b\u8fdb\u7a0b API&#xff1a;\u7ba1\u7406\u5e94\u7528\u751f\u547d\u5468\u671f\u7684app\u6a21\u5757&#xff0c;\u521b\u5efa\u548c\u7ba1\u7406\u7a97\u53e3\u7684BrowserWindow\u6a21\u5757&#xff0c;\u521b\u5efa\u83dc\u5355\u7684Menu\u6a21\u5757&#xff0c;\u7cfb\u7edf\u6258\u76d8\u56fe\u6807\u7684Tray\u6a21\u5757&#xff0c;\u4ee5\u53ca\u7cfb\u7edf\u5bf9\u8bdd\u6846\u7684Dialog\u6a21\u5757\u3002<\/li>\n<li>\u6e32\u67d3\u8fdb\u7a0b API&#xff1a;\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684ipcRenderer\u6a21\u5757&#xff0c;\u76f4\u63a5\u8bbf\u95ee\u4e3b\u8fdb\u7a0b\u5bf9\u8c61\u7684remote\u6a21\u5757&#xff0c;\u64cd\u4f5c\u7f51\u9875\u5185\u5bb9\u7684webFrame\u6a21\u5757&#xff0c;\u526a\u8d34\u677f\u64cd\u4f5c\u7684clipboard\u6a21\u5757&#xff0c;\u4ee5\u53ca\u5c4f\u5e55\u4fe1\u606f\u7684screen\u6a21\u5757\u3002<\/li>\n<li>\u8fdb\u7a0b\u95f4\u901a\u4fe1&#xff1a;\u4f7f\u7528ipcMain\u548cipcRenderer\u5b9e\u73b0\u4e3b\u8fdb\u7a0b\u548c\u6e32\u67d3\u8fdb\u7a0b\u4e4b\u95f4\u7684\u901a\u4fe1&#xff0c;\u4f20\u9012\u5404\u79cd\u7c7b\u578b\u7684\u6570\u636e&#xff0c;\u4ee5\u53caremote\u6a21\u5757\u7684\u66ff\u4ee3\u65b9\u6848\u3002<\/li>\n<li>\u5e94\u7528\u6253\u5305\u4e0e\u5206\u53d1&#xff1a;\u4f7f\u7528 Electron Forge \u548c Electron Packager \u6253\u5305\u5e94\u7528&#xff0c;\u5b9e\u73b0\u81ea\u52a8\u66f4\u65b0\u529f\u80fd\u3002<\/li>\n<li>\u5b9e\u6218\u6848\u4f8b&#xff1a;\u6784\u5efa\u4e86\u6587\u672c\u7f16\u8f91\u5668\u3001\u6587\u4ef6\u7ba1\u7406\u5668\u548c\u56fe\u50cf\u67e5\u770b\u5668\u4e09\u4e2a\u5b9e\u9645\u5e94\u7528&#xff0c;\u7efc\u5408\u8fd0\u7528\u6240\u5b66\u7684 API\u3002<\/li>\n<h4>6.2 \u6700\u4f73\u5b9e\u8df5\u4e0e\u6ce8\u610f\u4e8b\u9879<\/h4>\n<p>\u5728\u4f7f\u7528 Electron \u5f00\u53d1\u5e94\u7528\u65f6&#xff0c;\u9700\u8981\u9075\u5faa\u4ee5\u4e0b\u6700\u4f73\u5b9e\u8df5\u548c\u6ce8\u610f\u4e8b\u9879&#xff1a;<\/p>\n<li>\u5b89\u5168\u8003\u8651&#xff1a;<\/li>\n<ul>\n<li>\n<ul>\n<li>\u907f\u514d\u5728\u6e32\u67d3\u8fdb\u7a0b\u4e2d\u4f7f\u7528remote\u6a21\u5757&#xff0c;\u4f18\u5148\u4f7f\u7528ipcRenderer\u548cipcMain\u8fdb\u884c\u901a\u4fe1\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<ul>\n<li>\n<ul>\n<li>\u8c28\u614e\u542f\u7528 Node.js \u96c6\u6210&#xff08;nodeIntegration&#xff09;&#xff0c;\u53ea\u5728\u5fc5\u8981\u65f6\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<ul>\n<li>\n<ul>\n<li>\u5bf9\u6765\u81ea\u7528\u6237\u7684\u8f93\u5165\u8fdb\u884c\u5b89\u5168\u5904\u7406&#xff0c;\u9632\u6b62\u4ee3\u7801\u6ce8\u5165\u653b\u51fb\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<li>\u6027\u80fd\u4f18\u5316&#xff1a;<\/li>\n<ul>\n<li>\n<ul>\n<li>\u907f\u514d\u5728\u4e3b\u8fdb\u7a0b\u4e2d\u6267\u884c\u963b\u585e\u64cd\u4f5c&#xff0c;\u4f7f\u7528\u5f02\u6b65 API \u6216\u5de5\u4f5c\u7ebf\u7a0b\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<ul>\n<li>\n<ul>\n<li>\u5bf9\u4e8e\u5927\u578b\u6570\u636e\u4f20\u9012&#xff0c;\u8003\u8651\u4f7f\u7528Buffer\u5bf9\u8c61\u6216\u6d41\u5f0f\u4f20\u8f93\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<ul>\n<li>\n<ul>\n<li>\u5408\u7406\u7ba1\u7406\u7a97\u53e3\u8d44\u6e90&#xff0c;\u53ca\u65f6\u9500\u6bc1\u4e0d\u518d\u4f7f\u7528\u7684\u7a97\u53e3\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<li>\u8de8\u5e73\u53f0\u517c\u5bb9\u6027&#xff1a;<\/li>\n<ul>\n<li>\n<ul>\n<li>\u6d4b\u8bd5\u5e94\u7528\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u7684\u884c\u4e3a\u548c\u5916\u89c2\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<ul>\n<li>\n<ul>\n<li>\u4f7f\u7528process.platform\u68c0\u6d4b\u5f53\u524d\u5e73\u53f0&#xff0c;\u4e3a\u4e0d\u540c\u5e73\u53f0\u63d0\u4f9b\u9002\u5f53\u7684\u5904\u7406\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<ul>\n<li>\n<ul>\n<li>\u9075\u5faa\u5404\u5e73\u53f0\u7684\u7528\u6237\u754c\u9762\u89c4\u8303&#xff0c;\u5982 macOS \u7684\u83dc\u5355\u680f\u548c Windows \u7684\u4efb\u52a1\u680f\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<li>\u4ee3\u7801\u7ec4\u7ec7&#xff1a;<\/li>\n<ul>\n<li>\n<ul>\n<li>\u5c06\u4e3b\u8fdb\u7a0b\u548c\u6e32\u67d3\u8fdb\u7a0b\u7684\u4ee3\u7801\u5206\u79bb&#xff0c;\u4fdd\u6301\u4ee3\u7801\u6e05\u6670\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<ul>\n<li>\n<ul>\n<li>\u4f7f\u7528\u4e8b\u4ef6\u9a71\u52a8\u67b6\u6784&#xff0c;\u907f\u514d\u7d27\u5bc6\u8026\u5408\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<ul>\n<li>\n<ul>\n<li>\u4e3a Electron API \u8c03\u7528\u6dfb\u52a0\u9519\u8bef\u5904\u7406&#xff0c;\u63d0\u9ad8\u5e94\u7528\u7684\u7a33\u5b9a\u6027\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>6.3 \u8fdb\u9636\u5b66\u4e60\u8d44\u6e90<\/h4>\n<p>\u5982\u679c\u4f60\u5e0c\u671b\u8fdb\u4e00\u6b65\u6df1\u5165\u5b66\u4e60 Electron&#xff0c;\u4ee5\u4e0b\u662f\u4e00\u4e9b\u63a8\u8350\u7684\u8d44\u6e90&#xff1a;<\/p>\n<li>\u5b98\u65b9\u6587\u6863&#xff1a;Electron \u7684\u5b98\u65b9\u6587\u6863\u662f\u6700\u6743\u5a01\u7684\u5b66\u4e60\u8d44\u6e90&#xff0c;\u5305\u542b\u4e86\u6240\u6709 API \u7684\u8be6\u7ec6\u8bf4\u660e\u548c\u793a\u4f8b\u3002<\/li>\n<li>Electron Fiddle&#xff1a;Electron \u5b98\u65b9\u63d0\u4f9b\u7684\u5b9e\u9a8c\u5de5\u5177&#xff0c;\u53ef\u4ee5\u5feb\u901f\u521b\u5efa\u548c\u6d4b\u8bd5 Electron \u4ee3\u7801\u7247\u6bb5\u3002<\/li>\n<li>Electron API JSON&#xff1a;Electron \u7684\u6bcf\u4e2a\u65b0\u7248\u672c\u90fd\u4f1a\u63d0\u4f9b\u4e00\u4e2a\u63cf\u8ff0\u6240\u6709\u516c\u5171 API \u7684 JSON \u6587\u4ef6&#xff0c;\u53ef\u7528\u4e8e\u5f00\u53d1\u5de5\u5177\u548c\u6587\u6863\u751f\u6210\u3002<\/li>\n<li>\u793e\u533a\u8d44\u6e90&#xff1a;Electron \u793e\u533a\u6d3b\u8dc3&#xff0c;\u6709\u5927\u91cf\u7684\u6559\u7a0b\u3001\u535a\u5ba2\u548c\u5f00\u6e90\u9879\u76ee\u53ef\u4f9b\u5b66\u4e60\u3002<\/li>\n<li>\u9ad8\u7ea7\u4e3b\u9898&#xff1a;\u5982\u4f7f\u7528 TypeScript \u5f00\u53d1 Electron \u5e94\u7528&#xff0c;\u96c6\u6210 React\u3001Vue \u7b49\u524d\u7aef\u6846\u67b6&#xff0c;\u4ee5\u53ca\u4f7f\u7528 Native API \u6269\u5c55 Electron \u529f\u80fd\u3002<\/li>\n<h4>6.4 \u7ed3\u8bed<\/h4>\n<p>Electron \u4e3a Web \u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177&#xff0c;\u4f7f\u4ed6\u4eec\u80fd\u591f\u5229\u7528\u73b0\u6709\u7684\u6280\u80fd\u521b\u5efa\u8de8\u5e73\u53f0\u684c\u9762\u5e94\u7528\u3002\u901a\u8fc7\u672c\u4e66\u7684\u5b66\u4e60&#xff0c;\u4f60\u5e94\u8be5\u5df2\u7ecf\u638c\u63e1\u4e86 Electron \u7684\u57fa\u7840\u77e5\u8bc6\u548c\u6838\u5fc3 API&#xff0c;\u80fd\u591f\u5f00\u53d1\u7b80\u5355\u7684\u684c\u9762\u5de5\u5177\u5e94\u7528\u3002<\/p>\n<p>\u8bb0\u4f4f&#xff0c;\u5b66\u4e60\u4efb\u4f55\u6846\u67b6\u7684\u6700\u4f73\u65b9\u6cd5\u662f\u5b9e\u8df5\u3002\u5c1d\u8bd5\u5c06\u672c\u4e66\u4e2d\u5b66\u5230\u7684\u77e5\u8bc6\u5e94\u7528\u5230\u5b9e\u9645\u9879\u76ee\u4e2d&#xff0c;\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\u3002\u968f\u7740\u4f60\u5bf9 Electron \u7684\u6df1\u5165\u7406\u89e3&#xff0c;\u4f60\u4f1a\u53d1\u73b0\u5b83\u7684\u6f5c\u529b\u8fdc\u8fdc\u4e0d\u6b62\u4e8e\u6b64&#xff0c;\u80fd\u591f\u521b\u5efa\u51fa\u529f\u80fd\u66f4\u5f3a\u5927\u3001\u4f53\u9a8c\u66f4\u4e30\u5bcc\u7684\u684c\u9762\u5e94\u7528\u3002<\/p>\n<p>\u5e0c\u671b\u672c\u4e66\u80fd\u591f\u6210\u4e3a\u4f60\u5b66\u4e60 Electron \u7684\u8d77\u70b9&#xff0c;\u5e2e\u52a9\u4f60\u5f00\u542f\u684c\u9762\u5e94\u7528\u5f00\u53d1\u7684\u65b0\u65c5\u7a0b&#xff01;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb327\u6b21\uff0c\u70b9\u8d5e10\u6b21\uff0c\u6536\u85cf12\u6b21\u3002\u73af\u5883\u642d\u5efa\uff1a\u5b89\u88c5 Node.js \u548c Electron\uff0c\u521b\u5efa\u7b2c\u4e00\u4e2a Electron \u5e94\u7528\u3002\u4e3b\u8fdb\u7a0b API\uff1a\u7ba1\u7406\u5e94\u7528\u751f\u547d\u5468\u671f\u7684app\u6a21\u5757\uff0c\u521b\u5efa\u548c\u7ba1\u7406\u7a97\u53e3\u7684BrowserWindow\u6a21\u5757\uff0c\u521b\u5efa\u83dc\u5355\u7684Menu\u6a21\u5757\uff0c\u7cfb\u7edf\u6258\u76d8\u56fe\u6807\u7684Tray\u6a21\u5757\uff0c\u4ee5\u53ca\u7cfb\u7edf\u5bf9\u8bdd\u6846\u7684Dialog\u6a21\u5757\u3002\u6e32\u67d3\u8fdb\u7a0b API\uff1a\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684ipcRenderer\u6a21\u5757\uff0c\u76f4\u63a5\u8bbf\u95ee\u4e3b\u8fdb\u7a0b\u5bf9\u8c61\u7684remote\u6a21\u5757\uff0c\u64cd\u4f5c\u7f51\u9875\u5185\u5bb9\u7684webFrame\u6a21\u5757\uff0c\u526a\u8d34\u677f\u64cd\u4f5c\u7684clipboard\u6a21\u5757\uff0c\u4ee5\u53ca\u5c4f\u5e55\u4fe1\u606f\u7684screen\u6a21\u5757\u3002\u8fdb\u7a0b\u95f4\u901a\u4fe1\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3529,187,87],"topic":[],"class_list":["post-47400","post","type-post","status-publish","format-standard","hentry","category-server","tag-electron","tag-javascript","tag-87"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Electron \u5c0f\u767d\u5165\u95e8\uff1a\u4ece\u96f6\u5f00\u59cb\u6784\u5efa\u8de8\u5e73\u53f0\u684c\u9762\u5e94\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/47400.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Electron \u5c0f\u767d\u5165\u95e8\uff1a\u4ece\u96f6\u5f00\u59cb\u6784\u5efa\u8de8\u5e73\u53f0\u684c\u9762\u5e94\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb327\u6b21\uff0c\u70b9\u8d5e10\u6b21\uff0c\u6536\u85cf12\u6b21\u3002\u73af\u5883\u642d\u5efa\uff1a\u5b89\u88c5 Node.js \u548c Electron\uff0c\u521b\u5efa\u7b2c\u4e00\u4e2a Electron \u5e94\u7528\u3002\u4e3b\u8fdb\u7a0b API\uff1a\u7ba1\u7406\u5e94\u7528\u751f\u547d\u5468\u671f\u7684app\u6a21\u5757\uff0c\u521b\u5efa\u548c\u7ba1\u7406\u7a97\u53e3\u7684BrowserWindow\u6a21\u5757\uff0c\u521b\u5efa\u83dc\u5355\u7684Menu\u6a21\u5757\uff0c\u7cfb\u7edf\u6258\u76d8\u56fe\u6807\u7684Tray\u6a21\u5757\uff0c\u4ee5\u53ca\u7cfb\u7edf\u5bf9\u8bdd\u6846\u7684Dialog\u6a21\u5757\u3002\u6e32\u67d3\u8fdb\u7a0b API\uff1a\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684ipcRenderer\u6a21\u5757\uff0c\u76f4\u63a5\u8bbf\u95ee\u4e3b\u8fdb\u7a0b\u5bf9\u8c61\u7684remote\u6a21\u5757\uff0c\u64cd\u4f5c\u7f51\u9875\u5185\u5bb9\u7684webFrame\u6a21\u5757\uff0c\u526a\u8d34\u677f\u64cd\u4f5c\u7684clipboard\u6a21\u5757\uff0c\u4ee5\u53ca\u5c4f\u5e55\u4fe1\u606f\u7684screen\u6a21\u5757\u3002\u8fdb\u7a0b\u95f4\u901a\u4fe1\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/47400.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-07-29T23:41:57+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"24 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/47400.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/47400.html\",\"name\":\"Electron \u5c0f\u767d\u5165\u95e8\uff1a\u4ece\u96f6\u5f00\u59cb\u6784\u5efa\u8de8\u5e73\u53f0\u684c\u9762\u5e94\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-07-29T23:41:57+00:00\",\"dateModified\":\"2025-07-29T23:41:57+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/47400.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/47400.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/47400.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Electron \u5c0f\u767d\u5165\u95e8\uff1a\u4ece\u96f6\u5f00\u59cb\u6784\u5efa\u8de8\u5e73\u53f0\u684c\u9762\u5e94\u7528\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Electron \u5c0f\u767d\u5165\u95e8\uff1a\u4ece\u96f6\u5f00\u59cb\u6784\u5efa\u8de8\u5e73\u53f0\u684c\u9762\u5e94\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/47400.html","og_locale":"zh_CN","og_type":"article","og_title":"Electron \u5c0f\u767d\u5165\u95e8\uff1a\u4ece\u96f6\u5f00\u59cb\u6784\u5efa\u8de8\u5e73\u53f0\u684c\u9762\u5e94\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb327\u6b21\uff0c\u70b9\u8d5e10\u6b21\uff0c\u6536\u85cf12\u6b21\u3002\u73af\u5883\u642d\u5efa\uff1a\u5b89\u88c5 Node.js \u548c Electron\uff0c\u521b\u5efa\u7b2c\u4e00\u4e2a Electron \u5e94\u7528\u3002\u4e3b\u8fdb\u7a0b API\uff1a\u7ba1\u7406\u5e94\u7528\u751f\u547d\u5468\u671f\u7684app\u6a21\u5757\uff0c\u521b\u5efa\u548c\u7ba1\u7406\u7a97\u53e3\u7684BrowserWindow\u6a21\u5757\uff0c\u521b\u5efa\u83dc\u5355\u7684Menu\u6a21\u5757\uff0c\u7cfb\u7edf\u6258\u76d8\u56fe\u6807\u7684Tray\u6a21\u5757\uff0c\u4ee5\u53ca\u7cfb\u7edf\u5bf9\u8bdd\u6846\u7684Dialog\u6a21\u5757\u3002\u6e32\u67d3\u8fdb\u7a0b API\uff1a\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684ipcRenderer\u6a21\u5757\uff0c\u76f4\u63a5\u8bbf\u95ee\u4e3b\u8fdb\u7a0b\u5bf9\u8c61\u7684remote\u6a21\u5757\uff0c\u64cd\u4f5c\u7f51\u9875\u5185\u5bb9\u7684webFrame\u6a21\u5757\uff0c\u526a\u8d34\u677f\u64cd\u4f5c\u7684clipboard\u6a21\u5757\uff0c\u4ee5\u53ca\u5c4f\u5e55\u4fe1\u606f\u7684screen\u6a21\u5757\u3002\u8fdb\u7a0b\u95f4\u901a\u4fe1\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/47400.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-07-29T23:41:57+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"24 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/47400.html","url":"https:\/\/www.wsisp.com\/helps\/47400.html","name":"Electron \u5c0f\u767d\u5165\u95e8\uff1a\u4ece\u96f6\u5f00\u59cb\u6784\u5efa\u8de8\u5e73\u53f0\u684c\u9762\u5e94\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-07-29T23:41:57+00:00","dateModified":"2025-07-29T23:41:57+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/47400.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/47400.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/47400.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Electron \u5c0f\u767d\u5165\u95e8\uff1a\u4ece\u96f6\u5f00\u59cb\u6784\u5efa\u8de8\u5e73\u53f0\u684c\u9762\u5e94\u7528"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/47400","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=47400"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/47400\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=47400"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=47400"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=47400"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=47400"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}