winform(容器、打印、对话框)

时间:2021-08-23 02:29:21

一、布局:
2个属性:
Anchor:锁定位置
Dock:填充位置
一般Dock是与容器控件配合使用


二、容器控件:
Panel:就是一个区域,类似于DIV,可以独立布局,还可以让其它控件及容器在它的内部再次布局

FlowLayoutPanel:流式布局容器,内容会默认从左向右排列,如果宽度不够了,那么自动换行

GroupBox:依然是容器,拥有Panel全部属性,比Panel多了个标题和边框样式

TabControl:选项卡容器

SplitContainer:二分区容器,相当于两个panel,可以改变大小,,也可以继续添加

TableLayoutPanel:表格容器

--------------------------------
三、打印控件:
PrintDocument:要打印的对象,非常重要
程序不知道你要打印什么东西,它只会去找这个要打印的对象
你所要做的事情,就是将要打印的内容绘制到这个打印对象上去

PageSetupDialog:打印设置对话框

PrintPreviewControl:打印预览控件
PrintPreviewDialog:打印预览对话框

PrintDialog:打印对话框

DialogResult dr = printDialog1.ShowDialog();

if (dr == DialogResult.OK)//如果用户点击的是确定按钮,那么执行打印
{
printDocument1.Print();
}

DialogResult是一个可被实例化的枚举类,用来接收用户在对话框中的操作

打印代码:

private void 打印设置ToolStripMenuItem_Click(object sender, EventArgs e) { pageSetupDialog1.Document = printDocument1; pageSetupDialog1.ShowDialog(); } private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { Font f =new Font("黑体",18); SolidBrush b = new SolidBrush(Color.Black); e.Graphics.DrawString(textBox1.Text, f, b, 10, 10); } 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; DialogResult dr = printDialog1.ShowDialog(); if (dr == DialogResult.OK) { printDocument1.Print(); } }

colorDialog - 颜色选择对话框

private void 字体颜色ToolStripMenuItem_Click(object sender, EventArgs e) { DialogResult dr = colorDialog1.ShowDialog(); if (dr == DialogResult.OK) { textBox1.ForeColor = colorDialog1.Color; } }

folderBrowserDialog - 文件路径选择对话框

private void 选择文件夹ToolStripMenuItem_Click(object sender, EventArgs e) { folderBrowserDialog1.ShowDialog(); toolStripStatusLabel1.Text = folderBrowserDialog1.SelectedPath; }

fontDialog - 字体选择对话框,可在属性中打开ShowColor,在对话框中会出现颜色选择

private void 字体设置ToolStripMenuItem_Click(object sender, EventArgs e) { fontDialog1.ShowDialog(); textBox1.Font = fontDialog1.Font; textBox1.ForeColor = fontDialog1.Color; }

openFileDialog - 打开文件对话框
openFileDialog1.Filter = "文本文件.txt|*.txt|视频文件|*.avi|所有文件|*.*";

private void 打开OToolStripMenuItem_Click(object sender, EventArgs e) { openFileDialog1.Filter = "文本文件.txt|*.txt|视频文件|*.avi|所有文件|*.*"; DialogResult dr = openFileDialog1.ShowDialog(); if (dr == DialogResult.OK) { toolStripStatusLabel1.Text = openFileDialog1.FileName; StreamReader sr = new StreamReader(openFileDialog1.FileName, System.Text.Encoding.Default); textBox1.Text = sr.ReadToEnd(); sr.Close(); } }

saveFileDialog - 保存文件对话框
问题:如果我已经保存过了,那么在次保存不需要弹出对话框,而是直接执行保存就可以。

string path = ""; private void 保存SToolStripMenuItem_Click(object sender, EventArgs e) { if (path == "") { saveFileDialog1.FileName = "新建文本文件.txt"; DialogResult dr = saveFileDialog1.ShowDialog(); if (dr == DialogResult.OK) { toolStripStatusLabel1.Text = saveFileDialog1.FileName; StreamWriter sw = new StreamWriter(saveFileDialog1.FileName); sw.Write(textBox1.Text); sw.Close(); path = saveFileDialog1.FileName; } } else { StreamWriter sw = new StreamWriter(path); sw.Write(textBox1.Text); sw.Close(); }

绝对路径:带盘符的 G:\0720\...

相对路径:根据你当前程序的目录下,找到某个文件夹 /css/css1.css

流:IO
StreamReader - 读取流 sr.ReadToEnd();
StreamWriter - 输出流 sw.Write(要输出的内容);

winform(容器、打印、对话框)