home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / colorlab / modcolor.bas < prev    next >
Encoding:
BASIC Source File  |  1999-09-24  |  5.4 KB  |  145 lines

  1. Attribute VB_Name = "modColorDialog"
  2. Option Explicit
  3.  
  4. 'This module is what remains of a complete common dialog module by Paul Mather ;)
  5. 'http://www.planet-source-code.com/vb
  6.  
  7. Type RECT
  8.     left As Long
  9.     top As Long
  10.     Right As Long
  11.     Bottom As Long
  12. End Type
  13.  
  14. Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
  15. Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
  16. Declare Function GetCurrentThreadId Lib "kernel32" () As Long
  17. Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
  18. Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
  19. Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
  20.  
  21. Const GWL_HINSTANCE = (-6)
  22. Const SWP_NOSIZE = &H1
  23. Const SWP_NOZORDER = &H4
  24. Const SWP_NOACTIVATE = &H10
  25. Const HCBT_ACTIVATE = 5
  26. Const WH_CBT = 5
  27.  
  28. Dim hHook As Long
  29.  
  30. Declare Function ChooseColor Lib "comdlg32.dll" Alias "ChooseColorA" (pChoosecolor As CHOOSECOLORS) As Long
  31. Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
  32. Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDC As Long) As Long
  33.  
  34.  
  35. Type CHOOSECOLORS
  36.     lStructSize As Long
  37.     hwndOwner As Long
  38.     hInstance As Long
  39.     rgbResult As Long
  40.     lpCustColors As String
  41.     flags As Long
  42.     lCustData As Long
  43.     lpfnHook As Long
  44.     lpTemplateName As String
  45. End Type
  46.  
  47. Public Const CC_RGBINIT = &H1
  48. Public Const CC_FULLOPEN = &H2
  49. Public Const CC_ANYCOLOR = &H100
  50.  
  51. Public Const COLOR_FLAGS = CC_FULLOPEN Or CC_ANYCOLOR Or CC_RGBINIT
  52.  
  53. Public Type SelectedColor
  54.     oSelectedColor As OLE_COLOR
  55.     bCanceled As Boolean
  56. End Type
  57.  
  58. Public ColorDialog As CHOOSECOLORS
  59. Public cc As String
  60.  
  61. Dim ParenthWnd As Long
  62.  
  63. Public Function ShowColor(ByVal hwnd As Long, Optional ByVal centerForm As Boolean = True, Optional StartCol As OLE_COLOR = vbBlack) As SelectedColor
  64. ' This routine has only minor modifications (to persist custom colors) by Dan Redding
  65. Dim customcolors() As Byte  ' dynamic (resizable) array
  66. Dim i As Integer
  67. Dim ret As Long
  68. Dim hInst As Long
  69. Dim Thread As Long
  70.     ParenthWnd = hwnd
  71.     If ColorDialog.lpCustColors = "" Then
  72.         ReDim customcolors(0 To 16 * 4 - 1) As Byte  'resize the array
  73.     
  74.         For i = LBound(customcolors) To UBound(customcolors)
  75.           customcolors(i) = 254 ' sets all custom colors to white
  76.         Next i
  77.         
  78.         ColorDialog.lpCustColors = StrConv(customcolors, vbUnicode)  ' convert array
  79.     End If
  80.   
  81.     ColorDialog.hwndOwner = hwnd
  82.     ColorDialog.lStructSize = Len(ColorDialog)
  83.     ColorDialog.flags = COLOR_FLAGS
  84.     ColorDialog.rgbResult = StartCol
  85.  
  86.     'Set up the CBT hook
  87.     hInst = GetWindowLong(hwnd, GWL_HINSTANCE)
  88.     Thread = GetCurrentThreadId()
  89.     If centerForm = True Then
  90.         hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProcCenterForm, hInst, Thread)
  91.     Else
  92.         hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProcCenterScreen, hInst, Thread)
  93.     End If
  94.     
  95.     ret = ChooseColor(ColorDialog)
  96.     Debug.Print ColorDialog.lCustData
  97.     If ret Then
  98.         ShowColor.bCanceled = False
  99.         ShowColor.oSelectedColor = ColorDialog.rgbResult
  100.         cc = UtoH(ColorDialog.lpCustColors)
  101.         Exit Function
  102.     Else
  103.         ShowColor.bCanceled = True
  104.         ShowColor.oSelectedColor = &H0&
  105.         Exit Function
  106.     End If
  107. End Function
  108.  
  109. Private Function WinProcCenterScreen(ByVal lMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  110.     Dim rectForm As RECT, rectMsg As RECT
  111.     Dim x As Long, y As Long
  112.     If lMsg = HCBT_ACTIVATE Then
  113.         'Show the MsgBox at a fixed location (0,0)
  114.         GetWindowRect wParam, rectMsg
  115.         x = Screen.Width / Screen.TwipsPerPixelX / 2 - (rectMsg.Right - rectMsg.left) / 2
  116.         y = Screen.Height / Screen.TwipsPerPixelY / 2 - (rectMsg.Bottom - rectMsg.top) / 2
  117.         Debug.Print "Screen " & Screen.Height / 2
  118.         Debug.Print "MsgBox " & (rectMsg.Right - rectMsg.left) / 2
  119.         SetWindowPos wParam, 0, x, y, 0, 0, SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE
  120.         'Release the CBT hook
  121.         UnhookWindowsHookEx hHook
  122.     End If
  123.     WinProcCenterScreen = False
  124. End Function
  125.  
  126. Private Function WinProcCenterForm(ByVal lMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  127.     Dim rectForm As RECT, rectMsg As RECT
  128.     Dim x As Long, y As Long
  129.     'On HCBT_ACTIVATE, show the MsgBox centered over Form1
  130.     If lMsg = HCBT_ACTIVATE Then
  131.         'Get the coordinates of the form and the message box so that
  132.         'you can determine where the center of the form is located
  133.         GetWindowRect ParenthWnd, rectForm
  134.         GetWindowRect wParam, rectMsg
  135.         x = (rectForm.left + (rectForm.Right - rectForm.left) / 2) - ((rectMsg.Right - rectMsg.left) / 2)
  136.         y = (rectForm.top + (rectForm.Bottom - rectForm.top) / 2) - ((rectMsg.Bottom - rectMsg.top) / 2)
  137.         'Position the msgbox
  138.         SetWindowPos wParam, 0, x, y, 0, 0, SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE
  139.         'Release the CBT hook
  140.         UnhookWindowsHookEx hHook
  141.      End If
  142.      WinProcCenterForm = False
  143. End Function
  144.  
  145.