home *** CD-ROM | disk | FTP | other *** search
- Rem Demo for accessing Win 3.1 Common Dialogs
- Rem Author: Costas Kitsos, CIS 73667,1755
- Rem Revision: 1.00.00, July 4 1992
-
- Rem Modified: L.J. Johnson, CIS 70700,1334
- Rem Revision: 1.10.00, July 25 1992
-
- DefInt A-Z
-
- Declare Function lstrcpy Lib "Kernel" (ByVal lpDestString As Any, ByVal lpSourceString As Any) As Long
-
- Function PColors% (MyForm As Form, CError&, Flags&)
-
- MyForm.Cls
- PColors% = 0: CError& = 0
-
- Dim C As ChooseColor
- Dim Address As Long
- ReDim ClrArray(15) As Long ' Holds Custom Colors
-
- wSize = Len(ClrArray(0)) * 16 ' Size of Memory Block
-
-
- ' ----------------------------------------------------
- ' A global block is allocated to hold a copy of the
- ' custom colors
- ' ----------------------------------------------------
- MemHandle = GlobalAlloc(GHND, wSize)
- If MemHandle = 0 Then
- PColors% = 1
- Exit Function
- End If
-
- Address = GlobalLock(MemHandle)
- If Address = 0 Then
- PColors% = 2
- Exit Function
- End If
- ' ----------------------------------------------------
-
-
- ' ----------------------------------------------------
- ' Fill Custom Colors with White
- ' ----------------------------------------------------
- For i& = 0 To UBound(ClrArray)
- ClrArray(i&) = &HFFFFFF
- Next
- ' ----------------------------------------------------
-
-
- ' ----------------------------------------------------
- 'copy custom colors to the global block
- ' ----------------------------------------------------
- Call hmemcpy(ByVal Address, ClrArray(0), wSize)
- ' ----------------------------------------------------
-
-
- ' ----------------------------------------------------
- 'get ready to call ChooseColor
- ' ----------------------------------------------------
- C.lStructSize = Len(C)
- C.hwndOwner = MyForm.hwnd
- C.lpCustColors = Address
- C.RgbResult = Dialogs.BackColor
- C.Flags = Flags&
-
- Result = ChooseColor(C)
- CError& = CommDlgExtendedError()
-
- If Result = 0 Then
- PColors% = 3
- Exit Function
- End If
- ' ----------------------------------------------------
-
-
- ' ----------------------------------------------------
- ' copy the new custom colors locally
- ' ----------------------------------------------------
- Call hmemcpy(ClrArray(0), ByVal Address, wSize)
-
- OK = GlobalUnlock(MemHandle) 'Free The Memory
- OK = GlobalFree(MemHandle)
- ' ----------------------------------------------------
-
-
- ' ----------------------------------------------------
- ' Select the new color for the background
- ' Comment this out if it's distracting
- ' Print the new custom colors
- ' ----------------------------------------------------
- MyForm.BackColor = C.RgbResult
- For i& = 0 To UBound(ClrArray)
- MyForm.Print "Custom Color"; Str$(i&); ":", Hex$(ClrArray(i&))
- Next
- ' ----------------------------------------------------
-
- End Function
-
- Function PFileOpen% (MyForm As Form, FError&, Filter$, IDir$, Title$, Index%, Flags&)
-
- MyForm.Cls
- PFileOpen% = 0: SaveError% = 0
-
- Dim O As OPENFILENAME
- Dim Address As Long
-
- ' ----------------------------------------------------
- ' First Copy the strings to the Global Memory Block
- ' Use a sub-allocation scheme to avoid overloading
- ' the LDT
- ' ----------------------------------------------------
- szFile$ = String$(256, 0)
-
- szFilter$ = Filter$
- szInitialDir$ = IDir$
- szTitle$ = Title$
-
- wSize = Len(szFile$) + Len(szFilter$) + Len(szInitialDir$) + Len(szTitle$)
-
- MemHandle = GlobalAlloc(GHND, wSize)
-
- If MemHandle = 0 Then
- PFileOpen% = 1
- Exit Function
- End If
- ' ----------------------------------------------------
-
-
- ' ----------------------------------------------------
- ' Lock global memory, then copy it to local memory
- ' ----------------------------------------------------
- Address = GlobalLock(MemHandle)
- If Address = 0 Then
- PFileOpen% = 2
- Exit Function
- Else
- Call hmemcpy(ByVal Address, ByVal (szFile$ + szFilter$ + szInitialDir$ + szTitle$), wSize)
- End If
- ' ----------------------------------------------------
-
- O.lStructSize = Len(O)
- O.hwndOwner = MyForm.hwnd
- O.Flags = Flags&
- O.nFilterIndex = Index%
- O.lpstrFile = Address
- O.nMaxFile = Len(szFile$)
- O.lpstrFilter = Address + Len(szFile$)
- O.lpstrInitialDir = O.lpstrFilter + Len(szFilter$)
- O.lpstrTitle = O.lpstrInitialDir + Len(szInitialDir$)
-
- Result = GetOpenFileName(O)
- FError& = CommDlgExtendedError()
-
- If Result = 0 Then
- PFileOpen% = 3
- Else
- Call hmemcpy(ByVal szFile$, ByVal Address, Len(szFile$))
- End If
-
- OK = GlobalUnlock(MemHandle) 'Free The Memory
- OK = GlobalFree(MemHandle)
-
- If Result = 0 Then Exit Function
-
- File$ = Left$(szFile$, InStr(szFile$, Chr$(0)) - 1)
- MyForm.Print "Common Dialogs File Open"
- MyForm.Print
- MyForm.Print "You selected:", File$
- MyForm.Print "Path:", Left$(File$, O.nFileOffset)
- MyForm.Print "Filename:", Right$(File$, Len(File$) - O.nFileOffset)
- MyForm.Print "Extension:", Right$(File$, Len(File$) - O.nFileExtension)
-
- End Function
-
- Function PFileSave% (MyForm As Form, FError&, Filter$, IDir$, FileMask$, Index%, Title$, Flags&)
-
- MyForm.Cls
- PFileSave% = 0: FError& = 0
-
- ' This is similar to GetOpenFileName
- Dim S As OPENFILENAME
- Dim Address As Long
-
- ' ----------------------------------------------------
- ' First Copy the strings to the Global Memory Block
- ' Use a sub-allocation scheme to avoid wearing down
- ' the LDT
- ' ----------------------------------------------------
- NoTitle$ = FileMask$
- szFile$ = NoTitle$ + String$(256 - Len(NoTitle$), 0)
- szFilter$ = Filter$
- szInitialDir$ = IDir$
- szTitle$ = Title$
- wSize = Len(szFile$) + Len(szFilter$) + Len(szInitialDir$) + Len(szTitle$)
-
- MemHandle = GlobalAlloc(GHND, wSize)
-
- If MemHandle = 0 Then
- PFileSave% = 1
- Exit Function
- End If
-
- Address = GlobalLock(MemHandle)
- If Address = 0 Then
- PFileSave% = 2
- Exit Function
- Else
- Call hmemcpy(ByVal Address, ByVal (szFile$ + szFilter$ + szInitialDir$ + szTitle$), wSize)
- End If
-
- S.lStructSize = Len(S)
- S.hwndOwner = MyForm.hwnd
- S.Flags = Flags&
- S.nFilterIndex = Index%
- S.lpstrFile = Address
- S.nMaxFile = Len(szFile$)
- S.lpstrFilter = Address + Len(szFile$)
- S.lpstrInitialDir = S.lpstrFilter + Len(szFilter$)
- S.lpstrTitle = S.lpstrInitialDir + Len(szInitialDir$)
-
- Result = GetSaveFileName(S)
- FError& = CommDlgExtendedError()
-
- If Result = 0 Then
- PFileSave% = 3
- Exit Function
- Else
- Call hmemcpy(ByVal szFile$, ByVal Address, Len(szFile$))
- End If
-
- OK = GlobalUnlock(MemHandle) 'Free The Memory
- OK = GlobalFree(MemHandle)
-
- File$ = Left$(szFile$, InStr(szFile$, Chr$(0)) - 1)
- MyForm.Print "Common Dialogs File Save"
- MyForm.Print
- MyForm.Print "You selected:", File$
- MyForm.Print "Path:", Left$(File$, S.nFileOffset)
- MyForm.Print "Filename:", Right$(File$, Len(File$) - S.nFileOffset)
- MyForm.Print "Extension:", Right$(File$, Len(File$) - S.nFileExtension)
-
- End Function
-
- Function PFonts% (MyForm As Form, FError&, Flags&, FontType%)
-
- MyForm.Cls
- PFonts% = 0: FError& = 0
-
- Dim A As ChooseFont
- Dim F As LogFont
- Dim Address As Long
-
-
- ' ----------------------------------------------------
- ' Save the defaults
- ' ----------------------------------------------------
- OldFont$ = Dialogs.FontName
- OldFontSize = Dialogs.FontSize
- OldFontWeight = Dialogs.FontBold
- OldFontItalic = Dialogs.FontItalic
- OldFontStrikethru = Dialogs.FontStrikethru
- OldFontUnderline = Dialogs.FontUnderline
- OldForeColor& = Dialogs.ForeColor
- Debug.Print OldFont$, OldFontSize
- ' ----------------------------------------------------
-
-
- ' ----------------------------------------------------
- ' We need to use GlobalAlloc to provide lpLogFont
- ' which is a pointer to a LogFont structure
- ' ----------------------------------------------------
- MemHandle = GlobalAlloc(GHND, Len(F))
- If MemHandle = 0 Then
- PFonts% = 1
- Exit Function
- End If
-
- Address = GlobalLock(MemHandle)
- If Address = 0 Then
- PFonts% = 2
- Exit Function
- End If
- ' ----------------------------------------------------
-
-
- A.lStructSize = Len(A)
- A.hwndOwner = MyForm.hwnd
- A.Flags = Flags&
- A.nfonttype = FontType%
- A.lpLogFont = Address
-
- Result = ChooseFont(A)
- FError& = CommDlgExtendedError()
-
- ' ----------------------------------------------------
- ' make a local copy of the LogFont structure
- ' ----------------------------------------------------
- If Result = 0 Then
- PFonts% = 0
- Exit Function
- Else
- Call hmemcpy(F, ByVal Address, Len(F))
- End If
-
- OK = GlobalUnlock(MemHandle) 'Free The Memory
- OK = GlobalFree(MemHandle)
- ' ----------------------------------------------------
-
-
- ' ----------------------------------------------------
- 'calculate the font size in points
- ' ----------------------------------------------------
- FntSize = Abs(F.lfHeight * (72 / GetDeviceCaps(MyForm.hDC, LOGPIXELSY)))
- ' ----------------------------------------------------
-
-
- ' ----------------------------------------------------
- 'select the new font
- ' ----------------------------------------------------
- Dialogs.FontName = Left$(F.lfFaceName, InStr(F.lfFaceName, Chr$(0)) - 1)
- Dialogs.FontSize = FntSize
- Dialogs.FontItalic = Asc(F.lfItalic)
- Dialogs.FontStrikethru = Asc(F.lfStrikeOut)
- Dialogs.FontUnderline = Asc(F.lfUnderline)
- Dialogs.ForeColor = A.rgbColors
- If F.lfWeight < 500 Then Dialogs.FontBold = 0 Else Dialogs.FontBold = -1
- ' ----------------------------------------------------
-
-
- ' ----------------------------------------------------
- 'restore the defaults
- ' ----------------------------------------------------
- MyForm.Print "You have chosen: " + Left$(F.lfFaceName, InStr(F.lfFaceName, Chr$(0)) - 1) + ", Size = " + LTrim$(Str$(FntSize))
- MyForm.Print
-
- Dialogs.FontName = OldFont$
- Dialogs.FontSize = OldFontSize
- Dialogs.FontBold = OldFontWeight
- Dialogs.FontItalic = OldFontItalic
- Dialogs.FontStrikethru = OldFontStrikethru
- Dialogs.FontUnderline = OldFontUnderline
- Dialogs.ForeColor = OldForeColor&
- ' ----------------------------------------------------
-
-
- MyForm.Print "Font:", Left$(F.lfFaceName, InStr(F.lfFaceName, Chr$(0)) - 1)
- MyForm.Print "Height:", Str$(FntSize)
- MyForm.Print "Weight:", Str$(F.lfWeight)
- MyForm.Print "Italic:", Str$(Asc(F.lfItalic))
-
- End Function
-
- Function PPrinter% (MyForm As Form, PError&, Flags&, FPage%, TPage%, Min%, Max%, Copies%)
-
- MyForm.Cls
- PPrinter% = 0: PError& = 0
-
-
- ' ----------------------------------------------------
- 'This is similar to Printer Setup
- ' ----------------------------------------------------
- Dim Address As Long
- Dim P As PrintDlg
- Dim D As DevMode
- ' ----------------------------------------------------
-
-
- ' ----------------------------------------------------
- ' Set up structure, then call print dialog. Exit
- ' function set to 1 if get error at this point
- ' ----------------------------------------------------
- P.lStructSize = Len(P)
- P.hwndOwner = MyForm.hwnd
- P.Flags = Flags&
- P.nFromPage = FPage%
- P.nToPage = TPage%
- P.nMinPage = Min%
- P.nMaxPage = Max%
- P.nCopies = Copies%
-
- Result = PrintDlg(P)
- PError& = CommDlgExtendedError()
-
- If Result = 0 Then
- PPrinter% = 1
- Exit Function
- End If
- ' ----------------------------------------------------
-
-
- ' ----------------------------------------------------
- ' Delete the handle
- ' ----------------------------------------------------
- If P.hDC <> 0 Then
- OK = DeleteDC(P.hDC)
- End If
- ' ----------------------------------------------------
-
- ' ----------------------------------------------------
- ' Free the memory
- ' ----------------------------------------------------
-
-
- If P.hDevNames = 0 Then
- PPrinter% = 2
- Exit Function
- Else
- Dim N As DEVNAMES ' this works for old drivers as well
- Address = GlobalLock(P.hDevNames)
- Call hmemcpy(N, ByVal Address, Len(N))
- szDriver$ = String$(15, 0) 'filename buffer
- szDevice$ = String$(32, 0) 'device buffer
- szOutPut$ = String$(80, 0) 'port buffer
-
- L& = lstrcpy(szDriver$, Address + N.wDriverOffset)
- szDriver$ = Left$(szDriver$, InStr(szDriver$, Chr$(0)) - 1)
-
- L& = lstrcpy(szDevice$, Address + N.wDeviceOffset)
- szDevice$ = Left$(szDevice$, InStr(szDevice$, Chr$(0)) - 1)
-
- L& = lstrcpy(szOutPut$, Address + N.wOutputOffset)
- szOutPut$ = Left$(szOutPut$, InStr(szOutPut$, Chr$(0)) - 1)
-
- MyForm.Print szDriver$, szDevice$, szOutPut$
-
- OK = GlobalUnlock(P.hDevNames)
- OK = GlobalFree(P.hDevNames)
- End If
- ' ----------------------------------------------------
-
-
- ' ----------------------------------------------------
- ' Lock the address, then make a local copy of the
- ' global block (hDevMode)
- ' ----------------------------------------------------
- If P.hDevMode = 0 Then 'nothing to lock if hDevMode is NULL
- PPrinter% = 3
- Else
- Address = GlobalLock(P.hDevMode) 'hDevMode is returned when the driver supports ExtDeviceMode
- Call hmemcpy(D, ByVal Address, Len(D))
- OK = GlobalUnlock(P.hDevMode)
- OK = GlobalFree(P.hDevMode)
- End If
-
-
- If P.hDevMode <> 0 Then MyForm.Print "Printer:", Left$(D.dmDeviceName, InStr(D.dmDeviceName, Chr$(0)) - 1)
- MyForm.Print "From Page:", Str$(P.nFromPage)
- MyForm.Print "To Page:", Str$(P.nToPage)
- MyForm.Print "Copies:", Str$(P.nCopies)
-
- End Function
-
- Function PSetup% (MyForm As Form, PError&, Flags&)
-
- MyForm.Cls
- PSetup% = 0: PError& = 0
-
- Dim Address As Long
- Dim P As PrintDlg
- Dim D As DevMode
-
- P.lStructSize = Len(P)
- P.hwndOwner = MyForm.hwnd
- P.Flags = Flags&
-
- Result = PrintDlg(P)
-
- PError& = CommDlgExtendedError()
-
- If Result = 0 Then
- PSetup% = 1
- Exit Function
- End If
-
- ' ----------------------------------------------------
- ' PrintDlg() returns an hDC, a global handle to
- ' hDevNames and another to hDevMode. Delete the
- ' ones we don't need
- ' ----------------------------------------------------
- If P.hDC <> 0 Then
- OK = DeleteDC(P.hDC)
- End If
-
- If P.hDevNames = 0 Then
- PSetup% = 2
- Exit Function
- Else
- Dim N As DEVNAMES ' this works for old drivers as well
- Address = GlobalLock(P.hDevNames)
-
- Call hmemcpy(N, ByVal Address, Len(N))
-
- szDriver$ = String$(15, 0) 'filename buffer
- szDevice$ = String$(32, 0) 'device buffer
- szOutPut$ = String$(80, 0) 'port buffer
-
- L& = lstrcpy(szDriver$, Address + N.wDriverOffset)
- szDriver$ = Left$(szDriver$, InStr(szDriver$, Chr$(0)) - 1)
-
- L& = lstrcpy(szDevice$, Address + N.wDeviceOffset)
- szDevice$ = Left$(szDevice$, InStr(szDevice$, Chr$(0)) - 1)
-
- L& = lstrcpy(szOutPut$, Address + N.wOutputOffset)
- szOutPut$ = Left$(szOutPut$, InStr(szOutPut$, Chr$(0)) - 1)
-
- MyForm.Print szDriver$, szDevice$, szOutPut$
-
- OK = GlobalUnlock(P.hDevNames)
- OK = GlobalFree(P.hDevNames)
- End If
- ' ----------------------------------------------------
-
-
- ' ----------------------------------------------------
- ' Lock the address, then make a local copy of the
- ' global block (hDevMode)
- ' ----------------------------------------------------
- If P.hDevMode = 0 Then 'nothing to lock if hDevMode is NULL
- PSetup% = 3
- Else
- Address = GlobalLock(P.hDevMode) 'hDevMode is returned when the driver supports ExtDeviceMode
- Call hmemcpy(D, ByVal Address, Len(D))
- OK = GlobalUnlock(P.hDevMode)
- OK = GlobalFree(P.hDevMode)
- End If
-
-
- If P.hDevMode <> 0 Then
- MyForm.Print "Printer:", Left$(D.dmDeviceName, InStr(D.dmDeviceName, Chr$(0)) - 1)
- MyForm.Print "Orientation:", Str$(D.dmOrientation)
- End If
-
- End Function
-
-