你的位置:首页 > 操作系统

[操作系统]类模块基础


附带东野圭吾小说集(txt文件)http://pan.baidu.com/s/1slMSFxj

类模块有多种用途,主要用于以下几个方面:

1.封装相似功能到单个对象中

2.建立带有属性、方法和事件的对象

3.特为自定义集合建立类模块

封装相似功能:

以一个名为clsUStationDialog的类开始。

使用这个类能做些什么:

显示打开MicroStation DGN文件的FileOpen对话框

显示打开Microsoft Excel文件的FileOpen对话框

显示打开ASCII.txt文件的FileOpen对话框

显示打开用户指定扩展名的FileOpen对话框

文件名需要的属性

仅路径需要的属性

路径/文件名需要的属性

Private Declare Function mdlDialog_fileOpen Lib "stdmdlbltin.dll" (ByVal _fileName As String, ByVal rFileH As Long, ByVal _resourceId As Long, ByVal suggestedFileName As String, _ByVal filterString As String, ByVal defaultDirectory As String, _ByVal titleString As String) As LongPrivate Declare Function mdlDialog_fileCreate Lib _"stdmdlbltin.dll" (ByVal _fileName As String, ByVal rFileH As Long, _ByVal resourceId As Long, _ByVal suggestedFileName As String, _ByVal filterString As String, _ByVal defaultDirectory As String, _ByVal titleString As String) As LongPrivate pFilePath As StringPrivate pFileName As StringPrivate pDefFilePath As StringPrivate pDefFileName As StringPrivate pFileNameSelected As StringPrivate pRetVal As LongPrivate pFileExts() As StringProperty Get SelectedPath() As StringSelectedPath = pFilePathEnd PropertyProperty Get SelectedFile() As StringSelectedFile = pFileNameEnd PropertyProperty Get OpenSuccess() As BooleanSelect Case pRetValCase 1 '取消OpenSuccess = FalseCase 0 '打开OpenSuccess = TrueEnd SelectEnd PropertySub OpenDialog()Dim tmpFilter As StringpRetVal = 1tmpFilter = "*." & Join(GetExts, "; *.")pFileNameSelected = Space(255)pRetVal = mdlDialog_fileOpen(pFileNameSelected, 0, 0, _pDefFileName, tmpFilter, pDefFilePath, "Open File")Select Case pRetValCase 1 '取消Case 0 '打开Dim tmpFile As StringDim xSplit As VarianttmpFile = Left(pFileNameSelected, InStr(1, _pFileNameSelected, Chr(0)) - 1)xSplit = Split(tmpFile, "\")pFileName = xSplit(UBound(xSplit))xSplit(UBound(xSplit)) = ""pFilePath = Join(xSplit, "\")End SelectEnd SubProperty Get DefaultFile() As StringDefaultFile = pDefFileNameEnd PropertyProperty Let DefaultFile(strFilIn As String)pDefFileName = strFileInEnd PropertyProperty Get DefaultPath() As StringDefaultPath = pDefFilePathEnd PropertyProperty Let DefaultPath(strPathIN As String)On Error GoTo errhndIf Dir(strPathIN, vbDirectory) <> "" ThenpDefFilePath = strPathINEnd IfExit Propertyerrhnd:Select Case Err.NumberCase 52 '错误文件名或文件号Err.ClearEnd SelectEnd PropertyProperty Get ExtCount() As LongExtCount = UBound(pFileExts)End PropertyProperty Get GetExts() As String()If UBound(pFileExts) = 0 ThenExit PropertyEnd IfDim tmpGetExts() As StringReDim tmpGetExts(UBound(pFileExts) - 1) As StringDim I As LongFor I = 1 To UBound(pFileExts)tmpGetExts(I - 1) = pFileExts(I)Next IGetExts = tmpGetExtsEnd PropertyPrivate Sub Class_Initialize()ReDim pFileExts(0)End SubPublic Sub AddFileExt(FileExt As String)Dim I As LongDim tmpFileExt As StringtmpFileExt = LCase(Replace(FileExt, ".", ""))For I = 1 To UBound(pFileExts)If tmpFileExt = pFileExts(I) ThenExit SubEnd IfNext IReDim Preserve pFileExts(UBound(pFileExts) + 1)pFileExts(UBound(pFileExts)) = tmpFileExtEnd SubSub CreateDialog()Dim tmpFilter As StringpRetVal = 1tmpFilter = "*." & Join(GetExts, "; *.")pFileNameSelected = Space(255)pRetVal = mdlDialog_fileCreate(pFileNameSelected, 0, 0, _pDefFileName, tmpFilter, pDefFilePath, "Create File")Select Case pRetValCase 1 '取消Case 0 '打开Dim tmpFile As StringDim xSplit As VarianttmpFile = Left(pFileNameSelected, InStr(1, _pFileNameSelected, Chr(0) - 1))xSplit = Split(tmpFile, "\")pFileName = xSplit(UBound(xSplit))xSplit(UBound(xSplit)) = ""pFliePath = Join(xSplit, "\")End SelectEnd Sub

 

测试代码1:

Sub TestShowDialogA()Dim MyUSD As New clsUSataionDialogMyUSD.AddFileExt "dgn"MyUSD.DefaultPath = "c:\"MyUSD.DefaultFile = "temp.dgn"MyUSD.OpenDialogSelect Case MyUSD.OpenSuccessCase TrueMsgBox MyUSD.SelectedPath & MyUSD.SelectedFileEnd SelectEnd Sub

 

测试截图:

clip_image001

测试代码2:

Sub TestShowDialogB()Dim MyUSD As New clsUSataionDialogMyUSD.AddFileExt "dgn"MyUSD.AddFileExt "dwg"MyUSD.AddFileExt "dxf"MyUSD.DefaultFile = "c:\MicroStation VBA"MyUSD.DefaultFile = "test.dgn"MyUSD.OpenDialogSelect Case MyUSD.OpenSuccessCase TrueMsgBox MyUSD.SelectedPath & MyUSD.SelectedFileEnd SelectEnd Sub

 

测试截图2:

clip_image002

测试代码3:

Sub TestShowDialogC()Dim MyUSD As New clsUSataionDialogMyUSD.AddFileExt "dgn"MyUSD.DefaultFile = "c:\"MyUSD.DefaultFile = "test.dgn"MyUSD.CreateDialogSelect Case MyUSD.OpenSuccessCase TrueMsgBox MyUSD.SelectedPath & MyUSD.SelectedFileEnd SelectEnd Sub

 

测试截图3:

clip_image003

测试代码4:

Sub TestShowDialogD()Dim MyUSD As New clsUSataionDialogMyUSD.AddFileExt "ILoveyou"MyUSD.AddFileExt " LOVEYOU"MyUSD.AddFileExt "Forever"MyUSD.DefaultPath = "c:\MicroStation VBA"MyUSD.DefaultFile = "test.dgn"MyUSD.CreateDialogSelect Case MyUSD.OpenSuccessCase TrueMsgBox MyUSD.SelectedPath & MyUSD.SelectedFileEnd SelectEnd Sub

 

测试截图4:

clip_image004

测试代码5:

Sub TestShowDialogE()Dim MyUSD As New clsUSataionDialogMyUSD.AddFileExt "loveyou"MyUSD.DefaultPath = "c:\"MyUSD.DefaultFile = "test.dgn"MyUSD.OpenDialogSelect Case MyUSD.OpenSuccessCase TrueMsgBox "Open " & MyUSD.SelectedPath & _MyUSD.SelectedFileCase FalseIf MsgBox("Create a new file?", vbYesNo) = vbYes ThenMyUSD.CreateDialogIf MyUSD.OpenSuccess = True ThenMsgBox "Create" & MyUSD.SelectedPath & _MyUSD.SelectedFileEnd IfEnd IfEnd SelectEnd Sub

 

测试截图5:

clip_image005

 

 

image[3]