AutoCAD 对象类型

AutoCAD 二次开发时,可以使用 TypeOf 来检查对象是否为特定的 AutoCAD 对象类型,以决定下一步操作。AutoCAD ActiveX 支持的对象类型非常多,涵盖了 AutoCAD 中几乎所有的实体和组件。以下列举一些常见的 AutoCAD 对象类型:

一、基础对象类型:

AcadBlockReference:块参照(Block Reference)

AcadDimension:标注(Dimension)

AcadHatch:填充(Hatch)

AcadLayer:图层(Layer)

AcadLine:直线(Line)

AcadMLine:多线(Multiline)

AcadPoint:点(Point)

AcadPolyline:二维多段线(2D Polyline)

Acad3DPolyline:三维多段线(3D Polyline)

AcadCircle:圆(Circle)

AcadArc:圆弧(Arc)

AcadEllipse:椭圆(Ellipse)

AcadText:文本(Text)

AcadInsert:插入(Insert)

AcadRay:射线(Ray)

AcadXLine:构造线(Xline)

AcadSpline:样条曲线(Spline)

二、高级对象类型:

AcadBlockTable:块表(Block Table)

AcadBlockTableRecord:块表记录(Block Table Record)

AcadDictionary:字典(Dictionary)

AcadDictionaryEntry:字典条目(Dictionary Entry)

AcadLayout:布局(Layout)

AcadObjectId:对象ID(Object ID)

AcadObject:基础对象(Generic Object)

AcadSelectionSet:选择集(Selection Set)

AcadTransaction:事务(Transaction)

三、集合和数组:

AcadObjectIdArray:对象ID数组

AcadObjectArray:对象数组

AcadBlockTableRecordIterator:块表记录迭代器

四、其他:

AcadDatabase:数据库(Database)

AcadDocument:文档(Document)

AcadApplication:应用程序(Application)

AcadUtility:实用工具(Utility)

随着 AutoCAD 的升级,对象类型可能会被添加,而旧的对象类型则可能会被弃用或替换。因此,需要关注当前版本 AutoCAD 的官方文档或帮助文件。

使用TypeOf时,须确保正在操作的对象是所期望的类型,否则可能会遇到运行时错误。如果不能确定对象的类型,则可以先使用 TypeOf 来检查一下它是否属于一个更通用的类型(如AcadObject),然后再进一步细化检查。

枚举进程

‘调用示例
Dim I As Integer
Dim ShowArray As Variant
ShowArray = ProcessArray
For I = LBound(ShowArray) To UBound(ShowArray)
Debug.Print ShowArray(I)
Next I

Option Explicit
Private Declare Function CreateToolhelp32Snapshot Lib “kernel32” (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Private Declare Function Process32First Lib “kernel32” (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib “kernel32” (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function CloseHandle Lib “kernel32” (ByVal hObject As Long) As Long
Private Declare Function OpenProcess Lib “kernel32” (ByVal dwDesiredAccess As Long, ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As Long
Private Declare Function TerminateProcess Lib “kernel32” (ByVal ApphProcess As Long, ByVal uExitCode As Long) As Long

Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * 1024
End Type

Const TH32CS_SNAPHEAPLIST = &H1
Const TH32CS_SNAPPROCESS = &H2
Const TH32CS_SNAPTHREAD = &H4
Const TH32CS_SNAPMODULE = &H8
Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Const TH32CS_INHERIT = &H80000000

Private Function ProcessArray() As Variant ‘生成进程名数组
Dim pProcess As PROCESSENTRY32
Dim lRet As Long
Dim lRet1 As Long
Dim sName As String
Dim I As Integer
lRet = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If lRet Then
pProcess.dwSize = 1060
If (Process32First(lRet, pProcess)) Then ‘遍历第一个进程
Dim iCount As Integer
Dim TempArray() As String
Do
I = InStr(1, pProcess.szExeFile, Chr(0))
sName = LCase(Left(pProcess.szExeFile, I – 1))
ReDim Preserve TempArray(0 To iCount)
TempArray(iCount) = sName
iCount = iCount + 1
Loop Until (Process32Next(lRet, pProcess) < 1) ‘遍历所有进程知道返回值为False
End If
lRet1 = CloseHandle(lRet)
End If
ProcessArray = TempArray
End Function

图片颜色屏蔽

Option Explicit
Private Declare Function TransparentBlt Lib “msimg32” _
(ByVal hdcDest As Long, ByVal nXOriginDest As Long, _
ByVal nYOriginDest As Long, ByVal nWidthDest As Long, _
ByVal nHeightDest As Long, ByVal hdcSrc As Long, _
ByVal nXOriginSrc As Long, ByVal nYOriginSrc As Long, _
ByVal nWidthSrc As Long, ByVal nHeightSrc As Long, _
ByVal crTransparent As Long) As Long
Private Declare Function GetPixel Lib “gdi32” (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long

‘调用示例
‘添加一个 PictureBox,命名为 picSource,并加载图片
Dim TransparentColor As Long
TransparentColor = GetPixel(picSource.hdc, 0, 0) ‘选择左上角(0,0)的颜色为屏蔽色
‘thne simply draw with the transparent color equal to the value we found above
With picSource
TransparentBlt Me.hdc, 0, 0, .ScaleWidth, .ScaleHeight, .hdc, 0, 0, .ScaleWidth, .ScaleHeight, TransparentColor
End With
Me.Refresh

‘左图屏蔽(绿色)后显示为右图。

Zip 与 UnZip

Option Explicit

‘Example
Dim FileNames As Variant ‘Variant 必须的
Dim ZipFileName As String
sFilter = “All Files(.)|.
‘Get the file names
Dim NameGroup As String
NameGroup = DialogOpenFile(Me.hwnd, “选择文件”, sFilter, App.Path)
If NameGroup = “” Then Exit Sub
Dim TempArray As Variant
TempArray = Split(NameGroup, vbNullChar)
If UBound(TempArray) – LBound(TempArray) < 3 Then ‘单选或多选选了一个
ReDim FileNames(0 To 0)
FileNames(0) = TempArray(0)
Else ‘多选选了多个
ReDim FileNames(0 To UBound(TempArray) – LBound(TempArray) – 3)
For I = 0 To UBound(TempArray) – LBound(TempArray) – 3
FileNames(I) = GetFullName(TempArray(0), TempArray(I + 1))
Next I
End If
‘Exit if dialog box canceled
If Not IsArray(FileNames) Then Exit Sub
If UBound(FileNames) – LBound(FileNames) < 0 Then Exit Sub
For I = LBound(FileNames) To UBound(FileNames)
If Not FileExist(FileNames(I)) Then Exit Sub
Next I
Dim RecommendedName As String
RecommendedName = GetShortPath(FileNames(LBound(FileNames)))
sFilter = “Zip File(.zip)|.zip”
sTitle = “Save Zip File”
‘调用窗体句柄,Open(1)还是Save(非1),标题,打开时自动填入的文件名,模板,打开时自动选择的路径,无扩展名时自动增加的扩展名,目标路径
ZipFileName = DialogFile(Me.hwnd, 0, sTitle, RecommendedName, sFilter, GetPath(FileNames(LBound(FileNames))), “zip”, GetPath(FileNames(LBound(FileNames))), 1)
ZipFileName = Trim(ZipFileName)
If ZipFileName = “” Then Exit Sub
Call Zip(FileNames, ZipFileName)

Public Sub Zip(ByVal vFileNames As Variant, ByVal vZipFileName As Variant) ‘Variant 必须的
On Error Resume Next ‘有的系统不支持压缩与解压
Dim I As Integer, J As Integer
If Not IsArray(vFileNames) Then Exit Sub
Dim oShell As Object
‘Create empty Zip File with zip header
Dim ZipFileNumber As Integer
ZipFileNumber = FreeFile
Open vZipFileName For Output As #ZipFileNumber
Print #ZipFileNumber, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)
Close #ZipFileNumber
Set oShell = CreateObject(“Shell.Application”) ‘基于 shell32.dll
‘Copy the files to the compressed folder
Dim iCount As Integer
iCount = 0
For I = LBound(vFileNames) To UBound(vFileNames)
DoEvents
oShell.NameSpace(vZipFileName).CopyHere vFileNames(I)
‘Debug.Print CStr(I) & Space$(1) & “=” & Space$(1) & vFileNames(I)
‘Keep script waiting until Compressing is done
iCount = iCount + 1
On Error Resume Next
Do Until oShell.NameSpace(vZipFileName).items.Count = iCount
DoEvents
Wait 50
Loop
On Error GoTo 0
‘ShowMsg frmMain, “Zip File” & iCount & Space$(1) & “/” & Space$(1) & (UBound(vFileNames) – LBound(vFileNames) + 1)
Next I
Set oShell = Nothing
On Error GoTo 0
End Sub

Public Sub UnZip(ByVal vZipFile As Variant, ByVal vTargetFolder As Variant) ‘Variant 必须的
On Error Resume Next ‘有的系统不支持压缩与解压
Dim oShell As Object
‘Copy the zipped files to the newly created folder
Set oShell = CreateObject(“Shell.Application”) ‘基于 shell32.dll
oShell.NameSpace(vTargetFolder).CopyHere oShell.NameSpace(vZipFile).items
Set oShell = Nothing
On Error GoTo 0
End Sub

对话框函数系列之一

Option Explicit

Private Type OPENFILENAME
lStructSize As Long
hwnd As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
Flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type

Private Type COLORSTRUC
lStructSize As Long
hwnd As Long
hInstance As Long
rgbResult As Long
lpCustColors As String
Flags As Long
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type

Private Const LF_FACESIZE = 32

Private Type LogFont
lfHeight As Long
lfWidth As Long
lfEscapement As Long
lfOrientation As Long
lfWeight As Long
lfItalic As Byte
lfUnderline As Byte
lfStrikeOut As Byte
lfCharSet As Byte
lfOutPrecision As Byte
lfClipPrecision As Byte
lfQuality As Byte
lfPitchAndFamily As Byte
lfFaceName(LF_FACESIZE) As Byte
End Type

Private Type FONTSTRUC
lStructSize As Long
hwnd As Long
hDC As Long
lpLogFont As Long
iPointSize As Long
Flags As Long
rgbColors As Long
lCustData As Long
lpfnHook As Long
lpTemplateName As String
hInstance As Long
lpszStyle As String
nFontType As Integer
MISSING_ALIGNMENT As Integer
nSizeMin As Long
nSizeMax As Long
End Type

Public Type DEVMODE
dmDeviceName As String * 32
dmSpecVersion As Integer
dmDriverVersion As Integer
dmSize As Integer
dmDriverExtra As Integer
dmFields As Long
dmOrientation As Integer
dmPaperSize As Integer
dmPaperLength As Integer
dmPaperWidth As Integer
dmScale As Integer
dmCopies As Integer
dmDefaultSource As Integer
dmPrintQuality As Integer
dmColor As Integer
dmDuplex As Integer
dmYResolution As Integer
dmTTOption As Integer
dmCollate As Integer
dmFormName As String * 32
dmUnusedPadding As Integer
dmBitsPerPel As Integer
dmPelsWidth As Long
dmPelsHeight As Long
dmDisplayFlags As Long
dmDisplayFreq As Long
End Type

Private Type PRINTDLGSTRUC
lStructSize As Long
hwnd As Long
hDevMode As Long
hDevNames As Long
hDC As Long
Flags As Long
nFromPage As Integer
nToPage As Integer
nMinPage As Integer
nMaxPage As Integer
nCopies As Integer
hInstance As Long
lCustData As Long
lpfnPrintHook As Long
lpfnSetupHook As Long
lpPrintTemplateName As String
lpSetupTemplateName As String
hPrintTemplate As Long
hSetupTemplate As Long
End Type

Public Type PRINTPROPS
Cancel As Boolean
Device As String
Copies As Integer
Collate As Boolean
File As Boolean
All As Boolean
Pages As Boolean
Selection As Boolean
FromPage As Integer
ToPage As Integer
DM As DEVMODE
End Type

Private Type SHITEMID
cb As Long
abID As Byte
End Type

Private Type ITEMIDLIST
mkid As SHITEMID
End Type

Private Type BROWSEINFO
hOwner As Long ‘主句柄
pidlRoot As Long ‘展开根目录
pszDisplayName As String
lpszTitle As String ‘列表框标题,这里是用的long,所以得用lstrcat获取字符指针了
ulFlags As Long ‘规定只能选择文件夹,其他无效
‘lpfn As Long
lpfnCallback As Long
lParam As Long
iImage As Long
End Type

‘//
‘// Win32s (Private Functions for Wrappers Below)
‘//

Private Declare Function GetOpenFileName Lib “comdlg32.dll” Alias “GetOpenFileNameA” (pOpenfilename As OPENFILENAME) As Long
Private Declare Function GetSaveFileName Lib “comdlg32.dll” Alias “GetSaveFileNameA” (pOpenfilename As OPENFILENAME) As Long
Private Declare Function PrintDlg Lib “comdlg32.dll” Alias “PrintDlgA” (pPrintdlg As PRINTDLGSTRUC) As Long
Private Declare Function ChooseColor Lib “comdlg32.dll” Alias “ChooseColorA” (pChoosecolor As COLORSTRUC) As Long
Private Declare Function ChooseFont Lib “comdlg32.dll” Alias “ChooseFontA” (pChoosefont As FONTSTRUC) As Long
Private Declare Function GlobalAlloc Lib “kernel32” (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalFree Lib “kernel32” (ByVal hMem As Long) As Long
Private Declare Function GlobalLock Lib “kernel32” (ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib “kernel32” (ByVal hMem As Long) As Long
Private Declare Sub CopyMemory Lib “kernel32” Alias “RtlMoveMemory” (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
Private Declare Function SHGetPathFromIDList Lib “shell32.dll” Alias “SHGetPathFromIDListA” (ByVal pidl As Long, ByVal pszPath As String) As Long
Private Declare Function SHGetSpecialFolderLocation Lib “shell32.dll” (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As ITEMIDLIST) As Long
Private Declare Function SHBrowseForFolder Lib “shell32.dll” Alias “SHBrowseForFolderA” (lpBrowseInfo As BROWSEINFO) As Long ‘ITEMIDLIST
Private Declare Function WriteProfileString Lib “kernel32” Alias “WriteProfileStringA” (ByVal lpszSection As String, ByVal lpszKeyName As String, ByVal lpszString As String) As Long
Private Declare Function GetProfileString Lib “kernel32” Alias “GetProfileStringA” (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
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
Private Declare Function SendMessage Lib “user32” Alias “SendMessageA” (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal IParam As String) As Long
Private Declare Function lstrcat Lib “kernel32” Alias “lstrcatA” (ByVal lpStringl As String, ByVal lpString2 As String) As Long
Private Declare Sub CoTaskMemFree Lib “ole32.dll” (ByVal hMem As Long)
‘//
‘// Win32s (Public)
‘//

Declare Function WinHelp Lib “user32” Alias “WinHelpA” (ByVal hwnd As Long, ByVal lpHelpFile As String, ByVal wCommand As Long, ByVal dwData As Any) As Long
Declare Function HTMLHelp Lib “hhctrl.ocx” Alias “HtmlHelpA” (ByVal hwnd As Long, ByVal szFilename As String, ByVal dwCommand As Long, ByVal dwData As Any) As Long

‘//
‘// Constants (Public for Print Dialog Box)
‘//

Public Const PD_NOSELECTION = &H4
Public Const PD_DISABLEPRINTTOFILE = &H80000
Public Const PD_PRINTTOFILE = &H20
Public Const PD_RETURNDC = &H100
Public Const PD_RETURNDEFAULT = &H400
Public Const PD_RETURNIC = &H200
Public Const PD_SELECTION = &H1
Public Const PD_SHOWHELP = &H800
Public Const PD_NOPAGENUMS = &H8
Public Const PD_PAGENUMS = &H2
Public Const PD_ALLPAGES = &H0
Public Const PD_COLLATE = &H10
Public Const PD_HIDEPRINTTOFILE = &H100000

‘//
‘// Constants (Public for WinHelp)
‘//

Public Const HELP_COMMAND = &H102&
Public Const HELP_CONTENTS = &H3&
Public Const HELP_CONTEXT = &H1
Public Const HELP_CONTEXTPOPUP = &H8&
Public Const HELP_FORCEFILE = &H9&
Public Const HELP_HELPONHELP = &H4
Public Const HELP_INDEX = &H3
Public Const HELP_KEY = &H101
Public Const HELP_MULTIKEY = &H201&
Public Const HELP_PARTIALKEY = &H105&
Public Const HELP_QUIT = &H2
Public Const HELP_SETCONTENTS = &H5&
Public Const HELP_SETINDEX = &H5
Public Const HELP_SETWINPOS = &H203&

‘//
‘// Constants (Public for HTMLHelp)
‘//

Public Const HH_DISPLAY_TOPIC = &H0&
Public Const HH_HELP_FINDER = &H0&
Public Const HH_DISPLAY_TOC = &H1& ‘// Currently Not Implemented
Public Const HH_DISPLAY_INDEX = &H2& ‘// Currently Not Implemented
Public Const HH_DISPLAY_SEARCH = &H3& ‘// Currently Not Implemented
Public Const HH_SET_WIN_TYPE = &H4&
Public Const HH_GET_WIN_TYPE = &H5&
Public Const HH_GET_WIN_HANDLE = &H6&
Public Const HH_ENUM_INFO_TYPE = &H7&
Public Const HH_SET_INFO_TYPE = &H8&
Public Const HH_SYNC = &H9&
Public Const HH_ADD_NAV_UI = &H10& ‘// Currently Not Implemented
Public Const HH_ADD_BUTTON = &H11& ‘// Currently Not Implemented
Public Const HH_GETBROWSER_APP = &H12& ‘// Currently Not Implemented
Public Const HH_KEYWORD_LOOKUP = &H13&
Public Const HH_DISPLAY_TEXT_POPUP = &H14&
Public Const HH_HELP_CONTEXT = &H15&
Public Const HH_TP_HELP_CONTEXTMENU = &H16&
Public Const HH_TP_HELP_WM_HELP = &H17&
Public Const HH_CLOSE_ALL = &H18&
Public Const HH_ALINK_LOOKUP = &H19&
Public Const HH_GET_LAST_ERROR = &H20& ‘// Currently Not Implemented
Public Const HH_ENUM_CATEGORY = &H21&
Public Const HH_ENUM_CATEGORY_IT = &H22&
Public Const HH_RESET_IT_FILTER = &H23&
Public Const HH_SET_INCLUSIVE_FILTER = &H24&
Public Const HH_SET_EXCLUSIVE_FILTER = &H25&
Public Const HH_SET_GUID = &H26&
Public Const HH_INTERNAL = &H255&

‘//
‘// Constants (Private)
‘//

Private Const FW_BOLD = 700
Private Const GMEM_MOVEABLE = &H2
Private Const GMEM_ZEROINIT = &H40
Private Const GHND = (GMEM_MOVEABLE Or GMEM_ZEROINIT)
Private Const OFN_ALLOWMULTISELECT = &H200
Private Const OFN_CREATEPROMPT = &H2000
Private Const OFN_ENABLEHOOK = &H20
Private Const OFN_ENABLETEMPLATE = &H40
Private Const OFN_ENABLETEMPLATEHANDLE = &H80
Private Const OFN_EXPLORER = &H80000
Private Const OFN_EXTENSIONDIFFERENT = &H400
Private Const OFN_FILEMUSTEXIST = &H1000
Private Const OFN_HIDEREADONLY = &H4
Private Const OFN_LONGNAMES = &H200000
Private Const OFN_NOCHANGEDIR = &H8
Private Const OFN_NODEREFERENCELINKS = &H100000
Private Const OFN_NOLONGNAMES = &H40000
Private Const OFN_NONETWORKBUTTON = &H20000
Private Const OFN_NOREADONLYRETURN = &H8000
Private Const OFN_NOTESTFILECREATE = &H10000
Private Const OFN_NOVALIDATE = &H100
Private Const OFN_OVERWRITEPROMPT = &H2
Private Const OFN_PATHMUSTEXIST = &H800
Private Const OFN_READONLY = &H1
Private Const OFN_SHAREAWARE = &H4000
Private Const OFN_SHAREFALLTHROUGH = 2
Private Const OFN_SHARENOWARN = 1
Private Const OFN_SHAREWARN = 0
Private Const OFN_SHOWHELP = &H10
Private Const PD_ENABLEPRINTHOOK = &H1000
Private Const PD_ENABLEPRINTTEMPLATE = &H4000
Private Const PD_ENABLEPRINTTEMPLATEHANDLE = &H10000
Private Const PD_ENABLESETUPHOOK = &H2000
Private Const PD_ENABLESETUPTEMPLATE = &H8000
Private Const PD_ENABLESETUPTEMPLATEHANDLE = &H20000
Private Const PD_NONETWORKBUTTON = &H200000
Private Const PD_PRINTSETUP = &H40
Private Const PD_USEDEVMODECOPIES = &H40000
Private Const PD_USEDEVMODECOPIESANDCOLLATE = &H40000
Private Const PD_NOWARNING = &H80
Private Const CF_ANSIONLY = &H400&
Private Const CF_APPLY = &H200&
Private Const CF_BITMAP = 2
Private Const CF_PRINTERFONTS = &H2
Private Const CF_PRIVATEFIRST = &H200
Private Const CF_PRIVATELAST = &H2FF
Private Const CF_RIFF = 11
Private Const CF_SCALABLEONLY = &H20000
Private Const CF_SCREENFONTS = &H1
Private Const CF_BOTH = (CF_SCREENFONTS Or CF_PRINTERFONTS)
Private Const CF_DIB = 8
Private Const CF_DIF = 5
Private Const CF_DSPBITMAP = &H82
Private Const CF_DSPENHMETAFILE = &H8E
Private Const CF_DSPMETAFILEPICT = &H83
Private Const CF_DSPTEXT = &H81
Private Const CF_EFFECTS = &H100&
Private Const CF_ENABLEHOOK = &H8&
Private Const CF_ENABLETEMPLATE = &H10&
Private Const CF_ENABLETEMPLATEHANDLE = &H20&
Private Const CF_ENHMETAFILE = 14
Private Const CF_FIXEDPITCHONLY = &H4000&
Private Const CF_FORCEFONTEXIST = &H10000
Private Const CF_GDIOBJFIRST = &H300
Private Const CF_GDIOBJLAST = &H3FF
Private Const CF_INITTOLOGFONTSTRUCT = &H40&
Private Const CF_LIMITSIZE = &H2000&
Private Const CF_METAFILEPICT = 3
Private Const CF_NOFACESEL = &H80000
Private Const CF_NOVERTFONTS = &H1000000
Private Const CF_NOVECTORFONTS = &H800&
Private Const CF_NOOEMFONTS = CF_NOVECTORFONTS
Private Const CF_NOSCRIPTSEL = &H800000
Private Const CF_NOSIMULATIONS = &H1000&
Private Const CF_NOSIZESEL = &H200000
Private Const CF_NOSTYLESEL = &H100000
Private Const CF_OEMTEXT = 7
Private Const CF_OWNERDISPLAY = &H80
Private Const CF_PALETTE = 9
Private Const CF_PENDATA = 10
Private Const CF_SCRIPTSONLY = CF_ANSIONLY
Private Const CF_SELECTSCRIPT = &H400000
Private Const CF_SHOWHELP = &H4&
Private Const CF_SYLK = 4
Private Const CF_TEXT = 1
Private Const CF_TIFF = 6
Private Const CF_TTONLY = &H40000
Private Const CF_UNICODETEXT = 13
Private Const CF_USESTYLE = &H80&
Private Const CF_WAVE = 12
Private Const CF_WYSIWYG = &H8000
Private Const CFERR_CHOOSEFONTCODES = &H2000
Private Const CFERR_MAXLESSTHANMIN = &H2002
Private Const CFERR_NOFONTS = &H2001
Private Const CC_ANYCOLOR = &H100
Private Const CC_CHORD = 4
Private Const CC_CIRCLES = 1
Private Const CC_ELLIPSES = 8
Private Const CC_ENABLEHOOK = &H10
Private Const CC_ENABLETEMPLATE = &H20
Private Const CC_ENABLETEMPLATEHANDLE = &H40
Private Const CC_FULLOPEN = &H2
Private Const CC_INTERIORS = 128
Private Const CC_NONE = 0
Private Const CC_PIE = 2
Private Const CC_PREVENTFULLOPEN = &H4
Private Const CC_RGBINIT = &H1
Private Const CC_ROUNDRECT = 256 ‘
Private Const CC_SHOWHELP = &H8
Private Const CC_SOLIDCOLOR = &H80
Private Const CC_STYLED = 32
Private Const CC_WIDE = 16
Private Const CC_WIDESTYLED = 64
Private Const CCERR_CHOOSECOLORCODES = &H5000
Private Const LOGPIXELSY = 90
Private Const CCHDEVICENAME = 32
Private Const CCHFORMNAME = 32
Private Const SIMULATED_FONTTYPE = &H8000
Private Const PRINTER_FONTTYPE = &H4000
Private Const SCREEN_FONTTYPE = &H2000
Private Const BOLD_FONTTYPE = &H100
Private Const ITALIC_FONTTYPE = &H200
Private Const REGULAR_FONTTYPE = &H400
Private Const WM_CHOOSEFONT_GETLOGFONT = (&H400 + 1)
Private Const LBSELCHSTRING = “commdlg_LBSelChangedNotify”
Private Const SHAREVISTRING = “commdlg_ShareViolation”
Private Const FILEOKSTRING = “commdlg_FileNameOK”
Private Const COLOROKSTRING = “commdlg_ColorOK”
Private Const SETRGBSTRING = “commdlg_SetRGBColor”
Private Const FINDMSGSTRING = “commdlg_FindReplace”
Private Const HELPMSGSTRING = “commdlg_help”
Private Const CD_LBSELNOITEMS = -1
Private Const CD_LBSELCHANGE = 0
Private Const CD_LBSELSUB = 1
Private Const CD_LBSELADD = 2
Private Const NOERROR = 0
Private Const CSIDL_DESKTOP = &H0
Private Const CSIDL_PROGRAMS = &H2
Private Const CSIDL_CONTROLS = &H3
Private Const CSIDL_PRINTERS = &H4
Private Const CSIDL_PERSONAL = &H5
Private Const CSIDL_FAVORITES = &H6
Private Const CSIDL_STARTUP = &H7
Private Const CSIDL_RECENT = &H8
Private Const CSIDL_SENDTO = &H9
Private Const CSIDL_BITBUCKET = &HA
Private Const CSIDL_STARTMENU = &HB
Private Const CSIDL_DESKTOPDIRECTORY = &H10
Private Const CSIDL_DRIVES = &H11
Private Const CSIDL_NETWORK = &H12
Private Const CSIDL_NETHOOD = &H13
Private Const CSIDL_FONTS = &H14
Private Const CSIDL_TEMPLATES = &H15
Private Const BIF_RETURNONLYFSDIRS = &H1
Private Const BIF_DONTGOBELOWDOMAIN = &H2
Private Const BIF_STATUSTEXT = &H4
Private Const BIF_RETURNFSANCESTORS = &H8
Private Const BIF_BROWSEFORCOMPUTER = &H1000
Private Const BIF_BROWSEFORPRINTER = &H2000
Private Const HWND_BROADCAST = &HFFFF&
Private Const WM_WININICHANGE = &H1A
Private Const WM_USER = &H400
Private Const BFFM_INITIALIZED = 1
Private Const BFFM_SELCHANGED = 2
Private Const BFFM_SETSTATUSTEXT = (WM_USER + 100)
Private Const BFFM_SETSelectION = (WM_USER + 102)
Private Const BIF_USENEWUI = &H40 ‘对话框上带“新建文件夹”按钮

Private m_CurrentDirectory As String ‘The current directory

‘//
‘// SetDefaultPrinter Function
‘//
‘// Description:
‘// Sets the user’s default printer to the printer represented by the passed printer object.
‘//
‘// Syntax:
‘// BOOL = SetDefaultPrinter(object)
‘//
‘// Example:
‘// Dim objNewPrinter As Printer
‘// Set objNewPrinter = Printers(2)
‘// SetDefaultPrinter objNewPrinter
‘//
Public Function SetDefaultPrinter(objPrn As Printer) As Boolean
Dim szTmp As String
szTmp = objPrn.DeviceName & “,” & objPrn.DriverName & “,” & objPrn.Port
lRet = WriteProfileString(“windows”, “device”, szTmp)
lRet = SendMessageByString(HWND_BROADCAST, WM_WININICHANGE, 0&, “windows”)
End Function

‘//
‘// GetDefaultPrinter Function
‘//
‘// Description:
‘// Retuns the device name of the default printer.
‘//
‘// Syntax:
‘// StrVar = GetDefaultPrinter()
‘//
‘// Example:
‘// szDefPrinter = GetDefaultPrinter
‘//
Public Function GetDefaultPrinter() As String
Dim szTmp As String, dwBuf As Long
dwBuf = 1024
szTmp = Space$(dwBuf + 1)
lRet = GetProfileString(“windows”, “device”, “”, szTmp, dwBuf)
GetDefaultPrinter = Trim(Left(szTmp, lRet))
End Function

‘//
‘// ResetDefaultPrinter Function
‘//
‘// Description:
‘// Resets the default printer to the passed device name.
‘//
‘// Syntax:
‘// BOOL = ResetDefaultPrinter(StrVar)
‘//
‘// Example:
‘// szDefPrinter = GetDefaultPrinter()
‘// If Not ResetDefaultPrinter(szDefPrinter) Then
‘// MsgBox “Could not reset default printer.”, vbExclamation
‘// End If
‘//
Public Function ResetDefaultPrinter(szBuf As String) As Boolean
lRet = WriteProfileString(“windows”, “device”, szBuf)
lRet = SendMessageByString(HWND_BROADCAST, WM_WININICHANGE, 0&, “windows”)
End Function

‘//
‘// BrowseFolder Function
‘//
‘// Description:
‘// Allows the user to interactively browse and select a folder found in the file system.
‘//
‘// Syntax:
‘// StrVar = BrowseFolder(lhWnd, StrVar)
‘//
‘// Example:
‘// szFilename = BrowseFolder(Me.hWnd, “Browse for application folder:”)
‘//
Public Function BrowseFolder(lhWnd As Long, szDialogTitle As String) As String
Dim BI As BROWSEINFO, dwIList As Long, szPath As String, wPos As Integer
BI.hOwner = lhWnd
BI.lpszTitle = szDialogTitle
BI.ulFlags = BIF_RETURNONLYFSDIRS
dwIList = SHBrowseForFolder(BI)
szPath = Space$(512)
lRet = SHGetPathFromIDList(ByVal dwIList, ByVal szPath)
If lRet Then
wPos = InStr(szPath, Chr(0))
BrowseFolder = Left$(szPath, wPos – 1)
Else
BrowseFolder = “”
End If
End Function

‘//
‘// DialogConnectToPrinter Function
‘//
‘// Description:
‘// Allows users to interactively selection and connect to local and network printers.
‘//
‘// Syntax:
‘// DialogConnectToPrinter
‘//
‘// Example:
‘// DialogConnectToPrinter
‘//
Public Function DialogConnectToPrinter() As Boolean
Shell “rundll32.exe shell32.dll,SHHelpShortcuts_RunDLL AddPrinter”, vbNormalFocus
End Function

‘//
‘// ByteToString Function
‘//
‘// Description:
‘// Converts an array of bytes into a string
‘//
‘// Syntax:
‘// StrVar = ByteToString(ARRAY)
‘//
‘// Example:
‘// szBuf = BytesToString(aChars(10))
‘//
Private Function ByteToString(aBytes() As Byte) As String
Dim dwBytePoint As Long, dwByteVal As Long, szOut As String
dwBytePoint = LBound(aBytes)
While dwBytePoint <= UBound(aBytes)
dwByteVal = aBytes(dwBytePoint)
If dwByteVal = 0 Then
ByteToString = szOut
Exit Function
Else
szOut = szOut & Chr$(dwByteVal)
End If
dwBytePoint = dwBytePoint + 1
Wend
ByteToString = szOut
End Function

‘//
‘// DialogColor Function
‘//
‘// Description:
‘// Displays the Color common dialog box and sets a passed controls foreground color.
‘//
‘// Syntax:
‘// BOOL = DialogColor(lhWnd, CONTROL)
‘//
‘// Example:
‘// Dim yn as Boolean
‘// yn = DialogColor(Me.hWnd, txtEditor)
‘//
Public Function DialogColor(lhWnd As Long, C As Control) As Boolean
Dim CS As COLORSTRUC, CustColor(16) As Long
CS.lStructSize = Len(CS)
CS.hwnd = lhWnd
CS.hInstance = App.hInstance
CS.Flags = CC_SOLIDCOLOR
CS.lpCustColors = String$(16 * 4, 0)
lRet = ChooseColor(CS)
If lRet = 0 Then
DialogColor = False
Else
DialogColor = True
C.ForeColor = CS.rgbResult
End If
End Function

‘//
‘// DialogFont Function
‘//
‘// Description:
‘// Displays the Font common dialog box and sets a passed controls font properties.
‘//
‘// Syntax:
‘// BOOL = DialogFont(lhWnd, CONTROL)
‘//
‘// Example:
‘// Dim yn as Boolean
‘// yn = DialogFont(Me.hWnd, txtEditor)
‘//
Public Function DialogFont(lhWnd As Long, C As Control) As Boolean
Dim lf As LogFont, FS As FONTSTRUC
Dim lLogFontAddress As Long, lMemHandle As Long
If C.Font.Bold Then lf.lfWeight = FW_BOLD
If C.Font.Italic = True Then lf.lfItalic = 1
If C.Font.Underline = True Then lf.lfUnderline = 1
FS.lStructSize = Len(FS)
lMemHandle = GlobalAlloc(GHND, Len(lf))
If lMemHandle = 0 Then
DialogFont = False
Exit Function
End If
lLogFontAddress = GlobalLock(lMemHandle)
If lLogFontAddress = 0 Then
DialogFont = False
Exit Function
End If
CopyMemory ByVal lLogFontAddress, lf, Len(lf)
FS.lpLogFont = lLogFontAddress
FS.iPointSize = C.Font.Size * 10
FS.Flags = CF_SCREENFONTS Or CF_EFFECTS
If ChooseFont(FS) = 1 Then
CopyMemory lf, ByVal lLogFontAddress, Len(lf)
If lf.lfWeight >= FW_BOLD Then
C.Font.Bold = True
Else
C.Font.Bold = False
End If
If lf.lfItalic = 1 Then
C.Font.Italic = True
Else
C.Font.Italic = False
End If
If lf.lfUnderline = 1 Then
C.Font.Underline = True
Else
C.Font.Underline = False
End If
C.Font.Name = ByteToString(lf.lfFaceName())
C.Font.Size = CLng(FS.iPointSize / 10)
DialogFont = True
Else
DialogFont = False
End If
End Function

‘//
‘// DialogFile Function
‘//
‘// Description:
‘// Displays the File Open/Save As common dialog boxes.
‘//
‘// Syntax:
‘// StrVar = DialogFile(lhWnd, IntVar, StrVar, StrVar, StrVar, StrVar, StrVar, IntVar)
‘//
‘// Example:
‘// szFilename = DialogFile(Me.hWnd, 1, “Open”, “MyFileName.doc”, “Documents” & “|” & “.doc” & “|” & “All files” & “|” & “.*”, App.Path, “doc”, Path, 1)
‘//
Public Function DialogFile(lhWnd As Long, wMode As Integer, szDialogTitle As String, ByVal szFilename As String, szFilter As String, ByVal szDefDir As String, szDefExt As String, szDestDir As String, ByVal iFilterIndex As Integer) As String
szFilter = Replace(szFilter, “|”, Chr(0), 1, -1, vbBinaryCompare)
Dim OFN As OPENFILENAME, szFile As String, szFileTitle As String
OFN.lStructSize = Len(OFN)
OFN.hwnd = lhWnd
OFN.lpstrTitle = szDialogTitle
OFN.lpstrFile = szFilename & String$(250 – Len(szFilename), 0)
OFN.nMaxFile = BUFFER
OFN.lpstrFileTitle = String$(BUFFER, 0)
OFN.nMaxFileTitle = BUFFER
OFN.lpstrFilter = szFilter
OFN.nFilterIndex = iFilterIndex
OFN.lpstrInitialDir = szDefDir
OFN.lpstrDefExt = szDefExt
If wMode = 1 Then
OFN.Flags = OFN_HIDEREADONLY Or OFN_PATHMUSTEXIST Or OFN_FILEMUSTEXIST
lRet = GetOpenFileName(OFN)
Else
OFN.Flags = OFN_HIDEREADONLY Or OFN_OVERWRITEPROMPT Or OFN_PATHMUSTEXIST
lRet = GetSaveFileName(OFN)
End If
If lRet <> 0 Then
‘// If InStr(OFN.lpstrFileTitle, Chr$(0)) > 0 Then
‘// szFileTitle = Left$(OFN.lpstrFileTitle, InStr(OFN.lpstrFileTitle, Chr$(0)) – 1)
‘// End If
If InStr(OFN.lpstrFile, Chr$(0)) > 0 Then
szFile = Left$(OFN.lpstrFile, InStr(OFN.lpstrFile, Chr$(0)) – 1)
End If
‘// OFN.nFileOffset is the number of characters from the beginning of the
‘// full path to the start of the file name
‘// OFN.nFileExtension is the number of characters from the beginning of the
‘// full path to the file’s extention, including the (.)
‘// MsgBox “File Name is ” & szFileTitle & Chr$(13) & Chr$(10) & “Full path and file is ” & szFile, , “Open”

‘// DialogFile = szFile & “|” & szFileTitle
DialogFile = szFile
szDestDir = Left(szFile, OFN.nFileOffset)
Else
DialogFile = “”
End If
End Function

‘//
‘// DialogOpenFile Function
‘//
‘// Description:
‘// Displays the MultiFile Open As common dialog boxes.
‘//
‘// Syntax:
‘// StrVar = DialogOpenFile(lhWnd, StrVar, StrVar, StrVar)
‘//
‘// Example:
‘// szFilename = DialogOpenFile(Me.hWnd, “Open MultiFile”, “Documents” & “|” & “.doc” & “|” & “All files” & “|” & “.*”, App.Path)
‘//
Public Function DialogOpenFile(lhWnd As Long, szDialogTitle As String, szFilter As String, ByVal szDefDir As String) As String
szFilter = Replace(szFilter, “|”, Chr(0), 1, -1, vbBinaryCompare)
Dim OFN As OPENFILENAME, szFile As String, szFileTitle As String
OFN.lStructSize = Len(OFN)
OFN.hwnd = lhWnd
OFN.lpstrTitle = szDialogTitle
OFN.lpstrFile = Space$(BUFFER – 1) ‘String$(BUFFER, 0) ‘szFilename & String$(250 – Len(szFilename), 0)
OFN.nMaxFile = BUFFER
OFN.lpstrFileTitle = Space$(BUFFER – 1) ‘String$(BUFFER, 0)
OFN.nMaxFileTitle = BUFFER
OFN.lpstrFilter = szFilter
‘OFN.nFilterIndex = iFilterIndex
OFN.lpstrInitialDir = szDefDir
‘OFN.lpstrDefExt = szDefExt
OFN.Flags = OFN_ALLOWMULTISELECT Or OFN_EXPLORER Or OFN_FILEMUSTEXIST
lRet = GetOpenFileName(OFN)
If lRet <> 0 Then
DialogOpenFile = OFN.lpstrFile
Else
DialogOpenFile = “”
End If
End Function

‘//
‘// DialogPrint Function
‘//
‘// Description:
‘// Displays the Print common dialog box and returns a structure containing user entered
‘// information from the common dialog box.
‘//
‘// Syntax:
‘// PRINTPROPS = DialogPrint(lhWnd, BOOL, DWORD)
‘//
‘// Example:
‘// Dim PP As PRINTPROPS
‘// PP = DialogPrint(Me.hWnd, True, PD_PAGENUMS or PD_SELECTION or PD_SHOWHELP)
‘//
Public Function DialogPrint(lhWnd As Long, bPages As Boolean, Flags As Long) As PRINTPROPS
Dim DM As DEVMODE, PD As PRINTDLGSTRUC
Dim lpDM As Long, wNull As Integer, szDevName As String
PD.lStructSize = Len(PD)
PD.hwnd = lhWnd
PD.hDevMode = 0
PD.hDevNames = 0
PD.hDC = 0
PD.Flags = Flags
PD.nFromPage = 0
PD.nToPage = 0
PD.nMinPage = 0
If bPages Then PD.nMaxPage = bPages – 1
PD.nCopies = 0
DialogPrint.Cancel = True
If PrintDlg(PD) Then
lpDM = GlobalLock(PD.hDevMode)
CopyMemory DM, ByVal lpDM, Len(DM)
lpDM = GlobalUnlock(PD.hDevMode)
DialogPrint.Cancel = False
DialogPrint.Device = Left$(DM.dmDeviceName, InStr(DM.dmDeviceName, Chr(0)) – 1)
DialogPrint.FromPage = 0
DialogPrint.ToPage = 0
DialogPrint.All = True
If PD.Flags And PD_PRINTTOFILE Then DialogPrint.File = True Else DialogPrint.File = False
If PD.Flags And PD_COLLATE Then DialogPrint.Collate = True Else DialogPrint.Collate = False
If PD.Flags And PD_PAGENUMS Then
DialogPrint.Pages = True
DialogPrint.All = False
DialogPrint.FromPage = PD.nFromPage
DialogPrint.ToPage = PD.nToPage
Else
DialogPrint.Pages = False
End If
If PD.Flags And PD_SELECTION Then
DialogPrint.Selection = True
DialogPrint.All = False
Else
DialogPrint.Pages = False
End If
If PD.nCopies = 1 Then
DialogPrint.Copies = DM.dmCopies
End If
DialogPrint.DM = DM
End If
End Function

‘//
‘// DialogPrintSetup Function
‘//
‘// Description:
‘// Displays the Print Setup common dialog box.
‘//
‘// Syntax:
‘// BOOL = DialogPrintSetup(lhWnd)
‘//
‘// Example:
‘// If DialogPrintSetup(Me.hWnd) Then
‘// End If
‘//
Public Function DialogPrintSetup(lhWnd As Long) As Boolean
Dim PD As PRINTDLGSTRUC
PD.lStructSize = Len(PD)
PD.hwnd = lhWnd
PD.Flags = PD_PRINTSETUP
lRet = PrintDlg(PD)
End Function

‘//
‘// BrowseForFolder Function
‘//
‘// Description:
‘// Allows the user to interactively browse and select a folder found from a folder in the file system.
‘//
‘// Syntax:
‘// StrVar = BrowseForFolder(lhWnd, StrVar, StrVar)
‘//
‘// Example:
‘// szFilename = BrowseForFolder(Me.hWnd, “Browse for application folder:”, App.Path)
‘//
‘Public Function BrowseForFolder(oWner As Form, sTitle As String, sStartDir As String) As String
Public Function BrowseForFolder(lhWnd As Long, sTitle As String, sStartDir As String) As String
Dim lpIDList As Long
Dim szTitle As String
Dim sBuffer As String
Dim tBrowseInfo As BROWSEINFO
m_CurrentDirectory = sStartDir & vbNullChar
szTitle = sTitle
With tBrowseInfo
.hOwner = lhWnd
.lpszTitle = szTitle ‘lstrcat(szTitle, “”)
.ulFlags = BIF_RETURNONLYFSDIRS + BIF_DONTGOBELOWDOMAIN + BIF_STATUSTEXT
.lpfnCallback = GetAddressofFunction(AddressOf BrowseCallbackProc) ‘get address of function.
End With
lpIDList = SHBrowseForFolder(tBrowseInfo)
If (lpIDList) Then
sBuffer = Space$(BUFFER)
SHGetPathFromIDList lpIDList, sBuffer
sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) – 1)
BrowseForFolder = sBuffer
Else
BrowseForFolder = “”
End If
End Function

‘//
‘// BrowseForNewFolder Function
‘//
‘// Description:
‘// Allows the user to interactively browse and select a folder found from a folder in the file system with new folder.
‘//
‘// Syntax:
‘// StrVar = BrowseForNewFolder(lhWnd, StrVar, StrVar)
‘//
‘// Example:
‘// szFilename = BrowseForNewFolder(Me.hWnd, “Browse for application folder:”, App.Path)
‘//
‘Public Function BrowseForNewFolder(oWner As Form, sTitle As String, sStartDir As String) As String
Public Function BrowseForNewFolder(lhWnd As Long, sTitle As String, sStartDir As String) As String
Dim iNull As Integer
Dim lpIDList As Long
Dim szTitle As String
Dim sBuffer As String
Dim tBrowseInfo As BROWSEINFO
m_CurrentDirectory = sStartDir & vbNullChar
szTitle = sTitle
With tBrowseInfo
.hOwner = lhWnd ‘设置主窗体句柄
.lpszTitle = szTitle ‘lstrcat(szTitle, “”)
.ulFlags = BIF_RETURNONLYFSDIRS Or BIF_USENEWUI ‘新建文件夹
.lpfnCallback = GetAddressofFunction(AddressOf BrowseCallbackProc) ‘get address of function.
End With
‘显示列表框
lpIDList = SHBrowseForFolder(tBrowseInfo)
If (lpIDList) Then
sBuffer = String$(BUFFER, 0)
‘获取返回的路径
SHGetPathFromIDList lpIDList, sBuffer
‘释放内存块
CoTaskMemFree lpIDList
iNull = InStr(sBuffer, vbNullChar) ‘去除空格符
If iNull Then sBuffer = Left$(sBuffer, iNull – 1)
End If
BrowseForNewFolder = sBuffer
End Function

Private Function BrowseCallbackProc(ByVal lhWnd As Long, ByVal uMsg As Long, ByVal lp As Long, ByVal pData As Long) As Long
Dim lpIDList As Long
Dim sBuffer As String
If Not ExposeError Then On Error Resume Next
Select Case uMsg
Case BFFM_INITIALIZED
Call SendMessage(lhWnd, BFFM_SETSelectION, 1, m_CurrentDirectory)
Case BFFM_SELCHANGED
sBuffer = Space$(BUFFER)
lRet = SHGetPathFromIDList(lp, sBuffer)
If lRet = 1 Then
Call SendMessage(lhWnd, BFFM_SETSTATUSTEXT, 0, sBuffer)
End If
End Select
BrowseCallbackProc = 0
End Function

Private Function GetAddressofFunction(lAdd As Long) As Long
GetAddressofFunction = lAdd
End Function