📄 bascommondialog.bas
字号:
Public FontDialog As CHOOSEFONTS
Public PrintDialog As PRINTDLGS
Dim parenthWnd As Long
Public Function ShowOpen(ByVal hwnd As Long, Optional ByVal centerForm As Boolean = True) As SelectedFile
Dim ret As Long
Dim Count As Integer
Dim fileNameHolder As String
Dim LastCharacter As Integer
Dim NewCharacter As Integer
Dim tempFiles(1 To 200) As String
Dim hInst As Long
Dim Thread As Long
parenthWnd = hwnd
FileDialog.nStructSize = Len(FileDialog)
FileDialog.hwndOwner = hwnd
FileDialog.sFileTitle = Space$(2048)
FileDialog.nTitleSize = Len(FileDialog.sFileTitle)
FileDialog.sFile = FileDialog.sFile & Space$(2047) & Chr$(0)
FileDialog.nFileSize = Len(FileDialog.sFile)
'If FileDialog.flags = 0 Then
FileDialog.flags = OFS_FILE_OPEN_FLAGS
'End If
'Set up the CBT hook
hInst = GetWindowLong(hwnd, GWL_HINSTANCE)
Thread = GetCurrentThreadId()
If centerForm = True Then
hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProcCenterForm, hInst, Thread)
Else
hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProcCenterScreen, hInst, Thread)
End If
ret = GetOpenFileName(FileDialog)
If ret Then
If Trim$(FileDialog.sFileTitle) = "" Then
LastCharacter = 0
Count = 0
While ShowOpen.nFilesSelected = 0
NewCharacter = InStr(LastCharacter + 1, FileDialog.sFile, Chr$(0), vbTextCompare)
If Count > 0 Then
tempFiles(Count) = Mid(FileDialog.sFile, LastCharacter + 1, NewCharacter - LastCharacter - 1)
Else
ShowOpen.sLastDirectory = Mid(FileDialog.sFile, LastCharacter + 1, NewCharacter - LastCharacter - 1)
End If
Count = Count + 1
If InStr(NewCharacter + 1, FileDialog.sFile, Chr$(0), vbTextCompare) = InStr(NewCharacter + 1, FileDialog.sFile, Chr$(0) & Chr$(0), vbTextCompare) Then
tempFiles(Count) = Mid(FileDialog.sFile, NewCharacter + 1, InStr(NewCharacter + 1, FileDialog.sFile, Chr$(0) & Chr$(0), vbTextCompare) - NewCharacter - 1)
ShowOpen.nFilesSelected = Count
End If
LastCharacter = NewCharacter
Wend
ReDim ShowOpen.sFiles(1 To ShowOpen.nFilesSelected)
For Count = 1 To ShowOpen.nFilesSelected
ShowOpen.sFiles(Count) = tempFiles(Count)
Next
Else
ReDim ShowOpen.sFiles(1 To 1)
ShowOpen.sLastDirectory = left$(FileDialog.sFile, FileDialog.nFileOffset)
ShowOpen.nFilesSelected = 1
ShowOpen.sFiles(1) = Mid(FileDialog.sFile, FileDialog.nFileOffset + 1, InStr(1, FileDialog.sFile, Chr$(0), vbTextCompare) - FileDialog.nFileOffset - 1)
End If
ShowOpen.bCanceled = False
Exit Function
Else
ShowOpen.sLastDirectory = ""
ShowOpen.nFilesSelected = 0
ShowOpen.bCanceled = True
Erase ShowOpen.sFiles
Exit Function
End If
End Function
Public Function ShowSave(ByVal hwnd As Long, Optional ByVal centerForm As Boolean = True) As SelectedFile
Dim ret As Long
Dim hInst As Long
Dim Thread As Long
parenthWnd = hwnd
FileDialog.nStructSize = Len(FileDialog)
FileDialog.hwndOwner = hwnd
FileDialog.sFileTitle = Space$(2048)
FileDialog.nTitleSize = Len(FileDialog.sFileTitle)
If FileDialog.sFile = "" Then
FileDialog.sFile = Space$(2047) & Chr$(0)
ElseIf Right(FileDialog.sFile, 1) <> Chr$(0) Then
FileDialog.sFile = FileDialog.sFile & Space$(2047 - Len(FileDialog.sFile)) & Chr$(0)
End If
FileDialog.nFileSize = Len(FileDialog.sFile)
If FileDialog.flags = 0 Then
FileDialog.flags = OFS_FILE_SAVE_FLAGS
End If
'Set up the CBT hook
hInst = GetWindowLong(hwnd, GWL_HINSTANCE)
Thread = GetCurrentThreadId()
If centerForm = True Then
hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProcCenterForm, hInst, Thread)
Else
hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProcCenterScreen, hInst, Thread)
End If
ret = GetSaveFileName(FileDialog)
ReDim ShowSave.sFiles(1)
If ret Then
ShowSave.sLastDirectory = left$(FileDialog.sFile, FileDialog.nFileOffset)
ShowSave.nFilesSelected = 1
ShowSave.sFiles(1) = Mid(FileDialog.sFile, FileDialog.nFileOffset + 1, InStr(1, FileDialog.sFile, Chr$(0), vbTextCompare) - FileDialog.nFileOffset - 1)
ShowSave.bCanceled = False
Exit Function
Else
ShowSave.sLastDirectory = ""
ShowSave.nFilesSelected = 0
ShowSave.bCanceled = True
Erase ShowSave.sFiles
Exit Function
End If
End Function
Public Function ShowColor(ByVal hwnd As Long, Optional ByVal centerForm As Boolean = True) As SelectedColor
Dim customcolors() As Byte ' dynamic (resizable) array
Dim i As Integer
Dim ret As Long
Dim hInst As Long
Dim Thread As Long
parenthWnd = hwnd
If ColorDialog.lpCustColors = "" Then
ReDim customcolors(0 To 16 * 4 - 1) As Byte 'resize the array
For i = LBound(customcolors) To UBound(customcolors)
customcolors(i) = 254 ' sets all custom colors to white
Next i
ColorDialog.lpCustColors = StrConv(customcolors, vbUnicode) ' convert array
End If
ColorDialog.hwndOwner = hwnd
ColorDialog.lStructSize = Len(ColorDialog)
ColorDialog.flags = COLOR_FLAGS
'Set up the CBT hook
hInst = GetWindowLong(hwnd, GWL_HINSTANCE)
Thread = GetCurrentThreadId()
If centerForm = True Then
hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProcCenterForm, hInst, Thread)
Else
hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProcCenterScreen, hInst, Thread)
End If
ret = ChooseColor(ColorDialog)
If ret Then
ShowColor.bCanceled = False
ShowColor.oSelectedColor = ColorDialog.rgbResult
Exit Function
Else
ShowColor.bCanceled = True
ShowColor.oSelectedColor = &H0&
Exit Function
End If
End Function
Public Function ShowFont(ByVal hwnd As Long, ByVal startingFontName As String, Optional ByVal centerForm As Boolean = True) As SelectedFont
Dim ret As Long
Dim lfLogFont As LOGFONT
Dim hInst As Long
Dim Thread As Long
Dim i As Integer
parenthWnd = hwnd
FontDialog.nSizeMax = 0
FontDialog.nSizeMin = 0
FontDialog.nFontType = Screen.FontCount
FontDialog.hwndOwner = hwnd
FontDialog.hDC = 0
FontDialog.lpfnHook = 0
FontDialog.lCustData = 0
FontDialog.lpLogFont = VarPtr(lfLogFont)
FontDialog.iPointSize = 10
FontDialog.lpTemplateName = Space$(2048)
FontDialog.rgbColors = RGB(0, 255, 255)
FontDialog.lStructSize = Len(FontDialog)
If FontDialog.flags = 0 Then
FontDialog.flags = CF_SCREENFONTS Or CF_EFFECTS Or CF_INITTOLOGFONTSTRUCT 'Or CF_EFFECTS
End If
For i = 0 To Len(startingFontName) - 1
lfLogFont.lfFaceName(i) = Asc(Mid(startingFontName, i + 1, 1))
Next
'Set up the CBT hook
hInst = GetWindowLong(hwnd, GWL_HINSTANCE)
Thread = GetCurrentThreadId()
If centerForm = True Then
hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProcCenterForm, hInst, Thread)
Else
hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProcCenterScreen, hInst, Thread)
End If
ret = ChooseFont(FontDialog)
If ret Then
ShowFont.bCanceled = False
ShowFont.bBold = IIf(lfLogFont.lfWeight > 400, 1, 0)
ShowFont.bItalic = lfLogFont.lfItalic
ShowFont.bStrikeOut = lfLogFont.lfStrikeOut
ShowFont.bUnderline = lfLogFont.lfUnderline
ShowFont.lColor = FontDialog.rgbColors
ShowFont.nSize = FontDialog.iPointSize / 10
For i = 0 To 31
ShowFont.sSelectedFont = ShowFont.sSelectedFont + Chr(lfLogFont.lfFaceName(i))
Next
ShowFont.sSelectedFont = Mid(ShowFont.sSelectedFont, 1, InStr(1, ShowFont.sSelectedFont, Chr(0)) - 1)
Exit Function
Else
ShowFont.bCanceled = True
Exit Function
End If
End Function
Public Function ShowPrinter(ByVal hwnd As Long, Optional ByVal centerForm As Boolean = True) As Long
Dim hInst As Long
Dim Thread As Long
parenthWnd = hwnd
PrintDialog.hwndOwner = hwnd
PrintDialog.lStructSize = Len(PrintDialog)
'Set up the CBT hook
hInst = GetWindowLong(hwnd, GWL_HINSTANCE)
Thread = GetCurrentThreadId()
If centerForm = True Then
hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProcCenterForm, hInst, Thread)
Else
hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProcCenterScreen, hInst, Thread)
End If
ShowPrinter = PrintDlg(PrintDialog)
End Function
Private Function WinProcCenterScreen(ByVal lMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim rectForm As RECT, rectMsg As RECT
Dim x As Long, y As Long
If lMsg = HCBT_ACTIVATE Then
'Show the MsgBox at a fixed location (0,0)
GetWindowRect wParam, rectMsg
x = Screen.Width / Screen.TwipsPerPixelX / 2 - (rectMsg.Right - rectMsg.left) / 2
y = Screen.Height / Screen.TwipsPerPixelY / 2 - (rectMsg.Bottom - rectMsg.top) / 2
Debug.Print "Screen " & Screen.Height / 2
Debug.Print "MsgBox " & (rectMsg.Right - rectMsg.left) / 2
SetWindowPos wParam, 0, x, y, 0, 0, SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE
'Release the CBT hook
UnhookWindowsHookEx hHook
End If
WinProcCenterScreen = False
End Function
Private Function WinProcCenterForm(ByVal lMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim rectForm As RECT, rectMsg As RECT
Dim x As Long, y As Long
'On HCBT_ACTIVATE, show the MsgBox centered over Form1
If lMsg = HCBT_ACTIVATE Then
'Get the coordinates of the form and the message box so that
'you can determine where the center of the form is located
GetWindowRect parenthWnd, rectForm
GetWindowRect wParam, rectMsg
x = (rectForm.left + (rectForm.Right - rectForm.left) / 2) - ((rectMsg.Right - rectMsg.left) / 2)
y = (rectForm.top + (rectForm.Bottom - rectForm.top) / 2) - ((rectMsg.Bottom - rectMsg.top) / 2)
'Position the msgbox
SetWindowPos wParam, 0, x, y, 0, 0, SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE
'Release the CBT hook
UnhookWindowsHookEx hHook
End If
WinProcCenterForm = False
End Function
Public Function DetermineDirectory(inputString As String) As String
Dim pos As Integer
pos = InStrRev(inputString, "\", , vbTextCompare)
DetermineDirectory = Mid(inputString, 1, pos)
End Function
Public Function DetermineFilename(inputString As String) As String
Dim pos As Integer
If InStr(1, inputString, "\") = 0 Then
DetermineFilename = inputString
Else
pos = InStrRev(inputString, "\", , vbTextCompare)
DetermineFilename = Mid(inputString, pos + 1, Len(inputString) - pos)
End If
End Function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -