Skip to content

Commit

Permalink
Update Task Dialog example (#836)
Browse files Browse the repository at this point in the history
  • Loading branch information
halildurmus committed Mar 25, 2024
1 parent ddc7371 commit 8005168
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 82 deletions.
8 changes: 4 additions & 4 deletions example/bin/taskdialog.exe.manifest
Expand Up @@ -3,7 +3,7 @@
<assemblyIdentity
type="win32"
name="win32.taskdialog"
processorArchitecture="amd64"
processorArchitecture="amd64"
version="1.0.0.0"
/>
<description>taskdialog</description>
Expand All @@ -18,7 +18,7 @@
<!-- Windows 7 -->
<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
<!-- Windows Vista -->
<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/>
<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/>
</application>
</compatibility>
<dependency>
Expand All @@ -45,8 +45,8 @@
<requestedExecutionLevel
level="asInvoker"
uiAccess="false"
/>
/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
</assembly>
165 changes: 87 additions & 78 deletions example/taskdialog.dart
Expand Up @@ -13,25 +13,34 @@
// taskdialog.exe.manifest. Place the compiled taskdialog.exe in the same folder
// as the manifest and then when you run this it should display two task dialog
// samples.
//
// If that doesn't work, make sure that external manifests are enabled in the
// registry. This can be done by running the following command in an elevated
// command prompt:
// REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\SideBySide" /v PreferExternalManifest /t REG_DWORD /d 1 /f

import 'dart:ffi';

import 'package:ffi/ffi.dart';
import 'package:win32/win32.dart';

void showSimpleTaskDialog() {
final windowTitle = 'Dart TaskDialog Sample'.toNativeUtf16();
final mainInstruction = 'Please read this important message'.toNativeUtf16();
final content = 'Task dialogs are great for sharing a longer string of '
'explanatory content, where you need a user to read an instruction '
'before making a decision. Of course, you cannot guarantee that the '
"user will actually read the text, so it's important that you also "
'provide an undo function for when the wrong choice is selected.'
.toNativeUtf16();
final buttonSelected = calloc<Int32>();
using((arena) {
final windowTitle =
'Dart TaskDialog Sample'.toNativeUtf16(allocator: arena);
final mainInstruction =
'Please read this important message'.toNativeUtf16(allocator: arena);
final content = 'Task dialogs are great for sharing a longer string of '
'explanatory content, where you need a user to read an instruction '
'before making a decision. Of course, you cannot guarantee that '
'the user will actually read the text, so it\'s important that you '
'also provide an undo function for when the wrong choice is '
'selected.'
.toNativeUtf16(allocator: arena);
final buttonSelected = arena<Int32>();

try {
final hr = TaskDialog(
try {
final hr = TaskDialog(
NULL,
NULL,
windowTitle,
Expand All @@ -40,83 +49,83 @@ void showSimpleTaskDialog() {
TASKDIALOG_COMMON_BUTTON_FLAGS.TDCBF_OK_BUTTON |
TASKDIALOG_COMMON_BUTTON_FLAGS.TDCBF_CANCEL_BUTTON,
TD_INFORMATION_ICON,
buttonSelected);
if (SUCCEEDED(hr)) {
switch (buttonSelected.value) {
case MESSAGEBOX_RESULT.IDOK:
print('User clicked on the OK button.');
default:
print('User canceled the task dialog.');
buttonSelected,
);
if (SUCCEEDED(hr)) {
switch (buttonSelected.value) {
case MESSAGEBOX_RESULT.IDOK:
print('User clicked on the OK button.');
default:
print('User canceled the task dialog.');
}
}
} on ArgumentError {
print(
'If you see an error "Failed to lookup symbol", it\'s likely because '
'the app manifest declaring a dependency on comctl32.dll v6 is '
'missing.\n\nSee the comment at the top of the sample source code.\n',
);
rethrow;
}
// ignore: avoid_catching_errors
} on ArgumentError {
print('If you see an error "Failed to lookup symbol", it\'s likely because '
'the app manifest\ndeclaring a dependency on comctl32.dll v6 is '
'missing.\n\nSee the comment at the top of the sample source code.\n');
rethrow;
} finally {
free(windowTitle);
free(mainInstruction);
free(content);
}
});
}

void showCustomTaskDialog() {
// Note that this example does not explicitly free allocated memory, since it
// returns quickly to the command prompt. As part of a real app, you'd
// certainly want to free each string here.
final buttonSelected = calloc<Int32>();

final buttons = calloc<TASKDIALOG_BUTTON>(2);
buttons[0]
..nButtonID = 100
..pszButtonText =
'Take the blue pill\nThe story ends, you wake up in your bed and '
'believe whatever you want to believe.'
.toNativeUtf16();
buttons[1]
..nButtonID = 101
..pszButtonText =
'Take the red pill\nYou stay in Wonderland, and I show you how deep '
'the rabbit hole goes.'
.toNativeUtf16();
using((arena) {
final buttonSelected = arena<Int32>();

const matrixDescription =
'In The Matrix, the main character Neo is offered the choice between '
'a red pill and a blue pill by rebel leader Morpheus. The red pill '
'represents an uncertain future: it would free him from the enslaving '
'control of the machine-generated dream world and allow him to escape '
'into the real world, but living the "truth of reality" is harsher and '
'more difficult. On the other hand, the blue pill represents a '
'beautiful prison: it would lead him back to ignorance, living in '
'confined comfort without want or fear within '
'the simulated reality of the Matrix.';
const numberOfButtons = 2;
final buttons = arena<TASKDIALOG_BUTTON>(numberOfButtons);
buttons[0]
..nButtonID = 100
..pszButtonText =
'Take the blue pill\nThe story ends, you wake up in your bed and '
'believe whatever you want to believe.'
.toNativeUtf16(allocator: arena);
buttons[1]
..nButtonID = 101
..pszButtonText =
'Take the red pill\nYou stay in Wonderland, and I show you how deep '
'the rabbit hole goes.'
.toNativeUtf16(allocator: arena);

final config = calloc<TASKDIALOGCONFIG>()
..ref.cbSize = sizeOf<TASKDIALOGCONFIG>()
..ref.pszWindowTitle = 'TaskDialogIndirect Sample'.toNativeUtf16()
..ref.pszMainInstruction = 'Which pill will you take?'.toNativeUtf16()
..ref.pszContent =
'This is your last chance. There is no turning back.'.toNativeUtf16()
..ref.hMainIcon = TD_WARNING_ICON.address
..ref.pszCollapsedControlText = 'See more details.'.toNativeUtf16()
..ref.pszExpandedControlText = matrixDescription.toNativeUtf16()
..ref.dwFlags = TASKDIALOG_FLAGS.TDF_USE_COMMAND_LINKS
..ref.cButtons = 2
..ref.pButtons = buttons;
const matrixDescription =
'In The Matrix, the main character Neo is offered the choice between a '
'red pill and a blue pill by rebel leader Morpheus. The red pill '
'represents an uncertain future: it would free him from the enslaving '
'control of the machine-generated dream world and allow him to escape '
'into the real world, but living the "truth of reality" is harsher and '
'more difficult. On the other hand, the blue pill represents a '
'beautiful prison: it would lead him back to ignorance, living in '
'confined comfort without want or fear within the simulated reality of '
'the Matrix.';

final hr = TaskDialogIndirect(config, buttonSelected, nullptr, nullptr);
final config = arena<TASKDIALOGCONFIG>();
config.ref
..cbSize = sizeOf<TASKDIALOGCONFIG>()
..pszWindowTitle =
'TaskDialogIndirect Sample'.toNativeUtf16(allocator: arena)
..pszMainInstruction =
'Which pill will you take?'.toNativeUtf16(allocator: arena)
..pszContent = 'This is your last chance. There is no turning back.'
.toNativeUtf16(allocator: arena)
..hMainIcon = TD_WARNING_ICON.address
..pszExpandedInformation =
matrixDescription.toNativeUtf16(allocator: arena)
..dwFlags = TASKDIALOG_FLAGS.TDF_USE_COMMAND_LINKS |
TASKDIALOG_FLAGS.TDF_EXPAND_FOOTER_AREA
..cButtons = numberOfButtons
..pButtons = buttons;

if (SUCCEEDED(hr)) {
if (buttonSelected.value == 100) {
print('Ignorance is bliss.');
} else {
print("I've been expecting you, Mr Anderson.");
final hr = TaskDialogIndirect(config, buttonSelected, nullptr, nullptr);
if (SUCCEEDED(hr)) {
if (buttonSelected.value == 100) {
print('Ignorance is bliss.');
} else {
print("I've been expecting you, Mr. Anderson.");
}
}
} else {
print('that failed.');
}
});
}

void main() {
Expand Down

0 comments on commit 8005168

Please sign in to comment.