JSA里的FFI(7)——移动鼠标
wils
创作者俱乐部成员
昨天EH论坛有朋友问,如何在选中单元格时,立即将鼠标光标移动到当前单元格左上角
在lss001大佬的指点以及问ai之下,大概有点明白需要进行点和像素之间的转换
function Workbook_SheetSelectionChange(Sh, Target)
{
const user32 = FFI.LoadLibrary('user32')
const GetDpiForWindow = user32.LoadFunction('', 'GetDpiForWindow', '', 'uint32', ['uint32'])
const SetCursorPos = user32.LoadFunction('', 'SetCursorPos', '', 'int32', ['int32', 'int32'])
const s = GetDpiForWindow.Call(Application.Hwnd) / 72
const x = Windows(1).PointsToScreenPixelsX(Target.Left * s)
const y = Windows(1).PointsToScreenPixelsY(Target.Top * s)
console.log(SetCursorPos.Call(x, y))
}大概就是GetDpiForWindow获取dpi
dpi / 72算出像素和点的换算比例
然后将单元格Left、Top的点转换为像素
再PointsToScreenPixelsX得出相对Screen的像素
最后用SetCursorPos移动鼠标到指定的像素
这里有个问题,大佬和之前的论坛搜到的帖子,都说用这两句获取dpi
GetDeviceCaps(GetDC(0), 88)
GetDeviceCaps(GetDC(0), 90)想知道与下面这句获取dpi有啥区别
GetDpiForWindow(Hwnd)还有就是直接除以72是否就可以得出像素与点的比例
还是说需要再配合zoom进行缩放
希望更多朋友来尝试交流,指点一下具体该怎么计算这种位置信息
创作者俱乐部成员