简体   繁体   English

用C#画线

[英]Drawing Lines in C#

I am a C# newbie and have encountered a problem in my homework. 我是C#新手,在作业中遇到问题。

I have a panel in 'Form1.cs' called panel3. 我在“ Form1.cs”中有一个名为panel3的面板。

Now, I have a class called 'Staff' and I want to add a method in Staff to draw a sequence of lines underneath each other. 现在,我有一个名为“ Staff”的类,我想在Staff中添加一个方法以在彼此之间绘制一系列线。 These lines must be added and shown in panel3 (found in Form1.cs). 这些行必须添加并显示在panel3中(可在Form1.cs中找到)。

How can I do this please? 我该怎么做? Thank you. 谢谢。

I have something like this in the "Staff.cs": 我在“ Staff.cs”中有这样的内容:

My problem is how I am going to invoke it in Form1_Load event? 我的问题是如何在Form1_Load事件中调用它? What parameters should I pass to it? 我应该传递什么参数?

I want the 'Draw' method to draw the lines in panel3 found in 'Form1.cs'. 我希望“绘制”方法在“ Form1.cs”中的panel3中绘制线条。

Thanks. 谢谢。

Edit 编辑

Thanks a lot for your help :) I have solved it now thanks to you :) 非常感谢您的帮助:)感谢您,我现在已经解决了问题:)

  1. Override OnPaint event in your Staff class 在Staff类中覆盖OnPaint事件
  2. Create staffIndex propertie - then you can edit it directly in Properties window 创建staffIndex属性-然后可以直接在“属性”窗口中对其进行编辑
  3. Compile project - Staff control apears in Tool box 编译项目-工具箱中的员工控制提示
  4. Drag and Drop your Staff control on your Form. 将您的Staff控件拖放到窗体上。

public class Staff : Panel
{
    public const int kOffset = 30;
    public const int kSignatureOffset = 25;
    public const int kStaffSpacing = 70;
    public const int kBarSpacing = 7;
    const int kNumMeasuresOnAStaff = 4;
    public const int kStaffInPixels = 800;

    public int staffIndex { get; set; }

    public Staff()
    {
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        int yPos = kOffset + staffIndex * kStaffSpacing;
        for (int bars = 0; bars < 5; bars++)
        {
            e.Graphics.DrawLine(Pens.Black, 0, yPos, kStaffInPixels, yPos);
            yPos += kBarSpacing;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM