Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

WPF Common TaskDialog for Vista and XP

, 30 Dec 2007
WPF custom TaskDialog that renders the same on Vista and XP. This custom dialog simplifies programming by providing a simple interface for developers that is not Operating System dependent. This solution does not use the Windows API for rendering.
customdialogsample-12-30-2007.zip
CustomDialogSample
CommonDialog
CustomDialogInformation.png
CustomDialogQuestion.png
CustomDialogShield.png
CustomDialogStop.png
CustomDialogWarning.png
My Project
Settings.settings
CustomDialogSample
My Project
Settings.settings
customdialogsample.zip
CustomDialogInformation.png
CustomDialogQuestion.png
CustomDialogShield.png
CustomDialogStop.png
CustomDialogWarning.png
Settings.settings
Settings.settings
CustomDialogSample.suo
Partial Public Class CustomDialogWindow
    Inherits System.Windows.Window

#Region " Private Declarations "

    Private _bolAeroGlassEnabled As Boolean = False
    Private _enumCustomDialogResult As CustomDialogResults = CustomDialogResults.None
    Private _intButtonsDisabledDelay As Integer
    Private _objButtonDelayTimer As System.Windows.Forms.Timer

#End Region

#Region " Public Properties "

    Public ReadOnly Property CustomDialogResult() As CustomDialogResults
        Get
            Return _enumCustomDialogResult
        End Get
    End Property

#End Region

#Region " Constructors "

    Public Sub New(ByVal intButtonsDisabledDelay As Integer)
        InitializeComponent()

        If System.Environment.OSVersion.Version.Major < 6 Then
            Me.AllowsTransparency = True
            _bolAeroGlassEnabled = False

        Else
            _bolAeroGlassEnabled = True
        End If

        _intButtonsDisabledDelay = intButtonsDisabledDelay

    End Sub

#End Region

#Region " Methods "

    Protected Overrides Sub OnSourceInitialized(ByVal e As System.EventArgs)
        MyBase.OnSourceInitialized(e)

        If _bolAeroGlassEnabled = False Then
            'no aero glass
            Me.borderCustomDialog.Background = System.Windows.SystemColors.ActiveCaptionBrush
            Me.tbCaption.Foreground = System.Windows.SystemColors.ActiveCaptionTextBrush
            Me.borderCustomDialog.CornerRadius = New CornerRadius(10, 10, 0, 0)
            Me.borderCustomDialog.Padding = New Thickness(4, 0, 4, 4)
            Me.borderCustomDialog.BorderThickness = New Thickness(0, 0, 1, 1)
            Me.borderCustomDialog.BorderBrush = System.Windows.Media.Brushes.Black

        Else

            'aero glass
            If VistaAeroAPI.ExtendGlassFrame(Me, New Thickness(0, 25, 0, 0)) = False Then
                'aero didn't work make window without glass
                Me.borderCustomDialog.Background = System.Windows.SystemColors.ActiveCaptionBrush
                Me.tbCaption.Foreground = System.Windows.SystemColors.ActiveCaptionTextBrush
                Me.borderCustomDialog.Padding = New Thickness(4, 0, 4, 4)
                Me.borderCustomDialog.BorderThickness = New Thickness(0, 0, 1, 1)
                Me.borderCustomDialog.BorderBrush = System.Windows.Media.Brushes.Black

                _bolAeroGlassEnabled = False
            End If

        End If

    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnCancel.Click
        _enumCustomDialogResult = CustomDialogResults.Cancel
        Me.DialogResult = True
    End Sub

    Private Sub btnNo_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnNo.Click
        _enumCustomDialogResult = CustomDialogResults.No
        Me.DialogResult = True
    End Sub

    Private Sub btnOK_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnOK.Click
        _enumCustomDialogResult = CustomDialogResults.OK
        Me.DialogResult = True
    End Sub

    Private Sub btnYes_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnYes.Click
        _enumCustomDialogResult = CustomDialogResults.Yes
        Me.DialogResult = True
    End Sub

    Private Sub CustomDialogWindow_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.Closing

        'this prevents ALT-F4 from closing the dialog box
        If Me.DialogResult.HasValue AndAlso Me.DialogResult.Value = True Then
            e.Cancel = False

        Else
            e.Cancel = True
        End If

    End Sub

    Private Sub CustomDialogWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        Me.tbAdditionalDetailsText.Visibility = Windows.Visibility.Collapsed

        If Me.ResizeMode <> Windows.ResizeMode.NoResize Then
            'this work around is necessary when glass is enabled and the window style is None which removes the chrome because the resize mode MUST be set to CanResize or else glass won't display
            Me.MinHeight = Me.ActualHeight
            Me.MaxHeight = Me.ActualHeight

            Me.MinWidth = Me.ActualWidth
            Me.MaxWidth = Me.ActualWidth
        End If

        If _intButtonsDisabledDelay > 0 Then
            Me.pbDisabledButtonsProgressBar.Maximum = _intButtonsDisabledDelay
            Me.pbDisabledButtonsProgressBar.IsIndeterminate = False

            Dim objDuration As New Duration(TimeSpan.FromSeconds(_intButtonsDisabledDelay))
            Dim objDoubleAnimation As New System.Windows.Media.Animation.DoubleAnimation(_intButtonsDisabledDelay, objDuration)
            Me.pbDisabledButtonsProgressBar.BeginAnimation(ProgressBar.ValueProperty, objDoubleAnimation)
            btnCancel.IsEnabled = False
            btnNo.IsEnabled = False
            btnOK.IsEnabled = False
            btnYes.IsEnabled = False
            _objButtonDelayTimer = New System.Windows.Forms.Timer
            AddHandler _objButtonDelayTimer.Tick, AddressOf OnTimedEvent
            _objButtonDelayTimer.Interval = _intButtonsDisabledDelay * 1000
            _objButtonDelayTimer.Start()

        Else
            Me.pbDisabledButtonsProgressBar.Visibility = Windows.Visibility.Collapsed
        End If

    End Sub

    Private Sub expAdditionalDetails_Collapsed(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles expAdditionalDetails.Collapsed
        Me.expAdditionalDetails.Header = "See Details"
        Me.tbAdditionalDetailsText.Visibility = Windows.Visibility.Collapsed
        Me.UpdateLayout()

        If Me.ResizeMode <> Windows.ResizeMode.NoResize Then
            Me.MaxHeight = Me.ActualHeight
        End If

    End Sub

    Private Sub expAdditionalDetails_Expanded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles expAdditionalDetails.Expanded

        If Me.ResizeMode <> Windows.ResizeMode.NoResize Then
            Me.MaxHeight = Double.PositiveInfinity
        End If

        Me.expAdditionalDetails.Header = "Hide Details"
        Me.tbAdditionalDetailsText.Visibility = Windows.Visibility.Visible
        Me.UpdateLayout()

        If Me.ResizeMode <> Windows.ResizeMode.NoResize Then
            Me.MaxHeight = Me.ActualHeight
        End If

    End Sub

    Private Sub OnTimedEvent(ByVal source As Object, ByVal e As EventArgs)
        _objButtonDelayTimer.Stop()
        _objButtonDelayTimer.Dispose()
        _objButtonDelayTimer = Nothing
        btnCancel.IsEnabled = True
        btnNo.IsEnabled = True
        btnOK.IsEnabled = True
        btnYes.IsEnabled = True
        Me.pbDisabledButtonsProgressBar.Visibility = Windows.Visibility.Collapsed
    End Sub

    Private Sub tbCaption_MouseLeftButtonDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles tbCaption.MouseLeftButtonDown
        DragMove()
    End Sub

#End Region

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Karl Shifflett
Architect Gayle Manufacturing Company
United States United States
Karl loves .NET, WPF, WCF, ASP.NET, VB.NET and C#.
 
Awards:
 
  • December 2008 VB.NET Code Project Article Award
  • 2009 Code Project MVP
  • 2008 Code Project MVP
  • 2008 Microsoft MVP - Client App Dev
  • December 2007 VB.NET Code Project Article Award
  • Gold Medal Winner at IBM's 1998 PROIV Programming Contest in Las Vegas
Click here to check out my Blog
 
Click here to learn about Mole 2010 debugging tool for Visual Studio 2010
 
Click here to read about XAML Power Toys
 

Just a grain of sand on the worlds beaches.

Follow on   Twitter

You may also be interested in...

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 30 Dec 2007
Article Copyright 2007 by Karl Shifflett
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid