Visual Basic/Windows 對話方塊
Windows 對話方塊在您需要從使用者那裡獲取資訊時非常有用,例如詢問儲存檔案的位置,讓使用者選擇顏色或字型以及列印時要使用的設定(副本數,使用哪個印表機等)。這可以節省您大量不必要的重複工作 - 因為重新設計和製作對話方塊非常耗時 - 但對於在應用程式之間建立標準使用者介面也是必不可少的。使用者介面不同方面的標準化,包括正常widget以及組合它們的方法,確保應用程式直觀 - 也就是說,一旦某人學會了使用一個應用程式,這種知識也可以用於其他應用程式。
訪問這些控制元件的最簡單方法是使用名為Microsoft Common Dialog Control 6.0的元件。要將此元件新增到您的專案中,請選擇專案 - 元件,然後在控制元件列表中選中它。隨後,您的控制元件工具箱中應該會出現一個新的控制元件。選擇它,並將控制元件放置在您的窗體上(注意它只在設計時可見)。
該控制元件允許使用以下對話方塊。提到的標誌是可以在標誌屬性中設定的不同值,更改不同對話方塊的行為或設計。
開啟和儲存對話方塊
[edit | edit source]ShowOpen 顯示一個對話方塊,讓使用者選擇一個驅動器、目錄、檔名和副檔名(大概是為了開啟一個檔案)。
儲存對話方塊在外觀和功能上與開啟對話方塊相同,只是標題不同。在執行時,當用戶選擇一個檔案並關閉對話方塊時,FileName 屬性用於獲取所選的檔名。
顯示對話方塊之前
[edit | edit source]在使用這些對話方塊之前,您必須首先設定 Filter 屬性以允許使用者選擇副檔名(使用DefaultExt設定預設副檔名)。Filter 屬性是一個字串,具有以下結構
description1|filter1|description2|filter2|
此處,description 是顯示在所有可用過濾器列表框中的字串供使用者選擇。filter 是一個檔案 glob 表示式(也稱為萬用字元表示式),用於實際過濾,例如“*.txt”,它不允許任何副檔名,但 TXT 除外。重要的是要識別這類表示式和正則表示式之間的區別,正則表示式功能更強大,但也更復雜,請參見正則表示式。
程式碼示例
[edit | edit source] On Error Resume Next
' Clear errors
Err.Clear
' Use this common dialog control throughout the procedure
With CommonDialog1
' Raises an error when the user press cancel
.CancelError = True
' Set the file extensions to allow
CommonDialog1.Filter = "All Files (*.*)|*.*|TextFiles (*.txt)|*.txt|Batch Files (*.bat)|*.bat"
' Display the open dialog box.
.ShowOpen
' Ignore this if the user has canceled the dialog
If Err <> cdlCancel Then
' Open the file here using FileName
MsgBox "You've opened '" & .FileName & "'"
End If
End With
顏色對話方塊
[edit | edit source]顏色對話方塊允許使用者從調色盤中選擇顏色,也可以透過手動選擇 RGB 或 HSL 值來選擇顏色。您可以使用 Color 屬性檢索所選的顏色。
程式碼示例
[edit | edit source] ' Don't raise errors normally
On Error Resume Next
' Clear errors
Err.Clear
' Use this common dialog control throughout the procedure
With CommonDialog1
' Raises an error when the user press cancel
.CancelError = True
' Set the Flags property.
.Flags = cdlCCRGBInit
' Display the Color dialog box.
.ShowColor
' Ignore this if the user has canceled the dialog
If Err <> cdlCancel Then
' Set the form's background color to the selected color.
Me.BackColor = .Color
End If
End With
字型對話方塊
[edit | edit source]字型對話方塊允許使用者根據字型大小、顏色和樣式選擇字型。一旦使用者在字型對話方塊中做出選擇,以下屬性將包含有關使用者選擇的資訊。
屬性
[edit | edit source]| 屬性 | 確定 |
|---|---|
| 顏色 | 所選的顏色。要使用此屬性,您必須首先將 Flags 屬性設定為 cdlCFEffects。 |
| FontBold | 返回是否選中了粗體複選框。 |
| FontItalic | 返回是否選中了斜體複選框。 |
| FontStrikethru | 返回是否選中了刪除線複選框。 |
| FontUnderline | 返回是否選中了下劃線複選框。 |
| FontName | 返回所選的字型名稱。 |
| FontSize | 返回所選的字型大小。 |
顯示對話方塊之前
[edit | edit source]要顯示對話方塊,您必須首先將 Flags 屬性設定為 cdlCFScreenFonts 或 cdlCFPrinterFonts(或者 cdlCFBoth,如果您打算讓使用者在螢幕字型和印表機字型之間進行選擇)。
程式碼示例
[edit | edit source] ' *** This example require a textbox called ''txtText'' to execute properly. ***
On Error Resume Next
' Clear errors
Err.Clear
' Use this common dialog control throughout the procedure
With CommonDialog1
' Raises an error when the user press cancel
.CancelError = True
' Show printer and screen fonts, as well as a font color chooser.
.Flags = cdlCFBoth Or cdlCFEffects
' Display the font dialog box.
.ShowFont
End With
' Ignore this if the user has canceled the dialog
If Err <> cdlCancel Then
' Set the textbox's font properties according to the users selections.
With txtText
.Font.Name = CommonDialog1.FontName
.Font.Size = CommonDialog1.FontSize
.Font.Bold = CommonDialog1.FontBold
.Font.Italic = CommonDialog1.FontItalic
.Font.Underline = CommonDialog1.FontUnderline
.FontStrikethru = CommonDialog1.FontStrikethru
.ForeColor = CommonDialog1.Color
End With
End If
列印對話方塊
[edit | edit source]列印對話方塊允許使用者選擇如何列印輸出。使用者可訪問的選項和屬性包括要列印的副本數、列印質量、要列印的頁面範圍等。它還允許使用者選擇另一個印表機,以及配置有關當前印表機的不同設定。
屬性
[edit | edit source]| 屬性 | 確定 |
|---|---|
| 副本 | 要列印的副本數量。 |
| FromPage | 列印範圍的起始頁。 |
| ToPage | 列印範圍的結束頁。 |
| hDC | 所選印表機的裝置上下文。 |
| Orientation | 頁面的方向(如縱向或橫向)。 |
顯示對話方塊之前
[edit | edit source]在顯示對話方塊之前,您可以隨意設定適當的列印對話方塊屬性以設定要使用的預設值。
On Error Resume Next
' Variables that holds information regarding the print setup.
Dim BeginPage As Long, EndPage As Long, NumCopies As Long, Orientation As Long, Tell As Long
' Clear errors
Err.Clear
' Use this common dialog control throughout the procedure
With CommonDialog1
' Raises an error when the user press cancel
.CancelError = True
' Display the printer dialog box.
.ShowPrinter
' Ignore this if the user has canceled the dialog
If Err <> cdlCancel Then
' Retrieve the printer settings the user has chosen.
BeginPage = CommonDialog1.FromPage
EndPage = CommonDialog1.ToPage
NumCopies = CommonDialog1.Copies
Orientation = CommonDialog1.Orientation
' Start printing
For i = 1 To NumCopies
' Put code here to send data to your printer.
Next
End If
End With
| 上一頁:過程和函式 | 目錄 | 下一頁:資料庫 |


