openharmony 鸿蒙 arkts-apis-window-f

2025-06-12 浏览 (1)

Functions

NOTE

The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.

window.createWindow9+

createWindow(config: Configuration, callback: AsyncCallback<Window>): void

Creates a child window or system window. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.SYSTEM_FLOAT_WINDOW (required only when the window type is window.WindowType.TYPE_FLOAT.)

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.WindowManager.WindowManager.Core

Parameters

NameTypeMandatoryDescription
configConfigurationYesParameters used for creating the window.
callbackAsyncCallback<Window>YesCallback used to return the window created.

Error codes

For details about the error codes, see Universal Error Codes and Window Error Codes.

IDError Message
201Permission verification failed. The application does not have the permission required to call the API.
401Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
801Capability not supported. createWindow can not work correctly due to limited device capabilities.
1300001Repeated operation.
1300002This window state is abnormal.
1300004Unauthorized operation.
1300006This window context is abnormal.
1300009The parent window is invalid.

Example

import { UIAbility } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';

export default class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage: window.WindowStage): void {
    let windowClass: window.Window|undefined = undefined;
    let config: window.Configuration = {
      name: "test",
      windowType: window.WindowType.TYPE_DIALOG,
      ctx: this.context
    };
    try {
      window.createWindow(config, (err: BusinessError, data) => {
        const errCode: number = err.code;
        if (errCode) {
          console.error(`Failed to create the window. Cause code: ${err.code}, message: ${err.message}`);
          return;
        }
        windowClass = data;
        console.info('Succeeded in creating the window. Data: ' + JSON.stringify(data));
        windowClass.resize(500, 1000);
      });
    } catch (exception) {
      console.error(`Failed to create the window. Cause code: ${exception.code}, message: ${exception.message}`);
    }
  }
}

window.createWindow9+

createWindow(config: Configuration): Promise<Window>

Creates a child window or system window. This API uses a promise to return the result.

Required permissions: ohos.permission.SYSTEM_FLOAT_WINDOW (required only when the window type is window.WindowType.TYPE_FLOAT.)

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.WindowManager.WindowManager.Core

Parameters

NameTypeMandatoryDescription
configConfigurationYesParameters used for creating the window.

Return value

TypeDescription
Promise<Window>Promise used to return the window created.

Error codes

For details about the error codes, see Universal Error Codes and Window Error Codes.

IDError Message
201Permission verification failed. The application does not have the permission required to call the API.
401Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
801Capability not supported. createWindow can not work correctly due to limited device capabilities.
1300001Repeated operation.
1300002This window state is abnormal.
1300004Unauthorized operation.
1300006This window context is abnormal.
1300009The parent window is invalid.

Example

import { UIAbility } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';

export default class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage: window.WindowStage): void {
    let config: window.Configuration = {
      name: "test",
      windowType: window.WindowType.TYPE_DIALOG,
      ctx: this.context,
      defaultDensityEnabled: true
    };
    try {
      window.createWindow(config).then((value:window.Window) => {
        console.info('Succeeded in creating the window. Data: ' + JSON.stringify(value));
        value.resize(500, 1000);
      }).catch((err:BusinessError)=> {
        console.error(`Failed to create the window. Cause code: ${err.code}, message: ${err.message}`);
      });
    } catch (exception) {
      console.error(`Failed to create the window. Cause code: ${exception.code}, message: ${exception.message}`);
    }
  }
}

window.findWindow9+

findWindow(name: string): Window

Finds a window based on the name.

System capability: SystemCapability.WindowManager.WindowManager.Core

Atomic service API: This API can be used in atomic services since API version 11.

Parameters

NameTypeMandatoryDescription
namestringYesWindow name, that is, the value of name in Configuration.

Return value

TypeDescription
WindowWindow found.

Error codes

For details about the error codes, see Universal Error Codes and Window Error Codes.

IDError Message
401Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
1300002This window state is abnormal.

Example

let windowClass: window.Window|undefined = undefined;
try {
  windowClass = window.findWindow('test');
} catch (exception) {
  console.error(`Failed to find the Window. Cause code: ${exception.code}, message: ${exception.message}`);
}

window.getLastWindow9+

getLastWindow(ctx: BaseContext, callback: AsyncCallback<Window>): void

Obtains the topmost layer child window of the current application. This API uses an asynchronous callback to return the result.

If no child window exists or the child window is not displayed by calling showWindow(), the main window of the application is returned.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.WindowManager.WindowManager.Core

Parameters

NameTypeMandatoryDescription
ctxBaseContextYesCurrent application context.
callbackAsyncCallback<Window>YesCallback used to return the topmost layer window obtained.

Error codes

For details about the error codes, see Universal Error Codes and Window Error Codes.

IDError Message
401Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
1300002This window state is abnormal. Top window or main window is null or destroyed.
1300006This window context is abnormal.

Example

import { UIAbility } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';

export default class EntryAbility extends UIAbility {
  // ...
  onWindowStageCreate(windowStage: window.WindowStage): void {
    console.info('onWindowStageCreate');
    windowStage.createSubWindow('TestSubWindow').then((subWindow) => {
      subWindow.showWindow().then(() => {
        try {
          window.getLastWindow(this.context, (err: BusinessError, topWindow) => {
            const errCode: number = err.code;
            if (errCode) {
              console.error(`Failed to obtain the top window. Cause code: ${err.code}, message: ${err.message}`);
              return;
            }
            console.info(`Succeeded in obtaining the top window. Window id: ${topWindow.getWindowProperties().id}`);
          });
        } catch (exception) {
          console.error(`Failed to obtain the top window. Cause code: ${exception.code}, message: ${exception.message}`);
        }
      });
    });
  }
  //...
}

window.getLastWindow9+

getLastWindow(ctx: BaseContext): Promise<Window>

Obtains the topmost layer child window of the current application. This API uses a promise to return the result.

If no child window exists or the child window is not displayed by calling showWindow(), the main window of the application is returned.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.WindowManager.WindowManager.Core

Parameters

NameTypeMandatoryDescription
ctxBaseContextYesCurrent application context.

Return value

TypeDescription
Promise<Window>Promise used to return the topmost layer window obtained.

Error codes

For details about the error codes, see Universal Error Codes and Window Error Codes.

IDError Message
401Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
1300002This window state is abnormal. Top window or main window is null or destroyed.
1300006This window context is abnormal.

Example

// EntryAbility.ets
import { UIAbility } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';

export default class EntryAbility extends UIAbility {
  // ...
  onWindowStageCreate(windowStage: window.WindowStage): void {
    console.info('onWindowStageCreate');
    windowStage.createSubWindow('TestSubWindow').then((subWindow) => {
      subWindow.showWindow().then(() => {
        try {
          window.getLastWindow(this.context).then((topWindow) => {
            console.info(`Succeeded in obtaining the top window. Window id: ${topWindow.getWindowProperties().id}`);
          }).catch((err: BusinessError) => {
            console.error(`Failed to obtain the top window. Cause code: ${err.code}, message: ${err.message}`);
          });
        } catch (exception) {
          console.error(`Failed to obtain the top window. Cause code: ${exception.code}, message: ${exception.message}`);
        }
      });
    });
  }
  //...
}

window.shiftAppWindowFocus11+

shiftAppWindowFocus(sourceWindowId: number, targetWindowId: number): Promise<void>

Shifts the window focus from the source window to the target window in the same application. The window focus can be shifted between the main window and a child window.

Ensure that the focusable property of the target window is true (see setWindowFocusable()) and that showWindow() is successfully executed.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Window.SessionManager

Parameters

NameTypeMandatoryDescription
sourceWindowIdnumberYesID of the source window, which is having the focus.
targetWindowIdnumberYesID of the target window.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Universal Error Codes and Window Error Codes.

IDError Message
401Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
801Capability not supported. Failed to call the API due to limited device capabilities.
1300002This window state is abnormal.
1300003This window manager service works abnormally.
1300004Unauthorized operation.

Example

// EntryAbility.ets
import { UIAbility } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';

export default class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage: window.WindowStage) {
    // ...
    console.info('onWindowStageCreate');
    let mainWindow: window.Window|undefined = undefined;
    let subWindow: window.Window|undefined = undefined;
    let mainWindowId: number = -1;
    let subWindowId: number = -1;

    try {
      // Obtain the main window and ID of the application.
      windowStage.getMainWindow().then((data) => {
        if (data == null) {
          console.error('Failed to obtain the main window. Cause: The data is empty');
          return;
        }
        mainWindow = data;
        mainWindowId = mainWindow.getWindowProperties().id;
        console.info('Succeeded in obtaining the main window');
      }).catch((err: BusinessError) => {
        console.error(`Failed to obtain the main window. Cause code: ${err.code}, message: ${err.message}`);
      });

      // Create or obtain a child window and its ID. In this case, the child window has focus.
      windowStage.createSubWindow('testSubWindow').then((data) => {
        if (data == null) {
          console.error('Failed to obtain the sub window. Cause: The data is empty');
          return;
        }
        subWindow = data;
        subWindowId = subWindow.getWindowProperties().id;
        subWindow.resize(500, 500);
        subWindow.showWindow();

        // Listen for the window status and ensure that the window is ready.
        subWindow.on("windowEvent", (windowEvent) => {
          if (windowEvent == window.WindowEventType.WINDOW_ACTIVE) {
            // Switch the focus.
            window.shiftAppWindowFocus(subWindowId, mainWindowId).then(() => {
              console.info('Succeeded in shifting app window focus');
            }).catch((err: BusinessError) => {
              console.error(`Failed to shift app window focus. Cause code: ${err.code}, message: ${err.message}`);
            });
          }
        });
      });
    } catch (exception) {
      console.error(`Failed to shift app focus. Cause code: ${exception.code}, message: ${exception.message}`);
    }
  }
}

window.shiftAppWindowPointerEvent15+

shiftAppWindowPointerEvent(sourceWindowId: number, targetWindowId: number): Promise<void>

Transfers an input event from one window to another within the same application, particularly in split-window scenarios. This API uses a promise to return the result. It takes effect only for the main window and its child windows.

The source window must be in a mouse-down state for this API to work; otherwise, the call does not take effect. After the input event is transferred, a mouse-up event is sent to the source window, and a mouse-down event is sent to the target window.

This API can be used only on 2-in-1 devices.

Atomic service API: This API can be used in atomic services since API version 15.

System capability: SystemCapability.Window.SessionManager

Parameters

NameTypeMandatoryDescription
sourceWindowIdnumberYesID of the source window. You are advised to call getWindowProperties() to obtain the window ID.
targetWindowIdnumberYesID of the target window. You are advised to call getWindowProperties() to obtain the window ID.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Universal Error Codes and Window Error Codes.

IDError Message
401Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
801Capability not supported. Failed to call the API due to limited device capabilities.
1300002This window state is abnormal.
1300003This window manager service works abnormally.
1300004Unauthorized operation.

Example

// ets/pages/Index.ets
import { window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
struct Index {
  build() {
    Row() {
      Column() {
        Blank('160')
          .color(Color.Blue)
          .onTouch((event: TouchEvent) => {
            if (event.type === TouchType.Down) {
              try {
                let sourceWindowId = 1;
                let targetWindowId = 2;
                let promise = window.shiftAppWindowPointerEvent(sourceWindowId, targetWindowId);
                promise.then(() => {
                  console.info('Succeeded in shifting app window pointer event');
                }).catch((err: BusinessError) => {
                  console.error(`Failed to shift app window pointer event. Cause code: ${err.code}, message: ${err.message}`);
                });
              } catch (exception) {
                console.error(`Failed to shift app pointer event. Cause code: ${exception.code}, message: ${exception.message}`);
              }
            }
          })
      }.width('100%')
    }.height('100%').width('100%')
  }
}

window.shiftAppWindowTouchEvent20+

shiftAppWindowTouchEvent(sourceWindowId: number, targetWindowId: number, fingerId: number): Promise<void>

Moves touch screen input events from one window to another in scenarios where windows within the same application are being split or merged. This API uses a promise to return the result. It takes effect only for the main window and its child windows.

To transfer touch screen input events, the source window must call this API within the callback of the onTouch event (the event type must be TouchType.Down). After a successful call, the system sends a touch-up event to the source window and a touch-down event to the target window.

This API can be used only on 2-in-1 devices.

System capability: SystemCapability.Window.SessionManager

Parameters

NameTypeMandatoryDescription
sourceWindowIdnumberYesID of the source window. You are advised to call getWindowProperties() to obtain the window ID.
targetWindowIdnumberYesID of the target window. You are advised to call getWindowProperties() to obtain the window ID.
fingerIdnumberYesFinger ID of the touch event. You are advised to use the touches property in the touchEvent event to obtain the ID.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Universal Error Codes and Window Error Codes.

IDError Message
801Capability not supported. Function shiftAppWindowTouchEvent can not work correctly due to limited device capabilities.
1300002This window state is abnormal.
1300003This window manager service works abnormally.
1300004Unauthorized operation.
1300016Parameter error. Possible cause: 1. Invalid parameter range.

Example

// ets/pages/Index.ets
import { window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
struct Index {
  build() {
    Row() {
      Column() {
        Blank('160')
          .color(Color.Blue)
          .onTouch((event: TouchEvent) => {
            // The source window touch event type must be TouchType.Down.
            if (event.type === TouchType.Down) {
              try {
                let sourceWindowId = 1;
                let targetWindowId = 2;
                let promise = window.shiftAppWindowTouchEvent(sourceWindowId, targetWindowId, event.touches[0].id);
                promise.then(() => {
                  console.info(`Succeeded in shifting app window touch event`);
                }).catch((err: BusinessError) => {
                  console.error(`Failed to shift app window touch event. Cause code: ${err.code}, message: ${err.message}`);
                });
              } catch (exception) {
                console.error(`Failed to shift app touch event. Cause code: ${exception.code}, message: ${exception.message}`);
              }
            }
          })
      }.width('100%')
    }.height('100%').width('100%')
  }
}

window.getWindowsByCoordinate14+

getWindowsByCoordinate(displayId: number, windowNumber?: number, x?: number, y?: number): Promise<Array<Window>>

Obtains visible windows at the specified coordinates within the current application, sorted by their current layer order. The window at the topmost layer corresponds to index 0 of the array. This API uses a promise to return the result.

Atomic service API: This API can be used in atomic services since API version 14.

System capability: SystemCapability.Window.SessionManager

Parameters

NameTypeMandatoryDescription
displayIdnumberYesID of the display where the windows are located. The value must be an integer and can be obtained from WindowProperties.
windowNumbernumberNoNumber of windows to obtain. The value must be an integer greater than 0. If this parameter is not set or is less than or equal to 0, all windows that meet the conditions are returned.
xnumberNoX coordinate. The value must be a non-negative integer. If this parameter is not set or is less than 0, all visible windows are returned.
ynumberNoY coordinate. The value must be a non-negative integer. If this parameter is not set or is less than 0, all visible windows are returned.

Return value

TypeDescription
Promise<Array<Window>>Promise used to return an array of window objects.

Error codes

For details about the error codes, see Universal Error Codes and Window Error Codes.

IDError Message
401Parameter error. Possible cause: Incorrect parameter types.
801Capability not supported. Failed to call the API due to limited device capabilities.
1300003This window manager service works abnormally.
import { UIAbility } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';

export default class EntryAbility extends UIAbility {

  onWindowStageCreate(windowStage: window.WindowStage): void {
    try {
      let windowClass = windowStage.getMainWindowSync();
      let properties = windowClass.getWindowProperties();
      window.getWindowsByCoordinate(properties.displayId).then((data) => {
        console.info('Succeeded in creating the subwindow. Data: ' + JSON.stringify(data));
        for (let window of data) {
          // do something with window
        }
      }).catch((err: BusinessError) => {
        console.error(`Failed to get window from point. Cause code: ${err.code}, message: ${err.message}`);
      });
      window.getWindowsByCoordinate(properties.displayId, 2, 500, 500).then((data) => {
        console.info('Succeeded in creating the subwindow. Data: ' + JSON.stringify(data));
        for (let window of data) {
          // do something with window
        }
      }).catch((err: BusinessError) => {
        console.error(`Failed to get window from point. Cause code: ${err.code}, message: ${err.message}`);
      });
    } catch (exception) {
      console.error(`Failed to get window from point. Cause code: ${exception.code}, message: ${exception.message}`);
    }
  }
}

window.getAllWindowLayoutInfo15+

getAllWindowLayoutInfo(displayId: number): Promise<Array<WindowLayoutInfo>>

Obtains the layout information array of all windows visible on a display. The layout information is arranged based on the current window stacking order, and the topmost window in the hierarchy is at index 0 of the array. This API uses a promise to return the result.

Atomic service API: This API can be used in atomic services since API version 15.

System capability: SystemCapability.Window.SessionManager

Parameters

NameTypeMandatoryDescription
displayIdnumberYesID of the display where the windows are located. The value must be an integer and can be obtained from WindowProperties.

Return value

TypeDescription
Promise<Array<WindowLayoutInfo>>Promise used to return an array of window layout information objects.

Error codes

For details about the error codes, see Universal Error Codes and Window Error Codes.

IDError Message
401Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
801Capability not supported. function getAllWindowLayoutInfo can not work correctly due to limited device capabilities.
1300003This window manager service works abnormally.
import { window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';

try {
  let displayId = 0;
  let promise = window.getAllWindowLayoutInfo(displayId);
  promise.then((data) => {
    console.info('Succeeded in obtaining all window layout info. Data: ' + JSON.stringify(data));
  }).catch((err: BusinessError) => {
    console.error(`Failed to obtain all window layout info. Cause code: ${err.code}, message: ${err.message}`);
  });
} catch (exception) {
  console.error(`Failed to obtain all window layout info. Cause code: ${exception.code}, message: ${exception.message}`);
}

window.getVisibleWindowInfo18+

getVisibleWindowInfo(): Promise<Array<WindowInfo>>

Obtains information about visible main windows on the current screen. Visible main windows are main windows that are not returned to the background. This API uses a promise to return the result.

System capability: SystemCapability.Window.SessionManager

Required permissions: ohos.permission.VISIBLE_WINDOW_INFO

Return value

TypeDescription
Promise<Array<WindowInfo>>Promise used to return the information about visible windows.

Error codes

For details about the error codes, see Universal Error Codes and Window Error Codes.

IDError Message
201Permission verification failed. The application does not have the permission required to call the API.
801Capability not supported. Function getVisibleWindowInfo can not work correctly due to limited device capabilities.
1300003This window manager service works abnormally.

Example

import { window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';

try {
  let promise = window.getVisibleWindowInfo();
  promise.then((data) => {
    data.forEach(windowInfo=>{
      console.info(`left:${windowInfo.rect.left}`);
      console.info(`top:${windowInfo.rect.top}`);
      console.info(`width:${windowInfo.rect.width}`);
      console.info(`height:${windowInfo.rect.height}`);
      console.info(`windowId:${windowInfo.windowId}`);
      console.info(`windowStatusType:${windowInfo.windowStatusType}`);
      console.info(`abilityName:${windowInfo.abilityName}`);
      console.info(`bundleName:${windowInfo.bundleName}`);
      console.info(`isFocused:${windowInfo.isFocused}`);
    })
  }).catch((err: BusinessError) => {
    console.error('Failed to getWindowInfo. Cause: ' + JSON.stringify(err));
  });
} catch (exception) {
  console.error(`Failed to get visible window info. Cause code: ${exception.code}, message: ${exception.message}`);
}

window.getGlobalWindowMode20+

getGlobalWindowMode(displayId?: number): Promise<number>

Obtains the window mode of the window that is in the foreground lifecycle on the specified screen. This API uses a promise to return the result.

Atomic service API: This API can be used in atomic services since API version 20.

System capability: SystemCapability.Window.SessionManager

Parameters

NameTypeMandatoryDescription
displayIdnumberNoOptional display ID, which is used to obtain the window mode information on the corresponding screen. This parameter must be an integer greater than or equal to 0. If it is less than 0, error code 1300016 is returned. If this parameter is not passed or is set to null or undefined, all screens are queried. If the specified screen does not exist, the return value is 0.

Return value

TypeDescription
Promise<number>Promise used to return the window mode. Each binary bit represents a window mode. For details about the supported window modes, see GlobalWindowMode. The return value is the result of a bitwise OR operation on the corresponding window mode values. For example, if there are full-screen, floating, and PiP windows on the specified screen, the return value is 0b1|0b100|0b1000 = 13.

Error codes

For details about the error codes, see Universal Error Codes and Window Error Codes.

IDError Message
801Capability not supported. function getGlobalWindowMode can not work correctly due to limited device capabilities.
1300003This window manager service works abnormally.
1300016Parameter error. Possible cause: 1. Invalid parameter range.

Example

import { window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';

try {
  let displayId = 0;
  let promise = window.getGlobalWindowMode(displayId);
  promise.then((data) => {
    console.info(`Succeeded in obtaining global window mode. Data: ${data}`);
  }).catch((err: BusinessError) => {
    console.error(`Failed to obtain global window mode. Cause code: ${err.code}, message: ${err.message}`);
  });
} catch (exception) {
  console.error(`Failed to obtain global window mode. Cause code: ${exception.code}, message: ${exception.message}`);
}

window.create(deprecated)

create(id: string, type: WindowType, callback: AsyncCallback<Window>): void

Creates a child window. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 9. You are advised to use createWindow() instead.

Model restriction: This API can be used only in the FA model.

System capability: SystemCapability.WindowManager.WindowManager.Core

Parameters

NameTypeMandatoryDescription
idstringYesWindow name, that is, the value of name in Configuration.
typeWindowTypeYesWindow type.
callbackAsyncCallback<Window>YesCallback used to return the child window created.

Example

import { BusinessError } from '@kit.BasicServicesKit';

let windowClass: window.Window|undefined = undefined;
window.create('test', window.WindowType.TYPE_APP, (err: BusinessError, data) => {
  const errCode: number = err.code;
  if (errCode) {
    console.error(`Failed to create the subWindow. Cause code: ${err.code}, message: ${err.message}`);
    return;
  }
  windowClass = data;
  console.info('Succeeded in creating the subWindow. Data: ' + JSON.stringify(data));
});

window.create(deprecated)

create(id: string, type: WindowType): Promise<Window>

Creates a child window. This API uses a promise to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 9. You are advised to use createWindow() instead.

Model restriction: This API can be used only in the FA model.

System capability: SystemCapability.WindowManager.WindowManager.Core

Parameters

NameTypeMandatoryDescription
idstringYesWindow name, that is, the value of name in Configuration.
typeWindowTypeYesWindow type.

Return value

TypeDescription
Promise<Window>Promise used to return the child window created.

Example

import { BusinessError } from '@kit.BasicServicesKit';

let windowClass: window.Window|undefined = undefined;
let promise = window.create('test', window.WindowType.TYPE_APP);
promise.then((data) => {
  windowClass = data;
  console.info('Succeeded in creating the subWindow. Data: ' + JSON.stringify(data));
}).catch((err: BusinessError) => {
  console.error(`Failed to create the subWindow. Cause code: ${err.code}, message: ${err.message}`);
});

window.create(deprecated)

create(ctx: BaseContext, id: string, type: WindowType, callback: AsyncCallback<Window>): void

Creates a system window. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 9. You are advised to use createWindow() instead.

System capability: SystemCapability.WindowManager.WindowManager.Core

Parameters

NameTypeMandatoryDescription
ctxBaseContextYesCurrent application context.
idstringYesWindow name, that is, the value of name in Configuration.
typeWindowTypeYesWindow type.
callbackAsyncCallback<Window>YesCallback used to return the child window created.

Example

import { BusinessError } from '@kit.BasicServicesKit';

let windowClass: window.Window|undefined = undefined;
window.create('test', window.WindowType.TYPE_SYSTEM_ALERT, (err: BusinessError, data) => {
  const errCode: number = err.code;
  if (errCode) {
    console.error(`Failed to create the window. Cause code: ${err.code}, message: ${err.message}`);
    return;
  }
  windowClass = data;
  console.info('Succeeded in creating the window. Data: ' + JSON.stringify(data));
  windowClass.resetSize(500, 1000);
});

window.create(deprecated)

create(ctx: BaseContext, id: string, type: WindowType): Promise<Window>

Creates a system window. This API uses a promise to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 9. You are advised to use createWindow() instead.

System capability: SystemCapability.WindowManager.WindowManager.Core

Parameters

NameTypeMandatoryDescription
ctxBaseContextYesCurrent application context.
idstringYesWindow name, that is, the value of name in Configuration.
typeWindowTypeYesWindow type.

Return value

TypeDescription
Promise<Window>Promise used to return the child window created.

Example

import { BusinessError } from '@kit.BasicServicesKit';

let windowClass: window.Window|undefined = undefined;
let promise = window.create('test', window.WindowType.TYPE_SYSTEM_ALERT);
promise.then((data) => {
  windowClass = data;
  console.info('Succeeded in creating the window. Data:' + JSON.stringify(data));
}).catch((err: BusinessError) => {
  console.error(`Failed to create the Window. Cause code: ${err.code}, message: ${err.message}`);
});

window.find(deprecated)

find(id: string, callback: AsyncCallback<Window>): void

Finds a window based on the ID. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 9. You are advised to use findWindow() instead.

System capability: SystemCapability.WindowManager.WindowManager.Core

Parameters

NameTypeMandatoryDescription
idstringYesWindow name, that is, the value of name in Configuration.
callbackAsyncCallback<Window>YesCallback used to return the window found.

Example

import { BusinessError } from '@kit.BasicServicesKit';

let windowClass: window.Window|undefined = undefined;
window.find('test', (err: BusinessError, data) => {
  const errCode: number = err.code;
  if (errCode) {
    console.error(`Failed to find the Window. Cause code: ${err.code}, message: ${err.message}`);
    return;
  }
  windowClass = data;
  console.info('Succeeded in finding the window. Data: ' + JSON.stringify(data));
});

window.find(deprecated)

find(id: string): Promise<Window>

Finds a window based on the ID. This API uses a promise to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 9. You are advised to use findWindow() instead.

System capability: SystemCapability.WindowManager.WindowManager.Core

Parameters

NameTypeMandatoryDescription
idstringYesWindow name, that is, the value of name in Configuration.

Return value

TypeDescription
Promise<Window>Promise used to return the window found.

Example

import { BusinessError } from '@kit.BasicServicesKit';

let windowClass: window.Window|undefined = undefined;
let promise = window.find('test');
promise.then((data) => {
  windowClass = data;
  console.info('Succeeded in finding the window. Data: ' + JSON.stringify(data));
}).catch((err: BusinessError) => {
  console.error(`Failed to find the Window. Cause code: ${err.code}, message: ${err.message}`);
});

window.getTopWindow(deprecated)

getTopWindow(callback: AsyncCallback<Window>): void

Obtains the top window of the current application. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 6 and deprecated since API version 9. You are advised to use getLastWindow() instead.

Model restriction: This API can be used only in the FA model.

System capability: SystemCapability.WindowManager.WindowManager.Core

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<Window>YesCallback used to return the top window obtained.

Example

import { BusinessError } from '@kit.BasicServicesKit';

let windowClass: window.Window|undefined = undefined;
window.getTopWindow((err: BusinessError, data) => {
  const errCode: number = err.code;
  if (errCode) {
    console.error(`Failed to obtain the top window. Cause code: ${err.code}, message: ${err.message}`);
    return;
  }
  windowClass = data;
  console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data));
});

window.getTopWindow(deprecated)

getTopWindow(): Promise<Window>

Obtains the top window of the current application. This API uses a promise to return the result.

NOTE

This API is supported since API version 6 and deprecated since API version 9. You are advised to use getLastWindow() instead.

Model restriction: This API can be used only in the FA model.

System capability: SystemCapability.WindowManager.WindowManager.Core

Return value

TypeDescription
Promise<Window>Promise used to return the top window obtained.

Example

import { BusinessError } from '@kit.BasicServicesKit';

let windowClass: window.Window|undefined = undefined;
let promise = window.getTopWindow();
promise.then((data)=> {
    windowClass = data;
    console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data));
}).catch((err: BusinessError)=>{
    console.error(`Failed to obtain the top window. Cause code: ${err.code}, message: ${err.message}`);
});

window.getTopWindow(deprecated)

getTopWindow(ctx: BaseContext, callback: AsyncCallback<Window>): void

Obtains the top window of the current application. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use getLastWindow() instead.

System capability: SystemCapability.WindowManager.WindowManager.Core

Parameters

NameTypeMandatoryDescription
ctxBaseContextYesCurrent application context.
callbackAsyncCallback<Window>YesCallback used to return the top window obtained.

Example

// EntryAbility.ets
import { UIAbility } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

export default class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage:window.WindowStage){
    console.info('onWindowStageCreate');
    let windowClass: window.Window|undefined = undefined;
    try {
      window.getTopWindow(this.context, (err: BusinessError, data) => {
        const errCode: number = err.code;
        if(errCode){
          console.error(`Failed to obtain the top window. Cause code: ${err.code}, message: ${err.message}`);
          return ;
        }
        windowClass = data;
        console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data));
      });
    } catch(error){
      console.error(`Failed to obtain the top window. Cause code: ${error.code}, message: ${error.message}`);
    }
  }
}

window.getTopWindow(deprecated)

getTopWindow(ctx: BaseContext): Promise<Window>

Obtains the top window of the current application. This API uses a promise to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use getLastWindow() instead.

System capability: SystemCapability.WindowManager.WindowManager.Core

Parameters

NameTypeMandatoryDescription
ctxBaseContextYesCurrent application context.

Return value

TypeDescription
Promise<Window>Promise used to return the top window obtained.

Example

// EntryAbility.ets
import { UIAbility } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

export default class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage:window.WindowStage) {
    console.info('onWindowStageCreate');
    let windowClass: window.Window|undefined = undefined;
    let promise = window.getTopWindow(this.context);
    promise.then((data) => {
      windowClass = data;
      console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data));
    }).catch((error: BusinessError) => {
      console.error(`Failed to obtain the top window. Cause code: ${error.code}, message: ${error.message}`);
    });
  }
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArkUI

harmony 鸿蒙ARKUI_TextPickerCascadeRangeContent

harmony 鸿蒙ARKUI_TextPickerRangeContent

harmony 鸿蒙ArkUI_AnimateCompleteCallback

harmony 鸿蒙ArkUI_AttributeItem

harmony 鸿蒙ArkUI_ColorStop

harmony 鸿蒙ArkUI_ContextCallback

harmony 鸿蒙ArkUI_EventModule

harmony 鸿蒙ArkUI_ExpectedFrameRateRange

harmony 鸿蒙ArkUI_IntOffset

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/6Ld7Uw