全面掌握C#中的对话框

在编程中经常使用到的对话框大致有六个,他们分别是:"选择文件对话框"、"保存文件对话框"、"颜色对话框"、"打印预览对话框"、"打印机选择对话框"和"字体选择对话框"。那么在C#是否也有相应的对话框?答案是存在的。那么在C#中如何使用?本文就来探讨一下这个问题。

一. 本文介绍的程序设计和运行的软件环境

(1).微软公司视窗2000服务器版
(2)..Net FrameWork SDK Beta 2

二. 选择文件对话框

在命名空间System.Windows.Froms中封装了一个类(Class)"OpenFileDialog",在C#中的选择文件对话框就是继承这个类。下列代码是创建一个此类的对象:

OpenFileDialog openFileDialog1 = new OpenFileDialog();

(1).选择文件对话框一些常用的属性:

选择文件对话框的常用属性如下表:

属性名称作用
InitialDirectory设置在对话框中显示的初始化目录
Filter设定对话框中过滤文件字符串
FilterIndex设定显示的过滤字符串的索引
RestoreDirectory布尔型,设定是否重新回到关闭此对话框时候的当前目录
FileName设定在对话框中选择的文件名称
ShowHelp设定在对话框中是否显示"帮助"按钮
Title设定对话框的标题

(2).创建一个功能相对完整的选择文件对话框的对象:

//定义一个"打开文件对话框"
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)
{
    fName = openFileDialog1.FileName;
    showInfo();
    sr = new StreamReader(fName);
}  

此代码编译后的运行的界面如下:

diag2_1
图01:C#创建一个选择文件对话框对象

三. 选择字体对话框

在命名空间System.Windows.Froms中封装了一个类(Class)"FontDialog",在C#中的通过继承此类得到字体对话框的。下列代码是创建一个此类的对象:

FontDialog fontDialog1 = new FontDialog () ;

(1).字体对话框的一些常用的属性:

属性名称作用
ShowColor是否在对话框中显示"颜色"选项
AllowScriptChange是否允许使用者更改"字符集"选择
Font选择后的字体

(2).创建一个功能相对完整的字体对话框的对象:

FontDialog fontDialog1 = new FontDialog();
fontDialog1.ShowColor = true;
fontDialog1.AllowScriptChange = false;
if (fontDialog1.ShowDialog() != DialogResult.Cancel)
{
    fnt = fontDialog1.Font;
    showInfo();
}  

编译后的运行界面如下:

diag2_2
图02:C#创建一个字体对话框对象

四.颜色选择对话框

在C#中创建一个颜色对话框是通过继承命名空间System.Windows.Froms中类(Class)"ColorDialog"来实现的。下列代码是创建一个此类的对象:

ColorDialog ColorDialog1 = new ColorDialog();

(1). 颜色选择对话框一些常用的属性:

颜色选择对话框的常用属性如下表:

属性名称作用
AllowFullOpen设定用户是否可以使用自定义颜色
ShowHelp设定在对话框中是否显示"帮助"按钮
Color颜色对话框选择的颜色

(2).创建一个功能相对完整的颜色选择对话框的对象:

ColorDialog ColorDialog1 = new ColorDialog();
ColorDialog1.AllowFullOpen = true;
ColorDialog1.FullOpen = true;
//设定此颜色对话框存在"帮助"按钮,缺省是没有的
ColorDialog1.ShowHelp = true;
// 设定此颜色对话框的初始颜色,所以如果在对话框中选择"取消",则此对话框会重新此颜色
ColorDialog1.Color = Color.DarkBlue;
if ( ColorDialog1.ShowDialog() != DialogResult.Cancel)
{
    clr = ColorDialog1.Color;
    showInfo();
}  

此代码编译后的运行的界面如下:

diag2_3
图03:C#创建一个颜色对话框对象

五.保存文件对话框

在C#中创建一个保存文件对话框是通过继承命名空间System.Windows.Froms中类(Class)"SaveFileDialog"来实现的。下列代码是创建一个此类的对象:

SaveFileDialog saveFileDialog1 = new SaveFileDialog();

(1). 保存文件对话框一些常用的属性:

保存文件对话框的常用属性如下表:

属性名称作用
InitialDirectory设置在对话框中显示的初始化目录
Filter设定对话框中过滤文件字符串
FilterIndex设定显示的过滤字符串的索引
RestoreDirectory布尔型,设定是否重新回到关闭此对话框时候的当前目录
FileName设定在对话框中选择的文件名称
ShowHelp设定在对话框中是否显示"帮助"按钮
Title设定对话框的标题

(2).创建一个功能相对完整的保存文件对话框的对象:

Stream myStream;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = " txt files(*.txt)|*.txt|All files(*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
    if ((myStream = saveFileDialog1.OpenFile()) != null)
    {
        //设定"sr"对象的指针指向读取文件流的开始位置
        sr.BaseStream.Seek(0, SeekOrigin.Begin);
        int i;
        //判断"sr"中的指针是否指向文件流的结尾位置
        while ((i = sr.BaseStream.ReadByte()) != -1)
        {
            //不断的往文件中写入字节
            myStream.WriteByte((byte)i);
        }
        myStream.Close();
        sr.BaseStream.Seek(0, SeekOrigin.Begin);
    }
}

此代码编译后的运行的界面如下:

diag2_4
图04:C#创建一个保存文件对话框对象

六.设置打印机对话框

在C#中创建一个设置打印机对话框是通过继承命名空间System.Windows.Froms中类(Class)"PrintDialog"来实现的。下列代码是创建一个此类的对象:

PrintDialog PrintDialog1 = new PrintDialog();

(1). 设置打印机对话框一些常用的属性:

设置打印机对话框的常用属性如下表:

属性名称作用
AllowSelection设定是否显示"从…到…页"这个打印选项
AllowPrintToFile设定在对话框中"打印到文件"选项是否激活
Document设定要打印的文档
PrintToFile设定是否选中"打印到文件"选项
ShowNetwork设定在对话框是否显示"网络"这个按钮
ShowHelp设定在对话框中是否显示"帮助"按钮

(2).创建一个功能相对完整的设置打印机对话框的对象:

//创建一个设置打印机对话框对象
PrintDialog PrintDialog1 = new PrintDialog();
//首先要新建一个打印文档,
pd = new PrintDocument();
PrintDialog1.ShowNetwork = false;

PrintDialog1.PrintToFile = false;
PrintDialog1.Document = pd;
if (PrintDialog1.ShowDialog() != DialogResult.Cancel)
{
    try
    {
        pd.Print();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

此代码编译后的运行的界面如下:

diag2_5
图05:C#创建一个设置打印机对话框对象

七:打印预览对话框

在C#中创建一个打印预览对话框是通过继承命名空间System.Windows.Froms中类(Class)"PrintPreviewDialog"来实现的。下列代码是创建一个此类的对象:

PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();

(1). 打印预览对话框一些常用的属性:

设置打印机对话框的常用属性如下表:

属性名称作用
FormBorderStyle设定边距类型
Document设定要打印的文档

(2).创建一个可以打印一个空文档的打印预览对话框:

//创建一个打印文档
pd = new PrintDocument();
//创建一个打印预览对话框对象
PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
printPreviewDialog1.Document = this.pd;
printPreviewDialog1.FormBorderStyle = FormBorderStyle.Fixed3D;
printPreviewDialog1.ShowDialog();

此代码编译后的运行的界面如下:

diag2_6
图06:C#创建一个打印空文档的打印预览对话框对象

八.用C#例子编写一个综合六个对话框的例子(dialog.cs)

下图是dialog.cs编译后运行的界面:

diag2_7
图07:用C#编写的组合六个对话框程序的运行界面

dialog.cs的源代码如下:

using System;
using System.IO;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class Form1 : Form
{
    private Button btnOpenFileDialog;
    private Button btnSaveFileDialog;
    private Button btnColorDialog;
    private Button btnFontDialog;
    private Button btnPrintDialog;
    private Label lblOutputData;
    private Label lblCaption;
    private Font fnt;
    private Color clr;
    private string fName;
    private StreamReader sr;
    private PrintDocument pd;
    private Button btnPrintPreview;
    private System.ComponentModel.Container components = null;

    public Form1()
    {
        //初始化窗体中的各个组件
        InitializeComponent();
    }

    //清除程序中使用过的资源
    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (components != null)
            {
                components.Dispose();
            }
        }
        base.Dispose(disposing);
    }

    private void InitializeComponent()
    {
        btnColorDialog = new Button();
        btnFontDialog = new Button();
        lblCaption = new Label();
        btnOpenFileDialog = new Button();
        btnSaveFileDialog = new Button();
        btnPrintPreview = new Button();
        btnPrintDialog = new Button();
        lblOutputData = new Label();
        SuspendLayout();

        btnColorDialog.Location = new Point(20, 102);
        btnColorDialog.Name = "btnColorDialog";
        btnColorDialog.Size = new Size(140, 28);
        btnColorDialog.TabIndex = 2;
        btnColorDialog.Text = "颜色对话框";
        btnColorDialog.Click += new System.EventHandler(btnColorDialog_Click);

        btnFontDialog.Location = new Point(20, 65);
        btnFontDialog.Name = "btnFontDialog";
        btnFontDialog.Size = new Size(140, 28);
        btnFontDialog.TabIndex = 1;
        btnFontDialog.Text = "字体对话框";
        btnFontDialog.Click += new System.EventHandler(btnFontDialog_Click);

        lblCaption.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
        lblCaption.Location = new Point(184, 8);
        lblCaption.Name = "lblCaption";
        lblCaption.Size = new Size(246, 19);
        lblCaption.TabIndex = 2;
        lblCaption.Text = "选择对话框后的输出结果:";

        btnOpenFileDialog.Location = new Point(20, 28);
        btnOpenFileDialog.Name = "btnOpenFileDialog";
        btnOpenFileDialog.Size = new Size(140, 28);
        btnOpenFileDialog.TabIndex = 0;
        btnOpenFileDialog.Text = "打开文件对话框";
        btnOpenFileDialog.Click += new System.EventHandler(btnOpenFileDialog_Click);

        btnSaveFileDialog.Location = new Point(20, 213);
        btnSaveFileDialog.Name = "btnSaveFileDialog";
        btnSaveFileDialog.Size = new Size(140, 28);
        btnSaveFileDialog.TabIndex = 5;
        btnSaveFileDialog.Text = "保存文件对话框";
        btnSaveFileDialog.Click += new System.EventHandler(btnSaveFileDialog_Click);

        btnPrintPreview.Location = new Point(20, 139);
        btnPrintPreview.Name = "btnPrintPreview";
        btnPrintPreview.Size = new Size(140, 28);
        btnPrintPreview.TabIndex = 3;
        btnPrintPreview.Text = "打印预览对话框";
        btnPrintPreview.Click += new System.EventHandler(btnPrintPreview_Click);

        btnPrintDialog.Location = new Point(20, 176);
        btnPrintDialog.Name = "btnPrintDialog";
        btnPrintDialog.Size = new Size(140, 28);
        btnPrintDialog.TabIndex = 4;
        btnPrintDialog.Text = "打印对话框";
        btnPrintDialog.Click += new System.EventHandler(btnPrintDialog_Click);

        lblOutputData.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        lblOutputData.Location = new Point(176, 28);
        lblOutputData.Name = "lblOutputData";
        lblOutputData.Size = new Size(280, 213);
        lblOutputData.TabIndex = 1;

        this.AutoScaleBaseSize = new Size(6, 14);
        this.ClientSize = new Size(456, 264);
        this.Controls.Add(lblCaption);
        this.Controls.Add(btnPrintPreview);
        this.Controls.Add(lblOutputData);
        this.Controls.Add(btnSaveFileDialog);
        this.Controls.Add(btnOpenFileDialog);
        this.Controls.Add(btnColorDialog);
        this.Controls.Add(btnFontDialog);
        this.Controls.Add(btnPrintDialog);

        this.MaximizeBox = false;
        this.MinimizeBox = false;
        this.Name = "Form1";
        this.Text = "全面掌握C#中的对话框组件!";
        this.ResumeLayout(false);
    }

    static void Main()
    {
        Application.Run(new Form1());
    }

    private void btnOpenFileDialog_Click(object sender, System.EventArgs e)
    {
        //定义一个"打开文件对话框"
        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)
        {
            fName = openFileDialog1.FileName;
            showInfo();
            sr = new StreamReader(fName);
        }
    }

    //另存文件对话框
    private void btnSaveFileDialog_Click(object sender, System.EventArgs e)
    {
        Stream myStream;
        SaveFileDialog saveFileDialog1 = new SaveFileDialog();
        saveFileDialog1.Filter = " txt files(*.txt)|*.txt|All files(*.*)|*.*";
        saveFileDialog1.FilterIndex = 2;
        saveFileDialog1.RestoreDirectory = true;
        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            if ((myStream = saveFileDialog1.OpenFile()) != null)
            {
                //设定"sr"对象的指针指向读取文件流的开始位置
                sr.BaseStream.Seek(0, SeekOrigin.Begin);
                int i;
                //判断"sr"中的指针是否指向文件流的结尾位置
                while ((i = sr.BaseStream.ReadByte()) != -1)
                {
                    //不断的往文件中写入字节
                    myStream.WriteByte((byte)i);
                }
                myStream.Close();
                sr.BaseStream.Seek(0, SeekOrigin.Begin);
            }
        }
    }

    //颜色选择对话框
    private void btnColorDialog_Click(object sender, System.EventArgs e)
    {
        ColorDialog ColorDialog1 = new ColorDialog();
        ColorDialog1.AllowFullOpen = true;
        ColorDialog1.FullOpen = true;
        //设定此颜色对话框存在"帮助"按钮,缺省是没有的
        ColorDialog1.ShowHelp = true;
        // 设定此颜色对话框的初始颜色,所以如果在对话框中选择"取消",则此对话框会重新此颜色
        ColorDialog1.Color = Color.DarkBlue;
        if (ColorDialog1.ShowDialog() != DialogResult.Cancel)
        {
            clr = ColorDialog1.Color;
            showInfo();
        }
    }

    //打开字体对话框
    private void btnFontDialog_Click(object sender, System.EventArgs e)
    {
        FontDialog fontDialog1 = new FontDialog();
        fontDialog1.ShowColor = true;
        fontDialog1.AllowScriptChange = true;
        fontDialog1.ShowColor = false;
        if (fontDialog1.ShowDialog() != DialogResult.Cancel)
        {
            fnt = fontDialog1.Font;
            showInfo();
        }
    }

    //打印机对话框
    private void btnPrintDialog_Click(object sender, System.EventArgs e)
    {
        PrintDialog PrintDialog1 = new PrintDialog();
        pd = new PrintDocument();
        PrintDialog1.PrintToFile = false;
        PrintDialog1.Document = pd;
        if (PrintDialog1.ShowDialog() != DialogResult.Cancel)
        {
            try
            {
                pd.Print();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

    //打印预览对话框
    private void btnPrintPreview_Click(object sender, System.EventArgs e)
    {
        //创建一个新的文档
        pd = new PrintDocument();
        //创建一个打印预览对话框
        PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
        printPreviewDialog1.Document = this.pd;
        printPreviewDialog1.FormBorderStyle = FormBorderStyle.Fixed3D;
        printPreviewDialog1.ShowDialog();
    }

    //显示各个对话框选择后的信息
    private void showInfo()
    {
        lblOutputData.Text = "选择文件: " + fName.ToString() + "\n\r\n\r";
        lblOutputData.Text = lblOutputData.Text + "选择颜色: " + clr.ToString() + "\n\r\n\r";
        lblOutputData.Text = lblOutputData.Text + "选择字体:" + fnt + "\n\r\n\r";
    }
}

九.总结

对话框是编程过程中经常使用到的组件,希望上面的介绍对您在程序设计中使用这些对话框有所帮助。

Powered by DvNews.net
来源:网络
阅读:452 次
日期:2003-6-16
Contributors: FHL