1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| import log from 'electron-log'
const ffi = require('ffi-napi') const ref = require('ref-napi') // 对接C++指针 const refStruct = require('ref-struct-napi') // 对接C++结构体 const refArray = require('ref-array-napi') // 对接C++数组
// 屏幕的四种模式
export const sdcApplay = 0x80 // 屏幕设置辅助参数
export enum DisplayMode { internal = 1, // 仅电脑屏 clone = 2, // 复制 extend = 4, // 扩展 external = 8, // 仅第二屏幕 }
// 这两个结构体的内容太复杂,而且实际项目用不到,所以直接算一下结构体的大小填充个无用数据 const DISPLAYCONFIG_PATH_INFO = refStruct({ unuse: refArray(ref.types.int, 72 / 4), }) const DISPLAYCONFIG_MODE_INFO = refStruct({ unuse: refArray(ref.types.int, 64 / 4), })
const PathArrayType = refArray(ref.refType(DISPLAYCONFIG_PATH_INFO)) const ModeArrayType = refArray(ref.refType(DISPLAYCONFIG_MODE_INFO))
const libm = ffi.Library('user32', { SetDisplayConfig: ['long', ['int', 'pointer', 'int', 'pointer', 'int']], GetDisplayConfigBufferSizes: [ 'long', ['int', ref.refType(ref.types.int), ref.refType(ref.types.int)], ], QueryDisplayConfig: [ 'long', [ 'int', ref.refType(ref.types.int), PathArrayType, ref.refType(ref.types.int), ModeArrayType, ref.refType(ref.types.int), ], ], })
export default { setDisplayMode: (mode: DisplayMode) => { log.info( '=====================================================================setDisplayMode', mode, ) // eslint-disable-next-line no-bitwise const re = libm.SetDisplayConfig(0, null, 0, null, sdcApplay | mode) return re }, getDisplayMode: () => { const requiredPaths = ref.alloc(ref.types.int) const requiredModes = ref.alloc(ref.types.int) // 获取QueryDisplayConfig的2-5参数,不用动 const result2 = libm.GetDisplayConfigBufferSizes( 2, requiredPaths, requiredModes, ) log.info(result2, requiredPaths.deref(), requiredModes.deref())
const PathArray = refArray(DISPLAYCONFIG_PATH_INFO, requiredPaths.deref()) const ModeAyyay = refArray(DISPLAYCONFIG_MODE_INFO, requiredModes.deref()) const paths = new PathArray() const modes = new ModeAyyay()
// 查询结果存储在flags log.info('new flags') const flags1 = ref.alloc(ref.types.int) // 2到5参数没有用到,参数1是常量 log.info('start get') libm.QueryDisplayConfig( 4, requiredPaths, paths.ref(), requiredModes, modes.ref(), flags1, ) // log.info('save re') // const re = flags1.deref() log.info( '=====================================================================getDisplayMode', flags1.deref(), ) return flags1.deref() }, }
|