跳转到内容

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


上一页:过程和函数 目录 下一页:数据库
华夏公益教科书