如何获取窗口当前的显示大小、位置等属性?

如何获取窗口当前的显示大小、位置等属性

阅读 298
1 个回答

可以先通过所操作窗口对象的 getWindowProperties 接口获取类型为 WindowProperties 的返回值,WindowProperties 中的 windowRect 属性为窗口尺寸,该属性值为一个 Rect 类型的对象,在这种场景下 Rect 中的 width 和 height 提供了所操作窗口的大小,left 和 top 属性提供了所操作窗口的位置。

import window from '@ohos.window'; 
try { 
  let windowClass: window.Window = window.findWindow("test"); 
  let properties = windowClass.getWindowProperties(); 
} catch (exception) { 
  console.error('Failed to obtain the window properties. Cause: ' + JSON.stringify(exception)); 
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进