HarmonyOS鸿蒙Next中在子窗口的页面中如何获取父窗口的宽高
HarmonyOS鸿蒙Next中在子窗口的页面中如何获取父窗口的宽高 在子窗口的页面中如何获取父窗口的宽高
//Reference down demo:
//EntryAbility.ets import { appRecovery, ConfigurationConstant, UIAbility, Want } from ‘@kit.AbilityKit’; import { hilog } from ‘@kit.PerformanceAnalysisKit’; import { window } from ‘@kit.ArkUI’; import { BusinessError } from ‘@kit.BasicServicesKit’; import { i18n } from ‘@kit.LocalizationKit’;
export default class EntryAbility extends UIAbility {
onCreate(): void {
let appPreferredLanguage: string = i18n.System.getAppPreferredLanguage();
try {
i18n.System.setAppPreferredLanguage(appPreferredLanguage); // 设置应用偏好语言为zh-Hans
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error(call System.setAppPreferredLanguage failed, error code: ${err.code}, message: ${err.message}.
);
}
this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT);
hilog.info(0x0000, ‘testTag’, ‘%{public}s’, ‘Ability onCreate’);
hilog.info(0x0000, ‘testTag’, ‘%{public}s’, ‘Ability onCreate’);
appRecovery.enableAppRecovery(
appRecovery.RestartFlag.ALWAYS_RESTART,
appRecovery.SaveOccasionFlag.SAVE_WHEN_ERROR,
appRecovery.SaveModeFlag.SAVE_WITH_FILE
);
let wanttest: Want = {
bundleName: ‘com.example.testpage’, abilityName: “EntryAbility”
}
appRecovery.setRestartWant(wanttest)
}
onDestroy(): void { hilog.info(0x0000, ‘testTag’, ‘%{public}s’, ‘Ability onDestroy’); }
onWindowStageCreate(windowStage: window.WindowStage) { // Main window is created, set main page for this ability hilog.info(0x0000, ‘testTag’, ‘%{public}s’, ‘Ability onWindowStageCreate’); windowStage.loadContent(‘pages/Index’, (err, data) => { AppStorage.setOrCreate(‘windowStage’, windowStage); }); }
onWindowStageDestroy(): void { // Main window is destroyed, release UI related resources hilog.info(0x0000, ‘testTag’, ‘%{public}s’, ‘Ability onWindowStageDestroy’); }
onForeground(): void { // Ability has brought to foreground hilog.info(0x0000, ‘testTag’, ‘%{public}s’, ‘Ability onForeground’); }
onBackground(): void { // Ability has back to background hilog.info(0x0000, ‘testTag’, ‘%{public}s’, ‘Ability onBackground’); } }
// Index.ets
let windowStage_: window.WindowStage | undefined = undefined; let sub_windowClass: window.Window | undefined = undefined;
@Entry @Component struct Index { @State message: string = ‘Hello World’;
build() { Row() { Column() { Text(this.message) .fontSize(50) .fontWeight(FontWeight.Bold) Button() { Text(‘CreateSubWindow’) .fontSize(24) .fontWeight(FontWeight.Normal) }.width(220).height(68) .margin({ left: 10, top: 60 }) .onClick(() => { this.CreateSubWindow() })
Button() {
Text('destroySubWindow')
.fontSize(24)
.fontWeight(FontWeight.Normal)
}.width(220).height(68)
.margin({ left: 10, top: 60 })
.onClick(() => {
this.destroySubWindow()
})
}
.width('100%')
}
.height('100%')
}
private CreateSubWindow() { // 获取windowStage windowStage_ = AppStorage.get(‘windowStage’); // 1.创建应用子窗口。 if (windowStage_ == null) { console.error(‘Failed to create the subwindow. Cause: windowStage_ is null’); } else { windowStage_.createSubWindow(“mySubWindow”, (err: BusinessError, data) => { let errCode: number = err.code; if (errCode) { console.error('Failed to create the subwindow. Cause: ’ + JSON.stringify(err)); return; } sub_windowClass = data; console.info('Succeeded in creating the subwindow. Data: ’ + JSON.stringify(data)); // 2.子窗口创建成功后,设置子窗口的位置、大小及相关属性等。 sub_windowClass.moveWindowTo(300, 300, (err: BusinessError) => { let errCode: number = err.code; if (errCode) { console.error(‘Failed to move the window. Cause:’ + JSON.stringify(err)); return; } console.info(‘Succeeded in moving the window.’); }); sub_windowClass.resize(500, 500, (err: BusinessError) => { let errCode: number = err.code; if (errCode) { console.error(‘Failed to change the window size. Cause:’ + JSON.stringify(err)); return; } console.info(‘Succeeded in changing the window size.’); }); // 3.为子窗口加载对应的目标页面。 sub_windowClass.setUIContent(“pages/SubWindow”, (err: BusinessError) => { let errCode: number = err.code; if (errCode) { console.error(‘Failed to load the content. Cause:’ + JSON.stringify(err)); return; } console.info(‘Succeeded in loading the content.’); // 3.显示子窗口。 (sub_windowClass as window.Window).showWindow((err: BusinessError) => { let errCode: number = err.code; if (errCode) { console.error('Failed to show the window. Cause: ’ + JSON.stringify(err)); return; } console.info(‘Succeeded in showing the window.’); }); }); }) } }
private destroySubWindow() { // 4.销毁子窗口。当不再需要子窗口时,可根据具体实现逻辑,使用destroy对其进行销毁。 (sub_windowClass as window.Window).destroyWindow((err: BusinessError) => { let errCode: number = err.code; if (errCode) { console.error('Failed to destroy the window. Cause: ’ + JSON.stringify(err)); return; } console.info(‘Succeeded in destroying the window.’); }); } }
// SubWindow.ets
let windowStage_: window.WindowStage | undefined = undefined; let windowClass: window.Window | undefined = undefined;
@Entry @Component struct SubWindow { @State message: string = ‘Hello subWindow’;
build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.onClick(() => {
windowStage_ = AppStorage.get(‘windowStage’)
windowStage_?.getMainWindow((err: BusinessError, data) => {
const errCode: number = err.code;
if (errCode) {
console.error(Failed to obtain the main window. Cause code: ${err.code}, message: ${err.message}
);
return;
}
windowClass = data;
let properties = windowClass.getWindowProperties();
let rect = properties.windowRect;
console.info('Succeeded in obtaining the main window. Data: ’ + JSON.stringify(rect));
});
})
}
.width(‘100%’)
}
.backgroundColor(Color.Yellow)
.height(‘100%’)
}
}
更多关于HarmonyOS鸿蒙Next中在子窗口的页面中如何获取父窗口的宽高的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,若要在子窗口的页面中获取父窗口的宽高,可以使用Window
模块中的getWindowProperties
方法。首先通过getWindow
获取当前窗口对象,然后调用getWindowProperties
获取窗口属性,其中包括宽高信息。具体代码如下:
import window from '@ohos.window';
// 获取当前窗口对象
let currentWindow = window.getWindow(this.context);
// 获取窗口属性
currentWindow.getWindowProperties().then((properties) => {
let parentWidth = properties.windowRect.width;
let parentHeight = properties.windowRect.height;
console.log(`Parent Window Width: ${parentWidth}, Height: ${parentHeight}`);
});
通过properties.windowRect
可以获取到父窗口的宽高信息。注意,此方法获取的是窗口的全局坐标和尺寸。
在HarmonyOS鸿蒙Next中,子窗口可以通过Window
类获取父窗口的宽高。首先,通过WindowManager
获取当前窗口的父窗口实例,然后使用getWindowSize()
方法获取父窗口的宽高。示例代码如下:
WindowManager windowManager = WindowManager.getInstance();
Window parentWindow = windowManager.getParentWindow();
Size parentSize = parentWindow.getWindowSize();
int parentWidth = parentSize.getWidth();
int parentHeight = parentSize.getHeight();
通过这种方式,子窗口可以动态获取父窗口的宽高信息。