1e41f4b71Sopenharmony_ci# Building a Dialog Box
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ciDisplay a custom dialog box using a dialog controller, which allows you to set the style and content of the dialog box.
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ciThe dialog box APIs are defined within a struct named **ArkUI_NativeDialogAPI_*x***, where *x* represents the version number. These APIs implement various dialog box controls around the dialog controller.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci## Creating and Destroying a Dialog Controller
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci- Create a dialog controller.
13e41f4b71Sopenharmony_ci  **ArkUI_NativeDialogHandle** represents a pointer to the dialog controller, which you can create by calling the **create** API of **ArkUI_NativeDialogAPI_*x***.
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci  This API returns data of the **ArkUI_NativeDialogHandle** type.
16e41f4b71Sopenharmony_ci  ```
17e41f4b71Sopenharmony_ci  ArkUI_NativeDialogAPI_1 *dialogAPI = reinterpret_cast<ArkUI_NativeDialogAPI_1 *>(
18e41f4b71Sopenharmony_ci      OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_DIALOG, "ArkUI_NativeDialogAPI_1"));
19e41f4b71Sopenharmony_ci  auto dialogController = dialogAPI->create();
20e41f4b71Sopenharmony_ci  ```
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci- Destroy a dialog controller.
23e41f4b71Sopenharmony_ci  When dialog box operations are no longer needed, actively call the **dispose** API to destroy the dialog controller object.
24e41f4b71Sopenharmony_ci  ```
25e41f4b71Sopenharmony_ci  ArkUI_NativeDialogAPI_1 *dialogAPI = reinterpret_cast<ArkUI_NativeDialogAPI_1 *>(
26e41f4b71Sopenharmony_ci      OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_DIALOG, "ArkUI_NativeDialogAPI_1"));
27e41f4b71Sopenharmony_ci  dialogAPI->dispose(dialogController);
28e41f4b71Sopenharmony_ci  ```
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci## Setting Dialog Box Styles
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ciYou can set the alignment, offset, corner radius of the background, background color, mask color, and region of the dialog box.
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci1. Create a dialog box content node.
36e41f4b71Sopenharmony_ci   ```
37e41f4b71Sopenharmony_ci   ArkUI_NodeHandle CreateDialogContent() {
38e41f4b71Sopenharmony_ci       ArkUI_NativeNodeAPI_1 *nodeAPI = reinterpret_cast<ArkUI_NativeNodeAPI_1 *>(
39e41f4b71Sopenharmony_ci           OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_NODE, "ArkUI_NativeNodeAPI_1"));
40e41f4b71Sopenharmony_ci       ArkUI_NodeHandle text = nodeAPI->createNode(ARKUI_NODE_TEXT);
41e41f4b71Sopenharmony_ci       ArkUI_NumberValue textWidthValue[] = {{.f32 = 300}};
42e41f4b71Sopenharmony_ci       ArkUI_AttributeItem textWidthItem = {.value = textWidthValue,
43e41f4b71Sopenharmony_ci                                            .size = sizeof(textWidthValue) / sizeof(ArkUI_NumberValue)};
44e41f4b71Sopenharmony_ci       nodeAPI->setAttribute(text, NODE_WIDTH, &textWidthItem);
45e41f4b71Sopenharmony_ci       ArkUI_NumberValue textHeightValue[] = {{.f32 = 300}};
46e41f4b71Sopenharmony_ci       ArkUI_AttributeItem textHeightItem = {.value = textHeightValue,
47e41f4b71Sopenharmony_ci                                             .size = sizeof(textWidthValue) / sizeof(ArkUI_NumberValue)};
48e41f4b71Sopenharmony_ci       nodeAPI->setAttribute(text, NODE_HEIGHT, &textHeightItem);
49e41f4b71Sopenharmony_ci       ArkUI_NodeHandle span = nodeAPI->createNode(ARKUI_NODE_SPAN);
50e41f4b71Sopenharmony_ci       ArkUI_AttributeItem spanItem = {.string = "This is a dialog box"};
51e41f4b71Sopenharmony_ci       nodeAPI->setAttribute(span, NODE_SPAN_CONTENT, &spanItem);
52e41f4b71Sopenharmony_ci       ArkUI_NodeHandle imageSpan = nodeAPI->createNode(ARKUI_NODE_IMAGE_SPAN);
53e41f4b71Sopenharmony_ci       ArkUI_AttributeItem imageSpanItem = {.string = "/pages/common/sky.jpg"};
54e41f4b71Sopenharmony_ci       nodeAPI->setAttribute(imageSpan, NODE_IMAGE_SPAN_SRC, &imageSpanItem);
55e41f4b71Sopenharmony_ci       ArkUI_NumberValue imageSpanWidthValue[] = {{.f32 = 300}};
56e41f4b71Sopenharmony_ci       ArkUI_AttributeItem imageSpanWidthItem = {.value = imageSpanWidthValue,
57e41f4b71Sopenharmony_ci                                                 .size = sizeof(textWidthValue) / sizeof(ArkUI_NumberValue)};
58e41f4b71Sopenharmony_ci       nodeAPI->setAttribute(imageSpan, NODE_WIDTH, &imageSpanWidthItem);
59e41f4b71Sopenharmony_ci       ArkUI_NumberValue imageSpanHeightValue[] = {{.f32 = 200}};
60e41f4b71Sopenharmony_ci       ArkUI_AttributeItem imageSpanHeightItem = {.value = imageSpanHeightValue,
61e41f4b71Sopenharmony_ci                                                  .size = sizeof(textWidthValue) / sizeof(ArkUI_NumberValue)};
62e41f4b71Sopenharmony_ci       nodeAPI->setAttribute(imageSpan, NODE_HEIGHT, &imageSpanHeightItem);
63e41f4b71Sopenharmony_ci       nodeAPI->addChild(text, span);
64e41f4b71Sopenharmony_ci       nodeAPI->addChild(text, imageSpan);
65e41f4b71Sopenharmony_ci       return text;
66e41f4b71Sopenharmony_ci   }
67e41f4b71Sopenharmony_ci   ```
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ci2. Control dialog box styles with the controller.
70e41f4b71Sopenharmony_ci   ```
71e41f4b71Sopenharmony_ci   void ShowDialog() {
72e41f4b71Sopenharmony_ci       ArkUI_NativeDialogAPI_1 *dialogAPI = reinterpret_cast<ArkUI_NativeDialogAPI_1 *>(
73e41f4b71Sopenharmony_ci           OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_DIALOG, "ArkUI_NativeDialogAPI_1"));
74e41f4b71Sopenharmony_ci       if (!dialogController) {
75e41f4b71Sopenharmony_ci           dialogController = dialogAPI->create();
76e41f4b71Sopenharmony_ci       }
77e41f4b71Sopenharmony_ci       auto contentNode = CreateDialogContent();
78e41f4b71Sopenharmony_ci       dialogAPI->setContent(dialogController, contentNode);
79e41f4b71Sopenharmony_ci       dialogAPI->setContentAlignment(dialogController, static_cast<int32_t>(ARKUI_ALIGNMENT_BOTTOM), 0, 0);
80e41f4b71Sopenharmony_ci       dialogAPI->setBackgroundColor(dialogController, 0xffffffff);
81e41f4b71Sopenharmony_ci       dialogAPI->setCornerRadius(dialogController, 6, 6, 6, 6);
82e41f4b71Sopenharmony_ci       dialogAPI->setModalMode(dialogController, false);
83e41f4b71Sopenharmony_ci       dialogAPI->setAutoCancel(dialogController, true);
84e41f4b71Sopenharmony_ci       dialogAPI->show(dialogController, false);
85e41f4b71Sopenharmony_ci   }
86e41f4b71Sopenharmony_ci   ```
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ci3. Close the dialog box when it is not needed.
89e41f4b71Sopenharmony_ci   ```
90e41f4b71Sopenharmony_ci   void CloseDialog() {
91e41f4b71Sopenharmony_ci       ArkUI_NativeDialogAPI_1 *dialogAPI = reinterpret_cast<ArkUI_NativeDialogAPI_1 *>(
92e41f4b71Sopenharmony_ci           OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_DIALOG, "ArkUI_NativeDialogAPI_1"));
93e41f4b71Sopenharmony_ci       dialogAPI->close(dialogController);
94e41f4b71Sopenharmony_ci   }
95e41f4b71Sopenharmony_ci   ```
96e41f4b71Sopenharmony_ci
97e41f4b71Sopenharmony_ci
98e41f4b71Sopenharmony_ci## Interaction with the Dialog Box
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ciCreate an interactive page to open or close a dialog box.
101e41f4b71Sopenharmony_ci
102e41f4b71Sopenharmony_ci1. Create an interactive page, where a button click triggers the dialog box.
103e41f4b71Sopenharmony_ci   ```
104e41f4b71Sopenharmony_ci   constexpr int32_t BUTTON_CLICK_ID = 1;
105e41f4b71Sopenharmony_ci   bool isShown = false;
106e41f4b71Sopenharmony_ci   ArkUI_NativeDialogHandle dialogController;
107e41f4b71Sopenharmony_ci   ArkUI_NodeHandle buttonNode;
108e41f4b71Sopenharmony_ci   
109e41f4b71Sopenharmony_ci   void MainViewMethod(OH_NativeXComponent *component) {
110e41f4b71Sopenharmony_ci       ArkUI_NativeNodeAPI_1 *nodeAPI = reinterpret_cast<ArkUI_NativeNodeAPI_1 *>(
111e41f4b71Sopenharmony_ci           OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_NODE, "ArkUI_NativeNodeAPI_1"));
112e41f4b71Sopenharmony_ci       ArkUI_NodeHandle column = nodeAPI->createNode(ARKUI_NODE_COLUMN);
113e41f4b71Sopenharmony_ci       ArkUI_NumberValue widthValue[] = {{.f32 = 300}};
114e41f4b71Sopenharmony_ci       ArkUI_AttributeItem widthItem = {.value = widthValue, .size = sizeof(widthValue) / sizeof(ArkUI_NumberValue)};
115e41f4b71Sopenharmony_ci       nodeAPI->setAttribute(column, NODE_WIDTH, &widthItem);
116e41f4b71Sopenharmony_ci       ArkUI_NumberValue heightValue[] = {{.f32 = 300}};
117e41f4b71Sopenharmony_ci       ArkUI_AttributeItem heightItem = {.value = heightValue, .size = sizeof(heightValue) / sizeof(ArkUI_NumberValue)};
118e41f4b71Sopenharmony_ci       nodeAPI->setAttribute(column, NODE_HEIGHT, &heightItem);
119e41f4b71Sopenharmony_ci       
120e41f4b71Sopenharmony_ci       buttonNode = nodeAPI->createNode(ARKUI_NODE_BUTTON);
121e41f4b71Sopenharmony_ci       ArkUI_NumberValue buttonWidthValue[] = {{.f32 = 200}};
122e41f4b71Sopenharmony_ci       ArkUI_AttributeItem buttonWidthItem = {.value = buttonWidthValue,
123e41f4b71Sopenharmony_ci                                              .size = sizeof(buttonWidthValue) / sizeof(ArkUI_NumberValue)};
124e41f4b71Sopenharmony_ci       nodeAPI->setAttribute(buttonNode, NODE_WIDTH, &buttonWidthItem);
125e41f4b71Sopenharmony_ci       ArkUI_NumberValue buttonHeightValue[] = {{.f32 = 50}};
126e41f4b71Sopenharmony_ci       ArkUI_AttributeItem buttonHeightItem = {.value = buttonHeightValue,
127e41f4b71Sopenharmony_ci                                               .size = sizeof(buttonHeightValue) / sizeof(ArkUI_NumberValue)};
128e41f4b71Sopenharmony_ci       nodeAPI->setAttribute(buttonNode, NODE_HEIGHT, &buttonHeightItem);
129e41f4b71Sopenharmony_ci       ArkUI_AttributeItem labelItem = {.string = "Click Dialog Box};
130e41f4b71Sopenharmony_ci       nodeAPI->setAttribute(buttonNode, NODE_BUTTON_LABEL, &labelItem);
131e41f4b71Sopenharmony_ci       ArkUI_NumberValue buttonTypeValue[] = {{.i32 = static_cast<int32_t>(ARKUI_BUTTON_TYPE_NORMAL)}};
132e41f4b71Sopenharmony_ci       ArkUI_AttributeItem buttonTypeItem = {.value = buttonTypeValue,
133e41f4b71Sopenharmony_ci                                             .size = sizeof(buttonTypeValue) / sizeof(ArkUI_NumberValue)};
134e41f4b71Sopenharmony_ci       nodeAPI->setAttribute(buttonNode, NODE_BUTTON_TYPE, &buttonTypeItem);
135e41f4b71Sopenharmony_ci       nodeAPI->registerNodeEvent(buttonNode, NODE_ON_CLICK, BUTTON_CLICK_ID, nullptr);
136e41f4b71Sopenharmony_ci       nodeAPI->addNodeEventReceiver(buttonNode, OnButtonClicked);
137e41f4b71Sopenharmony_ci       nodeAPI->addChild(column, buttonNode);
138e41f4b71Sopenharmony_ci       OH_NativeXComponent_AttachNativeRootNode(component, column);
139e41f4b71Sopenharmony_ci   }
140e41f4b71Sopenharmony_ci   ```
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ci2. Create a button event callback function to trigger the display or closure of the dialog box when the button is clicked.
143e41f4b71Sopenharmony_ci   ```
144e41f4b71Sopenharmony_ci   void OnButtonClicked(ArkUI_NodeEvent *event) {
145e41f4b71Sopenharmony_ci       if (!event || !buttonNode) {
146e41f4b71Sopenharmony_ci           return;
147e41f4b71Sopenharmony_ci       }
148e41f4b71Sopenharmony_ci       auto eventId = OH_ArkUI_NodeEvent_GetTargetId(event);
149e41f4b71Sopenharmony_ci       if (eventId == BUTTON_CLICK_ID) {
150e41f4b71Sopenharmony_ci           ArkUI_NativeNodeAPI_1 *nodeAPI = reinterpret_cast<ArkUI_NativeNodeAPI_1 *>(
151e41f4b71Sopenharmony_ci               OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_NODE, "ArkUI_NativeNodeAPI_1"));
152e41f4b71Sopenharmony_ci           if (isShown) {
153e41f4b71Sopenharmony_ci               isShown = false;
154e41f4b71Sopenharmony_ci               ArkUI_AttributeItem labelItem = {.string = "Show Dialog Box"};
155e41f4b71Sopenharmony_ci               nodeAPI->setAttribute(buttonNode, NODE_BUTTON_LABEL, &labelItem);
156e41f4b71Sopenharmony_ci               CloseDialog();
157e41f4b71Sopenharmony_ci           } else {
158e41f4b71Sopenharmony_ci               isShown = true;
159e41f4b71Sopenharmony_ci               ArkUI_AttributeItem labelItem = {.string = "Close Dialog Box"};
160e41f4b71Sopenharmony_ci               nodeAPI->setAttribute(buttonNode, NODE_BUTTON_LABEL, &labelItem);
161e41f4b71Sopenharmony_ci               ShowDialog();
162e41f4b71Sopenharmony_ci           }
163e41f4b71Sopenharmony_ci       }
164e41f4b71Sopenharmony_ci   }
165e41f4b71Sopenharmony_ci   ```
166e41f4b71Sopenharmony_ci
167e41f4b71Sopenharmony_ci   ![en-us_image_0000001902966196](figures/en-us_image_0000001902966196.gif)
168