【问题标题】:Draw persistent grid in TabPage (.Net, C#)在 TabPage 中绘制持久网格(.Net,C#)
【发布时间】:2013-05-14 21:01:45
【问题描述】:

我需要在 TabPage 中显示一个持久网格。如果我可以绘制到 TabPage 的整个不可见部分并防止在滚动时擦除图形,我的问题将立即得到解决。

我能想到的唯一其他解决方案是跟踪选项卡中的滚动位置并以此为基础绘制网格。

为了首先绘制它,我必须为 TabPage.Paint 创建一个 EventHandler。

    //Code removed

此方法绘制垂直和水平线以在可见选项卡中创建网格,但只要发生 Paint 事件(即滚动),它就会继续绘制,因此它会创建重叠线并且不与任何东西对齐,除了选项卡的当前可见区域。

【问题讨论】:

    标签: c# .net grid drawing system.drawing


    【解决方案1】:

    也许这样的东西对你有用:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
    
            const int gridSpacing = 20;
            const int lineThickness = 1;
            Bitmap bmp = new Bitmap(gridSpacing, gridSpacing);
            using (System.Drawing.Pen pen = new System.Drawing.Pen(Color.Blue, lineThickness))
            {
                using (Graphics G = Graphics.FromImage(bmp))
                {
                    G.Clear(this.BackColor);
                    G.DrawLine(pen, 0, bmp.Height - pen.Width, bmp.Width, bmp.Height - pen.Width); // horizontal
                    G.DrawLine(pen, bmp.Width - pen.Width, 0, bmp.Width - pen.Width, bmp.Height); // vertical
                }
            }
    
            foreach (TabPage TP in tabControl1.TabPages)
            {
                TP.BackgroundImage = bmp;
                TP.BackgroundImageLayout = ImageLayout.Tile;
            }
        }
    }
    

    【讨论】:

    • 你只需要它以不同的方式对齐吗?...你能详细说明一下吗?也许我们可以调整它;或以某种方式改变您当前的方法。
    【解决方案2】:

    请记住,此解决方案只是伪解决方案。您还必须响应滚动。

    void form_draw()
    {
            spacingX = offsetX % scale * -1;
            spacingY = offsetY % scale * -1;
    
            if (form.HorizontalPosition != lastXPosition && form.VerticalPosition == lastYPosition)
                lastStartX += spacingX;
            else if (tab.HorizontalScroll.Value == lastXPosition && form.VerticalPosition != lastYPosition)
                lastStartY += spacingY;
    
            lastYPosition = form.VerticalPosition;
            lastXPosition = form.HorizontalPosition;
    
            for (int i = lastStartY; i < formHeight; i += scale)
                form.draw(0, i, formWidth, i);
            for (int i = lastStartX; i < formWidth; i += scale)
                form.draw(i, 0, i, formWidth);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多