📄 clscommondialog.cls
字号:
Select Case m_lApiReturn
Case 1
' Success
VBChooseColor = True
Color = chclr.rgbResult
Case 0
' Cancelled
VBChooseColor = False
Color = -1
Case Else
' Extended error
m_lExtendedError = CommDlgExtendedError()
VBChooseColor = False
Color = -1
End Select
End Function
Private Sub InitColors()
Dim i As Integer
' Initialize with first 16 system interface colors
For i = 0 To 15
alCustom(i) = GetSysColor(i)
Next
fNotFirst = True
End Sub
' Property to read or modify custom colors (use to save colors in registry)
Public Property Get CustomColor(i As Integer) As Long
' If first time, initialize to white
If fNotFirst = False Then InitColors
If i >= 0 And i <= 15 Then
CustomColor = alCustom(i)
Else
CustomColor = -1
End If
End Property
Public Property Let CustomColor(i As Integer, iValue As Long)
' If first time, initialize to system colors
If fNotFirst = False Then InitColors
If i >= 0 And i <= 15 Then
alCustom(i) = iValue
End If
End Property
' ChooseFont wrapper
Function VBChooseFont(CurFont As Font, _
Optional PrinterDC As Long = -1, _
Optional Owner As Long = -1, _
Optional Color As Long = vbBlack, _
Optional MinSize As Long = 0, _
Optional MaxSize As Long = 0, _
Optional flags As Long = 0) As Boolean
m_lApiReturn = 0
m_lExtendedError = 0
' Unwanted Flags bits
Const CF_FontNotSupported = CF_Apply Or CF_EnableHook Or CF_EnableTemplate
' Flags can get reference variable or constant with bit flags
' PrinterDC can take printer DC
If PrinterDC = -1 Then
PrinterDC = 0
If flags And CF_PrinterFonts Then PrinterDC = Printer.hdc
Else
flags = flags Or CF_PrinterFonts
End If
' Must have some fonts
If (flags And CF_PrinterFonts) = 0 Then flags = flags Or CF_ScreenFonts
' Color can take initial color, receive chosen color
If Color <> vbBlack Then flags = flags Or CF_EFFECTS
' MinSize can be minimum size accepted
If MinSize Then flags = flags Or CF_LimitSize
' MaxSize can be maximum size accepted
If MaxSize Then flags = flags Or CF_LimitSize
' Put in required internal flags and remove unsupported
flags = (flags Or CF_InitToLogFontStruct) And Not CF_FontNotSupported
' Initialize LOGFONT variable
Dim fnt As LOGFONT
Const PointsPerTwip = 1440 / 72
fnt.lfHeight = -(CurFont.Size * (PointsPerTwip / Screen.TwipsPerPixelY))
fnt.lfWeight = CurFont.Weight
fnt.lfItalic = CurFont.Italic
fnt.lfUnderline = CurFont.Underline
fnt.lfStrikeOut = CurFont.Strikethrough
' Other fields zero
StrToBytes fnt.lfFaceName, CurFont.Name
' Initialize TCHOOSEFONT variable
Dim cf As TCHOOSEFONT
cf.lStructSize = Len(cf)
If Owner <> -1 Then cf.hWndOwner = Owner
cf.hdc = PrinterDC
cf.lpLogFont = VarPtr(fnt)
cf.iPointSize = CurFont.Size * 10
cf.flags = flags
cf.rgbColors = Color
cf.nSizeMin = MinSize
cf.nSizeMax = MaxSize
' All other fields zero
m_lApiReturn = ChooseFont(cf)
Select Case m_lApiReturn
Case 1
' Success
VBChooseFont = True
flags = cf.flags
Color = cf.rgbColors
CurFont.Bold = cf.nFontType And Bold_FontType
'CurFont.Italic = cf.nFontType And Italic_FontType
CurFont.Italic = fnt.lfItalic
CurFont.Strikethrough = fnt.lfStrikeOut
CurFont.Underline = fnt.lfUnderline
CurFont.Weight = fnt.lfWeight
CurFont.Size = cf.iPointSize / 10
CurFont.Name = BytesToStr(fnt.lfFaceName)
Case 0
' Cancelled
VBChooseFont = False
Case Else
' Extended error
m_lExtendedError = CommDlgExtendedError()
VBChooseFont = False
End Select
End Function
' PrintDlg wrapper
Function VBPrintDlg(hdc As Long, _
Optional PrintRange As EPrintRange = eprAll, _
Optional DisablePageNumbers As Boolean, _
Optional FromPage As Long = 1, _
Optional ToPage As Long = &HFFFF, _
Optional DisableSelection As Boolean, _
Optional Copies As Integer, _
Optional ShowPrintToFile As Boolean, _
Optional DisablePrintToFile As Boolean = True, _
Optional PrintToFile As Boolean, _
Optional Collate As Boolean, _
Optional PreventWarning As Boolean, _
Optional Owner As Long, _
Optional Printer As Object, _
Optional flags As Long) As Boolean
Dim afFlags As Long, afMask As Long
m_lApiReturn = 0
m_lExtendedError = 0
' Set PRINTDLG flags
afFlags = (-DisablePageNumbers * PD_NOPAGENUMS) Or _
(-DisablePrintToFile * PD_DISABLEPRINTTOFILE) Or _
(-DisableSelection * PD_NOSELECTION) Or _
(-PrintToFile * PD_PRINTTOFILE) Or _
(-(Not ShowPrintToFile) * PD_HIDEPRINTTOFILE) Or _
(-PreventWarning * PD_NOWARNING) Or _
(-Collate * PD_COLLATE) Or _
PD_USEDEVMODECOPIESANDCOLLATE Or _
PD_RETURNDC
If PrintRange = eprPageNumbers Then
afFlags = afFlags Or PD_PAGENUMS
ElseIf PrintRange = eprSelection Then
afFlags = afFlags Or PD_SELECTION
End If
' Mask out unwanted bits
afMask = CLng(Not (PD_ENABLEPRINTHOOK Or _
PD_ENABLEPRINTTEMPLATE))
afMask = afMask And _
CLng(Not (PD_ENABLESETUPHOOK Or _
PD_ENABLESETUPTEMPLATE))
' Fill in PRINTDLG structure
Dim pd As TPRINTDLG
pd.lStructSize = Len(pd)
pd.hWndOwner = Owner
pd.flags = afFlags And afMask
pd.nFromPage = FromPage
pd.nToPage = ToPage
pd.nMinPage = 1
pd.nMaxPage = &HFFFF
' Show Print dialog
m_lApiReturn = PrintDlg(pd)
Select Case m_lApiReturn
Case 1
VBPrintDlg = True
' Return dialog values in parameters
hdc = pd.hdc
If (pd.flags And PD_PAGENUMS) Then
PrintRange = eprPageNumbers
ElseIf (pd.flags And PD_SELECTION) Then
PrintRange = eprSelection
Else
PrintRange = eprAll
End If
FromPage = pd.nFromPage
ToPage = pd.nToPage
PrintToFile = (pd.flags And PD_PRINTTOFILE)
' Get DEVMODE structure from PRINTDLG
Dim pDevMode As Long
pDevMode = GlobalLock(pd.hDevMode)
CopyMemory m_dvmode, ByVal pDevMode, Len(m_dvmode)
Call GlobalUnlock(pd.hDevMode)
' Get Copies and Collate settings from DEVMODE structure
Copies = m_dvmode.dmCopies
Collate = (m_dvmode.dmCollate = DMCOLLATE_TRUE)
' Set default printer properties
On Error Resume Next
If Not (Printer Is Nothing) Then
Printer.Copies = Copies
Printer.Orientation = m_dvmode.dmOrientation
Printer.PaperSize = m_dvmode.dmPaperSize
Printer.PrintQuality = m_dvmode.dmPrintQuality
End If
On Error GoTo 0
Case 0
' Cancelled
VBPrintDlg = False
Case Else
' Extended error:
m_lExtendedError = CommDlgExtendedError()
VBPrintDlg = False
End Select
End Function
Private Property Get DevMode() As DevMode
DevMode = m_dvmode
End Property
' PageSetupDlg wrapper
Function VBPageSetupDlg(Optional Owner As Long, _
Optional DisableMargins As Boolean, _
Optional DisableOrientation As Boolean, _
Optional DisablePaper As Boolean, _
Optional DisablePrinter As Boolean, _
Optional LeftMargin As Long, _
Optional MinLeftMargin As Long, _
Optional RightMargin As Long, _
Optional MinRightMargin As Long, _
Optional TopMargin As Long, _
Optional MinTopMargin As Long, _
Optional BottomMargin As Long, _
Optional MinBottomMargin As Long, _
Optional PaperSize As EPaperSize = epsLetter, _
Optional Orientation As EOrientation = eoPortrait, _
Optional PrintQuality As EPrintQuality = epqDraft, _
Optional Units As EPageSetupUnits = epsuInches, _
Optional Printer As Object, _
Optional flags As Long) As Boolean
Dim afFlags As Long, afMask As Long
m_lApiReturn = 0
m_lExtendedError = 0
' Mask out unwanted bits
afMask = Not (PSD_EnablePagePaintHook Or _
PSD_EnablePageSetupHook Or _
PSD_EnablePageSetupTemplate)
' Set TPAGESETUPDLG flags
afFlags = (-DisableMargins * PSD_DISABLEMARGINS) Or _
(-DisableOrientation * PSD_DISABLEORIENTATION) Or _
(-DisablePaper * PSD_DISABLEPAPER) Or _
(-DisablePrinter * PSD_DISABLEPRINTER) Or _
PSD_MARGINS Or PSD_MINMARGINS And afMask
Dim lUnits As Long
If Units = epsuInches Then
afFlags = afFlags Or PSD_INTHOUSANDTHSOFINCHES
lUnits = 1000
Else
afFlags = afFlags Or PSD_INHUNDREDTHSOFMILLIMETERS
lUnits = 100
End If
Dim psd As TPAGESETUPDLG
' Fill in PRINTDLG structure
psd.lStructSize = Len(psd)
psd.hWndOwner = Owner
psd.rtMargin.TOp = TopMargin * lUnits
psd.rtMargin.Left = LeftMargin * lUnits
psd.rtMargin.Bottom = BottomMargin * lUnits
psd.rtMargin.Right = RightMargin * lUnits
psd.rtMinMargin.TOp = MinTopMargin * lUnits
psd.rtMinMargin.Left = MinLeftMargin * lUnits
psd.rtMinMargin.Bottom = MinBottomMargin * lUnits
psd.rtMinMargin.Right = MinRightMargin * lUnits
psd.flags = afFlags
' Show Print dialog
If PageSetupDlg(psd) Then
VBPageSetupDlg = True
' Return dialog values in parameters
TopMargin = psd.rtMargin.TOp / lUnits
LeftMargin = psd.rtMargin.Left / lUnits
BottomMargin = psd.rtMargin.Bottom / lUnits
RightMargin = psd.rtMargin.Right / lUnits
MinTopMargin = psd.rtMinMargin.TOp / lUnits
MinLeftMargin = psd.rtMinMargin.Left / lUnits
MinBottomMargin = psd.rtMinMargin.Bottom / lUnits
MinRightMargin = psd.rtMinMargin.Right / lUnits
' Get DEVMODE structure from PRINTDLG
Dim dvmode As DevMode, pDevMode As Long
pDevMode = GlobalLock(psd.hDevMode)
CopyMemory dvmode, ByVal pDevMode, Len(dvmode)
Call GlobalUnlock(psd.hDevMode)
PaperSize = dvmode.dmPaperSize
Orientation = dvmode.dmOrientation
PrintQuality = dvmode.dmPrintQuality
' Set default printer properties
On Error Resume Next
If Not (Printer Is Nothing) Then
Printer.Copies = dvmode.dmCopies
Printer.Orientation = dvmode.dmOrientation
Printer.PaperSize = dvmode.dmPaperSize
Printer.PrintQuality = dvmode.dmPrintQuality
End If
On Error GoTo 0
End If
End Function
#If fComponent = 0 Then
Private Sub ErrRaise(e As Long)
Dim sText As String, sSource As String
If e > 1000 Then
sSource = App.EXEName & ".CommonDialog"
Err.Raise COMError(e), sSource, sText
Else
' Raise standard Visual Basic error
sSource = App.EXEName & ".VBError"
Err.Raise e, sSource
End If
End Sub
#End If
Private Sub StrToBytes(ab() As Byte, s As String)
If IsArrayEmpty(ab) Then
' Assign to empty array
ab = StrConv(s, vbFromUnicode)
Else
Dim cab As Long
' Copy to existing array, padding or truncating if necessary
cab = UBound(ab) - LBound(ab) + 1
If Len(s) < cab Then s = s & String$(cab - Len(s), 0)
'If UnicodeTypeLib Then
' Dim st As String
' st = StrConv(s, vbFromUnicode)
' CopyMemoryStr ab(LBound(ab)), st, cab
'Else
CopyMemoryStr ab(LBound(ab)), s, cab
'End If
End If
End Sub
Private Function BytesToStr(ab() As Byte) As String
BytesToStr = StrConv(ab, vbUnicode)
End Function
Private Function COMError(e As Long) As Long
COMError = e Or vbObjectError
End Function
'
Private Function IsArrayEmpty(va As Variant) As Boolean
Dim v As Variant
On Error Resume Next
v = va(LBound(va))
IsArrayEmpty = (Err <> 0)
End Function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -