MSDN上有一些不错的例子,楼主可以参考一下。
解决方案 »
- Entity Framework 按条件多个Where性能问题
- excel导入到DATATABLE之后出现问题
- C#初学者,请教如何在源码编辑器里生成一个GUID,我想用它作主键?多谢大虾!
- 如何从数据库中读取image类型的变量
- 求解快速掌握.Net C#语言 开发的方法,请各位大虾指路
- CacheItemRemovedCallback的用法,我是这样用的,有没错呢
- 问一个关于堆栈的问题
- 关于使用字段、方法和属性的问题
- 为什么我的vs,已安装的模板那么少?
- C#的messagebox.show的用法
- 问过好多人!难道众位高手就没有遇到过类似问题吗?一个.net2003 framework的问题。
- winform中的数据库当调用updata()方法以后,就无法查询数据了。
这个是我的blog,全源代码di,楼主可以参考一下。
Point currentPoint; // 当前绘图点
Pen thepen; // 绘图使用的画笔
float penwidth; // 画笔的宽度
SolidBrush thebrush; // 绘图使用的画刷
Color thecolor; // 画笔或画刷的颜色
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent(); //
// TODO: Add any constructor code after InitializeComponent call
//
currentPoint = new Point(50,10);
points = new ArrayList();
points.Add( currentPoint );
penwidth = 1;
thecolor = Color.Black;
thepen = new Pen(thecolor);
thebrush = new SolidBrush( thecolor );
}
private void Form1_MouseUp(Object sender,MouseEventArgs e)
{
if(currentPoint.X !=e.X || currentPoint.Y != e.Y)
{
currentPoint.X = e.X;
currentPoint.Y = e.Y;
points.Add( currentPoint );
this.Invalidate(); // 更新窗体
this.Update();
}
} private void Form1_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
Point[] ps = (Point[])points.ToArray(typeof(Point));
if( rbLine.Checked )
g.DrawLine( Pens.Black,ps[0],ps[ps.Length-1]); // 绘制直线
else if( rbRectangle.Checked )
g.DrawRectangle( thepen, // 绘制矩形
ps[0].X,ps[0].Y,
ps[ps.Length-1].X - ps[0].X,
ps[ps.Length-1].Y - ps[0].Y);
else if( rbCurve.Checked )
g.DrawCurve(thepen,ps); // 绘制曲线
else if( rbPolygon.Checked )
g.DrawPolygon( thepen,ps ); // 绘制多边形
else if( rbEllipse.Checked )
g.DrawEllipse(thepen,ps[0].X,ps[0].Y, // 绘制椭圆
ps[ps.Length-1].X-ps[0].X,
ps[ps.Length-1].Y-ps[0].Y);
} private void ChangeDrawType(object sender, System.EventArgs e)
{
points.Clear(); // 清除所有绘图点
currentPoint = new Point(50,10);
points.Add( currentPoint);
}
private void lstPenStyle_MeasureItem(object sender,
System.Windows.Forms.MeasureItemEventArgs e)
{
e.ItemHeight = 12; // 设置列表框每一项的高度
}private void lstPenStyle_DrawItem(object sender,
System.Windows.Forms.DrawItemEventArgs e)
{
Pen p = new Pen( Color.Black );
Graphics g = e.Graphics;
e.DrawBackground();
// 使用不同的风格和宽度画出列表框的每一项
if( e.Index == 0 )
{
p.DashStyle=System.Drawing.Drawing2D. DashStyle. Solid;
p.Width = 1;
}
else if( e.Index == 1 )
{
p.DashStyle = System.Drawing.Drawing2D.Dash Style. Solid;
p.Width = 3;
}
else if( e.Index == 2 )
{
p.DashStyle = System.Drawing.Drawing2D.Dash Style. Dash;
p.Width = 1;
}
else if( e.Index == 3 )
{
p.DashStyle = System.Drawing.Drawing2D.Dash Style. Dash;
p.Width = 3;
}
else if( e.Index == 4 )
{
p.DashStyle = System.Drawing.Drawing2D.Dash Style. Dot;
p.Width = 1;
}
else if( e.Index == 5 )
{
p.DashStyle = System.Drawing.Drawing2D.Dash Style. Dot;
p.Width = 3;
}
else
{
p.DashStyle = System.Drawing.Drawing2D.Dash Style. Solid;
p.Width = 1;
}
p.Color = e.ForeColor;g.DrawLine(p,e.Bounds.X,e.Bounds.Y+4,e.Bounds.Right,e.Bounds.Y+4);
e.DrawFocusRectangle(); } // 根据列表框当前选择项的不同,来设置画笔的不同属性
private void lstPenStyle_SelectedIndexChanged(object sender, System.EventArgs e)
{
if( lstPenStyle.SelectedIndex == 0 )
{
thepen.DashStyle = System.Drawing.Drawing2D. Dash Style.Solid;
thepen.Width = 1;
}
else if( lstPenStyle.SelectedIndex == 1 )
{
thepen.DashStyle = System.Drawing.Drawing2D. Dash Style.Solid;
thepen.Width = 3;
}
else if( lstPenStyle.SelectedIndex == 2 )
{
thepen.DashStyle = System.Drawing.Drawing2D. Dash Style.Dash;
thepen.Width = 1;
}
else if( lstPenStyle.SelectedIndex == 3 )
{
thepen.DashStyle = System.Drawing.Drawing2D. Dash Style.Dash;
thepen.Width = 3;
}
else if( lstPenStyle.SelectedIndex == 4 )
{
thepen.DashStyle = System.Drawing.Drawing2D. Dash Style.Dot;
thepen.Width = 1;
}
else if( lstPenStyle.SelectedIndex == 5 )
{
thepen.DashStyle = System.Drawing.Drawing2D. Dash Style.Dot;
thepen.Width = 3;
}
this.Invalidate(); // 刷新窗体
this.Update();
}private void rbPen_CheckedChanged(object sender, System. EventArgs e)
{
if( rbPen.Checked )
lstPenStyle.Visible = True;
else
lstPenStyle.Visible = False;
}
public PrintDocument pd = new PrintDocument();
private void FormPageSetting_Load(object sender, System.EventArgs e)
{
// 把默认打印机支持的纸张大小添加到组合框cbxSize中
for(int i=0;i< pd.PrinterSettings.PaperSizes.Count;i++ )
{cbxSize.Items.Add( pd.PrinterSettings.PaperSizes[i].PaperName );
} // 把默认打印机支持的纸张来源添加到组合框cbxSource中
for( int i=0;i<pd.PrinterSettings.PaperSources.Count;i++)
{
cbxSource.Items.Add(pd.PrinterSettings.PaperSources
[i].SourceName );
}
// 设置纸张大小和来源的初始值
cbxSize.Text = pd.DefaultPageSettings.PaperSize.PaperName;
cbxSource.Text = pd.DefaultPageSettings.PaperSource. Source Name; // 把打印机的默认页边距赋给4个文本框控件
txtLeft.Text = pd.DefaultPageSettings.Margins.Left.ToString ();
txtRight.Text = pd.DefaultPageSettings.Margins.Right.ToString ();
txtTop.Text = pd.DefaultPageSettings.Margins.Top.ToString();
txtBottom.Text = pd.DefaultPageSettings.Margins.Bottom.To String (); // 根据打印机的默认打印方向设置2个单选按钮的选中状态
if( pd.DefaultPageSettings.Landscape )
rbHorz.Checked = True;
else
rbVert.Checked = True;
}
string[] lines; // 保存读入的C#源文件
public PrintDocument pd ; // 打印文档对象
Font printfont; // 打印使用的字体
int curline; // 缓冲区中的当前行
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent(); //
// TODO: Add any constructor code after InitializeComponent
// call
//
pd = new PrintDocument(); // 创建打印文档实例
// 为打印文档的PrintPage事件绑定处理方法
pd.PrintPage += new PrintPageEventHandler ( this.Print Every Page );
// 创建打印使用的字体
printfont = new Font("宋体",9.5F);
curline = 0;
}
// 显示打开文件对话框并把所选择的
// C#源文件读到缓冲区中
private void miOpen_Click(object sender, System.EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.CheckFileExists = True;
dlg.Filter = "C#源文件(*.cs)|*.cs";
dlg.Title = "打开C#源文件";
dlg.ValidateNames = True;
try
{
if( dlg.ShowDialog() == DialogResult.OK )
{
StreamReader sr = new StreamReader(
dlg.FileName,System.Text.Encoding.Default);
ArrayList buffer = new ArrayList();
// 读入文件中的所有内容
while( sr.Peek() != -1 )
{
buffer.Add( sr.ReadLine() );
}
// 把数组列表转换为字符串数组
lines = (string[])buffer.ToArray(typeof(string));
txtContent.Lines = lines;
txtContent.SelectionLength = 0;
sr.Close();
// 设置打印文档的标题
pd.DocumentName = "C#源文件:" +
Path.GetFileName(dlg.FileName);
// 激活打印和打印预览以及打印菜单项
miPrintPreview.Enabled = True;
miPrint.Enabled = True;
}
}
catch( Exception ex )
{
MessageBox.Show(this,ex.Message,"打开文件错误",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
} // 显示页面设置对话框设置打印页面的一些属性
private void miPageSetting_Click(object sender, System. Event
Args e)
{
// 创建并显示页面设置对话框
FormPageSetting ps = new FormPageSetting();
if( ps.ShowDialog(this) == DialogResult.OK )// 如果返回确定
{ // 根据对话框中输入的页边距设置打印文档的页边距
pd.DefaultPageSettings.Margins =
new Margins( int.Parse(ps.txtLeft.Text),
int.Parse(ps.txtRight.Text),
int.Parse(ps.txtTop.Text),
int.Parse(ps.txtBottom.Text));
// 设置打印文档的页面大小
PrinterSettings.PaperSizeCollection sizes =
ps.pd.PrinterSettings.PaperSizes;
for(int i=0;i<sizes.Count;i++ )
{
if( ps.cbxSize.Text == sizes[i].PaperName )
{
pd.DefaultPageSettings.PaperSize = sizes[i];
break;
}
} // 设置打印文档的纸张来源
PrinterSettings.PaperSourceCollection sources =
ps.pd.PrinterSettings.PaperSources;
for( int i=0;i<sources.Count;i++)
{
if( ps.cbxSource.Text == sources[i].SourceName )
{
pd.DefaultPageSettings.PaperSource = sources
[i];
break;
}
} // 设置打印文档的打印方向
if( ps.rbVert.Checked )
pd.DefaultPageSettings.Landscape = False;
else
pd.DefaultPageSettings.Landscape = True;
}
} // 显示打印预览对话框
private void miPrintPreview_Click(object sender, System.Event
Args e)
{
PrintPreviewDialog ppd = new PrintPreviewDialog();
ppd.PrintPreviewControl.Document = pd;
ppd.ShowDialog(this);
} // 打印每一页
private void PrintEveryPage( object sender,
System.Drawing.Printing.PrintPageEventArgs e )
{
float fltLines = 0; // 一页中的行数
float fltYPos = 0; // 每一行的Y坐标
int nCount = 0; // 已打印行数
float fltLeftMargin = e.MarginBounds.Left; // 获取打印的起
// 始位置
float fltTopMargin = e.MarginBounds.Top; // 计算一页中的行数
fltLines = e.MarginBounds.Height / printfont.GetHeight
(e. Graphics); // 打印一页
while( nCount < fltLines && (nCount+curline<lines.Length))
{
fltYPos = fltTopMargin + ( nCount * printfont.GetHeight
(e. Graphics) ); e.Graphics.DrawString( lines[curline+nCount],printfont,
Brushes.Black, fltLeftMargin, fltYPos );
nCount++;
}
curline += nCount; // 如果未到达文件的末尾,则继续打印
if( curline < lines.Length )
e.HasMorePages = True;
else // 否则停止打印
{
e.HasMorePages = False;
curline = 0;
} } // 显示打印对话框并执行打印操作
private void miPrint_Click(object sender, System.EventArgs e)
{
PrintDialog dlg = new PrintDialog();
dlg.AllowPrintToFile = True;
dlg.AllowSelection = True;
dlg.AllowSomePages = True;
dlg.Document = pd;
if( dlg.ShowDialog() == DialogResult.OK )
{
pd.Print();
}
} // 退出程序
private void miExit_Click(object sender, System.EventArgs e)
{
pd.Dispose();
this.Close();
}
Petzold这人应该认识吧。
可惜好象没电子版。