electron launcher
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.8 KiB

4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
  1. // All of the Node.js APIs are available in the preload process.
  2. // It has the same sandbox as a Chrome extension.
  3. const {ipcRenderer, desktopCapturer} = require('electron');
  4. const remote = require('@electron/remote');
  5. window.currentFrame = 'platform';
  6. window.api = [
  7. 'showSuspensionWindow',
  8. 'resizeWindow',
  9. 'setUnReadMessage',
  10. 'getUnReadMessage',
  11. 'windowMoveHandle',
  12. 'exitSystem',
  13. 'openDevTools',
  14. 'hideMainWindow',
  15. 'showMainWindow',
  16. 'minimize',
  17. 'hideSuspensionWindow',
  18. 'closeWindowById',
  19. 'PrtSc',
  20. 'downloadImg',
  21. 'removeCanvas',
  22. ].reduce(
  23. (acc, apiName) => {
  24. acc[apiName] = (...params) => {
  25. return ipcRenderer.send(apiName, ...params);
  26. };
  27. return acc;
  28. },
  29. {
  30. on: ipcRenderer.on.bind(ipcRenderer),
  31. send: ipcRenderer.send.bind(ipcRenderer),
  32. removeListener: ipcRenderer.removeListener.bind(ipcRenderer),
  33. }
  34. );
  35. ipcRenderer.on('getUnReadMessage', (e, args) => {
  36. window.getUnReadMessage(args[0]);
  37. });
  38. let ImgSrc = null;
  39. // 监听用户截屏
  40. ipcRenderer.on('PrtSc', (e, args) => {
  41. // 显示截屏区域
  42. let screenContent = document.querySelector('.screen-canvas');
  43. screenContent.style.display = 'block';
  44. // 获取当前屏幕的宽高
  45. const {width, height} = remote.getCurrentWindow().getBounds();
  46. console.log(width, height);
  47. // 获取canvas元素
  48. const canvas = document.getElementById('thumbnailCanvas');
  49. const ctx = canvas.getContext('2d');
  50. // 这里的300可根据需要自定义,显示的是截屏后图片显示的大小
  51. canvas.width = (height - 300) * 16 / 9;
  52. canvas.height = height - 300;
  53. desktopCapturer.getSources({types: ['screen'], thumbnailSize: {width: canvas.width, height: canvas.height}}).then(async sources => {
  54. try {
  55. sources.forEach(source => {
  56. const img = new Image();
  57. img.src = source.thumbnail.toDataURL();
  58. ImgSrc = img.src
  59. img.onload = () => {
  60. ctx.drawImage(img, 0, 0);
  61. };
  62. });
  63. } catch (error) {
  64. console.log(error);
  65. }
  66. });
  67. });
  68. // 监听用户下载截屏图片
  69. ipcRenderer.on('downloadImg', (e, args) => {
  70. downLoadImage(ImgSrc);
  71. });
  72. // 下载图片
  73. function downLoadImage(src) {
  74. const link = document.createElement('a');
  75. link.href = src;
  76. link.download = new Date().getTime() + '.png';
  77. document.body.appendChild(link);
  78. link.click();
  79. document.body.removeChild(link);
  80. }
  81. // 监听用户取消截屏图片显示
  82. ipcRenderer.on('removeCanvas', (e, args) => {
  83. removeCanvas()
  84. });
  85. function removeCanvas() {
  86. // 获取canvas元素
  87. const canvas = document.getElementById('thumbnailCanvas');
  88. const ctx = canvas.getContext('2d');
  89. // 清除画布
  90. ctx.clearRect(0, 0, canvas.width, canvas.height);
  91. // 隐藏截屏区域
  92. let screenContent = document.querySelector('.screen-canvas');
  93. screenContent.style.display = 'none';
  94. }