example.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. 'use strict';
  2. /**
  3. * 前端(html)调用electron功能时,建议使用该模块
  4. *
  5. * 定义的function 接收三个参数
  6. * @param event ipcMain事件对象
  7. * @param channel 频道
  8. * @param arg 接收到的消息
  9. */
  10. const {dialog} = require('electron');
  11. let myTimer = null;
  12. exports.hello = function (event, channel, msg) {
  13. let newMsg = msg + " +1"
  14. let reply = ''
  15. reply = '收到:' + msg + ',返回:' + newMsg
  16. return reply
  17. }
  18. exports.messageShow = function (event, channel, arg) {
  19. dialog.showMessageBoxSync({
  20. type: 'info', // "none", "info", "error", "question" 或者 "warning"
  21. title: '自定义标题-message',
  22. message: '自定义消息内容',
  23. detail: '其它的额外信息'
  24. })
  25. return '打开了消息框';
  26. }
  27. exports.messageShowConfirm = function (event, channel, arg) {
  28. const res = dialog.showMessageBoxSync({
  29. type: 'info',
  30. title: '自定义标题-message',
  31. message: '自定义消息内容',
  32. detail: '其它的额外信息',
  33. cancelId: 1, // 用于取消对话框的按钮的索引
  34. defaultId: 0, // 设置默认选中的按钮
  35. buttons: ['确认', '取消'], // 按钮及索引
  36. })
  37. let data = (res === 0) ? '点击确认按钮' : '点击取消按钮';
  38. console.log('[electron] [example] [messageShowConfirm] 结果:', res, );
  39. return data;
  40. }
  41. /**
  42. * 长消息 - 开始
  43. */
  44. exports.socketMessageStart = function (event, channel, arg) {
  45. // 每隔1秒,向前端页面发送消息
  46. // 用定时器模拟
  47. myTimer = setInterval(function(e, c, msg) {
  48. let timeNow = Date.now();
  49. let data = msg + ':' + timeNow;
  50. e.reply(`${c}`, data)
  51. }, 1000, event, channel, arg)
  52. return '开始了'
  53. }
  54. /**
  55. * 长消息 - 停止
  56. */
  57. exports.socketMessageStop = function () {
  58. clearInterval(myTimer);
  59. return '停止了'
  60. }