各种对话框(总结)
'
'标准对话框(smdialog)
'
option explicit
''定义一个全局变量,用于保存字体的各种属性
public type smfontattr
fontname as string '字体名
fontsize as integer '字体大小
fontbod as boolean '是否黑体
fontitalic as boolean '是否斜体
fontunderline as boolean '是否下划线
fontstrikeou as boolean
fontcolor as long
winhwnd as long
end type
dim m_getfont as smfontattr
''**系统常量------------------------------------------
private const swp_noownerzorder = &h200
private const swp_hidewindow = &h80
private const swp_noactivate = &h10
private const swp_nomove = &h2
private const swp_noredraw = &h8
private const swp_noreposition = swp_noownerzorder
private const swp_nosize = &h1
private const swp_nozorder = &h4
private const swp_showwindow = &h40
private const resourcetype_disk = &h1 '网络驱动器
private const resourcetype_print = &h2 '网络打印机
'/------------------------------------------------------------
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 lf_facesize = 32
private const max_path = 260
private const cf_inittologfontstruct = &h40&
private const cf_fixedpitchonly = &h4000&
private const cf_effects = &h100&
private const italic_fonttype = &h200
private const bold_fonttype = &h100
private const cf_nofacesel = &h80000
private const cf_noscriptsel = &h800000
private const cf_printerfonts = &h2
private const cf_scalableonly = &h20000
private const cf_screenfonts = &h1
private const cf_showhelp = &h4&
private const cf_both = (cf_screenfonts or cf_printerfonts)
'/------------------------------------------
private type choosecolor
lstructsize as long
hwndowner 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 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
'/-----------------------------------------------------------
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 as string * lf_facesize
end type
private type choosefont
lstructsize as long
hwndowner 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
'/--------------
private type shitemid
cb as long
abid() as byte
end type
private type itemidlist
mkid as shitemid
end type
'/------------------------------------------
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 getsavefilename lib "comdlg32.dll" alias "getsavefilenamea"
(popenfilename as openfilename) as long
private declare function getopenfilename lib "comdlg32.dll" alias "getopenfilenamea"
(popenfilename as openfilename) as long
private declare function choosecolor lib "comdlg32.dll" alias "choosecolora"
(pchoosecolor as choosecolor) as long
private declare function wnetconnectiondialog lib "mpr.dll" (byval hwnd as long,
byval dwtype as long) as long
private declare function choosefont lib "comdlg32.dll" alias "choosefonta"
(pchoosefont as choosefont) as long
'/=======显示断开网络资源对话框============
private declare function wnetdisconnectdialog lib "mpr.dll" _
(byval hwnd as long, byval dwtype as long) as long
'/================================================================================
private declare sub cotaskmemfree lib "ole32.dll" (byval pv as long)
private declare function shbrowseforfolder lib "shell32.dll" alias
"shbrowseforfoldera" _
(lpbrowseinfo as browseinfo) as long
private type browseinfo
howner as long
pidlroot as long
pszdisplayname as string
lpsztitle as string
ulflags as long
lpfn as long
lparam as long
iimage as long
end type
'/结构说明: _
howner 调用这个对话框的窗口的句柄 _
pidlroot 指向你希望浏览的最上面的文件夹的符列表 _
pszdisplayname 用于保存用户所选择的文件夹的显示名的缓冲区 _
lpsztitle 浏览对话框的标题 _
ulflags 决定浏览什么的标志(见下) _
lpfn 当事件发生时对话框调用的回调函数的地址.可将它设定为null _
lparam 若定义了回调函数,则为传递给回调函数的值 _
iimage as long 保存所选文件夹映像索引的缓冲区 _
ulflags参数(见下:)
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 '允许游览打印机文件夹
'/--------------------------------------------------------------------------------
dim fontinfo as smfontattr '字体
'/--------------------------------------------------------------------------------
private function getfoldervalue(widx as integer) as long
if widx < 2 then
getfoldervalue = 0
elseif widx < 12 then
getfoldervalue = widx
else
getfoldervalue = widx + 4
end if
end function
'
private function getreturntype() as long
dim dwrtn as long
dwrtn = dwrtn or bif_returnonlyfsdirs
getreturntype = dwrtn
end function
'
'文件夹选择对话框
'函数:savefile
'参数:title 设置对话框的标签.
' hwnd 调用此函数的hwnd
' folderid smbrowfolder枚举(默认:我的电脑).
'返回值:string 文件夹路径.
'例子:
public function getfolder(optional title as string, _
optional hwnd as long, _
optional folderid as smbrowfolder = mycomputer) as string
dim bi as browseinfo
dim pidl as long
dim folder as string
dim idl as itemidlist
dim nfolder as long
dim returnfol as string
dim fid as integer
fid = folderid
folder = string$(255, chr$(0))
with bi
.howner = hwnd
nfolder = getfoldervalue(fid)
if shgetspecialfolderlocation(byval hwnd, byval nfolder, idl) = noerror then
.pidlroot = idl.mkid.cb
end if
.pszdisplayname = string$(max_path, fid)
if len(title) > 0 then
.lpsztitle = title & chr$(0)
else
.lpsztitle = "请选择文件夹:" & chr$(0)
end if
.ulflags = getreturntype()
end with
pidl = shbrowseforfolder(bi)
'/返回所选的文件夹路径
if shgetpathfromidlist(byval pidl, byval folder) then
returnfol = left$(folder, instr(folder, chr$(0)) - 1)
if right$(trim$(returnfol), 1) <> "/" then returnfol = returnfol & "/"
getfolder = returnfol
else
getfolder = ""
end if
end function
'
'文件保存对话框
'函数:savefile
'参数:winhwnd 调用此函数的hwnd
' boxlabel 设置对话框的标签.
' startpath 设置初始化路径.
' filterstr 文件过滤.
' flag 标志.(参考msdn)
'返回值:string 文件名.
'例子:
public function savefile(winhwnd as long, _
optional boxlabel as string = "", _
optional startpath as string = "", _
optional filterstr = "*.*|*.*", _
optional flag as variant = &h4 or &h200000) as string
dim rc as long
dim popenfilename as openfilename
dim fstr1() as string
dim fstr as string
dim i as long
const max_buffer_length = 256
on error resume next
if len(trim$(startpath)) > 0 then
if right$(startpath, 1) <> "/" then startpath = startpath & "/"
if dir$(startpath, vbdirectory + vbhidden) = "" then
startpath = app.path
end if
else
startpath = app.path
end if
if len(trim$(filterstr)) = 0 then
fstr = "*.*|*.*"
end if
fstr1 = split(filterstr, "|")
for i = 0 to ubound(fstr1)
fstr = fstr & fstr1(i) & vbnullchar
next
'/--------------------------------------------------
with popenfilename
.hwndowner = winhwnd
.hinstance = app.hinstance
.lpstrtitle = boxlabel
.lpstrinitialdir = startpath
.lpstrfilter = fstr
.nfilterindex = 1
.lpstrdefext = vbnullchar & vbnullchar
.lpstrfile = string(max_buffer_length, 0)
.nmaxfile = max_buffer_length - 1
.lpstrfiletitle = .lpstrfile
.nmaxfiletitle = max_buffer_length
.lstructsize = len(popenfilename)
.flags = flag
end with
rc = getsavefilename(popenfilename)
if rc then
savefile = left$(popenfilename.lpstrfile, popenfilename.nmaxfile)
else
savefile = ""
end if
end function
'
'文件打开对话框
'函数:openfile
'参数:winhwnd 调用此函数的hwnd
' boxlabel 设置对话框的标签.
' startpath 设置初始化路径.
' filterstr 文件过滤.
' flag 标志.(参考msdn)
'返回值:string 文件名.
'例子:
public function openfile(winhwnd as long, _
optional boxlabel as string = "", _
optional startpath as string = "", _
optional filterstr = "*.*|*.*", _
optional flag as variant = &h8 or &h200000) as string
dim rc as long
dim popenfilename as openfilename
dim fstr1() as string
dim fstr as string
dim i as long
const max_buffer_length = 256
on error resume next
if len(trim$(startpath)) > 0 then
if right$(startpath, 1) <> "/" then startpath = startpath & "/"
if dir$(startpath, vbdirectory + vbhidden) = "" then
startpath = app.path
end if
else
startpath = app.path
end if
if len(trim$(filterstr)) = 0 then
fstr = "*.*|*.*"
end if
fstr = ""
fstr1 = split(filterstr, "|")
for i = 0 to ubound(fstr1)
fstr = fstr & fstr1(i) & vbnullchar
next
with popenfilename
.hwndowner = winhwnd
.hinstance = app.hinstance
.lpstrtitle = boxlabel
.lpstrinitialdir = startpath
.lpstrfilter = fstr
.nfilterindex = 1
.lpstrdefext = vbnullchar & vbnullchar
.lpstrfile = string(max_buffer_length, 0)
.nmaxfile = max_buffer_length - 1
.lpstrfiletitle = .lpstrfile
.nmaxfiletitle = max_buffer_length
.lstructsize = len(popenfilename)
.flags = flag
end with
rc = getopenfilename(popenfilename)
if rc then
openfile = left$(popenfilename.lpstrfile, popenfilename.nmaxfile)
else
openfile = ""
end if
end function
'
'颜色对话框
'函数:getcolor
'参数:
'返回值:long,用户所选择的颜色.
'例子:
public function getcolor() as long
dim rc as long
dim pchoosecolor as choosecolor
dim customcolor() as byte
with pchoosecolor
.hwndowner = 0
.hinstance = app.hinstance
.lpcustcolors = strconv(customcolor, vbunicode)
.flags = 0
.lstructsize = len(pchoosecolor)
end with
rc = choosecolor(pchoosecolor)
if rc then
getcolor = pchoosecolor.rgbresult
else
getcolor = -1
end if
end function
'
'显示映射网络驱动器对话框
'函数:connectdisk
'参数:hwnd 调用此函数的窗口hwnd.(me.hwn)
'返回值:=0,成功,<>0,失败.
'例子:
public function connectdisk(optional hwnd as long) as long
dim rc as long
if isnumeric(hwnd) then
rc = wnetconnectiondialog(hwnd, resourcetype_disk)
else
rc = wnetconnectiondialog(0, resourcetype_disk)
end if
connectdisk = rc
end function
'
'显示映射网络打印机对话框
'函数:connectprint
'参数:hwnd 调用此函数的窗口hwnd.(me.hwn)
'返回值:=0,成功,<>0,失败.
'例子:
public function connectprint(optional hwnd as long) as long
dim rc as long
if isnumeric(hwnd) then
rc = wnetconnectiondialog(hwnd, resourcetype_print)
else
rc = wnetconnectiondialog(0, resourcetype_print)
end if
end function
'
'断开映射网络驱动器对话框
'函数:disconnectdisk
'参数:hwnd 调用此函数的窗口hwnd.(me.hwn)
'返回值:=0,成功,<>0,失败.
'例子:
public function disconnectdisk(optional hwnd as long) as long
dim rc as long
if isnumeric(hwnd) then
rc = wnetdisconnectdialog(hwnd, resourcetype_disk)
else
rc = wnetdisconnectdialog(0, resourcetype_disk)
end if
end function
'
'断开映射网络打印机关话框
'函数:disconnectprint
'参数:hwnd 调用此函数的窗口hwnd.(me.hwn)
'返回值:=0,成功,<>0,失败.
'例子:
public function disconnectprint(optional hwnd as long) as long
dim rc as long
if isnumeric(hwnd) then
rc = wnetdisconnectdialog(hwnd, resourcetype_print)
else
rc = wnetdisconnectdialog(0, resourcetype_print)
end if
end function
'
'字体选择对话框
'函数:getfont
'参数:winhwnd 调用此函数的窗口hwnd.(me.hwn)
'返回值:smfontattr 结构变量.
'例子:
' dim mdialog as new smdialog
' dim mfontinfo as smfontattr
' mfontinfo = mdialog.getfont(me.hwnd)
' set mdialog = nothing
public function getfont(winhwnd as long) as smfontattr
dim rc as long
dim pchoosefont as choosefont
dim plogfont as logfont
with plogfont
.lffacename = strconv(fontinfo.fontname, vbfromunicode)
.lfitalic = fontinfo.fontitalic
.lfunderline = fontinfo.fontunderline
.lfstrikeout = fontinfo.fontstrikeou
end with
with pchoosefont
.hinstance = app.hinstance
if isnumeric(winhwnd) then .hwndowner = winhwnd
.flags = cf_both + cf_inittologfontstruct + cf_effects + cf_noscriptsel
if isnumeric(fontinfo.fontsize) then .ipointsize = fontinfo.fontsize *
10
if fontinfo.fontbod then .nfonttype = .nfonttype + bold_fonttype
if isnumeric(fontinfo.fontcolor) then .rgbcolors = fontinfo.fontcolor
.lstructsize = len(pchoosefont)
.lplogfont = varptr(plogfont)
end with
rc = choosefont(pchoosefont)
if rc then
fontinfo.fontname = strconv(plogfont.lffacename, vbunicode)
fontinfo.fontname = left$(fontinfo.fontname, instr(fontinfo.fontname,
vbnullchar) - 1)
with pchoosefont
fontinfo.fontsize = .ipointsize / 10 '返回字体大
小
fontinfo.fontbod = (.nfonttype and bold_fonttype) '返回是/否黑
体
fontinfo.fontitalic = (.nfonttype and italic_fonttype) '是/否斜体
fontinfo.fontunderline = (plogfont.lfunderline) '是/否下划线
fontinfo.fontstrikeou = (plogfont.lfstrikeout)
fontinfo.fontcolor = .rgbcolors
end with
end if
getfont = fontinfo
end function
'
'文件打开.(带预览文件功能)
'函数:browfile
'参数:pattern 文件类型字符串,starpath 开始路径,isbrow 是否生成预览
'返回值:[确定] 文件路径.[取消] 空字符串
'例:me.caption =
filebrow.browfile("图片文件|*.jpg;*.gif;*.bmp|媒体文件|*.dat;*.mpg;*.swf;*.mp3;*.mp2
")
public function browfile(optional pattern as string = "*,*|*.*", _
optional starpath as string = "c:/", _
optional isbrow as boolean = true) as string
on error resume next
if len(trim$(pattern)) = 0 then pattern = "*.*|*.*"
p_filepart = pattern
p_starpath = starpath
p_isbrow = isbrow
frmbrowfile.show 1
browfile = p_fullfilename
end function
'
'显示网上邻居
'函数:shownetwork
'参数:frmcap 窗口标题,labction 提示标签名.
'返回值:[确定] 所选计算机名称.[取消] 空字符串.
'例:
public function shownetwork(optional frmcap as string = "网上邻居", _
optional labction as string = "选择计算机名称.") as
string
showlan.hide
showlan.caption = frmcap
showlan.labnncaption = labction
showlan.show 1
shownetwork = p_netreturnval
end function