コード例 #1
0
        private static void ShowAboutDialog(TaskDialogPage origpage)
        {
            var newPage = new TaskDialogPage()
            {
                Heading = "USB Function Mode Switcher",
                Text    = "Version " + Assembly.GetEntryAssembly().GetCustomAttribute <AssemblyFileVersionAttribute>().Version +
                          "\n" + Assembly.GetEntryAssembly().GetCustomAttribute <AssemblyCopyrightAttribute>().Copyright +
                          "\nReleased under the MIT License",
                Caption       = "About",
                Icon          = TaskDialogIcon.Information,
                AllowCancel   = true,
                SizeToContent = true
            };

            var srcbutton   = new TaskDialogCommandLinkButton("Source Code", allowCloseDialog: false);
            var closebutton = new TaskDialogCommandLinkButton("Close", allowCloseDialog: false);

            newPage.Buttons.Add(srcbutton);
            newPage.Buttons.Add(closebutton);

            srcbutton.Click += (object sender2, EventArgs args2) =>
            {
                Process.Start("https://github.com/WOA-Project/USBFunctionModeSwitcher");
            };

            closebutton.Click += (object sender2, EventArgs args2) =>
            {
                newPage.Navigate(origpage);
            };

            origpage.Navigate(newPage);
        }
コード例 #2
0
        private static void ShowDisclaimerDialog(TaskDialogPage origpage, Action action)
        {
            var newPage = new TaskDialogPage()
            {
                Heading       = "USB Function Mode Switcher",
                Text          = "Switching to this mode will enable power output from the USB Type C port. This may harm your device if you plug in a charging cable or a continuum dock. In this mode NEVER plug in any charging cable, wall charger, PC USB Cable (connected to a PC) or any externally powered USB hub! We cannot be taken responsible for any damage caused by this, you have been warned!",
                Caption       = "Do you really want to do this?",
                Icon          = TaskDialogIcon.Warning,
                AllowCancel   = true,
                SizeToContent = true
            };

            var nobutton  = new TaskDialogCommandLinkButton("No", allowCloseDialog: false);
            var yesbutton = new TaskDialogCommandLinkButton("Yes I understand all the risks");

            newPage.Buttons.Add(nobutton);
            newPage.Buttons.Add(yesbutton);

            yesbutton.Click += (object sender2, EventArgs args2) =>
            {
                action();
            };

            nobutton.Click += (object sender2, EventArgs args2) =>
            {
                newPage.Navigate(origpage);
            };

            origpage.Navigate(newPage);
        }
コード例 #3
0
        static TaskDialogPage CreatetDownloadPage()
        {
            var downloadCancellationSource = new CancellationTokenSource();

            var downloadProgressBar = new TaskDialogProgressBar()
            {
                Minimum = 0,
                Maximum = 100,
                State   = TaskDialogProgressBarState.Marquee,
            };
            var cancelDownloadButton = new TaskDialogButton()
            {
                Text = "Cancel Download",
            };

            cancelDownloadButton.Click += (s, e) => downloadCancellationSource.Cancel();

            var downloadPage = new TaskDialogPage()
            {
                AllowMinimize = true,
                Caption       = "FFmpeg missing",
                Heading       = "Downloading FFmpeg...",
                Text          = "We're shifting some bits around to get you ready.",
                ProgressBar   = downloadProgressBar,
                Buttons       = new TaskDialogButtonCollection()
                {
                    cancelDownloadButton,
                },
                // DefaultButton = None,
                Expander = new TaskDialogExpander()
                {
                    Text = "Initializing...",
                    CollapsedButtonText = "More status info",
                    Position            = TaskDialogExpanderPosition.AfterFootnote,
                },
            };

            downloadPage.Created += async(o, e) =>
            {
                downloadPage.Expander.Text = "Fetching info...";

                var url = await FFmpegFetcher.GetUrlOfLatestBinary();

                if (url == null)
                {
                    downloadPage.Navigate(CreateDownloadFailedPage("Unable to get link to FFmpeg download"));
                    return;
                }

                var progress = new DialogTransferProgress(downloadPage);

                downloadPage.Expander.Text = "Downloading...";
                try
                {
                    await FFmpegFetcher.LoadAndUnzipToDirectory(FFmpegManager.FFmpegAppDataPath, url, progress, downloadCancellationSource.Token);
                }
                catch (TaskCanceledException)
                {
                    // Well, it's actually not an error, but we're too lazy for a separate doaloge for this
                    downloadPage.Navigate(CreateDownloadFailedPage("You cancelled the download."));
                    return;
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Failed to download FFmpeg.");
                    Debug.WriteLine(ex);
                    downloadPage.Navigate(CreateDownloadFailedPage(ex.Message));
                    return;
                }

                downloadPage.Expander.Text = "Done!";

                // Cross-check if downloading ffmpeg fixed the problem
                var path = FFmpegManager.GetAbsoluteFFmpegPath(true);
                if (path == null)
                {
                    downloadPage.Navigate(CreateDownloadFailedPage("The download was successful, but it did not fix the issue."));
                }
                else
                {
                    downloadPage.Navigate(CreateDownloadFinishedPage());
                }
            };

            return(downloadPage);
        }
コード例 #4
0
        private static void ShowPolarityDialog(TaskDialogPage origpage)
        {
            var newPage = new TaskDialogPage()
            {
                Heading       = "USB Function Mode Switcher",
                Text          = "You can change the polarity of the USB C port. This effectively allows you to use the cable in another direction.",
                Caption       = "Polarity",
                AllowCancel   = true,
                SizeToContent = true
            };

            var PolarityFirst  = new TaskDialogCommandLinkButton("Polarity 1");
            var PolaritySecond = new TaskDialogCommandLinkButton("Polarity 2");
            var closebutton    = new TaskDialogCommandLinkButton("Close", allowCloseDialog: false);

            newPage.Buttons.Add(PolarityFirst);
            newPage.Buttons.Add(PolaritySecond);
            newPage.Buttons.Add(closebutton);

            int pol = 0;

            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\usbc", true))
            {
                if (key != null && key.GetValueNames().Any(x => x.ToLower() == "polarity"))
                {
                    pol = (int)key.GetValue("Polarity");
                }
            }

            if (pol == 0)
            {
                PolarityFirst.Enabled = false;
            }
            else
            {
                PolaritySecond.Enabled = false;
            }

            PolarityFirst.Click += (object sender2, EventArgs args2) =>
            {
                using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\usbc", true))
                {
                    key.SetValue("Polarity", 0, RegistryValueKind.DWord);
                }
                RebootDevice();
            };

            PolaritySecond.Click += (object sender2, EventArgs args2) =>
            {
                using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\usbc", true))
                {
                    key.SetValue("Polarity", 1, RegistryValueKind.DWord);
                }
                RebootDevice();
            };

            closebutton.Click += (object sender2, EventArgs args2) =>
            {
                newPage.Navigate(origpage);
            };

            origpage.Navigate(newPage);
        }
コード例 #5
0
        private void ShowEventsDemoTaskDialog()
        {
            var page1 = new TaskDialogPage()
            {
                Caption = Text,
                Heading = "Event Demo",
                Text    = "Event Demo...",
            };

            page1.Created     += (s, e) => Console.WriteLine("Page1 Created");
            page1.Destroyed   += (s, e) => Console.WriteLine("Page1 Destroyed");
            page1.HelpRequest += (s, e) => Console.WriteLine("Page1 HelpRequest");

            page1.Expander = new TaskDialogExpander("Expander")
            {
                Position = TaskDialogExpanderPosition.AfterFootnote
            };

            page1.Expander.ExpandedChanged += (s, e) => Console.WriteLine("Expander ExpandedChanged: " + page1.Expander.Expanded);

            var buttonOK              = TaskDialogButton.OK;
            var buttonHelp            = TaskDialogButton.Help;
            var buttonCancelClose     = new TaskDialogCommandLinkButton("C&ancel Close", allowCloseDialog: false);
            var buttonShowInnerDialog = new TaskDialogCommandLinkButton("&Show (modeless) Inner Dialog", "(and don't cancel the Close)");
            var buttonNavigate        = new TaskDialogCommandLinkButton("&Navigate", allowCloseDialog: false);

            page1.Buttons.Add(buttonOK);
            page1.Buttons.Add(buttonHelp);
            page1.Buttons.Add(buttonCancelClose);
            page1.Buttons.Add(buttonShowInnerDialog);
            page1.Buttons.Add(buttonNavigate);

            buttonOK.Click   += (s, e) => Console.WriteLine($"Button '{s}' Click");
            buttonHelp.Click += (s, e) => Console.WriteLine($"Button '{s}' Click");

            buttonCancelClose.Click += (s, e) =>
            {
                Console.WriteLine($"Button '{s}' Click");
            };

            buttonShowInnerDialog.Click += (s, e) =>
            {
                Console.WriteLine($"Button '{s}' Click");
                TaskDialog.ShowDialog(new TaskDialogPage()
                {
                    Text = "Inner Dialog"
                });
                Console.WriteLine($"(returns) Button '{s}' Click");
            };

            buttonNavigate.Click += (s, e) =>
            {
                Console.WriteLine($"Button '{s}' Click");

                // Navigate to a new page.
                var page2 = new TaskDialogPage()
                {
                    Heading = "AfterNavigation.",
                    Buttons =
                    {
                        TaskDialogButton.Close
                    }
                };
                page2.Created   += (s, e) => Console.WriteLine("Page2 Created");
                page2.Destroyed += (s, e) => Console.WriteLine("Page2 Destroyed");

                page1.Navigate(page2);
            };

            page1.Verification = new TaskDialogVerificationCheckBox("&CheckBox1");
            page1.Verification.CheckedChanged += (s, e) => Console.WriteLine("CheckBox CheckedChanged: " + page1.Verification.Checked);

            var radioButton1 = page1.RadioButtons.Add("Radi&oButton1");
            var radioButton2 = page1.RadioButtons.Add("RadioB&utton2");

            radioButton1.CheckedChanged += (s, e) => Console.WriteLine("RadioButton1 CheckedChanged: " + radioButton1.Checked);
            radioButton2.CheckedChanged += (s, e) => Console.WriteLine("RadioButton2 CheckedChanged: " + radioButton2.Checked);

            var dialogResult = TaskDialog.ShowDialog(page1);

            Console.WriteLine("---> Dialog Result: " + dialogResult);
        }
コード例 #6
0
        private void ShowMultiPageTaskDialog()
        {
            // Disable the "Yes" button and only enable it when the check box is checked.
            // Also, don't close the dialog when this button is clicked.
            var initialButtonYes = TaskDialogButton.Yes;

            initialButtonYes.Enabled          = false;
            initialButtonYes.AllowCloseDialog = false;

            var initialPage = new TaskDialogPage()
            {
                Caption     = "My Application",
                Heading     = "Clean up database?",
                Text        = "Do you really want to do a clean-up?\nThis action is irreversible!",
                Icon        = TaskDialogIcon.ShieldWarningYellowBar,
                AllowCancel = true,
                // A modeless dialog can be minimizable.
                AllowMinimize = true,

                Verification = new TaskDialogVerificationCheckBox()
                {
                    Text = "I know what I'm doing"
                },

                Buttons =
                {
                    TaskDialogButton.No,
                    initialButtonYes
                },
                DefaultButton = TaskDialogButton.No
            };

            // For the "In Progress" page, don't allow the dialog to close, by adding
            // a disabled button (if no button was specified, the task dialog would
            // get an (enabled) 'OK' button).
            var inProgressCloseButton = TaskDialogButton.Close;

            inProgressCloseButton.Enabled = false;

            var inProgressPage = new TaskDialogPage()
            {
                Caption       = "My Application",
                Heading       = "Operation in progress...",
                Text          = "Please wait while the operation is in progress.",
                Icon          = TaskDialogIcon.Information,
                AllowMinimize = true,

                ProgressBar = new TaskDialogProgressBar()
                {
                    State = TaskDialogProgressBarState.Marquee
                },

                Expander = new TaskDialogExpander()
                {
                    Text     = "Initializing...",
                    Position = TaskDialogExpanderPosition.AfterFootnote
                },

                Buttons =
                {
                    inProgressCloseButton
                }
            };

            // Add an invisible Cancel button where we will intercept the Click event
            // to prevent the dialog from closing (when the User clicks the "X" button
            // in the title bar or presses ESC or Alt+F4).
            var invisibleCancelButton = TaskDialogButton.Cancel;

            invisibleCancelButton.Visible          = false;
            invisibleCancelButton.AllowCloseDialog = false;
            inProgressPage.Buttons.Add(invisibleCancelButton);

            var finishedPage = new TaskDialogPage()
            {
                Caption       = "My Application",
                Heading       = "Success!",
                Text          = "The operation finished.",
                Icon          = TaskDialogIcon.ShieldSuccessGreenBar,
                AllowMinimize = true,
                Buttons       =
                {
                    TaskDialogButton.Close
                }
            };

            TaskDialogButton showResultsButton = new TaskDialogCommandLinkButton("Show &Results");

            finishedPage.Buttons.Add(showResultsButton);

            // Enable the "Yes" button only when the checkbox is checked.
            TaskDialogVerificationCheckBox checkBox = initialPage.Verification;

            checkBox.CheckedChanged += (sender, e) =>
            {
                initialButtonYes.Enabled = checkBox.Checked;
            };

            // When the user clicks "Yes", navigate to the second page.
            initialButtonYes.Click += (sender, e) =>
            {
                // Navigate to the "In Progress" page that displays the
                // current progress of the background work.
                initialPage.Navigate(inProgressPage);

                // NOTE: When you implement a "In Progress" page that represents
                // background work that is done e.g. by a separate thread/task,
                // which eventually calls Control.Invoke()/BeginInvoke() when
                // its work is finished in order to navigate or update the dialog,
                // then DO NOT start that work here already (directly after
                // setting the Page property). Instead, start the work in the
                // TaskDialogPage.Created event of the new page.
                //
                // See comments in the code sample in https://github.com/dotnet/winforms/issues/146
                // for more information.
            };

            // Simulate work by starting an async operation from which we are updating the
            // progress bar and the expander with the current status.
            inProgressPage.Created += async(s, e) =>
            {
                // Run the background operation and iterate over the streamed values to update
                // the progress. Because we call the async method from the GUI thread,
                // it will use this thread's synchronization context to run the continuations,
                // so we don't need to use Control.[Begin]Invoke() to schedule the callbacks.
                var progressBar = inProgressPage.ProgressBar;

                await foreach (int progressValue in StreamBackgroundOperationProgressAsync())
                {
                    // When we display the first progress, switch the marquee progress bar
                    // to a regular one.
                    if (progressBar.State == TaskDialogProgressBarState.Marquee)
                    {
                        progressBar.State = TaskDialogProgressBarState.Normal;
                    }

                    progressBar.Value            = progressValue;
                    inProgressPage.Expander.Text = $"Progress: {progressValue} %";
                }

                // Work is finished, so navigate to the third page.
                inProgressPage.Navigate(finishedPage);
            };

            // Show the dialog (modeless).
            TaskDialogButton result = TaskDialog.ShowDialog(initialPage);

            if (result == showResultsButton)
            {
                Console.WriteLine("Showing Results!");
            }