当前位置:首页 > 编程学习 > 用VB类实现文件对话框

用VB类实现文件对话框

编程学习2007-11-0450530

用VB类实现文件对话框

'类名:ComDlg.cls
'作用:文件打开保存对话框
'
'
'
'By:Apull
'2007-5-21
'http://www.apull.net
       
       
Option Explicit
       
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 Type OPENFILENAME
lStructSize   As Long
hWndOwner   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
       
' file  constants
Const cdlOFNAllowMultiselect =  &H200
Const  cdlOFNCreatePrompt = &H2000
Const cdlOFNExplorer =  &H80000
Const  cdlOFNExtensionDifferent = &H400
Const cdlOFNFileMustExist =  &H1000
Const  cdlOFNHelpButton = &H10
Const cdlOFNHideReadOnly =  4
Const cdlOFNLongNames =  &H200000
Const  cdlOFNNoChangeDir = 8
Const cdlOFNNoDereferenceLinks =  &H100000
Const  cdlOFNNoLongNames = &H40000
Const cdlOFNNoReadOnlyReturn =  &H8000
Const  cdlOFNNoValidate = &H100
Const cdlOFNOverwritePrompt =  2
Const cdlOFNPathMustExist =  &H800
Const  cdlOFNReadOnly = 1
Const cdlOFNShareAware =  &H4000
       
       
'属性  =======================
'保持属性值的局部变量
Private  sPath As String
       
Private mFileName As String
Private mFileTitle As String
Private mhOwner As Long
Private mDialogTitle As String
Private mFilter As String
Private mInitDir As String
Private mDefaultExt As String
Private mFilterIndex As Long
Private mFlags As Long
Private mHelpFile As String
Private mHelpCommand As Long
Private mHelpKey As String
       
       
       
Friend Property Get DefaultExt() As String
DefaultExt =  mDefaultExt
End  Property
       
Friend  Property Let DefaultExt(sDefExt  As String)
mDefaultExt =  sDefExt
End  Property
       
Friend  Property Get DialogTitle() As String
DialogTitle =  mDialogTitle
End  Property
       
Friend  Property Let DialogTitle(sTitle  As String)
mDialogTitle =  sTitle
End  Property
       
Friend  Property Get FileName() As String
FileName =  mFileName
End  Property
       
Friend  Property Let FileName(sFileName  As String)
mFileName =  sFileName
End  Property
       
Friend  Property Get FileTitle() As String
FileTitle =  mFileTitle
End  Property
       
Friend  Property Let FileTitle(sTitle  As String)
mFileTitle =  sTitle
End  Property
       
Friend  Property Get Filter() As String
Filter = mFilter
End Property
       
Friend Property Let Filter(sFilter As String)
mFilter =  sFilter
End  Property
       
Friend  Property Get FilterIndex() As Long
FilterIndex =  mFilterIndex
End  Property
       
Friend  Property Let FilterIndex(lIndex  As Long)
mFilterIndex =  lIndex
End  Property
       
Friend  Property Get Flags() As Long
Flags =  mFlags
End  Property
       
Friend  Property Let Flags(lFlags  As Long)
mFlags =  lFlags
End  Property
       
Friend  Property Get HelpCommand() As Long
HelpCommand =  mHelpCommand
End  Property
       
Friend  Property Let HelpCommand(lCommand  As Long)
mHelpCommand =  lCommand
End  Property
       
Friend  Property Get HelpFile() As String
HelpFile =  mHelpFile
End  Property
       
Friend  Property Let HelpFile(sFile  As String)
mHelpFile =  sFile
End  Property
       
Friend  Property Get HelpKey() As String
HelpKey =  mHelpKey
End  Property
       
Friend  Property Let HelpKey(sKey  As String)
mHelpKey =  sKey
End  Property
       
Friend  Property Get InitDir() As String
InitDir =  mInitDir
End  Property
       
Friend  Property Let InitDir(sDir  As String)
mInitDir =  sDir
End  Property
       
Friend  Property Get Path() As String
Path =  sPath
End  Property
       
       
'方法 ==========================
       
'显示打开文件对话框
'参数:  父窗口句柄
Public Sub ShowOpen(hWndOwner As Long)
       
Dim strFile     As String
Dim OFName     As OPENFILENAME
       
OFName.lStructSize  = Len(OFName)
OFName.hWndOwner = hWndOwner
OFName.hInstance  = vbNull 'App.hInstance
    OFName.lpstrFile =  Space$(254)
OFName.nMaxFile =  255
OFName.lpstrFileTitle  = Space$(254)
OFName.nMaxFileTitle =  255
OFName.lpstrDefExt  = mDefaultExt & Chr(0)
OFName.lpstrTitle =  "选择要打开的文件"  & Chr(0)
OFName.lpstrFilter =  mFilter & Chr(0)
OFName.Flags =  mFlags
       
If  GetOpenFileName(OFName) Then
Dim s() As String
s = Split(Trim(OFName.lpstrFile), Chr(0))
       
mFileName  = s(0)
Else
mFileName  = ""
End If
       
End Sub
       
'显示保存文件对话框
'参数:父窗口句柄
Public  Sub ShowSave(hWndOwner As Long)
       
Dim strFile     As String
Dim OFName     As OPENFILENAME
       
OFName.lStructSize  = Len(OFName)
OFName.hWndOwner = hWndOwner
OFName.hInstance  = App.hInstance
OFName.lpstrFile =  Space$(254)
OFName.nMaxFile =  255
OFName.lpstrFileTitle  = Space$(254)
OFName.nMaxFileTitle =  255
'OFName.lpstrDefExt =  "所有文件(*.*)" & Chr(0) & "*.*" & Chr(0)
    OFName.lpstrDefExt =  mDefaultExt & Chr(0)
       
OFName.lpstrTitle =  "保存"  & Chr(0)
OFName.lpstrFilter =  mFilter
       
OFName.Flags = mFlags '
                   
If GetSaveFileName(OFName) Then
Dim s() As String
s = Split(Trim(OFName.lpstrFile), Chr(0))
       
mFileName  = s(0)
Else
mFileName  = ""
End If
       
End Sub
       
'初始化函数  =========================================
Public  Sub Class_Initialize()
     mFlags = cdlOFNHideReadOnly  Or cdlOFNExplorer Or cdlOFNOverwritePrompt Or cdlOFNLongNames Or cdlOFNPathMustExist 'Or  cdlOFNFileMustExist
             
End Sub
       
'析构函数  =========================================
Public  Sub Class_Terminate()
       
End Sub


使用方法:

在工程中添加一个类文件,粘贴上面的内容并保存为ComDlg.cls。

在窗体上放一个按钮,写入下面的代码。

'在form中定义
Dim  comdlg new ComDlg
Dim FileName As String
       
'按钮单击事件
Private Sub Command1_Click()    
With comdlg
.Filter =  "文本文件(*.txt)"  & Chr(0) & "*.txt" & Chr(0)  & _
"所有文件(*.*)"  & Chr(0) & "*.*" & Chr(0)
.ShowSave (Me.hWnd)
FileName =  .FileName
End With
End  Sub


扫描二维码推送至手机访问。

版权声明:本文由海阔天空发布,如需转载请注明出处。

本文链接:https://www.apull.net/html/20071104224.html

标签: 编程VB
分享给朋友:

相关文章

密码的故事

密码的故事

密码的故事Billy Hollis 本文是由一个问题引出的。我需要一种将密码保存在加密文件中的方法,因为我需要记住许多密码,但记忆力却已大不如前。我知道有许多商用工具能够做到这一点,但我感到学习  .NET 中的一项新技术真的很有好处。 我用 Visual Basic® .NET  完成了一个简单而完整的程序,用于加密和解密文件,从中学到了许多知识。既然加密对于多种开发都是一个重要问题,本文就介绍一下如何构造这样的程序。 有各种低级别的技术可以用于加密,...

制作网页28个常用小代码

制作网页28个常用小代码

1、oncontextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键<table border oncontextmenu=return(false)><td>no</table>  可用于Table2、<body onselectstart="return false"> 取消选取、防止复制3、onpaste="retu...

VB.NET关于加密算法

VB.NET关于加密算法

加密将防止数据被查看或修改,并在原本不安全的信道上提供安全的通信信道,它达到以下目的:  保密性:防止用户的标识或数据被读取。  数据完整性:防止数据被更改。  身份验证:确保数据发自特定的一方。  基本概念  1、散列(HASH)函数  散列(HASH)函数H也称哈希函数或杂凑函数等,是典型的多到一的函数,其输入为一可变长x(可以足够的长),输出一固定长的串h(一般为128位、160位,比输入的串短),该串h被称为输入x的Hash值(或称消息摘要Message  ...

用vb模拟键盘

用vb模拟键盘

  键盘是我们使用计算机的一个很重要的输入设备了,即使在鼠标大行其道的今天,很多程序依然离不开键盘来操作。但是有时候,一些重复性的,很繁琐的键盘操作总会让人疲惫,于是就有了用程序来代替人们按键的方法,这样可以把很多重复性的键盘操作交给程序来模拟,省了很多精力,按键精灵就是这样的一个软件。那么我们怎样才能用VB来写一个程序,达到与按键精灵类似的功能呢?那就让我们来先了解一下windows中响应键盘事件的机制。   当用户按下键盘上的一个键时,键盘内的芯片会...