zoukankan      html  css  js  c++  java
  • [引]Windows窗体编程基础学习: 对话框组件

    1. ColorDialog 组件
    2. FolderBrowserDialog 组件
    3. FontDialog 组件
    4. OpenFileDialog 组件
    5. PageSetupDialog 组件
    6. PrintDialog 组件
    7. PrintPreviewDialog 控件
    8. SaveFileDialog 组件
    ===========================
    1. ColorDialog 组件

       使用户可以在预先配置的对话框中从调色板选择颜色
       以及将自定义颜色添加到该调色板中。

       Windows 窗体 ColorDialog 组件是一个预先配置的对话框,
       它允许用户从调色板选择颜色以及将自定义颜色添加到该调色板。
       此对话框与您在其他基于 Windows 的应用程序中看到的用于选择颜色的对话框相同。
       可在基于 Windows 的应用程序中使用它作为简单的解决方案,而不用配置自己的对话框。

       此对话框中选择的颜色在 Color 属性中返回。
       如果 AllowFullOpen 属性设置为 false,
       则将禁用“定义自定义颜色”按钮,并且用户只能使用调色板中的预定义颜色。
       如果 SolidColorOnly 属性设置为 true,则用户无法选择抖色。
       若要显示此对话框,必须调用它的 ShowDialog 方法。

    [示例]
       下面的代码示例使用 CommonDialog 的 ColorDialog 实现,
       并演示如何创建和显示对话框。
       此示例要求从一个现有窗体调用该方法,并且该窗体上须有一个 TextBox 和 Button。

    Visual Basic  
    Private Sub button1_Click(sender As Object, e As System.EventArgs)
        Dim MyDialog As New ColorDialog()
        ' Keeps the user from selecting a custom color.
        MyDialog.AllowFullOpen = False
        ' Allows the user to get help. (The default is false.)
        MyDialog.ShowHelp = True
        ' Sets the initial color select to the current text color,
        MyDialog.Color = textBox1.ForeColor

        ' Update the text box color if the user clicks OK
        If (MyDialog.ShowDialog() = DialogResult.OK) Then
            textBox1.ForeColor =  MyDialog.Color
        End If

    End Sub 'button1_Click

     
    C#  
    private void button1_Click(object sender, System.EventArgs e)
     {
        ColorDialog MyDialog = new ColorDialog();
        // Keeps the user from selecting a custom color.
        MyDialog.AllowFullOpen = false ;
        // Allows the user to get help. (The default is false.)
        MyDialog.ShowHelp = true ;
        // Sets the initial color select to the current text color.
        MyDialog.Color = textBox1.ForeColor ;
       
        // Update the text box color if the user clicks OK
        if (MyDialog.ShowDialog() == DialogResult.OK)
            textBox1.ForeColor =  MyDialog.Color;
     }

    -------------------------------------
       
    2. FolderBrowserDialog 组件

       使用户可以浏览和选择文件夹。

       使用 ShowDialog 方法可在运行时显示 FolderBrowserDialog 组件。
       设置 RootFolder 属性可确定将出现在对话框树视图内的顶级文件夹和任何子文件夹。
       在显示对话框后,您就可以使用 SelectedPath 属性获取所选文件夹的路径了。

       将 FolderBrowserDialog 组件添加到窗体后,它就会出现在“Windows 窗体设计器”底部的栏中。

    [示例]
       在下面的示例中,FolderBrowserDialog 组件用于选择文件夹,
       类似于在 Visual Studio 中创建项目时,提示选择保存它的文件夹。
       在本示例中,文件夹名称随后显示在窗体上的 TextBox 控件中。
       将文件夹名称置于可编辑区域(例如 TextBox 控件)中是个好主意,
       这样,用户就可在出现错误或其他问题时编辑他们的选择。
       本示例假定窗体具有一个 FolderBrowserDialog 组件和一个 TextBox 控件。

    Visual Basic  
    Public Sub ChooseFolder()
        If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
            TextBox1.Text = FolderBrowserDialog1.SelectedPath
        End If
    End Sub

    C# 
    public void ChooseFolder()
    {
        if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
        {
            textBox1.Text = folderBrowserDialog1.SelectedPath;
        }
    }

    -------------------------------------
    3. FontDialog 组件

       公开系统上当前安装的字体。

       Windows 窗体 FontDialog 组件是一个预先配置的对话框,
       该对话框是标准的 Windows“字体”对话框,用于公开系统上当前安装的字体。
       可在基于 Windows 的应用程序中将其用作简单的字体选择解决方案,而不是配置您自己的对话框。

       默认情况下,该对话框显示字体、字体样式和字体大小的列表框;
       删除线和下划线等效果的复选框;脚本的下拉列表以及字体外观的示例。
       (脚本是指给定字体可用的不同字符脚本,如希伯来语或日语。)
       若要显示字体对话框,请调用 ShowDialog 方法。

    [示例]
       下面的代码示例使用 ShowDialog 显示 FontDialog。
        此代码要求已经用 TextBox 和其上的按钮创建了 Form。
        它还要求已经创建了 fontDialog1。Font 包含大小信息,但不包含颜色信息。

    Visual Basic  
    Private Sub button1_Click(sender As Object, e As System.EventArgs)
        fontDialog1.ShowColor = True

        fontDialog1.Font = textBox1.Font
        fontDialog1.Color = textBox1.ForeColor

        If fontDialog1.ShowDialog() <> DialogResult.Cancel Then
            textBox1.Font = fontDialog1.Font
            textBox1.ForeColor = fontDialog1.Color
        End If
    End Sub 'button1_Click

     
    C# 
    private void button1_Click(object sender, System.EventArgs e)
     {
        fontDialog1.ShowColor = true;

        fontDialog1.Font = textBox1.Font;
        fontDialog1.Color = textBox1.ForeColor;

        if(fontDialog1.ShowDialog() != DialogResult.Cancel )
        {
           textBox1.Font = fontDialog1.Font ;
           textBox1.ForeColor = fontDialog1.Color;
        }
     }

    -------------------------------------

    4. OpenFileDialog 组件

       允许用户通过预先配置的对话框打开文件。

       可使用 ShowDialog 方法在运行时显示该对话框。
       使用 Multiselect 属性可使用户选择多个要打开的文件。
       另外,可使用 ShowReadOnly 属性确定在对话框中是否出现只读复选框。
       ReadOnlyChecked 属性指示是否选中只读复选框。
       最后,Filter 属性设置当前文件名筛选字符串,
       该字符串确定出现在对话框的“文件类型”框中的选择。

       将 OpenFileDialog 组件添加到窗体后,
       它出现在 Windows 窗体设计器底部的栏中。


    [示例]
       下面的代码示例创建一个 OpenFileDialog,设置几个属性,
        并使用 CommonDialog.ShowDialog 方法显示此对话框。
        该示例要求窗体上放置了一个 Button,并在其中添加了 System.IO 命名空间。

    Visual Basic 
    Private Sub button1_Click(sender As Object, e As System.EventArgs)
        Dim myStream As Stream
        Dim openFileDialog1 As New OpenFileDialog()
          
        openFileDialog1.InitialDirectory = "c:\"
        openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        openFileDialog1.FilterIndex = 2
        openFileDialog1.RestoreDirectory = True
          
        If openFileDialog1.ShowDialog() = DialogResult.OK Then
            myStream = openFileDialog1.OpenFile()
            If Not (myStream Is Nothing) Then
                ' Insert code to read the stream here.
                myStream.Close()
            End If
        End If
    End Sub


     
    C# 
    private void button1_Click(object sender, System.EventArgs e)
    {
        Stream myStream;
        OpenFileDialog openFileDialog1 = new OpenFileDialog();

        openFileDialog1.InitialDirectory = "c:\\" ;
        openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
        openFileDialog1.FilterIndex = 2 ;
        openFileDialog1.RestoreDirectory = true ;

        if(openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            if((myStream = openFileDialog1.OpenFile())!= null)
            {
                // Insert code to read the stream here.
                myStream.Close();
            }
        }
    }

    -------------------------------------
    5. PageSetupDialog 组件

       通过预先配置的对话框设置供打印的页详细信息。

       可使用 ShowDialog 方法在运行时显示该对话框。
       该组件具有与单页(PrintDocument 类)或任何文档(PageSettings 类)相关的可设置属性。
       此外,PageSetupDialog 组件可用于确定特定的打印机设置,
       这些设置存储在 PrinterSettings 类中。

       PageSetupDialog 对话框对给定 Document 的 PageSettings 和 PrinterSettings 信息进行修改。
       用户可启用该对话框的各个部分以控制打印和边距,控制纸张方向、大小和来源,
       还可以显示“帮助”和网络按钮。
       MinMargins 属性定义用户可选择的最小边距。

       创建 PageSetupDialog 类的实例时,读/写属性将被设置为初始值。
        有关这些值的列表,请参见 PageSetupDialog 构造函数。

       因为 PageSetupDialog 需要显示页面设置,
       所以需要在调用 ShowDialog 之前设置 Document、PrinterSettings 或 PageSettings 属性;
       否则将会发生异常。

    [示例]
       下面的代码示例使用 PageSettings、PrinterSettings
         和 ShowNetwork 属性阐释了 PageSetupDialog。
       若要运行此示例,请将它放到一个包含一个 Button(名为 Button1)、
       一个 ListBox(名为 ListBox1)和一个 PageSetupDialog(名为 PageSetupDialog1)的窗体中。
       确保该按钮的 Click 事件与此示例中的事件处理方法相连接。

    Visual Basic 
    'This method displays a PageSetupDialog object. If the
    ' user clicks OK in the dialog, selected results of
    ' the dialog are displayed in ListBox1.
    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

        ' Initialize the dialog's PrinterSettings property to hold user
        ' defined printer settings.
        PageSetupDialog1.PageSettings = _
            New System.Drawing.Printing.PageSettings

        ' Initialize dialog's PrinterSettings property to hold user
        ' set printer settings.
        PageSetupDialog1.PrinterSettings = _
            New System.Drawing.Printing.PrinterSettings

        'Do not show the network in the printer dialog.
        PageSetupDialog1.ShowNetwork = False

        'Show the dialog storing the result.
        Dim result As DialogResult = PageSetupDialog1.ShowDialog()

        ' If the result is OK, display selected settings in
        ' ListBox1. These values can be used when printing the
        ' document.
        If (result = DialogResult.OK) Then
            Dim results() As Object = New Object() _
                {PageSetupDialog1.PageSettings.Margins, _
                 PageSetupDialog1.PageSettings.PaperSize, _
                 PageSetupDialog1.PageSettings.Landscape, _
                 PageSetupDialog1.PrinterSettings.PrinterName, _
                 PageSetupDialog1.PrinterSettings.PrintRange}
            ListBox1.Items.AddRange(results)
        End If
    End Sub

     
    C#  
    //This method displays a PageSetupDialog object. If the
    // user clicks OK in the dialog, selected results of
    // the dialog are displayed in ListBox1.
    private void Button1_Click(System.Object sender, System.EventArgs e)
    {

        // Initialize the dialog's PrinterSettings property to hold user
        // defined printer settings.
        PageSetupDialog1.PageSettings =
            new System.Drawing.Printing.PageSettings();

        // Initialize dialog's PrinterSettings property to hold user
        // set printer settings.
        PageSetupDialog1.PrinterSettings =
            new System.Drawing.Printing.PrinterSettings();

        //Do not show the network in the printer dialog.
        PageSetupDialog1.ShowNetwork = false;

        //Show the dialog storing the result.
        DialogResult result = PageSetupDialog1.ShowDialog();

        // If the result is OK, display selected settings in
        // ListBox1. These values can be used when printing the
        // document.
        if ( result == DialogResult.OK)
        {
            object[] results = new object[]{
                PageSetupDialog1.PageSettings.Margins,
                PageSetupDialog1.PageSettings.PaperSize,
                PageSetupDialog1.PageSettings.Landscape,
                PageSetupDialog1.PrinterSettings.PrinterName,
                PageSetupDialog1.PrinterSettings.PrintRange};
            ListBox1.Items.AddRange(results);
        }
    }

    -------------------------------------
    6. PrintDialog 组件

       选择打印机,选择要打印的页,
       并确定其他与打印相关的设置。

       可使用 ShowDialog 方法在运行时显示该对话框。
       该组件具有与单个打印作业(PrintDocument 类)或
       个别打印机的设置(PrinterSettings 类)相关的属性。
        这两类属性反过来可由多个打印机共享。


    [示例]
        下面的代码示例演示如何使用 PrintDialog 控件
        来设置 AllowSomePages、ShowHelp 和 Document 属性。
         若要运行此示例,请将以下代码粘贴到一个窗体中,
         让该窗体包含一个名为 PrintDialog1 的 PrintDialog 控件
         和一个名为 Button1 的按钮。
          此示例假定按钮的 Click 事件和 docToPrint 的 PrintPage 事件
          已连接到此示例中定义的事件处理方法。

    Visual Basic  
    ' Declare the PrintDocument object.
    Private WithEvents docToPrint As New Printing.PrintDocument

    ' This method will set properties on the PrintDialog object and
    ' then display the dialog.
    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

        ' Allow the user to choose the page range he or she would
        ' like to print.
        PrintDialog1.AllowSomePages = True

        ' Show the help button.
        PrintDialog1.ShowHelp = True

        ' Set the Document property to the PrintDocument for
        ' which the PrintPage Event has been handled. To display the
        ' dialog, either this property or the PrinterSettings property
        ' must be set
        PrintDialog1.Document = docToPrint

        Dim result As DialogResult = PrintDialog1.ShowDialog()

        ' If the result is OK then print the document.
        If (result = DialogResult.OK) Then
            docToPrint.Print()
        End If

    End Sub

    ' The PrintDialog will print the document
    ' by handling the document's PrintPage event.
    Private Sub document_PrintPage(ByVal sender As Object, _
       ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
           Handles docToPrint.PrintPage

        ' Insert code to render the page here.
        ' This code will be called when the control is drawn.

        ' The following code will render a simple
        ' message on the printed document.
        Dim text As String = "In document_PrintPage method."
        Dim printFont As New System.Drawing.Font _
            ("Arial", 35, System.Drawing.FontStyle.Regular)

        ' Draw the content.
        e.Graphics.DrawString(text, printFont, _
            System.Drawing.Brushes.Black, 10, 10)
    End Sub

     
    C#  
    // Declare the PrintDocument object.
    private System.Drawing.Printing.PrintDocument docToPrint =
        new System.Drawing.Printing.PrintDocument();

    // This method will set properties on the PrintDialog object and
    // then display the dialog.
    private void Button1_Click(System.Object sender,
        System.EventArgs e)
    {

        // Allow the user to choose the page range he or she would
        // like to print.
        PrintDialog1.AllowSomePages = true;

        // Show the help button.
        PrintDialog1.ShowHelp = true;

        // Set the Document property to the PrintDocument for
        // which the PrintPage Event has been handled. To display the
        // dialog, either this property or the PrinterSettings property
        // must be set
        PrintDialog1.Document = docToPrint;

        DialogResult result = PrintDialog1.ShowDialog();

        // If the result is OK then print the document.
        if (result==DialogResult.OK)
        {
            docToPrint.Print();
        }

    }

    // The PrintDialog will print the document
    // by handling the document's PrintPage event.
    private void document_PrintPage(object sender,
        System.Drawing.Printing.PrintPageEventArgs e)
    {

        // Insert code to render the page here.
        // This code will be called when the control is drawn.

        // The following code will render a simple
        // message on the printed document.
        string text = "In document_PrintPage method.";
        System.Drawing.Font printFont = new System.Drawing.Font
            ("Arial", 35, System.Drawing.FontStyle.Regular);

        // Draw the content.
        e.Graphics.DrawString(text, printFont,
            System.Drawing.Brushes.Black, 10, 10);
    }

     
    -------------------------------------
    7. PrintPreviewDialog 控件

       按文档打印时的样式显示文档。

       该控件包含打印、放大、显示一页或多页和关闭此对话框的按钮。

       该控件的主要属性是 Document,用于设置要预览的文档。
       文档必须是 PrintDocument 对象。
       若要显示此对话框,必须调用它的 ShowDialog 方法。
        消除锯齿可使文字显得更齐整平滑,但也会使显示更慢;
        若要使用它,请将 UseAntiAlias 属性设置为 true。

       有些属性可通过 PrintPreviewDialog 包含的 PrintPreviewControl 获得。
       (不必向窗体添加此 PrintPreviewControl,
        向窗体添加 PrintPreviewDialog 对话框时它自动包含在此对话框中。)
        可通过 PrintPreviewControl 使用的属性示例是 Columns 和 Rows 属性,
        它们确定在控件上水平和垂直显示的页的数目。
        您可以像访问 Visual Basic 中的 PrintPreviewDialog1.PrintPreviewControl.Columns、
        Visual C# 中的 printPreviewDialog1.PrintPreviewControl.Columns
        和 Visual C++ 中的 printPreviewDialog1->PrintPreviewControl->Columns
        一样访问 Columns 属性。


    [示例]
       下面的代码示例演示 PrintPreviewDialog
       如何设置 Document 和 UseAntiAlias 属性。
       该示例假定窗体包含一个名为 TreeView1 的 TreeView,
       其中包含 TreeNode 对象。
        每个 TreeNode 对象的 Tag 属性必须设置为一个能被运行该示例的计算机访问的完全限定文档名。
       将每个 TreeNode.Text 属性设置为一个标识由 TreeNode.Tag 属性指定的文件的字符串。
       例如,可以将 TreeNode1.Tag 设置为“c:\myDocuments\recipe.doc”,
       将 TreeNode1.Text 设置为“recipe.doc”。
       该示例还假定窗体包含一个名为 PrintPreviewDialog1 的 PrintPreviewDialog
       和一个名为 Button1 的按钮。
       若要运行此示例,请调用位于该窗体的构造函数或 Load 事件处理程序中的
       InitializePrintPreviewDialog 方法。

    Visual Basic  
    ' Declare the dialog.
    Friend WithEvents PrintPreviewDialog1 As PrintPreviewDialog

    ' Declare a PrintDocument object named document.
    Private WithEvents document As New System.Drawing.Printing.PrintDocument

    ' Initalize the dialog.
    Private Sub InitializePrintPreviewDialog()

        ' Create a new PrintPreviewDialog using constructor.
        Me.PrintPreviewDialog1 = New PrintPreviewDialog

        'Set the size, location, and name.
        Me.PrintPreviewDialog1.ClientSize = New System.Drawing.Size(400, 300)
        Me.PrintPreviewDialog1.Location = New System.Drawing.Point(29, 29)
        Me.PrintPreviewDialog1.Name = "PrintPreviewDialog1"

        ' Set the minimum size the dialog can be resized to.
        Me.PrintPreviewDialog1.MinimumSize = New System.Drawing.Size(375, 250)

        ' Set the UseAntiAlias property to true, which will allow the
        ' operating system to smooth fonts.
        Me.PrintPreviewDialog1.UseAntiAlias = True
    End Sub

    Private Sub Button1_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

        If Not (TreeView1.SelectedNode Is Nothing) Then

            ' Set the PrintDocument object's name to the selectedNode
            ' object's  tag, which in this case contains the
            ' fully-qualified name of the document. This value will
            ' show when the dialog reports progress.
            document.DocumentName = TreeView1.SelectedNode.Tag
        End If

        ' Set the PrintPreviewDialog.Document property to
        ' the PrintDocument object selected by the user.
        PrintPreviewDialog1.Document = document

        ' Call the ShowDialog method. This will trigger the document's
        '  PrintPage event.
        PrintPreviewDialog1.ShowDialog()
    End Sub

    Private Sub document_PrintPage(ByVal sender As Object, _
        ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
            Handles document.PrintPage

        ' Insert code to render the page here.
        ' This code will be called when the PrintPreviewDialog.Show
        ' method is called.

        ' The following code will render a simple
        ' message on the document in the dialog.
        Dim text As String = "In document_PrintPage method."
        Dim printFont As New System.Drawing.Font _
            ("Arial", 35, System.Drawing.FontStyle.Regular)

        e.Graphics.DrawString(text, printFont, _
            System.Drawing.Brushes.Black, 0, 0)

    End Sub

     
    C#  
    // Declare the dialog.
    internal PrintPreviewDialog PrintPreviewDialog1;

    // Declare a PrintDocument object named document.
    private System.Drawing.Printing.PrintDocument document =
        new System.Drawing.Printing.PrintDocument();

    // Initalize the dialog.
    private void InitializePrintPreviewDialog()
    {

        // Create a new PrintPreviewDialog using constructor.
        this.PrintPreviewDialog1 = new PrintPreviewDialog();

        //Set the size, location, and name.
        this.PrintPreviewDialog1.ClientSize =
            new System.Drawing.Size(400, 300);
        this.PrintPreviewDialog1.Location =
            new System.Drawing.Point(29, 29);
        this.PrintPreviewDialog1.Name = "PrintPreviewDialog1";
       
        // Associate the event-handling method with the
        // document's PrintPage event.
        this.document.PrintPage +=
            new System.Drawing.Printing.PrintPageEventHandler
            (document_PrintPage);

        // Set the minimum size the dialog can be resized to.
        this.PrintPreviewDialog1.MinimumSize =
            new System.Drawing.Size(375, 250);

        // Set the UseAntiAlias property to true, which will allow the
        // operating system to smooth fonts.
        this.PrintPreviewDialog1.UseAntiAlias = true;
    }

    private void Button1_Click(object sender, System.EventArgs e)
    {

        if (TreeView1.SelectedNode != null)

            // Set the PrintDocument object's name to the selectedNode
            // object's  tag, which in this case contains the
            // fully-qualified name of the document. This value will
            // show when the dialog reports progress.
        {
            document.DocumentName = TreeView1.SelectedNode.Tag.ToString();
        }

        // Set the PrintPreviewDialog.Document property to
        // the PrintDocument object selected by the user.
        PrintPreviewDialog1.Document = document;

        // Call the ShowDialog method. This will trigger the document's
        //  PrintPage event.
        PrintPreviewDialog1.ShowDialog();
    }

    private void document_PrintPage(object sender,
        System.Drawing.Printing.PrintPageEventArgs e)
    {

        // Insert code to render the page here.
        // This code will be called when the PrintPreviewDialog.Show
        // method is called.

        // The following code will render a simple
        // message on the document in the dialog.
        string text = "In document_PrintPage method.";
        System.Drawing.Font printFont =
            new System.Drawing.Font("Arial", 35,
            System.Drawing.FontStyle.Regular);

        e.Graphics.DrawString(text, printFont,
            System.Drawing.Brushes.Black, 0, 0);

    }


    -------------------------------------
    8. SaveFileDialog 组件

       选择要保存的文件和该文件的保存位置。

       使用该控件作为一个简单的解决方案,使用户能够保存文件,而不用配置您自己的对话框。
       利用标准的 Windows 对话框,创建基本功能可立即为用户所熟悉的应用程序。
       但是应注意,使用 SaveFileDialog 组件时,必须编写您自己的文件保存逻辑。

       可使用 ShowDialog 方法在运行时显示该对话框。
       使用 OpenFile 方法可以读写方式打开文件。

    [示例]
       显示“保存文件”对话框并调用一个方法保存用户选定的文件。

       使用 SaveFileDialog 组件的 OpenFile 方法保存文件。
       此方法提供了一个可以写入的 Stream 对象。

       以下示例使用 DialogResult 属性获取文件的名称,并使用 OpenFile 方法保存文件。
       OpenFile 方法提供了可以写入文件的流。

       在下面的示例中,有一个分配了图像的 Button 控件。
       单击该按钮时,将使用一个允许 .gif、.jpeg 和 .bmp 类型文件的筛选器
       实例化 SaveFileDialog 组件。

       如果在“保存文件”对话框中选定了此类型的文件,那么将保存按钮的图像。

       安全注意
       若要获取或设置 FileName 属性,
       程序集要求有 System.Security.Permissions.FileIOPermission 类授予的权限级别。
        如果在部分信任的上下文中运行,则该进程可能会因特权不足而引发一个异常。

       该示例假设窗体上有一个 Button 控件,
       该控件的 Image 属性设置为 .gif、.jpeg 或 .bmp 类型的文件。

       注意
       FileDialog 类的 FilterIndex 属性(根据继承的特性,该属性属于 SaveFileDialog 类)
       使用从 1 开始的索引。
       如果您通过编写代码以特定格式保存数据(例如,以纯文本而不是二进制格式保存文件),
       那么这一点很重要。以下示例介绍了该属性。
     
    Visual Basic  
    Private Sub Button2_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button2.Click
       ' Displays a SaveFileDialog so the user can save the Image
       ' assigned to Button2.
       Dim saveFileDialog1 As New SaveFileDialog()
       saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif"
       saveFileDialog1.Title = "Save an Image File"
       saveFileDialog1.ShowDialog()
      
       ' If the file name is not an empty string open it for saving.
       If saveFileDialog1.FileName <> "" Then
          ' Saves the Image via a FileStream created by the OpenFile method.
          Dim fs As System.IO.FileStream = Ctype _
             (saveFileDialog1.OpenFile(), System.IO.FileStream)
          ' Saves the Image in the appropriate ImageFormat based upon the
          ' file type selected in the dialog box.
          ' NOTE that the FilterIndex property is one-based.
          Select Case saveFileDialog1.FilterIndex
             Case 1
                Me.button2.Image.Save(fs, _
                   System.Drawing.Imaging.ImageFormat.Jpeg)

             Case 2
                Me.button2.Image.Save(fs, _
                   System.Drawing.Imaging.ImageFormat.Bmp)

             Case 3
                Me.button2.Image.Save(fs, _
                   System.Drawing.Imaging.ImageFormat.Gif)
           End Select

           fs.Close()
        End If
    End Sub

     

    C#  
    private void button2_Click(object sender, System.EventArgs e)
    {
       // Displays a SaveFileDialog so the user can save the Image
       // assigned to Button2.
       SaveFileDialog saveFileDialog1 = new SaveFileDialog();
       saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
       saveFileDialog1.Title = "Save an Image File";
       saveFileDialog1.ShowDialog();

       // If the file name is not an empty string open it for saving.
       if(saveFileDialog1.FileName != "")
       {
          // Saves the Image via a FileStream created by the OpenFile method.
          System.IO.FileStream fs =
             (System.IO.FileStream)saveFileDialog1.OpenFile();
          // Saves the Image in the appropriate ImageFormat based upon the
          // File type selected in the dialog box.
          // NOTE that the FilterIndex property is one-based.
          switch(saveFileDialog1.FilterIndex)
          {
             case 1 :
             this.button2.Image.Save(fs,
                System.Drawing.Imaging.ImageFormat.Jpeg);
             break;

             case 2 :
             this.button2.Image.Save(fs,
                System.Drawing.Imaging.ImageFormat.Bmp);
             break;

             case 3 :
             this.button2.Image.Save(fs,
                System.Drawing.Imaging.ImageFormat.Gif);
             break;
          }

       fs.Close();
       }
    }

     

  • 相关阅读:
    前端把html表格生成为excel表格
    图片预加载
    angular开发中的两大问题
    3d图片点击切换
    图片懒加载
    angular排序
    vue1.0 与 Vue2.0的一些区别 及用法
    图片懒加载
    图片放大镜
    图片小精灵 & 解决同时给一个元素设置背景问题 &jq登录注册切换
  • 原文地址:https://www.cnblogs.com/freeliver54/p/690839.html
Copyright © 2011-2022 走看看