(Windowds under test, other systems not tested)
1, System tray icon,
Main.js:
Const Tray = require (' electron '). Tray;const Menu = require (' electron '). Menu;let Tray;tray = new Tray (__dirname + '/build/img/icon.png ');//System tray icon tray.settooltip (' miaolian.in ');// Tips;const menu = menu.buildfromtemplate When mouse is placed on the system tray icon ([//define right-side menu {label: "Home", Click:gohomehandle}, {label: "register" , Click:registerhandle}, {label: "Logout", Click:logouthandle}, {label: "Exit", Click:closehandle}]); tray.setcontextmen U (menu);//Apply right-built menu Tray.on (' click ', function () {//left-click to show Window Mainwindow.show ();});
2, the render thread sends a message to the main thread
Render Threads: Files in your normal project
Main thread: Electron required to have the Main.js file;
Main.js:
Receive Message const IPCMAIN = require (' electron '). Ipcmain;ipcmain.on (' CloseWindow ', function () {//CloseWindow can define the APP.Q at its own discretion Uit ();}); Ipcmain.on (' Minimize ', function () {//minimize can freely define mainwindow.hide ();});
Project Xx.js:
Send Message Const Ipcrenderer = global.require (' electron '). Ipcrenderer;minimizebtnclickhandle () {ipcrenderer.send (' Minimize ')//and the string that receives the message corresponds};
3,main thread sends a message to the render thread
Main.js:
Send Message Const Browserwindow = electron. Browserwindow;let Mainwindow;mainwindow = new Browserwindow ({xx:xx,xx:xx}), function CloseHandle () {//See (1) Right-click menu in System tray icon The exit option in the single//Closefunc can be MainWindow.webContents.send ("Closefunc") by arbitrarily defining and receiving the message. }
Project XX. Js:
Receive Message Const Ipcrenderer = global.require (' electron '). Ipcrenderer;ipcrenderer.once (' Closefunc ', This.closeHandle.bind (this)); CloseHandle () {/* own logic processing */ipcrenderer.send (' CloseWindow ');}
Note: All monitoring can be used on or once but the difference between the two, on is always listening, triggering events can continue to listen. However, the once can only be triggered once, and then it will be automatically canceled, please use the appropriate method when appropriate.
4. Open the default browser and jump to the specified page
Const SHELL = require (' electron '). Shell;shell.openexternal (' https://miaolian.in ');
Other:
The APIs in the electron are not available in any file.
Some can only be used in the main thread, such as Ipcmain
Some can only be used in render threads, such as Ipcrenderer
Part of it is generic, like the shell.
For more details see document: http://electron.atom.io/docs/
English Document: Https://github.com/electron/electron/tree/master/docs-translations/zh-CN
This article is from "__ No acted" blog, please make sure to keep this source http://wuzishu.blog.51cto.com/6826059/1829551
The realization of common functions of electron part