zoukankan      html  css  js  c++  java
  • winform 对话框控件,打印控件

    1、文件对话框(FileDialog) 

    它又常用到两个:

    打开文件对话框(OpenFileDialog) 

    保存文件对话框(SaveFileDialog) 

    2、字体对话框(FontDialog) 

    3、颜色对话框(ColorDialog)

    4、打开文件夹对话框 FolderBrowserDialog

    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 duihuakuang控件
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            //dialog  对话,会话,对话框
            //ShowDialog 将窗体显示为具有指定所有者模式的对话框
            //DialogResult 指定标识符以指示对话框的返回值
            //floder   文件夹,文书夹
            //Browser  浏览器
            //file     文件;档案;文件夹
            //stream   流媒体类
            private void 字体颜色ToolStripMenuItem_Click(object sender, EventArgs e)
            {
               DialogResult dr= colorDialog1.ShowDialog();//显示为调色板对话框
               if (dr == DialogResult.OK)//如果用户点击的是确定才赋值
               {
                   textBox1.ForeColor = colorDialog1.Color;//textbox1中的前景色
               }
            }
    
            private void 选择文件夹ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                folderBrowserDialog1.ShowDialog();//显示为文件夹浏览器样式
                textBox1.Text = folderBrowserDialog1.SelectedPath;//将选择路径里的内容显示在文本框
                //绝对路径,相对路径
               
            }
    
            private void 字体设置ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                fontDialog1.ShowColor = true;//开启字体设置对话框里的颜色
                fontDialog1.ShowDialog();//开启字体设置对话框
                textBox1.Font = fontDialog1.Font;//文本框字体设置引用字体设置
                textBox1.ForeColor = fontDialog1.Color;//文本框颜色引用字体设置对话框里的颜色
            }
    
            private string lujing;//定义一个变量lujing来存放文件存放的路径
    
            private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
            {
    
                if (lujing == "")//如果没有该保存路径
                {
                    saveFileDialog1.FileName = "新建文件.txt";//设置保存名称及后缀文件类型
                    DialogResult dr = saveFileDialog1.ShowDialog();//设置一个对话框变量dr接收显示保存对话框的内容
                    if (dr == DialogResult.OK)//如果用户点击确定
                    {
                        //lujing = saveFileDialog1.FileName;//定义一个变量来接收保存文件的名称
                        StreamWriter sw = new StreamWriter(saveFileDialog1.FileName);//开启流通道
                        sw.Write(textBox1.Text);//保存编辑文档
                        sw.Close();//关闭流通道
                    }
                }
                else
                {
                    StreamWriter sw = new StreamWriter(lujing);//开启流通道
                    sw.Write(textBox1.Text);//保存编辑文档
                    sw.Close();//关闭流通道
                }
           
            }
    
            private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if(this.textBox1.Text.Length>0)//如果打开记事本中有输入得东西
                {
                    DialogResult dr = MessageBox.Show("是否进行保存","保存对话框",MessageBoxButtons.YesNo);
                    //弹出是否保存的对话框
                    if(dr==DialogResult.OK)//用户点击确定
                    {
                        if (lujing == "")//如果没有该保存路径
                        {
                            saveFileDialog1.FileName = "新建文件.txt";//设置保存名称及后缀文件类型
                            DialogResult dr1 = saveFileDialog1.ShowDialog();//设置一个对话框变量dr接收显示保存对话框的内容
                            if (dr1 == DialogResult.OK)//如果用户点击确定
                            {
                                //lujing = saveFileDialog1.FileName;//定义一个变量来接收保存文件的名称
                                StreamWriter sw = new StreamWriter(saveFileDialog1.FileName);//开启流通道
                                sw.Write(textBox1.Text);//保存编辑文档
                                sw.Close();//关闭流通道
                            }
                        }
                        else
                        {
                            StreamWriter sw = new StreamWriter(lujing);//开启流通道
                            sw.Write(textBox1.Text);//保存编辑文档
                            sw.Close();//关闭流通道
                        }
                    }
    
                }
                lujing = null;//执行完新建将路径变为空,因为是新建的文本是不会有路径的.
                this.textBox1.Text = ""; //将文本框清空
            }
    
            private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
            {
                openFileDialog1.Filter = "文本文件|*.txt|所有文件|*.*";//筛选打开文件类型
                DialogResult dr = openFileDialog1.ShowDialog();//设置一个对话框变量接收
                if (dr == DialogResult.OK)//点击确定按钮
                {
    
                    lujing = openFileDialog1.FileName;//将文件路径的值赋给lujing这个变量
                    StreamReader sr = new StreamReader(openFileDialog1.FileName,UnicodeEncoding.GetEncoding("GB2312"));//初始化读取流媒体类,并将打开的文件名称,及当前编码形式
                    //相当于用水管连接两个水龙头,打开开关数据进行读写操作
                    textBox1.Text = sr.ReadToEnd();
                    sr.Close();
                }
                //第二种方法
                //OpenFileDialog ofd = new OpenFileDialog();//初始化打开文件夹这个类,并定义一个变量ofd
                //ofd.Title = "打开";
                //ofd.Filter = "文本文件|*.txt|所有文件|*.*";//设置打开文件类型对话框中文件类型,文本文件txt或者所有文件
                //if (ofd.ShowDialog() == DialogResult.OK)//如果用户点击打开文件对话框
                //{
                //    lujing = saveFileDialog1.FileName;
                //    FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                //    //调用以文件为主的stream类,既支持同步操作又支持异步操作。将开启的权限赋给变量fs
                //    //ofd.FileName 获取文件名称及路径
                //    //FileMode.Open 打开选定文件
                //    //FileAccess.Read 设置文件操作为读取
                //    //FileShare.Read   允许随后打开文件的读取
                //    StreamReader sr = new StreamReader(fs, Encoding.Default);//读取流媒体初始化,将fs的值及当前字符编码赋值给sr
                //    textBox1.Text = sr.ReadToEnd();//用TextBox1接收读取到的内容
                //    sr.Close();//关闭读取的流媒体通道
                //    sr.Close();//关闭以文件操作为主的流媒体通道
                //}
            }
    
            private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e)
            {
    
                saveFileDialog1.FileName = "新建为文件.txt";//设置保存名称及后缀文件类型
                saveFileDialog1.ShowDialog();
                StreamWriter sw = new StreamWriter(saveFileDialog1.FileName);//开启流通道
                sw.Write(textBox1.Text);//保存编辑文档
                sw.Close();//关闭流通道
            }
        }
    }
    View Code
    //窗体移动API
    [DllImport("user32.dll")]
    public static extern bool ReleaseCapture();
    [DllImport("user32.dll")]
    public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int IParam);
    public const int WM_SYSCOMMAND = 0x0112;
    public const int SC_MOVE = 0xF010;
    public const int HTCAPTION = 0x0002;
    [DllImport("user32")]
    private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, IntPtr lParam);
    private const int WM_SETREDRAW = 0xB;
    
    
    
    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        if (this.WindowState == FormWindowState.Normal)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }
    }
    窗体移动API
         //窗体阴影API
            const int CS_DropSHADOW = 0x20000;
            const int GCL_STYLE = (-26);
            [DllImport("user32.dll", CharSet = CharSet.Auto)]
            public static extern int SetClassLong(IntPtr hwnd, int nIndex, int dwNewLong);
            [DllImport("user32.dll", CharSet = CharSet.Auto)]
            public static extern int GetClassLong(IntPtr hwnd, int nIndex);
    
    
            public Form1()
            {
                InitializeComponent();
                SetClassLong(this.Handle, GCL_STYLE, GetClassLong(this.Handle, GCL_STYLE) | CS_DropSHADOW);
            }
    窗体阴影API

    打印控件 

    printDocument 设置打印对象的各项初始属性,需要首先设置,面向对象的操作

    打印控件分三步

    1、打印页面设置 pageSetupDialog

    2、打印预览 printPreviewControl  打印预览控件,不经常用

                       printPreviewDialog  打印预览对话框,常用

    3、打印       printDialog

    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;
    
    namespace dayyin
    {
        public partial class Form1 : Form
        {
            //打印步骤
         
            //打印设置
            //打印预览
            //打印
            public Form1()
            {
                InitializeComponent();
            }
    
            private void 页面设置ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                pageSetupDialog1.Document = printDocument1;//将打印设置的指向打印对象1
                pageSetupDialog1.ShowDialog();//弹出打印设置对话框
            }
    
            private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {
                //首先要设置打印对象,类似于画板
                Font f=new Font("宋体",14);//设置字体
                Brush b=new SolidBrush(Color.Black);//设置画刷样式
                PointF p = new PointF(10,10);//定义坐标点
                e.Graphics.DrawString(textBox1.Text,f,b,p);//设置绘画参数,要绘制的字符串,字体,格式刷,坐标
                //System.Drawing.Printing 命名空间提供与打印相关的服务。
                //PrintPageEventArgs为 PrintPage 事件提供数据。
                //Graphics 图形,图形,显卡
                //DrawString 绘制字符串形式
            }
    
            private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
            {
    
            }
    
            private void 打印预览VToolStripMenuItem_Click(object sender, EventArgs e)
            {
                //printPreviewControl1.Document = printDocument1;//打印预览控件
                printPreviewDialog1.Document = printDocument1;//打印对话框
                printPreviewDialog1.ShowDialog();
            }
    
            private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
            {
                printDialog1.Document = printDocument1;//指向打印内容
                printDialog1.ShowDialog();//显示打印对话框
            }
    
    
    
        }
    }
    View Code
  • 相关阅读:
    (转) hive调优(2)
    (转)hive调优(1) coding调优
    hive on tez 错误记录
    tez 0.9.0 配置
    hive on tez
    让博客园自动生成目录
    hive --metastore三种模式
    hive 使用beelin连接报错
    mysql my.cnf文件
    1、Kfaka 部署
  • 原文地址:https://www.cnblogs.com/1030351096zzz/p/6160415.html
Copyright © 2011-2022 走看看