简体   繁体   English

C# 在面板上绘图

[英]C# Drawing on Panels

I'm drawing up a day schedule and representing timeslots with panels, and appointments are yet more panels on top.我正在制定一天时间表并用面板表示时间段,而约会则是顶部的更多面板。

The user is able to scroll up and down so that the range they can see is shifted earlier or later.用户可以上下滚动,以便他们可以看到的范围更早或更晚地移动。 When an appointment runs off the end of the visible range, I want there to be a zig-zag indicating that the appointment extends beyond the visible bounds.当约会超出可见范围的末尾时,我希望有一个锯齿形,表明约会超出了可见范围。

I've identified the case where this occurs, and I call a private function drawZigZag(Panel p, int direction);我已经确定了发生这种情况的情况,并且我调用了一个私有函数drawZigZag(Panel p, int direction); to draw it.绘制它。 The day is spread horizontally, and the direction -1 indicates a zigzag on the left and 1 indicates a zigzag on the right.一天是水平分布的,方向 -1 表示左侧的锯齿形,1 表示右侧的锯齿形。

So far, I'm not up to the zigzag yet, I'm just experimenting with CreateGraphics() and FillPolygon() .到目前为止,我还没有达到锯齿形,我只是在试验CreateGraphics()FillPolygon() So far I have:到目前为止,我有:

    private void drawZigZag(Panel p, int direction) // 1 = right, -1 = left
    {
        Graphics g = p.CreateGraphics();

        g.FillRectangle(new SolidBrush(Color.FromArgb(0, Color.Black)), p.DisplayRectangle);

        Point[] points = new Point[4];

        points[0] = new Point(0, 0);
        points[1] = new Point(0, p.Height);
        points[2] = new Point(p.Width, p.Height);
        points[3] = new Point(p.Width, 0);

        Brush brush = new SolidBrush(Color.DarkGreen);

        g.FillPolygon(brush, points);
    }

The first FillRectangle() I didn't originally have.我最初没有的第一个FillRectangle() I only added that when the FillPolygon() didn't work.我只在FillPolygon()不起作用时添加了它。

Basically, it's not working and I'm not sure why.基本上,它不起作用,我不知道为什么。 The panel is the original colour - it hasn't been filled DarkGreen.面板是原始颜色 - 它没有填充深绿色。 I've used CreateGraphics() before for other things, and I'm not really sure why it's not working in this instance.我之前已经将CreateGraphics()用于其他用途,但我不确定为什么它在这种情况下不起作用。 Any ideas?有任何想法吗?

Edit: Sorry, I thought I should mention: There are several Label controls on my Panel which describe the appointment.编辑:对不起,我想我应该提到:我的Panel上有几个描述约会的Label控件。 These shouldn't be covered if possible.如果可能,这些不应该被覆盖。

You need to call this method from the paint event handler, not just whenever you like.您需要从绘制事件处理程序中调用此方法,而不仅仅是在您喜欢的时候。 So in your constructor you might have:所以在你的构造函数中你可能有:

panel1.Paint += new PaintEventHandler(panel1_Paint);

and then the implementation:然后是实现:

    private void panel1_Paint( object sender, PaintEventArgs e )
    {
        var p = sender as Panel;
        var g = e.Graphics;

        g.FillRectangle( new SolidBrush( Color.FromArgb( 0, Color.Black ) ), p.DisplayRectangle );

        Point[] points = new Point[4];

        points[0] = new Point( 0, 0 );
        points[1] = new Point( 0, p.Height );
        points[2] = new Point( p.Width, p.Height);
        points[3] = new Point( p.Width, 0 );

        Brush brush = new SolidBrush( Color.DarkGreen );

        g.FillPolygon( brush, points );
    }

For example we have this drawing event which is drawing a text from textBox1:例如,我们有这个绘制事件,它从 textBox1 绘制文本:

private void panel1_draw(object sender, PaintEventArgs e)
    {
        var g = e.Graphics;
        Pen myp = new Pen(System.Drawing.Color.Red, 4);
        Font fy = new Font("Helvetica", 10, FontStyle.Bold);
        Brush br = new SolidBrush(System.Drawing.Color.Red);
        g.DrawString(textBox1.Text, fy, br, 0,0);
    }

In order to draw on your panel1, you need to write this code in your button event handler:为了在您的 panel1 上绘图,您需要在按钮事件处理程序中编写以下代码:

private void button1_Click(object sender, EventArgs e)
    {
        panel1.Paint+=new PaintEventHandler(panel1_draw);
        panel1.Refresh();
    }

The first line draws the text in your panel and if you want the text to appear you must refresh the panel.第一行在您的面板中绘制文本,如果您希望文本出现,您必须刷新面板。 The main thing is in using the panel1.Pain += new PaintEventHandler(your void name);主要是在使用 panel1.Pain += new PaintEventHandler(your void name); and panel1.Refresh();和 panel1.Refresh();

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

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