0

我正在处理一个新项目。我有一个类可以将矩形绘制到 windows 窗体。我想把这个类嵌入到另一个类中。代码如下;

主代码将调用形状代码;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using sekilciz_uygulama;



namespace xml_test_v1

{
class Program
{
    static void Main(string[] args)
    {
        XmlDocument xDoc = new XmlDocument();

        xDoc.Load("c:\\sw_xml_test_4.xml");
        int rad=0;
        string giris_text = Console.ReadLine().ToString();

        Console.WriteLine(giris_text);
            foreach(XmlNode node in xDoc.SelectNodes("network/switch"))



            {

                string ip_adress =      node.SelectSingleNode("ip_adress").InnerText.ToString();

                Console.WriteLine(ip_adress);

                if (ip_adress.Contains(giris_text))

                {
                   // call for shape code!!!
                }


                }}}}

创建形状的代码;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace sekilciz_uygulama
{
public class Sekilciz
{

    public Rectangle[] skare;

    private SolidBrush firca;
    private int x,y, genislik, yukseklik;

    public Sekilciz()

    {
      skare = new Rectangle[5];
        firca = new SolidBrush(Color.Blue);

        x = 500;
        y = 200;
        genislik= 100;

        yukseklik =100;


        for(int i=0; i< skare.Length;i++)

        {
             skare[i] = new Rectangle(x,y,genislik,yukseklik);

            x-=150;
        }
    }

    public void kareciz(Graphics duzlem)

    {
        foreach(Rectangle rec in skare)

        {
            duzlem.FillRectangle(firca,rec);
        }
    }
    }

}

4

2 回答 2

0
// "this" is  your windows form, or control like button
var myGraphic = this.CreateGraphics();
var sekilciz = new Sekilciz(myGraphic);
sekilciz.kareciz();

但是您的构造函数中正在进行太多处理。最好在同一个类中以其他方法移动该代码。

在此处查看在 Windows 窗体上绘图的完整示例: 在 Windows 窗体上绘制图形的基础知识

于 2012-10-18T07:24:18.830 回答
0
var sekilciz = new Sekilciz();
sekilciz.kareciz(null);

您需要将参数传递给您的方法kareciz

于 2012-10-18T07:28:50.910 回答