8.8 通用對(duì)話框控件
Windows應(yīng)用程序里的Open對(duì)話框,Save As對(duì)話框在各個(gè)應(yīng)用程序里看起來都是一樣的,通用對(duì)話框控件就可以提供這些對(duì)話框的標(biāo)準(zhǔn)功能。
1.Open對(duì)話框及Save As對(duì)話框
打開Open對(duì)話框使用ShowOpen方法,打開Save As對(duì)話框使用ShowSave方法。
Private Sub mnuOpen_Click ()
On Error GoTo ErrorHandler
CommonDialog1.CancelError = True
CommonDialog1.Filter = "Text Files (*.txt)|*.txt|Batch Files (*.bat)|*.bat|All Files (*.*)|*.*"
CommonDialog1.ShowOpen ' 顯示打開對(duì)話框
Call OpenFile(CommonDialog1.FileName)
ErrorHandler:
Exit Sub
End Sub
其中第三行決定了在文件格式類型欄里出現(xiàn)的文件類型。第五行需要一個(gè)自己的打開文件的過程,這個(gè)過程需要的參數(shù)就是通用對(duì)話框返回的文件名。通用對(duì)話框的CancelError屬性設(shè)為True的話,用戶單擊Cancel按鈕將產(chǎn)生一個(gè)錯(cuò)誤信息程序,憑借這個(gè)信息程序可以檢測(cè)到用戶的放棄操作。
2.Color對(duì)話框
下面的過程可用用戶選擇的顏色作為窗體的底色。
Private Sub mnuColor_Click ()
On Error GoTo CancelButton
CommonDialog1.CancelError = True
CommonDialog1.ShowColor
Form1.BackColor = CommonDialog1.Color
CancelButton:
Exit Sub
End Sub
3.Fonts對(duì)話框
下面的過程可用字體對(duì)話框改變文本框的字體:
Private Sub mnuFonts_Click ()
On Error GoTo CancelButton
CommonDialog1.CancelError = True
CommonDialog1.Flags = cdlCFBoth ' Flags property must be set to cdlCFBoth
CommonDialog1.ShowFont ' Display Font common dialog box.
Text1.FontName = CommonDialog1.FontName
Text1.FontSize = CommonDialog1.FontSize
Text1.FontBold = CommonDialog1.FontBold
Text1.FontItalic = CommonDialog1.FontItalic
Text1.FontUnderline = CommonDialog1.FontUnderline
Text1.FontStrikethru = CommonDialog1.FontStrikethru
Text1.ForeColor = CommonDialog1.Color
CancelButton:
Exit Sub
End Sub
代碼的第三行出現(xiàn)了通用對(duì)話框的Flags屬性決定了通用對(duì)話框的一些可選項(xiàng),不過即使不賦值給Flags,代碼也一樣會(huì)按缺省的情況去執(zhí)行的。
Windows應(yīng)用程序里的Open對(duì)話框,Save As對(duì)話框在各個(gè)應(yīng)用程序里看起來都是一樣的,通用對(duì)話框控件就可以提供這些對(duì)話框的標(biāo)準(zhǔn)功能。
1.Open對(duì)話框及Save As對(duì)話框
打開Open對(duì)話框使用ShowOpen方法,打開Save As對(duì)話框使用ShowSave方法。
Private Sub mnuOpen_Click ()
On Error GoTo ErrorHandler
CommonDialog1.CancelError = True
CommonDialog1.Filter = "Text Files (*.txt)|*.txt|Batch Files (*.bat)|*.bat|All Files (*.*)|*.*"
CommonDialog1.ShowOpen ' 顯示打開對(duì)話框
Call OpenFile(CommonDialog1.FileName)
ErrorHandler:
Exit Sub
End Sub
其中第三行決定了在文件格式類型欄里出現(xiàn)的文件類型。第五行需要一個(gè)自己的打開文件的過程,這個(gè)過程需要的參數(shù)就是通用對(duì)話框返回的文件名。通用對(duì)話框的CancelError屬性設(shè)為True的話,用戶單擊Cancel按鈕將產(chǎn)生一個(gè)錯(cuò)誤信息程序,憑借這個(gè)信息程序可以檢測(cè)到用戶的放棄操作。
2.Color對(duì)話框
下面的過程可用用戶選擇的顏色作為窗體的底色。
Private Sub mnuColor_Click ()
On Error GoTo CancelButton
CommonDialog1.CancelError = True
CommonDialog1.ShowColor
Form1.BackColor = CommonDialog1.Color
CancelButton:
Exit Sub
End Sub
3.Fonts對(duì)話框
下面的過程可用字體對(duì)話框改變文本框的字體:
Private Sub mnuFonts_Click ()
On Error GoTo CancelButton
CommonDialog1.CancelError = True
CommonDialog1.Flags = cdlCFBoth ' Flags property must be set to cdlCFBoth
CommonDialog1.ShowFont ' Display Font common dialog box.
Text1.FontName = CommonDialog1.FontName
Text1.FontSize = CommonDialog1.FontSize
Text1.FontBold = CommonDialog1.FontBold
Text1.FontItalic = CommonDialog1.FontItalic
Text1.FontUnderline = CommonDialog1.FontUnderline
Text1.FontStrikethru = CommonDialog1.FontStrikethru
Text1.ForeColor = CommonDialog1.Color
CancelButton:
Exit Sub
End Sub
代碼的第三行出現(xiàn)了通用對(duì)話框的Flags屬性決定了通用對(duì)話框的一些可選項(xiàng),不過即使不賦值給Flags,代碼也一樣會(huì)按缺省的情況去執(zhí)行的。