4.C#编程学习——窗体Paint事件处理程序
2016-11-21 23:06
337 查看
4.C#编程学习——窗体Paint事件处理程序
usingSystem.Drawing;
usingSystem.Windows.Forms;
classPaintEvent
{
publicstaticvoid Main()
{
Form form =
newForm();
form.Text =
"Paint Event";
form.Paint +=
newPaintEventHandler(MyPaintHandler);
Application.Run(form);
}
staticvoid MyPaintHandler(object
objSender, PaintEventArgs pea)
{
Graphics grfx = pea.Graphics;
grfx.Clear(Color.Chocolate);
}
}
当在Main中创建窗体之后,名为MyPaintHandler方法附加到这个窗体的Paint事件中。
从PaintEventArgs类获得一个Graphics对象,并使用它来调用Clear方法。
usingSystem.Drawing;
usingSystem.Windows.Forms;
classPaintTwoForms
{
staticForm form1, form2;
publicstaticvoid Main()
{
form1 =
newForm();
form2 =
newForm();
form1.Text =
"First Form";
form1.BackColor =
Color.White;
form1.Paint +=
newPaintEventHandler(MyPaintHandler);
form2.Text =
"Second Form";
form2.BackColor =
Color.White;
form2.Paint +=
newPaintEventHandler(MyPaintHandler);
form2.Show();
Application.Run(form1);
}
staticvoid MyPaintHandler(object
objSender, PaintEventArgs pea)
{
Form form = (Form)objSender;
Graphics grfx = pea.Graphics;
string str;
if (form == form1)
str =
"Hello from the first form";
else
str =
"Hello from the second form";
grfx.DrawString(str, form.Font,
Brushes.Black, 0, 0);
}
}
usingSystem.Drawing;
usingSystem.Windows.Forms;
classHelloWorld :
Form
{
publicstaticvoid Main()
{
Application.Run(newHelloWorld());
}
public HelloWorld()
{
Text =
"Hello World";
BackColor =
Color.White;
}
protectedoverridevoid OnPaint(PaintEventArgs
pea)
{
Graphics grfx = pea.Graphics;
grfx.DrawString("Hello, Windows Forms!", Font,
Brushes.Black, 0, 0);
}
}
源码
usingSystem;usingSystem.Drawing;
usingSystem.Windows.Forms;
classPaintEvent
{
publicstaticvoid Main()
{
Form form =
newForm();
form.Text =
"Paint Event";
form.Paint +=
newPaintEventHandler(MyPaintHandler);
Application.Run(form);
}
staticvoid MyPaintHandler(object
objSender, PaintEventArgs pea)
{
Graphics grfx = pea.Graphics;
grfx.Clear(Color.Chocolate);
}
}
当在Main中创建窗体之后,名为MyPaintHandler方法附加到这个窗体的Paint事件中。
从PaintEventArgs类获得一个Graphics对象,并使用它来调用Clear方法。
Paint事件
相当频繁、有时出乎意料到调用这个方法。可以不中断的快速重新绘制客户区。多个窗体
usingSystem;usingSystem.Drawing;
usingSystem.Windows.Forms;
classPaintTwoForms
{
staticForm form1, form2;
publicstaticvoid Main()
{
form1 =
newForm();
form2 =
newForm();
form1.Text =
"First Form";
form1.BackColor =
Color.White;
form1.Paint +=
newPaintEventHandler(MyPaintHandler);
form2.Text =
"Second Form";
form2.BackColor =
Color.White;
form2.Paint +=
newPaintEventHandler(MyPaintHandler);
form2.Show();
Application.Run(form1);
}
staticvoid MyPaintHandler(object
objSender, PaintEventArgs pea)
{
Form form = (Form)objSender;
Graphics grfx = pea.Graphics;
string str;
if (form == form1)
str =
"Hello from the first form";
else
str =
"Hello from the second form";
grfx.DrawString(str, form.Font,
Brushes.Black, 0, 0);
}
}
OnPaint方 法
通过继承Form而不只是创建一个实例可以获得一些好处。源码
usingSystem;usingSystem.Drawing;
usingSystem.Windows.Forms;
classHelloWorld :
Form
{
publicstaticvoid Main()
{
Application.Run(newHelloWorld());
}
public HelloWorld()
{
Text =
"Hello World";
BackColor =
Color.White;
}
protectedoverridevoid OnPaint(PaintEventArgs
pea)
{
Graphics grfx = pea.Graphics;
grfx.DrawString("Hello, Windows Forms!", Font,
Brushes.Black, 0, 0);
}
}
相关文章推荐
- ASP.NET 3.5核心编程学习笔记(41):HTTP处理程序与HTTP模块
- 窗体事件的处理程序
- js事件处理程序学习与总结
- Java 组件及事件处理实训 实训2:编写一个窗体程序,用于计算一元二次方程
- js学习笔记:事件——事件流、事件处理程序、事件对象
- JavaScript Event学习第三章 早期的事件处理程序
- Java Swing界面编程(21)---事件处理:窗体事件
- DOM事件处理程序学习随笔1(IMOOC)
- JavaScript高级程序设计学习笔记--事件(一)(事件流、事件处理程序/事件侦听器)
- Java 组件及事件处理实训 实训1: 编写一个窗体程序,能够对文本区中的文字设置字体和大小。
- ASP.NET 3.5核心编程学习笔记(42):HTTP处理程序的编写之建立数据报表
- 【js学习笔记-092】-----事件处理程序的调用
- 【js学习笔记-091】-----------注册事件处理程序
- JavaScript事件处理程序 学习笔记
- 【翻译作品】JavaScript Event学习第三章:早期的事件处理程序
- JavaScript Event学习第三章:早期的事件处理程序
- ASP.NET 3.5核心编程学习笔记(45):HTTP处理程序之高级编程
- JavaScript高级程序设计学习笔记--事件(一)(事件流、事件处理程序/事件侦听器)
- head first C#学习笔记:如何创建事件以及事件处理程序
- JavaScript事件学习小结(二)js事件处理程序