example.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. 'use strict';
  2. /**
  3. * 前端(html)调用electron功能时,建议使用该模块
  4. *
  5. * 定义的function 接收三个参数
  6. * @param event ipcMain事件对象
  7. * @param channel 频道
  8. * @param arg 接收到的消息
  9. */
  10. const {app, dialog, BrowserWindow, BrowserView} = require('electron');
  11. const path = require('path');
  12. let myTimer = null;
  13. let browserViewObj = null;
  14. exports.hello = function (event, channel, msg) {
  15. let newMsg = msg + " +1"
  16. let reply = ''
  17. reply = '收到:' + msg + ',返回:' + newMsg
  18. return reply
  19. }
  20. exports.messageShow = function (event, channel, arg) {
  21. dialog.showMessageBoxSync({
  22. type: 'info', // "none", "info", "error", "question" 或者 "warning"
  23. title: '自定义标题-message',
  24. message: '自定义消息内容',
  25. detail: '其它的额外信息'
  26. })
  27. return '打开了消息框';
  28. }
  29. exports.messageShowConfirm = function (event, channel, arg) {
  30. const res = dialog.showMessageBoxSync({
  31. type: 'info',
  32. title: '自定义标题-message',
  33. message: '自定义消息内容',
  34. detail: '其它的额外信息',
  35. cancelId: 1, // 用于取消对话框的按钮的索引
  36. defaultId: 0, // 设置默认选中的按钮
  37. buttons: ['确认', '取消'], // 按钮及索引
  38. })
  39. let data = (res === 0) ? '点击确认按钮' : '点击取消按钮';
  40. console.log('[electron] [ipc] [example] [messageShowConfirm] 结果:', res, );
  41. return data;
  42. }
  43. /**
  44. * 长消息 - 开始
  45. */
  46. exports.socketMessageStart = function (event, channel, arg) {
  47. // 每隔1秒,向前端页面发送消息
  48. // 用定时器模拟
  49. myTimer = setInterval(function(e, c, msg) {
  50. let timeNow = Date.now();
  51. let data = msg + ':' + timeNow;
  52. e.reply(`${c}`, data)
  53. }, 1000, event, channel, arg)
  54. return '开始了'
  55. }
  56. /**
  57. * 长消息 - 停止
  58. */
  59. exports.socketMessageStop = function () {
  60. clearInterval(myTimer);
  61. return '停止了'
  62. }
  63. /**
  64. * 加载视图内容
  65. */
  66. exports.loadViewContent = function (event, channel, arg) {
  67. let content = null;
  68. if (arg.type == 'html') {
  69. content = path.join('file://', app.getAppPath(), arg.content)
  70. } else {
  71. content = arg.content;
  72. }
  73. browserViewObj = new BrowserView();
  74. MAIN_WINDOW.setBrowserView(browserViewObj)
  75. browserViewObj.setBounds({
  76. x: 300,
  77. y: 80,
  78. width: 650,
  79. height: 480
  80. });
  81. browserViewObj.webContents.loadURL(content);
  82. return true
  83. }
  84. /**
  85. * 移除视图内容
  86. */
  87. exports.removeViewContent = function () {
  88. MAIN_WINDOW.removeBrowserView(browserViewObj);
  89. return true
  90. }