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

  1. Attribute VB_Name = "CmnDialog"
  2. Option Explicit
  3. Const FW_NORMAL = 400
  4. Const DEFAULT_CHARSET = 1
  5. Const OUT_DEFAULT_PRECIS = 0
  6. Const CLIP_DEFAULT_PRECIS = 0
  7. Const DEFAULT_QUALITY = 0
  8. Const DEFAULT_PITCH = 0
  9. Const FF_ROMAN = 16
  10. Const CF_PRINTERFONTS = &H2
  11. Const CF_SCREENFONTS = &H1
  12. Const CF_BOTH = (CF_SCREENFONTS Or CF_PRINTERFONTS)
  13. Const CF_EFFECTS = &H100&
  14. Const CF_FORCEFONTEXIST = &H10000
  15. Const CF_INITTOLOGFONTSTRUCT = &H40&
  16. Const CF_LIMITSIZE = &H2000&
  17. Const REGULAR_FONTTYPE = &H400
  18. Const LF_FACESIZE = 32
  19. Const CCHDEVICENAME = 32
  20. Const CCHFORMNAME = 32
  21. Const GMEM_MOVEABLE = &H2
  22. Const GMEM_ZEROINIT = &H40
  23. Const DM_DUPLEX = &H1000&
  24. Const DM_ORIENTATION = &H1&
  25. Const PD_PRINTSETUP = &H40
  26. Const PD_DISABLEPRINTTOFILE = &H80000
  27. Private Type OPENFILENAME
  28.     lStructSize As Long
  29.     hwndOwner As Long
  30.     hInstance As Long
  31.     lpstrFilter As String
  32.     lpstrCustomFilter As String
  33.     nMaxCustFilter As Long
  34.     nFilterIndex As Long
  35.     lpstrFile As String
  36.     nMaxFile As Long
  37.     lpstrFileTitle As String
  38.     nMaxFileTitle As Long
  39.     lpstrInitialDir As String
  40.     lpstrTitle As String
  41.     flags As Long
  42.     nFileOffset As Integer
  43.     nFileExtension As Integer
  44.     lpstrDefExt As String
  45.     lCustData As Long
  46.     lpfnHook As Long
  47.     lpTemplateName As String
  48. End Type
  49. Private Type CHOOSECOLOR
  50.     lStructSize As Long
  51.     hwndOwner As Long
  52.     hInstance As Long
  53.     rgbResult As Long
  54.     lpCustColors As String
  55.     flags As Long
  56.     lCustData As Long
  57.     lpfnHook As Long
  58.     lpTemplateName As String
  59. End Type
  60. Private Type LOGFONT
  61.         lfHeight As Long
  62.         lfWidth As Long
  63.         lfEscapement As Long
  64.         lfOrientation As Long
  65.         lfWeight As Long
  66.         lfItalic As Byte
  67.         lfUnderline As Byte
  68.         lfStrikeOut As Byte
  69.         lfCharSet As Byte
  70.         lfOutPrecision As Byte
  71.         lfClipPrecision As Byte
  72.         lfQuality As Byte
  73.         lfPitchAndFamily As Byte
  74.         lfFaceName As String * 31
  75. End Type
  76. Private Type CHOOSEFONT
  77.         lStructSize As Long
  78.         hwndOwner As Long
  79.         hDC As Long
  80.         lpLogFont As Long
  81.         iPointSize As Long
  82.         flags As Long
  83.         rgbColors As Long
  84.         lCustData As Long
  85.         lpfnHook As Long
  86.         lpTemplateName As String
  87.         hInstance As Long
  88.         lpszStyle As String
  89.         nFontType As Integer
  90.         MISSING_ALIGNMENT As Integer
  91.         nSizeMin As Long
  92.         nSizeMax As Long
  93. End Type
  94. Private Declare Function CHOOSECOLOR Lib "comdlg32.dll" Alias "ChooseColorA" (pChoosecolor As CHOOSECOLOR) As Long
  95. Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
  96. Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
  97. Private Declare Function CHOOSEFONT Lib "comdlg32.dll" Alias "ChooseFontA" (pChoosefont As CHOOSEFONT) As Long
  98. Private Declare Sub CopyMemory Lib "Kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
  99. Private Declare Function GlobalLock Lib "Kernel32" (ByVal hMem As Long) As Long
  100. Private Declare Function GlobalUnlock Lib "Kernel32" (ByVal hMem As Long) As Long
  101. Private Declare Function GlobalAlloc Lib "Kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
  102. Private Declare Function GlobalFree Lib "Kernel32" (ByVal hMem As Long) As Long
  103. Dim OFName As OPENFILENAME
  104. Dim mHwnd As Long
  105. Dim CustomColors() As Byte
  106. Private Declare Function ShellExecute Lib "shell32.dll" Alias _
  107.     "ShellExecuteA" (ByVal Hwnd As Long, ByVal lpOperation _
  108.     As String, ByVal lpFile As String, ByVal lpParameters _
  109.     As String, ByVal lpDirectory As String, ByVal nShowCmd _
  110.     As Long) As Long
  111. Private Const BIF_STATUSTEXT = &H4&
  112. Private Const BIF_RETURNONLYFSDIRS = 1
  113. Private Const BIF_DONTGOBELOWDOMAIN = 2
  114. Private Const MAX_PATH = 260
  115. Private Const WM_USER = &H400
  116. Private Const BFFM_INITIALIZED = 1
  117. Private Const BFFM_SELCHANGED = 2
  118. Private Const BFFM_SETSTATUSTEXT = (WM_USER + 100)
  119. Private Const BFFM_SETSELECTION = (WM_USER + 102)
  120. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal Hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
  121. Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
  122. Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
  123. Private Declare Function lstrcat Lib "Kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
  124. Private Type BrowseInfo
  125.   hwndOwner      As Long
  126.   pIDLRoot       As Long
  127.   pszDisplayName As Long
  128.   lpszTitle      As Long
  129.   ulFlags        As Long
  130.   lpfnCallback   As Long
  131.   lParam         As Long
  132.   iImage         As Long
  133. End Type
  134. Private m_CurrentDirectory As String
  135. Public mFontName As String
  136. Public mFontsize As Integer
  137. Public mBold As Boolean
  138. Public mItalic As Boolean
  139. Public mUnderline As Boolean
  140. Public mStrikethru As Boolean
  141. Public mFontColor As Long
  142. Public mFilterIndex As Integer
  143.  
  144.  
  145. Public Function BrowseForFolder(Optional StartDir As String, Optional mTitle As String) As String
  146.   Dim lpIDList As Long
  147.   Dim szTitle As String
  148.   Dim sBuffer As String
  149.   Dim tBrowseInfo As BrowseInfo
  150.   If StartDir = "" Then StartDir = "c:\"
  151.   If mTitle = "" Then mTitle = App.Title + " : Select a Folder"
  152.   m_CurrentDirectory = StartDir & vbNullChar
  153.   szTitle = mTitle
  154.   With tBrowseInfo
  155.     .hwndOwner = mHwnd
  156.     .lpszTitle = lstrcat(szTitle, "")
  157.     .ulFlags = BIF_RETURNONLYFSDIRS + BIF_DONTGOBELOWDOMAIN + BIF_STATUSTEXT
  158.     .lpfnCallback = GetAddressofFunction(AddressOf BrowseCallbackProc)
  159.   End With
  160.   lpIDList = SHBrowseForFolder(tBrowseInfo)
  161.   If (lpIDList) Then
  162.     sBuffer = Space(MAX_PATH)
  163.     SHGetPathFromIDList lpIDList, sBuffer
  164.     sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1)
  165.     BrowseForFolder = sBuffer
  166.   Else
  167.     BrowseForFolder = ""
  168.   End If
  169. End Function
  170. Private Function BrowseCallbackProc(ByVal Hwnd As Long, ByVal uMsg As Long, ByVal lp As Long, ByVal pData As Long) As Long
  171. Dim lpIDList As Long
  172. Dim ret As Long
  173. Dim sBuffer As String
  174. On Error Resume Next
  175. Select Case uMsg
  176.   Case BFFM_INITIALIZED
  177.     Call SendMessage(Hwnd, BFFM_SETSELECTION, 1, m_CurrentDirectory)
  178.   Case BFFM_SELCHANGED
  179.     sBuffer = Space(MAX_PATH)
  180.     ret = SHGetPathFromIDList(lp, sBuffer)
  181.     If ret = 1 Then
  182.       Call SendMessage(Hwnd, BFFM_SETSTATUSTEXT, 0, sBuffer)
  183.     End If
  184. End Select
  185. BrowseCallbackProc = 0
  186. End Function
  187. Private Function GetAddressofFunction(add As Long) As Long
  188.   GetAddressofFunction = add
  189. End Function
  190.  
  191.  
  192. Public Sub MyFind(initDir As String)
  193. ShellExecute 0, "find", initDir, vbNullString, vbNullString, 5
  194.  
  195. End Sub
  196. Public Sub InitCmnDlg(myhwnd As Long)
  197. 'Required to use custom colors
  198. ReDim CustomColors(0 To 16 * 4 - 1) As Byte
  199. Dim i As Integer
  200. For i = LBound(CustomColors) To UBound(CustomColors)
  201.     CustomColors(i) = 0
  202. Next i
  203. 'need a window handle to run the functions
  204. mHwnd = myhwnd
  205. End Sub
  206.  
  207. Public Function ShowColor() As Long
  208.     Dim cc As CHOOSECOLOR
  209.     Dim Custcolor(16) As Long
  210.     Dim lReturn As Long
  211.     cc.lStructSize = Len(cc)
  212.     cc.hwndOwner = mHwnd
  213.     cc.hInstance = App.hInstance
  214.     cc.lpCustColors = StrConv(CustomColors, vbUnicode)
  215.     cc.flags = 0
  216.     If CHOOSECOLOR(cc) <> 0 Then
  217.         ShowColor = cc.rgbResult
  218.         CustomColors = StrConv(cc.lpCustColors, vbFromUnicode)
  219.     Else
  220.         ShowColor = -1
  221.     End If
  222. End Function
  223. Public Function ShowOpen(Optional mFilter As String, Optional mflags As Long, Optional mInitDir As String, Optional mTitle As String) As String
  224.     If mInitDir = "" Then mInitDir = "c:\"
  225.     If mFilter = "" Then mFilter = "All Files (*.*)" + Chr(0) + "*.*" + Chr(0)
  226.     If mTitle = "" Then mTitle = App.Title
  227.     OFName.lStructSize = Len(OFName)
  228.     OFName.hwndOwner = mHwnd
  229.     OFName.hInstance = App.hInstance
  230.     OFName.lpstrFilter = mFilter
  231.     OFName.lpstrFile = Space$(254)
  232.     OFName.nMaxFile = 255
  233.     OFName.lpstrFileTitle = Space$(254)
  234.     OFName.nMaxFileTitle = 255
  235.     OFName.lpstrInitialDir = mInitDir
  236.     OFName.lpstrTitle = mTitle
  237.     OFName.flags = mflags
  238.     If GetOpenFileName(OFName) Then
  239.         ShowOpen = StripTerminator(OFName.lpstrFile)
  240.     Else
  241.         ShowOpen = ""
  242.     End If
  243. End Function
  244. Public Function ShowFont() As Boolean
  245.     Dim cf As CHOOSEFONT, lfont As LOGFONT, hMem As Long, pMem As Long
  246.     Dim RetVal As Long
  247.     mFontName = ""
  248.     lfont.lfHeight = 0
  249.     lfont.lfWidth = 0
  250.     lfont.lfEscapement = 0
  251.     lfont.lfOrientation = 0
  252.     lfont.lfWeight = FW_NORMAL
  253.     lfont.lfCharSet = DEFAULT_CHARSET
  254.     lfont.lfOutPrecision = OUT_DEFAULT_PRECIS
  255.     lfont.lfClipPrecision = CLIP_DEFAULT_PRECIS
  256.     lfont.lfQuality = DEFAULT_QUALITY
  257.     lfont.lfPitchAndFamily = DEFAULT_PITCH Or FF_ROMAN
  258.     lfont.lfFaceName = "Times New Roman" & vbNullChar
  259.     hMem = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, Len(lfont))
  260.     pMem = GlobalLock(hMem)
  261.     CopyMemory ByVal pMem, lfont, Len(lfont)
  262.     cf.lStructSize = Len(cf)
  263.     cf.hwndOwner = Form1.Hwnd
  264.     cf.hDC = Printer.hDC
  265.     cf.lpLogFont = pMem
  266.     cf.iPointSize = 120
  267.     cf.flags = CF_BOTH Or CF_EFFECTS Or CF_FORCEFONTEXIST Or CF_INITTOLOGFONTSTRUCT Or CF_LIMITSIZE
  268.     cf.rgbColors = RGB(0, 0, 0)
  269.     cf.nFontType = REGULAR_FONTTYPE
  270.     cf.nSizeMin = 10
  271.     cf.nSizeMax = 72
  272.     RetVal = CHOOSEFONT(cf)
  273.     If RetVal <> 0 Then
  274.         ShowFont = True
  275.         CopyMemory lfont, ByVal pMem, Len(lfont)
  276.         mFontName = Left(lfont.lfFaceName, InStr(lfont.lfFaceName, vbNullChar) - 1)
  277.         mBold = False
  278.         mItalic = False
  279.         mUnderline = False
  280.         mStrikethru = False
  281.         mFontsize = cf.iPointSize / 10
  282.         If lfont.lfItalic = 255 Then mItalic = True
  283.         If lfont.lfUnderline = 255 Then mUnderline = True
  284.         If lfont.lfWeight = 700 Then mBold = True
  285.         If lfont.lfStrikeOut = 255 Then mStrikethru = True
  286.         mFontColor = cf.rgbColors
  287.     Else
  288.         ShowFont = False
  289.     End If
  290.     RetVal = GlobalUnlock(hMem)
  291.     RetVal = GlobalFree(hMem)
  292. End Function
  293. Public Function ShowSave(Optional mFilter As String, Optional mflags As Long, Optional mInitDir As String, Optional mTitle As String) As String
  294.     If mInitDir = "" Then mInitDir = "c:\"
  295.     If mFilter = "" Then mFilter = "All Files (*.*)" + Chr$(0) + "*.*" + Chr$(0)
  296.     If mTitle = "" Then mTitle = App.Title
  297.     OFName.lStructSize = Len(OFName)
  298.     OFName.hwndOwner = mHwnd
  299.     OFName.hInstance = App.hInstance
  300.     OFName.lpstrFilter = mFilter
  301.     OFName.lpstrFile = Space$(254)
  302.     OFName.nMaxFile = 255
  303.     OFName.lpstrFileTitle = Space$(254)
  304.     OFName.nMaxFileTitle = 255
  305.     OFName.lpstrInitialDir = mInitDir
  306.     OFName.lpstrTitle = mTitle
  307.     OFName.flags = mflags
  308.     
  309.     If GetSaveFileName(OFName) Then
  310.         mFilterIndex = OFName.nFilterIndex
  311.         ShowSave = StripTerminator(OFName.lpstrFile)
  312.     Else
  313.         ShowSave = ""
  314.     End If
  315. End Function
  316. Public Function StripTerminator(ByVal strString As String) As String
  317. 'gets rid of anything not required returned by API calls
  318.     Dim intZeroPos As Integer
  319.     intZeroPos = InStr(strString, Chr$(0))
  320.     If intZeroPos > 0 Then
  321.         StripTerminator = Left$(strString, intZeroPos - 1)
  322.     Else
  323.         StripTerminator = strString
  324.     End If
  325. End Function
  326.  
  327.  
  328.