Skip to content

Repo sync for protected branch #5178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 51 additions & 101 deletions docs/mfc/walkthrough-adding-a-ctaskdialog-to-an-application.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
---
description: "Learn more about: Walkthrough: Adding a CTaskDialog to an Application"
description: "Learn how to add a CTaskDialog to a MFC application"
title: "Walkthrough: Adding a CTaskDialog to an Application"
ms.date: "04/25/2019"
ms.date: "2/7/2025"
helpviewer_keywords: ["CTaskDialog, adding", "walkthroughs [MFC], dialogs"]
ms.assetid: 3a62abb8-2d86-4bec-bdb8-5784d5f9a9f8
---
# Walkthrough: Adding a CTaskDialog to an Application
# Walkthrough: Adding a CTaskDialog to an application

This walkthrough introduces the [CTaskDialog Class](../mfc/reference/ctaskdialog-class.md) and shows you how to add one to your application.
This walkthrough introduces the [`CTaskDialog` class](../mfc/reference/ctaskdialog-class.md) and shows how to add it to your application.

The `CTaskDialog` is a task dialog box that replaces the Windows message box in Windows Vista or later. The `CTaskDialog` improves the original message box and adds functionality. The Windows message box is still supported in Visual Studio.
The `CTaskDialog` is a task dialog box that replaces the Windows message box in Windows Vista or later. The `CTaskDialog` improves on the original message box and adds functionality. The Windows message box is still supported in Visual Studio.

> [!NOTE]
> Versions of Windows earlier than Windows Vista do not support the `CTaskDialog`. You must program an alternative dialog box option if you want to show a message to a user who runs your application on an earlier version of Windows. You can use the static method [CTaskDialog::IsSupported](../mfc/reference/ctaskdialog-class.md#issupported) to determine at run time whether a user's computer can display a `CTaskDialog`. In addition, the `CTaskDialog` is only available when your application is built with the Unicode library.
> Versions of Windows earlier than Windows Vista don't support the `CTaskDialog`. You must program an alternative dialog box option if you want to show a message to a user who runs your application on an earlier version of Windows. You can use the static method [CTaskDialog::IsSupported](../mfc/reference/ctaskdialog-class.md#issupported) to determine at run time whether a user's computer can display a `CTaskDialog`. In addition, the `CTaskDialog` is only available when your application is built with the Unicode library.

The `CTaskDialog` supports several optional elements to gather and display information. For example, a `CTaskDialog` can display command links, customized buttons, customized icons, and a footer. The `CTaskDialog` also has several methods that enable you to query the state of the task dialog box to determine what optional elements the user selected.

Expand All @@ -21,83 +20,49 @@ The `CTaskDialog` supports several optional elements to gather and display infor
You need the following components to complete this walkthrough:

- Visual Studio 2010 or later

- Windows Vista or later

## Replacing a Windows Message Box with a CTaskDialog

The following procedure demonstrates the most basic use of the `CTaskDialog`, which is to replace the Windows message box. This example also changes the icon associated with the task dialog box. Changing the icon makes the `CTaskDialog` appear same to the Windows message box.

### To Replace a Windows Message Box with a CTaskDialog
## Replace the Windows message box with a CTaskDialog

1. Use the **MFC Application Wizard** to create an MFC application with all the default settings. See [Walkthrough: Using the New MFC Shell Controls](walkthrough-using-the-new-mfc-shell-controls.md) for instructions on how to open the wizard for your version of Visual Studio.
The following demonstrates the most basic use of the `CTaskDialog`, which is to replace the Windows message box. This example also changes the icon associated with the task dialog box. Changing the icon makes the `CTaskDialog` appear similar to the Windows message box.

1. Use the **MFC Application Wizard** to create a Microsoft Foundation Classes (MFC) application with all the default settings. See [Walkthrough: Using the New MFC Shell Controls](walkthrough-using-the-new-mfc-shell-controls.md) for instructions on how to open the wizard for your version of Visual Studio.
1. Call it *MyProject*.

1. Use the **Solution Explorer** to open the file MyProject.cpp.

1. Use the **Solution Explorer** to open `MyProject.cpp`.
1. Add `#include "afxtaskdialog.h"` after the list of includes.

1. Find the method `CMyProjectApp::InitInstance`. Insert the following lines of code before the `return TRUE;` statement. This code creates the strings that we use in either the Windows message box or in the `CTaskDialog`.

```cpp
CString message("My message to the user");
CString dialogTitle("My Task Dialog title");
CString emptyString;
```

1. Add the following code after the code from step 4. This code guarantees that the user's computer supports the `CTaskDialog`. If the dialog isn't supported, the application displays a Windows message box instead.

```cpp
// Check whether the user's computer supports `CTaskDialog`.
// If not, display a Windows message box instead.
if (CTaskDialog::IsSupported())
{

CTaskDialog taskDialog(message, emptyString, dialogTitle, TDCBF_OK_BUTTON);
taskDialog.SetMainIcon(TD_WARNING_ICON); // Set the icon to be the same as the Windows message box
taskDialog.DoModal();
}
else
{
AfxMessageBox(message);
}
```

1. Insert the following code between the brackets after the **`if`** statement from step 5. This code creates the `CTaskDialog`.

```cpp
CTaskDialog taskDialog(message, emptyString, dialogTitle, TDCBF_OK_BUTTON);
```

1. On the next line, add the following code. This code sets the warning icon.

```cpp
taskDialog.SetMainIcon(TD_WARNING_ICON);
```

1. On the next line, add the following code. This code displays the task dialog box.

```cpp
taskDialog.DoModal();
```

You can avoid step 7 if you don't want the `CTaskDialog` to display the same icon as the Windows message box. If you avoid that step, the `CTaskDialog` has no icon when the application displays it.

Compile and run the application. The application displays the task dialog box after it starts.

## Adding Functionality to the CTaskDialog

The following procedure shows you how to add functionality to the `CTaskDialog` that you created in the previous procedure. The example code shows you how to execute specific instructions based on the user's selections.

### To Add Functionality to the CTaskDialog
## Add functionality to the CTaskDialog

1. Navigate to the **Resource View**. If you can't see the **Resource View**, you can open it from the **View** menu.
The following shows you how to add functionality to the `CTaskDialog` that you created in the previous procedure. The example code shows you how to execute specific instructions based on the user's selections.

1. Expand the **Resource View** until you can select the **String Table** folder. Expand it and double-click the **String Table** entry.

1. Scroll to the bottom of the string table and add a new entry. Change the ID to `TEMP_LINE1`. Set the caption to **Command Line 1**.

1. Add another new entry. Change the ID to `TEMP_LINE2`. Set the caption to **Command Line 2**.

1. Navigate back to MyProject.cpp.

1. After `CString emptyString;`, add the following code:
1. Navigate to the **Resource View** via **View** > **Other Windows** > **Resource View**.
1. Expand the **Resource View** to the **String Table** folder. Expand it and double-click **String Table**.
1. Scroll to the bottom of the string table and add a new entry. Change the ID to `TEMP_LINE1`. Set the caption to `Command Line 1`.
1. Add another new entry. Change the ID to `TEMP_LINE2`. Set the caption to `Command Line 2`.
1. Navigate back to `MyProject.cpp`.
1. In the `CMyProjectApp::InitInstance()` function, after `CString emptyString;` add the following code:

```cpp
CString expandedLabel("Hide extra information");
Expand All @@ -109,67 +74,52 @@ The following procedure shows you how to add functionality to the `CTaskDialog`

```cpp
taskDialog.SetMainInstruction(L"Warning");
taskDialog.SetCommonButtons(
TDCBF_YES_BUTTON | TDCBF_NO_BUTTON | TDCBF_CANCEL_BUTTON);
taskDialog.SetCommonButtons(TDCBF_YES_BUTTON | TDCBF_NO_BUTTON | TDCBF_CANCEL_BUTTON);
taskDialog.LoadCommandControls(TEMP_LINE1, TEMP_LINE2);
taskDialog.SetExpansionArea(
expansionInfo, collapsedLabel, expandedLabel);
taskDialog.SetExpansionArea(expansionInfo, collapsedLabel, expandedLabel);
taskDialog.SetFooterText(L"This is a small footnote to the user");
taskDialog.SetVerificationCheckboxText(L"Remember your selection");
```

1. Add the following line of code that displays the task dialog box to the user and retrieves the user's selection:

```cpp
INT_PTR result = taskDialog.DoModal();
```

1. Insert the following code after the call to `taskDialog.DoModal()`. This section of code processes the user's input:

```cpp
if (taskDialog.GetVerificationCheckboxState())
{
// PROCESS IF the user selects the verification checkbox
// Your code if the user selects the verification checkbox
}

switch (result)
{
case TEMP_LINE1:
// PROCESS IF the first command line
break;
case TEMP_LINE2:
// PROCESS IF the second command line
break;
case IDYES:
// PROCESS IF the user clicks yes
break;
case IDNO:
// PROCESS IF the user clicks no
break;
case IDCANCEL:
// PROCESS IF the user clicks cancel
break;
default:
// This case should not be hit because closing
// the dialog box results in IDCANCEL
break;
case TEMP_LINE1:
// PROCESS IF the first command line
break;
case TEMP_LINE2:
// PROCESS IF the second command line
break;
case IDYES:
// PROCESS IF the user clicks yes
break;
case IDNO:
// PROCESS IF the user clicks no
break;
case IDCANCEL:
// PROCESS IF the user clicks cancel
break;
default:
// This case should not be hit because closing
// the dialog box results in IDCANCEL
break;
}
```

In the code in step 9, replace the comments that start with `PROCESS IF` with the code that you want to execute under the specified conditions.

Compile and run the application. The application displays the task dialog box that uses the new controls and additional information.

## Displaying a CTaskDialog Without Creating a CTaskDialog Object

The following procedure shows you how to display a `CTaskDialog` without first creating a `CTaskDialog` object. This example continues the previous procedures.
The following shows you how to display a `CTaskDialog` without first creating a `CTaskDialog` object. This example continues the previous procedures.

### To Display a CTaskDialog Without Creating a CTaskDialog Object

1. Open the MyProject.cpp file if it isn't already open.

1. Navigate to the closing bracket for the `if (CTaskDialog::IsSupported())` statement.

1. Open the `MyProject.cpp` file.
1. In the `CMyProjectApp::InitInstance()` function, navigate to the closing bracket for the `if (CTaskDialog::IsSupported())` statement.
1. Insert the following code immediately before the closing bracket of the **`if`** statement (before the **`else`** block):

```cpp
Expand All @@ -180,12 +130,12 @@ The following procedure shows you how to display a `CTaskDialog` without first c
TEMP_LINE2);
```

Compile and run the application. The application displays two task dialog boxes. The first dialog box is from the **To Add Functionality to the CTaskDialog** procedure; the second dialog box is from the last procedure.
Compile and run the application. The application displays two task dialog boxes. The first dialog box is from the **To Add Functionality to the CTaskDialog** procedure; the second dialog box is from the previous procedure.

These examples don't demonstrate all the available options for a `CTaskDialog`, but should help you get started. See [CTaskDialog Class](../mfc/reference/ctaskdialog-class.md) for a full description of the class.

## See also

[Dialog Boxes](../mfc/dialog-boxes.md)<br/>
[CTaskDialog Class](../mfc/reference/ctaskdialog-class.md)<br/>
[Dialog Boxes](../mfc/dialog-boxes.md)\
[CTaskDialog Class](../mfc/reference/ctaskdialog-class.md)\
[CTaskDialog::CTaskDialog](../mfc/reference/ctaskdialog-class.md#ctaskdialog)
24 changes: 12 additions & 12 deletions docs/preprocessor/check-stack.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
description: "Learn more about the check_stack pragma directive in Microsoft C/C++"
title: "check_stack pragma"
ms.date: 01/22/2021
ms.date: 2/7/2025
f1_keywords: ["vc-pragma.check_stack", "check_stack_CPP"]
helpviewer_keywords: ["check_stack pragma", "pragma, check_stack", "pragma, check_stack usage table"]
helpviewer_keywords: ["check_stack pragma", "pragma, check_stack"]
no-loc: ["pragma"]
---
# `check_stack` pragma
Expand All @@ -12,24 +12,24 @@ Instructs the compiler to turn off stack probes if **`off`** (or **`-`**) is spe

## Syntax

> **`#pragma check_stack(`** [{ **`on`** | **`off`** }] **`)`**\
> **`#pragma check_stack(`** { **`on`** | **`off`** } **`)`**\
> **`#pragma check_stack`** { **`+`** | **`-`** }

## Remarks

This pragma takes effect at the first function defined after the pragma is seen. Stack probes are neither a part of macros nor of functions that are generated inline.
This pragma only applies to 32-bit platforms (x86, ARM32). It has no effect on 64-bit platforms.

If you don't give an argument for the **`check_stack`** pragma, stack checking reverts to the behavior specified on the command line. For more information, see [Compiler options](../build/reference/compiler-options.md). The interaction of the `#pragma check_stack` and the [`/Gs`](../build/reference/gs-control-stack-checking-calls.md) option is summarized in the following table.
This pragma takes effect at the first function defined after the pragma is seen. Stack probes are not inserted for macros or functions that are generated inline.

### Using the check_stack Pragma
`#pragma check_stack(off)` / `#pragma Check_stack-` is ignored if the size of the function locals is larger than 4096 or the value specified by `/Gs`.

| Syntax | Compiled with<br /><br /> `/Gs` option? | Action |
|--|--|--|
| `#pragma check_stack( )` or<br /><br /> `#pragma check_stack` | Yes | Turns off stack checking for functions that follow |
| `#pragma check_stack( )` or<br /><br /> `#pragma check_stack` | No | Turns on stack checking for functions that follow |
| `#pragma check_stack(on)`<br /><br /> or `#pragma check_stack +` | Yes or No | Turns on stack checking for functions that follow |
| `#pragma check_stack(off)`<br /><br /> or `#pragma check_stack -` | Yes or No | Turns off stack checking for functions that follow |
The default behavior of the compiler is to insert stack probes at the beginning of each function if the size of the locals exceeds 4096 or the value specified by `/Gs`.

Use [/Gs (Control stack checking calls)](../build/reference/gs-control-stack-checking-calls.md) to change the threshold of the locals that trigger stack probes. Use with caution.

Using `#pragma check_stack()` without arguments is deprecated.

## See also

[Compiler options](../build/reference/compiler-options.md)\
[Pragma directives and the `__pragma` and `_Pragma` keywords](./pragma-directives-and-the-pragma-keyword.md)