Example usage:
Private Sub PicBox_Click(sender As Object, e As EventArgs) Handles PicBox.Click
Dim prevColor As Color = PicBox.BackColor
ColorDlg = New Colordialog_Realtime
ColorDlg.Color = PicBox.backcolor
ColorDlg.Title = "Please Select a New Color:"
ColorDlg.Location = New Point(Me.Location.X + Me.Width, Me.Location.Y)
If ColorDlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
PicBox.BackColor = ColorDlg.Color
Else
PicBox.BackColor = prevColor
End If
ColorDlg = Nothing
End Sub
New Colordialog_Realtime
Class:
Public Class Colordialog_Realtime
Inherits ColorDialog
Public Event CurrentColor(ByVal c As Color)
Private Const GA_ROOT As Integer = 2
Private Const WM_PAINT As Integer = &HF
Private Const WM_CTLCOLOREDIT As Integer = &H133
Public Declare Function GetAncestor Lib "user32.dll" _
(ByVal hWnd As IntPtr, ByVal gaFlags As Integer) As IntPtr
Private EditWindows As List(Of ApiWindow) = Nothing
Public Sub New()
Me.FullOpen = True
End Sub
<Runtime.InteropServices.DllImport("user32.dll")> _
Private Shared Function SetWindowText(hWnd As IntPtr, lpString As String) As Boolean
End Function
Private Const SWP_NOSIZE As Integer = &H1
Private Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" _
(ByVal hwnd As IntPtr, ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
Private m_title As String = String.Empty
Private titleSet As Boolean = False
Public Property Title() As String
Get
Return m_title
End Get
Set(value As String)
If value IsNot Nothing AndAlso value <> m_title Then
m_title = value
titleSet = False
End If
End Set
End Property
Private m_location As Point = Point.Empty
Private locationSet As Boolean = False
Public Property Location() As Point
Get
Return m_location
End Get
Set(value As Point)
If Not value.Equals(Point.Empty) AndAlso Not value.Equals(m_location) Then
m_location = value
locationSet = False
End If
End Set
End Property
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Protected Overrides Function HookProc(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
Select Case msg
Case WM_PAINT
If Not titleSet AndAlso Title <> String.Empty Then
SetWindowText(GetAncestor(hWnd, GA_ROOT), Title)
titleSet = True
End If
If Not locationSet AndAlso Not m_location.Equals(Point.Empty) Then
SetWindowPos(GetAncestor(hWnd, GA_ROOT), 0, m_location.X, m_location.Y, 0, 0, SWP_NOSIZE)
locationSet = True
End If
Case WM_CTLCOLOREDIT
If IsNothing(EditWindows) Then
Dim mainWindow As IntPtr = GetAncestor(hWnd, GA_ROOT)
If Not mainWindow.Equals(IntPtr.Zero) Then
EditWindows = New List(Of ApiWindow)((New WindowsEnumerator).GetChildWindows(mainWindow, "Edit"))
End If
End If
If Not IsNothing(EditWindows) AndAlso EditWindows.Count = 6 Then
Dim strRed As String = WindowsEnumerator.WindowText(EditWindows(3).hWnd)
Dim strGreen As String = WindowsEnumerator.WindowText(EditWindows(4).hWnd)
Dim strBlue As String = WindowsEnumerator.WindowText(EditWindows(5).hWnd)
Dim Red, Green, Blue As Integer
If Integer.TryParse(strRed, Red) Then
If Integer.TryParse(strGreen, Green) Then
If Integer.TryParse(strBlue, Blue) Then
RaiseEvent CurrentColor(Color.FromArgb(Red, Green, Blue))
End If
End If
End If
End If
End Select
Return MyBase.HookProc(hWnd, msg, wParam, lParam)
End Function
End Class