C# 将图片框添加到面板

标签 c# winforms panel

我有一个名为 panel3 的面板,位于“Form1.cs”中。在这个面板中,我画了 5 条线来代表音乐人员。

现在我想将钢琴上按下的音符的图片添加到乐谱中。基本上,我想在音乐谱表的特定位置添加一个图片框。

为了做到这一点,我在“Staff.cs”中创建了一个名为“addPictureBox”的方法来构建一个图片框,并在每次按下按键时将其放置在面板上。

这是相关代码。问题是面板上不显示图片框。在 panel3 中只能看到之前绘制的线条。

请问我该如何解决这个问题?为什么panel3中不显示图片框?谢谢。

编辑

感谢您的帮助:)我已经解决了问题

最佳答案

恕我直言,放置图片框不是个好主意 - 它不透明。所以我建议你手动做笔记。例如:


创建基注释类:

public class Note
{
    public Point Location { get; set; } 

    public virtual void Draw(Graphics g)
    {

    }
}

创建笔记,比如说简单、困难和图片(绘制图片):

public class SimpleNote:Note
{
    public SimpleNote(Point position)
    {
        Location = position;
    }

    public override void Draw(Graphics g)
    {
        g.FillEllipse(Brushes.Red, Location.X, Location.Y, 5, 5);
        g.DrawLine(new Pen(Color.Red), Location.X+5, Location.Y, Location.X+5, Location.Y-15);
    }
}

public class DifficultNote:Note
{
    public DifficultNote(Point position)
    {
        Location = position;
    }

    public override void Draw(Graphics g)
    {
        SimpleNote left = new SimpleNote(Location);
        SimpleNote right = new SimpleNote(new Point(Location.X + 20, Location.Y));

        left.Draw(g);
        right.Draw(g);
        g.DrawLine(new Pen(Color.Red), Location.X+5, Location.Y - 15, Location.X+25, Location.Y-15);
    }
}

public class PictureNote:Note
{
    private Image _image;
    public PictureNote(Image image, Point position)
    {
        Location = new Point(position.X - image.Width/2, position.Y - image.Height/2);
        _image = image;
    }

    public override void Draw(Graphics g)
    {
        g.DrawImage(_image, Location);
    }
}

在Staff类中添加笔记列表,并在OnPaint方法中调用draw方法:

public List<Note> noteList = new List<Note>(); 
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;
    }

    foreach (var note in noteList)
    {
        note.Draw(e.Graphics);
    }
}

并在您的人员面板中创建鼠标单击事件处理程序,例如如下所示:

private void staff1_MouseClick(object sender, MouseEventArgs e)
{
    if(e.Button == MouseButtons.Left)
        staff1.noteList.Add(new SimpleNote(new Point(e.X, e.Y)));
    if(e.Button == MouseButtons.Right)
        staff1.noteList.Add(new DifficultNote(new Point(e.X, e.Y)));
    if(e.Button == MouseButtons.Middle)
            staff1.noteList.Add(new PictureNote(new Bitmap("c:\\note.png"), new Point(e.X, e.Y)));
       staff1.Invalidate();
}

结果(第一个是图片,第二个是困难的,第三个是简单的):

enter image description here

P.s.代码不是最好的,我只是说明我的想法。

关于C# 将图片框添加到面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8657168/

相关文章:

使用多个变量和一些时间不变的从宽到面板 reshape 数据框

python - Pandas 面板中的 bool 掩码

javascript - ASP :LinkButton created via literal control will not fire code behind

c# - 在更新 WinForms 中的控件(例如 DataGridView)时闪烁

c# - 在 Unity 中创建能力对象

c++ - 二进制 '=' : no operator found which takes a right-hand operand of type 'std::unique_ptr<char [],std::default_delete<_Ty>>'

c# - 在 C# 和 WinForms 中将筛选框添加到 ListView 的列标题

html - 使 Bootstrap 面板内容 div 拉伸(stretch)到其父级的 100%,没有硬编码值

c# - c#中的互斥量和信号量是什么?我们需要在哪里实现?

c# - ~1 秒 TcpListener Pending()/AcceptTcpClient() 滞后