微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

OnPaintBackground 未绘制完整区域

如何解决OnPaintBackground 未绘制完整区域

我遇到了一些困难,我可以使用一些帮助来理解为什么此代码不起作用,但仅限于一部分(它在绘图中的控件中途停止,或者图像的底部被剪裁...无论发生什么)。

我提供了一个屏幕截图,以展示控件的绘制方式,以及与该问题相关的方法中的代码

Control Display Example

    private void CreateDoubleBuffer()
    {
        Int32 height,width = this.Width;
        height = this.Font.Height + 9;
        height += ((_workHours * 2) * 21);
        _dblBuffer = new Bitmap(height,width);
        RefreshBackground();
    }
    private void RefreshBackground()
    {
        Graphics g = Graphics.FromImage(_dblBuffer);
        Int32 height,width = this.Width;
        height = this.Font.Height + 9;
        height += ((_workHours * 2) * 21);
        //MessageBox.Show("Drawing Size: " + width.ToString() + " x " + height.ToString() + "\nControl Size: " + this.Width.ToString() + " x " + this.Height.ToString());
        g.Clip = new Region(new Rectangle(this.Location,new Size(width,height)));
        g.FillRectangle(new SolidBrush(this.DateBackColor),new Rectangle(0,width,this.Font.Height + 8));
        String curDate = "";
        DateTime n = this._selectedDate;
        switch (n.Month)
        {
            case 1:
                curDate = "January";
                break;
            case 2:
                curDate = "February";
                break;
            case 3:
                curDate = "march";
                break;
            case 4:
                curDate = "April";
                break;
            case 5:
                curDate = "May";
                break;
            case 6:
                curDate = "June";
                break;
            case 7:
                curDate = "July";
                break;
            case 8:
                curDate = "August";
                break;
            case 9:
                curDate = "September";
                break;
            case 10:
                curDate = "October";
                break;
            case 11:
                curDate = "November";
                break;
            case 12:
                curDate = "December";
                break;
        }
        curDate += " " + n.Day.ToString() + "," + n.Year.ToString();
        g.DrawString(curDate,this.Font,new SolidBrush(this.TimesForeColor),new Point(5,5));
        g.FillRectangle(new SolidBrush(this.TimesBackColor),this.Font.Height + 8,40,height - (this.Font.Height + 8)));

        Pen sepPen = new Pen(new SolidBrush(this.TimesSeparatorColor));
        Int32 sections = _workHours * 2;
        for (Int32 i = 1; i <= sections; i++)
        {
            Int32 curHeight = (this.Font.Height + 9) + (i * 21),startY = ((i % 2) == 1) ? 20 : 0;
            Point p1 = new Point(startY,curHeight),p2 = new Point(width,curHeight);
            g.DrawLine(sepPen,p1,p2);
            Int32 curWorkHour = i / 2;
            DateTime curTime = ((0 == curWorkHour) ? _startTime : _startTime.AddHours(curWorkHour));
            String time = ((curTime.Hour > 12) ? curTime.Hour - 12 : curTime.Hour).ToString() + " " + ((curTime.TimeOfDay.Hours < 12) ? "AM" : "PM");
            if ((i % 2) == 1)
                g.DrawString(time,curHeight - this.Font.Height));
        }
        g.dispose();
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        if (null == _dblBuffer)
            CreateDoubleBuffer();
        else
            RefreshBackground();
        base.OnPaint(e);
    }
    protected override void OnPaintBackground(PaintEventArgs e)
    {
        e.Graphics.Clear(this.BackColor);
        e.Graphics.DrawImageUnscaled(_dblBuffer,new Point(0,0));
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。