C#表单 - 使用Paint方法? [英] C# Forms - Using Paint methods?

查看:167
本文介绍了C#表单 - 使用Paint方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在c#表格中,我创建了一个新的绘图方法:

prerivate $ thisPolygon(PaintEventArgs e)
{
Pen clrBlue =新笔(Color.Blue,3);
Point [] Wst = new Point [5];
Wst [0] =新点(20,350);
Wst [1] =新点(110,200);
Wst [2] =新点(200,190);
Wst [3] =新点(210,275);
Wst [4] =新点(190,400);
Wst [5] =新点(50,390);
e.Graphics.DrawPolygon(clrBlue,Wst);
}

现在,我该如何调用它?我不能使它工作,这是行不通的:

pre $ private $ Form1_Load(object sender,EventArgs e)
{
thisPolygon(); ///我试过在括号内添加一些东西,失败了。


解决方案

您有几个不同的问题。



(1)阵列容量。你的数组被初始化为5个存储位置,但你试图设置第六个值。

  Point [] Wst = new Point [5]; // 5个索引
...
Wst [5] =新点(50,390); //尝试访问第六个,但超出界限

将其更改为。

  Point [] Wst = new Point [6]; 

请记住,数组是从零开始索引的。



(2)不使用OnPaint 。您在 OnLoad 方法中调用 thisPolygon ,该方法不会保留绘图。将您的调用移到表单的 OnPaint 方法中。

 保护覆盖void OnPaint(PaintEventArgs e){
base.OnPaint(e);
thisPolygon();

(3)不传递PaintEventArgs 。您没有将任何事件参数传递给 thisPolygon 方法,它甚至不会按原样进行编译。传入来自 OnPaint 方法的paint参数。

  protected override void OnPaint(PaintEventArgs e){
base.OnPaint(e); //传入e
thisPolygon();
}


In c# forms I have created a new paint method:

private void thisPolygon(PaintEventArgs e)
{
    Pen clrBlue = new Pen(Color.Blue, 3);
    Point[] Wst = new Point[5];
    Wst[0] = new Point(20, 350);
    Wst[1] = new Point(110, 200);
    Wst[2] = new Point(200, 190);
    Wst[3] = new Point(210, 275);
    Wst[4] = new Point(190, 400);
    Wst[5] = new Point(50, 390);
    e.Graphics.DrawPolygon(clrBlue, Wst);
}

Now, how do I call it? I can't make it work, this doesn't work:

private void Form1_Load(object sender, EventArgs e)
{
    thisPolygon(); ///I've tried adding some stuff in brackets area, failed.
}

解决方案

You have a few different problems.

(1) Array Capacity. Your array is initialized with 5 storage locations, but you are attempting to set a sixth value.

Point[] Wst = new Point[5]; // 5 indexes
...
Wst[5] = new Point(50, 390); // Tries to access a sixth, but is out of bounds

Change this to.

Point[] Wst = new Point[6];

Remember that arrays are zero-based indexed.

(2) Not using OnPaint. You're calling thisPolygon in the OnLoad method, which won't persist your drawing. Move your call to the OnPaint method of the form.

protected override void OnPaint(PaintEventArgs e) {
    base.OnPaint(e);
    thisPolygon();
}

(3) Not passing PaintEventArgs. You're not passing in any event arguments to your thisPolygon method, and it won't even compile as it is. Pass in the paint arguments from the OnPaint method.

protected override void OnPaint(PaintEventArgs e) {
    base.OnPaint(e); // Pass in e
    thisPolygon();
}

这篇关于C#表单 - 使用Paint方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆