A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 孟庆波 中级黑马   /  2012-4-20 11:35  /  1366 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private void butOpenFile_Click(object sender, EventArgs e)
        {

            OpenFileDialog openFiledDialog = new OpenFileDialog();
            openFiledDialog.InitialDirectory = "c:\\";
            openFiledDialog.Title = "打开和选择文件:孟庆波";
            openFiledDialog.Filter = "文本|*.txt|图片|*.jpg"; //设置文件类型 筛选模式为 文本|*.txt|所有的文件|*
            openFiledDialog.FilterIndex = 1;//设置默认文件类型显示顺序 //1-文本|*.txt, 2-图片|*.jpg
            openFiledDialog.RestoreDirectory = true;//RestoreDirectory有问题??

            if (openFiledDialog.ShowDialog() == DialogResult.OK)
            {
                //打开文件对话框中选择的文件名
                string fname = openFiledDialog.FileName;
                this.richTextBox1.Text += fname + "\n\r";

            }
        }

        private void btnSaveFile_Click(object sender, EventArgs e)
        {
            //#region;保存文本
            //SaveFileDialog saveFileDialog = new SaveFileDialog();
            ////saveFileDialog.Title = "保存啊!!";
            //saveFileDialog.Filter = "文本|*.txt|图片|*.jpg|word文档|*.docx";//设置文件类型
            //saveFileDialog.FilterIndex = 1;//设置默认文件类型显示顺序 //1-文本|*.txt, 2-图片|*.jpg
            //saveFileDialog.RestoreDirectory = true;//保存对话框是否记忆上次打开的目录
            //if (saveFileDialog.ShowDialog() == DialogResult.OK)
            //{
            //    #region; 文件名,文件路径,文件父目录
            //    ////获得文件的路径
            //    //string locaFilePath = saveFileDialog.FileName.ToString();
            //    ////获取文件名,不带路径
            //    //string fileName = locaFilePath.Substring(locaFilePath.LastIndexOf("\\") + 1);
            //    ////获取文件父目录
            //    //string filePath = locaFilePath.Substring(0, locaFilePath.LastIndexOf("\\"));
            //    ////给文件名加上时间
            //    //string newFileName = DateTime.Now.ToString("yymmdd") + fileName;
            //    ////在文件名里夹加字符
            //    // saveFileDialog.FileName.Insert(1, "damemgqimngbo");
            //    //this.richTextBox1.Text = locaFilePath;
            //    //System.IO.FileStream fs = (System.IO.FileStream)saveFileDialog.OpenFile();
            //    #endregion;


            //    //// System.IO.File.WriteAllText(saveFileDialog.FileName, richTextBox1.Text);
            //    //using (FileStream fs = new FileStream(saveFileDialog.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
            //    //{
            //    //    using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
            //    //    {
            //    //        sw.Write(this.richTextBox1.Text);
            //    //    }
            //    //}
            //    //MessageBox.Show("成功");

            //}
            //#endregion;
            #region;保存图片
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Title = "图片保存";
            saveFileDialog.Filter = "JPeg Image(*.jpg)|*.jpg|Bitmap Image(*.bmp)|*.bmp|Gif Image(*.gif)|*.gif|All Image(*.*)|*.*";//设置文件类型
            //saveFileDialog.FilterIndex = 1;
            saveFileDialog.RestoreDirectory = true;//保存对话框是否记忆上次打开的目录

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                //Image img = this.pictureBox1.Image;
                //Bitmap bit = new Bitmap(img, 100, 100);
                //bit.Save(saveFileDialog.FileName, System.Drawing.Imaging.ImageFormat.Gif);

                //this.pictureBox1.Image.Save(saveFileDialog.FileName);

                using (FileStream fs = new FileStream(saveFileDialog.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                {
                    switch (saveFileDialog.FilterIndex)
                    {
                        case 1:
                            this.pictureBox1.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
                            break;
                        case 2:
                            this.pictureBox1.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Bmp);
                            break;
                        case 3:
                            this.pictureBox1.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Gif);
                            break;
                        default:
                            this.pictureBox1.Image.Save(fs, System.Drawing.Imaging.ImageFormat.MemoryBmp);//为null??异常
                            break;
                    }

                }
            }
            #endregion;


        }

        private void btnColorDialog_Click(object sender, EventArgs e)
        {

            ColorDialog colorDialog = new ColorDialog();
            colorDialog.AllowFullOpen = true;
            colorDialog.FullOpen = true;
            colorDialog.ShowHelp = true;
            if (colorDialog.ShowDialog() == DialogResult.OK)
            {
                this.richTextBox1.SelectionColor = colorDialog.Color;
            }
            else
            {
                this.richTextBox1.SelectionColor = Color.Red;
            }

        }

评分

参与人数 1技术分 +2 收起 理由
宋天琪 + 2

查看全部评分

1 个回复

倒序浏览
   private void btnFontDialog_Click(object sender, EventArgs e)
        {

            FontDialog fontDialog = new FontDialog();
            fontDialog.AllowScriptChange = true;//false只有中文字符集 true 全部的字符集
            fontDialog.ShowColor = true;//是否显示 颜色的选择框

            if (fontDialog.ShowDialog() == DialogResult.OK)
            {
                this.richTextBox1.SelectionFont = fontDialog.Font;
                this.richTextBox1.SelectionColor = fontDialog.Color;
            }
            else
            {
                this.richTextBox1.SelectionFont = new Font("Tahoma", 12, FontStyle.Bold);
                this.richTextBox1.SelectionColor = Color.Red;
            }



        }

        private void btnPrintDialog_Click(object sender, EventArgs e)
        {
            PrintDialog printDialog = new PrintDialog();
            printDialog.AllowPrintToFile = true;
            printDialog.AllowSomePages = true;
            printDialog.ShowDialog();
            #region;打印对话框
            //float linesPerPage=0;//页面的行号
            //float yPos=0;//打印字符串的纵向位置
            //int count=0;//行计数器
            //float leftMargin =e.MarginBounds.Left;//左边距
            //float topMargin=e.MarginBounds.Top;//上边距
            //string line=null;//行字符串
            //Color clr=richTextBox1.SelectionColor;//当前的打印颜色,在我这个程序没有实现不同颜色打印
            //SolidBrush b =new SolidBrush(clr);//刷子
            //fnt=richTextBox1.SelectionFont;//当前的打印字体
            //linesPerPage=e.MarginBounds.Height/fnt.GetHeight(e.Graphics);//每页可打印的行数
            //file://逐行循行打印一页
            //while(count {
            //yPos=topMargin+(count*fnt.GetHeight(e.Graphics));
            //e.Graphics.DrawString(line,fnt,b,leftMargin,yPos,new StringFormat());
            //count++;
            //} file://如果该页打印完成而line不为空说明还有没完成的页面,发出下一次的打印事件,
            //file://在下一次的打印中lineReader会自动读取上次没有打印完的内容。lineReader可以记录当前读取的位置
            //if(line!=null)
            //e.HasMorePages=true;
            //else
            //e.HasMorePages=false;
            //}

            //        private void printPreviewBTN_Click(object sender, System.EventArgs e)
            //        {
            //            lineReader = new StringReader(richTextBox1.Text);
            //            try
            //            {
            //                PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
            //                printPreviewDialog1.Document = printDocument;
            //                printPreviewDialog1.FormBorderStyle = FormBorderStyle.Fixed3D;
            //                printPreviewDialog1.ShowDialog(this);
            //            }
            //            catch (Exception excep)
            //            {
            //                MessageBox.Show(excep.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //                return;
            //            }
            //        }

            //        private void printDialogBTN_Click(object sender, System.EventArgs e)
            //        {
            //            PrintDialog printDialog = new PrintDialog();
            //            printDialog.Document = printDocument;
            //            if (printDialog.ShowDialog() != DialogResult.Cancel)
            //            {
            //                try
            //                {
            //                    printDocument.Print();
            //                }
            //                catch (Exception ex)
            //                {
            //                    MessageBox.Show(ex.Message);
            //                }
            //            }

            #endregion;
        }

        private void btnFolderBrowserDialog_Click(object sender, EventArgs e)
        {
            // 选择文件夹
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                this.richTextBox1.Text = fbd.SelectedPath;
            }

        }

    }
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马