面板上的C#绘图 [英] C# Drawing on Panels

查看:92
本文介绍了面板上的C#绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在草拟一个日程安排,并用面板表示时隙,而约会则是面板上方的更多面板。

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.

我已经确定在这种情况下,我调用了一个私有函数 drawZigZag(Panel p,int direction); 进行绘制。这一天是水平分布的,方向-1表示左侧为锯齿形,方向1表示右侧为锯齿形。

I've identified the case where this occurs, and I call a private function 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.

到目前为止,我还没有意识到之字形,我只是在尝试 CreateGraphics() FillPolygon()。到目前为止,我有:

So far, I'm not up to the zigzag yet, I'm just experimenting with CreateGraphics() and 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);
    }

第一个 FillRectangle()我本来没有。我只补充说,当 FillPolygon()不起作用时。

The first FillRectangle() I didn't originally have. I only added that when the FillPolygon() didn't work.

基本上,它不起作用,我我不确定为什么。面板是原始颜色-尚未填充为深绿色。之前我曾使用 CreateGraphics()做其他事情,但我不确定为什么它在这种情况下不起作用。有什么想法吗?

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. Any ideas?

编辑:对不起,我想我应该提一下: Label 控件上有几个 Label 控件c $ c> Panel 描述约会。

Sorry, I thought I should mention: There are several Label controls on my Panel which describe the appointment. These shouldn't be covered if possible.

推荐答案

您需要从paint事件处理程序中调用此方法,而不仅是在您需要的任何时候。喜欢。因此,在您的构造函数中,您可能会拥有:

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);

然后执行:

    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 );
    }

这篇关于面板上的C#绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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