|
|||||||||||||||||||||||||
|
|||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionThis article covers an important topic, dialog boxes. With the release of Vista also came the cool TaskDialog that all Vista users are getting used to. It provides a richer dialog than the standard message box dialog available on Windows XP. However, this great new feature causes a minor bump in the carpet for WPF developers. Which dialog should WPF developers use? Should WPF developers customize their dialogs for each operating system? What about application documentation? Should documentation have to cover two flavors of every dialog box? The TaskDialog clearly provides for much more information than the message box, so what to do? This is the very question that tapped on my shoulder about three months ago. So I developed a custom dialog class that works on both Vista and XP. It looks just like Vista’s TaskDialog, yet without all the messy API business. (Microsoft, please add TaskDialog to the 3.6 Framework so we WPF developers do not need to fool with the Windows API and so that it can be an operating system independent feature.) If you’re interested in looking at the Windows API TaskDialog code, you can have a look at the VistaBridge application in the Windows Vista SDK. We will look at some dialog usage examples and then have a look at some of the code. Features
UsageFor demonstration purposes, I have provided an image and corresponding code used to open the dialog. Sample One - All TaskDialog Properties SetVista Version
Windows XP Version
Dim obj As New CommonDialog.CustomDialog
obj.AdditionalDetailsText = "These are additional details"
obj.Buttons = CommonDialog.CustomDialogButtons.OKCancel
obj.ButtonsDisabledDelay = 5
obj.Caption = "The buttons are disabled for 5 seconds"
obj.DefaultButton = CommonDialog.CustomDialogResults.OK
obj.FooterIcon = CommonDialog.CustomDialogIcons.Information
obj.FooterText = "This is the footer text"
obj.InstructionHeading = "This is the instruction heading"
obj.InstructionIcon = CommonDialog.CustomDialogIcons.Question
obj.InstructionText = "Do you want to proceed with the next task?"
'show the dialog and get the result
Dim objResults As CommonDialog.CustomDialogResults = obj.Show
The above image was captured after the delay timer had completed and the user had clicked "See Details" which exposed the Comparing the two images with the You can also see how the dialog will look on a Windows XP system. Allow me to touch on the Sample Two - Standard DialogDim obj As New CommonDialog.CustomDialog
obj.Buttons = CommonDialog.CustomDialogButtons.YesNoCancel
obj.Caption = "Standard Dialog - default button No"
obj.DefaultButton = CommonDialog.CustomDialogResults.No
obj.InstructionHeading = "This is the instruction heading"
obj.InstructionIcon = CommonDialog.CustomDialogIcons.Question
obj.InstructionText = "Do you want to proceed with the next task?"
'show the dialog and get the result
Dim objResults As CommonDialog.CustomDialogResults = obj.Show
In this dialog, the Sample Three - Dialog Using Different IconsDim obj As New CommonDialog.CustomDialog
obj.AdditionalDetailsText = "Cool WPF dialog box!P"
obj.Buttons = CommonDialog.CustomDialogButtons.OK
obj.Caption = "Works with Vista and XP"
obj.DefaultButton = CommonDialog.CustomDialogResults.OK
obj.FooterIcon = CommonDialog.CustomDialogIcons.Shield
obj.FooterText = "This is a secure program"
obj.InstructionHeading = "Big Brother Is Watching"
obj.InstructionIcon = CommonDialog.CustomDialogIcons.Warning
obj.InstructionText = "Do you want to proceed with the next task?"
'show the dialog and get the result
Dim objResults As CommonDialog.CustomDialogResults = obj.Show
In this dialog, I've selected different icons and changed the text. Vista and XP DifferencesThe only difference in the way this custom dialog renders is that on Vista, the dialog takes advantage of Aero Glass and if available uses it on the CommonDialog ProjectThe above Logging FeatureThe In addition to the properties used to construct the dialog, the users response, the following properties can also be logged:
Public Function Show() As CustomDialogResults
'get the calling code information
Dim objTrace As System.Diagnostics.StackTrace = _
New System.Diagnostics.StackTrace()
If _strCallingReflectedTypeName.Length = 0 Then
_strCallingReflectedTypeName = _
objTrace.GetFrame(1).GetMethod.ReflectedType.Name
End If
If _strCallingMethodName.Length = 0 Then
_strCallingMethodName = objTrace.GetFrame(1).GetMethod.Name
End If
If _strCallingModuleName.Length = 0 Then
_strCallingModuleName = objTrace.GetFrame(1).GetMethod.Module.Name
End If
rest of the code...
The above code snippet shows how the Aero GlassThe Using Aero GlassAero Glass is a feature of Windows Vista. So XP users do not have this feature. Additionally, Vista users can turn Aero Glass off. So, when using this feature in your applications, you must be able to handle XP and Aero-off situations. 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
Protected Overrides Sub OnSourceInitialized(ByVal e As System.EventArgs)
MyBase.OnSourceInitialized(e)
'these two sections of code are different because the first one is dealing with XP
' so we need to change the Window a bit to make it look like the Vista Window
'
'
If _bolAeroGlassEnabled = False Then
'no aero glass - we are XP
Me.ResizeMode = Windows.ResizeMode.NoResize
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
'Vista is here. However, if the ExtendGlassFrame fails, we need a fallback
' plan to make our Window look good
'
'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
The first logical step when working with the Aero Glass feature is to determine the operating system version. I have elected to perform this check in the constructor using this code, When using the Aero Glass feature you need to This is also the location to handle non-Aero or Aero-off scenarios. In the case of this application, I have coded the This is an important and necessary step when implementing Aero Glass in your WPF applications. Additional ResourcesI have started a WPF Sample Series on my blog. I will be posting 3-6 WPF Sample Applications a month. I won't be posting every sample here on Code Project, but you can read still read them and download the code from my blog. CloseHope this code helps someone learn a little more about WPF. History
|
||||||||||||||||||||||||