home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CommonDial18020462001.psc / CmnDialog.bas < prev    next >
Encoding:
BASIC Source File  |  2001-04-07  |  10.9 KB  |  264 lines

  1. Attribute VB_Name = "CmnDialog"
  2. Option Explicit
  3. 'Win 2K compliant FileExists
  4. Private Const INVALID_HANDLE_VALUE = -1
  5. Private Const MAX_PATH = 260
  6. Private Type FILETIME
  7.     dwLowDateTime As Long
  8.     dwHighDateTime As Long
  9. End Type
  10. Private Type WIN32_FIND_DATA
  11.     dwFileAttributes As Long
  12.     ftCreationTime As FILETIME
  13.     ftLastAccessTime As FILETIME
  14.     ftLastWriteTime As FILETIME
  15.     nFileSizeHigh As Long
  16.     nFileSizeLow As Long
  17.     dwReserved0 As Long
  18.     dwReserved1 As Long
  19.     cFileName As String * MAX_PATH
  20.     cAlternate As String * 14
  21. End Type
  22. Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
  23. Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
  24. Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
  25. 'API to manipulate windows and draw pics
  26. Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
  27. Public Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
  28. Private Declare Function GetFullPathName Lib "kernel32" Alias "GetFullPathNameA" (ByVal lpFileName As String, ByVal nBufferLength As Long, ByVal lpBuffer As String, ByVal lpFilePart As String) As Long
  29. Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
  30. Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
  31. Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
  32. Private Declare Sub 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)
  33. Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
  34. Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
  35. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
  36. Private Declare Function SendMessageByString Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
  37. Private Type RECT
  38.    Left As Long
  39.    Top As Long
  40.    Right As Long
  41.    Bottom As Long
  42. End Type
  43. 'Standard API for commondialog
  44. Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
  45. Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
  46. Public Const ScrCopy = &HCC0020
  47. Const OFN_ALLOWMULTISELECT As Long = &H200
  48. Const OFN_CREATEPROMPT As Long = &H2000
  49. Const OFN_ENABLEHOOK As Long = &H20
  50. Const OFN_ENABLETEMPLATE As Long = &H40
  51. Const OFN_ENABLETEMPLATEHANDLE As Long = &H80
  52. Const OFN_EXPLORER As Long = &H80000
  53. Const OFN_EXTENSIONDIFFERENT As Long = &H400
  54. Const OFN_FILEMUSTEXIST As Long = &H1000
  55. Const OFN_HIDEREADONLY As Long = &H4
  56. Const OFN_LONGNAMES As Long = &H200000
  57. Const OFN_NOCHANGEDIR As Long = &H8
  58. Const OFN_NODEREFERENCELINKS As Long = &H100000
  59. Const OFN_NOLONGNAMES As Long = &H40000
  60. Const OFN_NONETWORKBUTTON As Long = &H20000
  61. Const OFN_NOREADONLYRETURN As Long = &H8000&
  62. Const OFN_NOTESTFILECREATE As Long = &H10000
  63. Const OFN_NOVALIDATE As Long = &H100
  64. Const OFN_OVERWRITEPROMPT As Long = &H2
  65. Const OFN_PATHMUSTEXIST As Long = &H800
  66. Const OFN_READONLY As Long = &H1
  67. Const OFN_SHAREAWARE As Long = &H4000
  68. Const OFN_SHAREFALLTHROUGH As Long = 2
  69. Const OFN_SHAREWARN As Long = 0
  70. Const OFN_SHARENOWARN As Long = 1
  71. Const OFN_SHOWHELP As Long = &H10
  72. Const OFS_MAXPATHNAME As Long = 260
  73. Const OFN_SELECTED As Long = &H78
  74. Const WM_INITDIALOG = &H110
  75. Const SW_SHOWNORMAL = 1
  76. Const HWND_TOPMOST = -1
  77. Const HWND_NOTOPMOST = -2
  78. Const SWP_NOSIZE = &H1
  79. Const SWP_NOMOVE = &H2
  80. Const SWP_NOACTIVATE = &H10
  81. Const SWP_SHOWWINDOW = &H40
  82. Const GW_NEXT = 2
  83. Const GW_CHILD = 5
  84. Const WM_GETTEXT = &HD
  85. Const WM_GETTEXTLENGTH = &HE
  86. Private Type OPENFILENAME
  87.     lStructSize As Long
  88.     hwndOwner As Long
  89.     hInstance As Long
  90.     lpstrFilter As String
  91.     lpstrCustomFilter As String
  92.     nMaxCustFilter As Long
  93.     nFilterIndex As Long
  94.     lpstrFile As String
  95.     nMaxFile As Long
  96.     lpstrFileTitle As String
  97.     nMaxFileTitle As Long
  98.     lpstrInitialDir As String
  99.     lpstrTitle As String
  100.     Flags As Long
  101.     nFileOffset As Integer
  102.     nFileExtension As Integer
  103.     lpstrDefExt As String
  104.     lCustData As Long
  105.     lpfnHook As Long
  106.     lpTemplateName As String
  107. End Type
  108. Dim OFN As OPENFILENAME
  109.  
  110. 'Variables
  111. Dim cdlhwnd As Long 'commondialog handle
  112. Public ThePic As PictureBox 'Hidden picturebox on UserControl
  113. Public TheTimer As Timer 'Timer on UserControl
  114. Public hwndParent As Long
  115. Public mDC As Long
  116. Public mCdlW As Long
  117. Public mCdlH As Long
  118. Public tmpFilename As String
  119. Public tmpFullname As String
  120. Dim ShowPics As Boolean
  121. Public Function ShowOpen(hParent As Long, Optional mFilter As String, Optional mflags As Long, Optional mInitDir As String, Optional mTitle As String, Optional Pictures As Boolean) As String
  122. 'standard open file code
  123.     If mInitDir = "" Then mInitDir = "c:\"
  124.     If mFilter = "" Then mFilter = "All Files (*.*)" + Chr$(0) + "*.*" + Chr$(0)
  125.     If mTitle = "" Then mTitle = App.Title
  126.     ShowPics = Pictures
  127.     OFN.lStructSize = Len(OFN)
  128.     OFN.hwndOwner = hParent
  129.     OFN.hInstance = App.hInstance
  130.     OFN.lpstrFilter = mFilter
  131.     OFN.lpstrFile = Space$(254)
  132.     OFN.nMaxFile = 255
  133.     OFN.lpstrFileTitle = Space$(254)
  134.     OFN.nMaxFileTitle = 255
  135.     OFN.lpstrInitialDir = mInitDir
  136.     OFN.lpstrTitle = mTitle
  137.     OFN.Flags = mflags Or OFN_ENABLEHOOK Or OFN_EXPLORER
  138.     OFN.lpfnHook = DummyProc(AddressOf CdlgHook)
  139.     If GetOpenFileName(OFN) Then
  140.         ShowOpen = Trim$(OFN.lpstrFile)
  141.     Else
  142.         ShowOpen = ""
  143.     End If
  144. End Function
  145. Public Function ShowSave(hParent As Long, Optional mFilter As String, Optional mflags As Long, Optional mInitDir As String, Optional mTitle As String) As String
  146. 'standard save file code
  147.     If mInitDir = "" Then mInitDir = "c:\"
  148.     If mFilter = "" Then mFilter = "All Files (*.*)" + Chr$(0) + "*.*" + Chr$(0)
  149.     If mTitle = "" Then mTitle = App.Title
  150.     ShowPics = False
  151.     OFN.lStructSize = Len(OFN)
  152.     OFN.hwndOwner = hParent
  153.     OFN.hInstance = App.hInstance
  154.     OFN.lpstrFilter = mFilter
  155.     OFN.lpstrFile = Space$(254)
  156.     OFN.nMaxFile = 255
  157.     OFN.lpstrFileTitle = Space$(254)
  158.     OFN.nMaxFileTitle = 255
  159.     OFN.lpstrInitialDir = mInitDir
  160.     OFN.lpstrTitle = mTitle
  161.     OFN.Flags = mflags Or OFN_ENABLEHOOK Or OFN_EXPLORER
  162.     OFN.lpfnHook = DummyProc(AddressOf CdlgHook)
  163.     If GetSaveFileName(OFN) Then
  164.         ShowSave = Trim$(OFN.lpstrFile)
  165.     Else
  166.         ShowSave = ""
  167.     End If
  168. End Function
  169. Private Function CdlgHook(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  170. Dim hwnda As Long, ClWind As String * 5
  171. Dim Buffer As String, Ret As Long
  172. Dim R As RECT
  173. Dim NewCdlL As Long
  174. Dim NewCdlT As Long
  175. Dim scrWidth As Long
  176. Dim scrHeight As Long
  177. If ShowPics Then 'hook only used for Showing Pictures
  178.     Select Case uMsg
  179.         Case WM_INITDIALOG
  180.             hwndParent = GetParent(hwnd)
  181.             mDC = GetDC(hwndParent)
  182.             cdlhwnd = hwndParent
  183.             If hwndParent <> 0 Then
  184.                 Call GetWindowRect(hwndParent, R)
  185.                 mCdlW = R.Right - R.Left
  186.                 mCdlH = R.Bottom - R.Top
  187.                 'centre the dialog
  188.                 scrWidth = Screen.Width \ Screen.TwipsPerPixelX
  189.                 scrHeight = Screen.Height \ Screen.TwipsPerPixelY
  190.                 NewCdlL = (scrWidth - mCdlW) \ 2
  191.                 NewCdlT = (scrHeight - mCdlH) \ 2
  192.                 'Resize dialog to accomodate pictures
  193.                 Call MoveWindow(hwndParent, NewCdlL, NewCdlT, mCdlW, mCdlH + 107, True)
  194.                 CdlgHook = 1
  195.             End If
  196.         Case 78 'selection has changed
  197.             hwndParent = GetParent(hwnd)
  198.             hwnda = GetWindow(hwndParent, GW_CHILD)
  199.             Do While hwnda <> 0
  200.                 GetClassName hwnda, ClWind, 5
  201.                 'Whats the filename ?
  202.                 If Left(ClWind, 4) = "Edit" Then
  203.                     tmpFilename = gettext(hwnda)
  204.                     Exit Do
  205.                 End If
  206.                 hwnda = GetWindow(hwnda, GW_NEXT)
  207.             Loop
  208.             If tmpFilename <> "" Then
  209.                 Buffer = Space(255)
  210.                 'Get a path
  211.                 Ret = GetFullPathName(tmpFilename, 255, Buffer, "")
  212.                 Buffer = Left(Buffer, Ret)
  213.                 If FileExists(Buffer) Then
  214.                     tmpFullname = Buffer
  215.                     'Empty the picturebox
  216.                     ThePic.Picture = LoadPicture()
  217.                     'Paint the dialog to clear old data
  218.                     StretchBlt mDC, 20, 250, mCdlW - 40, 80, ThePic.hdc, 0, 0, ThePic.ScaleWidth, ThePic.ScaleHeight, ScrCopy
  219.                     'load the new picture - The UserControls' timer will take care of painting
  220.                     ThePic.Picture = LoadPicture(Buffer)
  221.                 End If
  222.             End If
  223.         'cancel or close was pressed so bailout at this point
  224.         Case 2
  225.             TheTimer.Enabled = False
  226.         Case 130
  227.             TheTimer.Enabled = False
  228.         Case Else
  229.     End Select
  230. End If
  231. End Function
  232. Private Function gettext(lngwindow As Long) As String
  233. 'Used to read the filename from the dialog
  234.     Dim strbuffer As String, lngtextlen As Long
  235.     Let lngtextlen& = SendMessage(lngwindow&, WM_GETTEXTLENGTH, 0&, 0&)
  236.     Let strbuffer$ = String(lngtextlen&, 0&)
  237.     Call SendMessageByString(lngwindow&, WM_GETTEXT, lngtextlen& + 1&, strbuffer$)
  238.     Let gettext$ = strbuffer$
  239. End Function
  240. Private Function FileExists(sSource As String) As Boolean
  241. If Right(sSource, 2) = ":\" Then
  242.     Dim allDrives As String
  243.     allDrives = Space$(64)
  244.     Call GetLogicalDriveStrings(Len(allDrives), allDrives)
  245.     FileExists = InStr(1, allDrives, Left(sSource, 1), 1) > 0
  246.     Exit Function
  247. Else
  248.     If Not sSource = "" Then
  249.         Dim WFD As WIN32_FIND_DATA
  250.         Dim hFile As Long
  251.         hFile = FindFirstFile(sSource, WFD)
  252.         FileExists = hFile <> INVALID_HANDLE_VALUE
  253.         Call FindClose(hFile)
  254.     Else
  255.         FileExists = False
  256.     End If
  257. End If
  258. End Function
  259. Private Function DummyProc(ByVal dProc As Long) As Long
  260. 'Used to implement the hook
  261.   DummyProc = dProc
  262. End Function
  263.  
  264.