Imports System.Runtime.InteropServices
''' <summary>
''' Extends the ColorDialog class and allows the initial location of the window to be specified.
''' </summary>
Public Class ColorDialogEx
Inherits ColorDialog
#Region " Types "
''' <summary>
''' Defines values that can be assigned to the Flags field of a WindowPos object.
''' </summary>
<Flags()> _
Private Enum WindowPosFlags As UInteger
NoResize = &H1
NoMove = &H2
NoZOrder = &H4
NoRedraw = &H8
NoActivate = &H10
FrameChanged = &H20
ShowWindow = &H40
HideWindow = &H80
NoCopyBits = &H100
NoOwnerZOrder = &H200
NoSendChanging = &H400
DeferErase = &H2000
AsyncWindowPos = &H4000
End Enum
''' <summary>
''' Received as the lParam value when a WM_WINDOWPOSCHANGING message is received.
''' </summary>
Private Structure WindowPos
Public HWnd As IntPtr
Public HWndInsertAfter As IntPtr
Public X As Integer
Public Y As Integer
Public Cx As Integer
Public Cy As Integer
Public Flags As WindowPosFlags
End Structure
#End Region 'Types
#Region " Constants "
''' <summary>
''' The message received when the window position is changing.
''' </summary>
Private Const WM_WINDOWPOSCHANGING As Integer = &H46
#End Region 'Constants
#Region " Variables "
''' <summary>
''' The location at which the window will be initially displayed.
''' </summary>
''' <remarks>
''' If no value is assigned the window is displayed at the default location.
''' </remarks>
Private initialLocation As Nullable(Of Point) = Nothing
#End Region 'Variables
#Region " Constructors "
''' <summary>
''' Creates a new instance of the ColorDialogEx class.
''' </summary>
Public Sub New()
MyBase.New()
End Sub
''' <summary>
''' Creates a new instance of the ColorDialogEx class.
''' </summary>
''' <param name="initialLocation">
''' The location at which the window will be initially dispalyed.
''' </param>
Public Sub New(ByVal initialLocation As Point)
Me.New()
Me.initialLocation = initialLocation
End Sub
#End Region 'Constructors
#Region " Methods "
''' <summary>
''' Hooks into the common dialog's message queue.
''' </summary>
''' <param name="hWnd">
''' The handle to the native window.
''' </param>
''' <param name="msg">
''' The message received from the operating system.
''' </param>
''' <param name="wparam">
''' Message-specific data.
''' </param>
''' <param name="lparam">
''' Message-specific data.
''' </param>
''' <returns>
''' A zero value if the default dialog box procedure processes the message; a nonzero value if the default dialog box procedure ignores the message.
''' </returns>
Protected Overrides Function HookProc(ByVal hWnd As System.IntPtr, _
ByVal msg As Integer, _
ByVal wparam As System.IntPtr, _
ByVal lparam As System.IntPtr) As System.IntPtr
'Trap the WM_WINDOWPOSCHANGING message if an initial location has been specified.
If msg = WM_WINDOWPOSCHANGING AndAlso Me.initialLocation.HasValue Then
'Get the data provided by the OS.
Dim pos As WindowPos = DirectCast(Marshal.PtrToStructure(lparam, _
GetType(WindowPos)), _
WindowPos)
'Look for the specific combination of flags that indicates that
'this is the appropriate message on which to set the location.
If pos.Flags = (WindowPosFlags.NoResize Or _
WindowPosFlags.NoZOrder Or _
WindowPosFlags.NoActivate) Then
With Me.initialLocation.Value
pos.X = .X
pos.Y = .Y
End With
'Make the new data available to unmanaged code.
Marshal.StructureToPtr(pos, lparam, True)
End If
End If
'Pass on the message.
Return MyBase.HookProc(hWnd, msg, wparam, lparam)
End Function
#End Region 'Methods
End Class