各種對話框(總結)
 '
'標準對話框(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