在面板控件中滚动GDI像素 [英] Scrolling GDI pixels in Panel control

查看:252
本文介绍了在面板控件中滚动GDI像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何使Panel控件滚动其中的任何内容?我不是在谈论控件或用户控件或自定义控件.我只说像素.用GDI +绘制:

How do we make a Panel control scroll whatever's inside of it? I'm not talking about controls or user controls or custom controls. I'm talking only about pixels; drawn with GDI+:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GDITEST
{
    public partial class Form1 : Form
    {
        public class Item
        {
            public int Height { get; set; }
            public int Width { get; set; }
            public int Top { get; set; }
        }

        internal List<Item> items = new List<Item>();

        public Form1()
        {
            InitializeComponent();
        }

        private void panel_Paint(object sender, PaintEventArgs e)
        {
            if (items != null)
            {
                if (items.Count >= 1)
                {
                    foreach (Item item in items)
                    {
                        using (Pen pen = new Pen(Color.Blue, 1))
                        {
                            int count;
                            count = items.Count;

                            count = count >= 1 ? count : 1;
                            e.Graphics.DrawRectangle(pen, 0, item.Top, (item.Width - SystemInformation.VerticalScrollBarWidth), item.Height);
                        }
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            items.Add(new Item() { Width = this.Width, Height = 25, Top = (items.Count * 25) });
            panel.Refresh();
        }
    }
}

上面的代码绘制了一个蓝色矩形(有点像一个垂直列表).当矩形的数量扩展面板的高度时,我希望面板滚动.

The above code draws a blue rectangle (kinda like a vertical list). When the number of rectangles extends the height of the panel, I want the panel to scroll.

由于大多数结果仅返回与滚动自定义控件有关的内容,因此我无法找到具体方法.

I've not been able to find out how to do this, since most of the results only return stuff related to scrolling custom controls.

我确实读过某个地方(我再也找不到了),您可以使用某些translateX或translateY方法...但是,我很难找到关于这些方法的更多信息.

I did read somewhere (which I can no longer find) that you can use some translateX or translateY methods... Yet I am having a hard time trying to find out anything more about those methods.

推荐答案

代码中有一个简单的错误,您忘了用滚动量抵消绘制的内容. Panel类还有另外两个怪癖,它已被优化为容器控件,并在绘制方式上有所作为.通过从Panel创建派生类,可以摆脱所有三个问题.项目>添加类>粘贴下面显示的代码.生成>将新控件从工具箱的顶部拖放到您的表单上.

There's a simple bug in your code, you forgot to offset what you draw by the scroll amount. The Panel class has two other quirks, it was optimized to be a container control and cuts corners on the way it paints. You get rid of all three issues by creating your derived class from Panel. Project > Add Class > paste the code shown below. Build > Build and drop the new control from the top of the toolbox onto your form.

using System;
using System.Windows.Forms;

class MyPanel : Panel {
    public MyPanel() {
        this.DoubleBuffered = this.ResizeRedraw = true;
    }
    protected override void OnPaint(PaintEventArgs e) {
        e.Graphics.TranslateTransform(this.AutoScrollPosition.X, this.AutoScrollPosition.Y);
        base.OnPaint(e);
    }
}

您可以通过注意e.ClipRectangle属性来进一步优化Paint事件处理程序,并在剪切时跳过绘制项目.以防万一:分配AutoScrollMinSize属性以适合项目.

You can further optimize your Paint event handler by paying attention to the e.ClipRectangle property and skip drawing an item when it is clipped. Just in case: assign the AutoScrollMinSize property to fit the items.

这篇关于在面板控件中滚动GDI像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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