Making progress on scrolling?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi,

I would like to scroll an object on a panel with a fixed grid for the
background. I couldn't figure out how to use the AutoScroll feature of the
Panel tool to do this. My latest approach is to use a horizontal scroll bar
docked on the panel, using its scrolling event to invalidate the panel (see
the code below - sorry, it's a bit long). It works, but flickers badly. I
tried to move the grid calculations out of the Paint event, but it doesn't
seem to help. Is there a better way to do this? I would like to reduce as
much of the flicker as possible.

Thanks in advance,

Steve

using System;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

namespace WindowsApplication2

{

public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.Panel panel1;

private System.ComponentModel.Container components = null;

private PointF[] pts_vleft;

private PointF[] pts_vright;

private PointF[] pts_htop;

private PointF[] pts_hbot;

const int numpts = 9;

private System.Windows.Forms.HScrollBar hScrollBar1;

private int ScrollXpos;

public Form1()

{

InitializeComponent();

// Calculate the origin and end points for the background grid to reduce
calcs in Paint

pts_vleft = new PointF[numpts];

pts_vright = new PointF[numpts];

pts_htop = new PointF[numpts];

pts_hbot = new PointF[numpts];

float vertstep = ((float) (panel1.DisplayRectangle.Height -
hScrollBar1.Height)) / 10f;

float horzstep = ((float) panel1.DisplayRectangle.Width) / 10f;

for (int i = 0; i < numpts; i++)

{

pts_vleft[i] = new PointF (0, (i + 1) * vertstep);

pts_vright[i] = new PointF(panel1.DisplayRectangle.Width, (i + 1) *
vertstep);

pts_htop[i] = new PointF((i + 1) * horzstep, 0);

pts_hbot[i] = new PointF((i + 1) * horzstep,
panel1.DisplayRectangle.Height - hScrollBar1.Height);

}

ScrollXpos = 0;

}

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

private void InitializeComponent()

{

this.panel1 = new System.Windows.Forms.Panel();

this.hScrollBar1 = new System.Windows.Forms.HScrollBar();

this.panel1.SuspendLayout();

this.SuspendLayout();

//

// panel1

//

this.panel1.Anchor =
((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top
| System.Windows.Forms.AnchorStyles.Bottom)

| System.Windows.Forms.AnchorStyles.Left)

| System.Windows.Forms.AnchorStyles.Right)));

this.panel1.BackColor = System.Drawing.SystemColors.ControlDark;

this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;

this.panel1.Controls.Add(this.hScrollBar1);

this.panel1.Location = new System.Drawing.Point(56, 56);

this.panel1.Name = "panel1";

this.panel1.Size = new System.Drawing.Size(336, 232);

this.panel1.TabIndex = 0;

this.panel1.Resize += new System.EventHandler(this.panel1_Resize);

this.panel1.Paint += new
System.Windows.Forms.PaintEventHandler(this.panel1_Paint);

//

// hScrollBar1

//

this.hScrollBar1.Dock = System.Windows.Forms.DockStyle.Bottom;

this.hScrollBar1.Location = new System.Drawing.Point(0, 211);

this.hScrollBar1.Name = "hScrollBar1";

this.hScrollBar1.Size = new System.Drawing.Size(332, 17);

this.hScrollBar1.TabIndex = 0;

this.hScrollBar1.Scroll += new
System.Windows.Forms.ScrollEventHandler(this.hScrollBar1_Scroll);

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(464, 350);

this.Controls.Add(this.panel1);

this.Name = "Form1";

this.Text = "Form1";

this.panel1.ResumeLayout(false);

this.ResumeLayout(false);

}

#endregion

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void hScrollBar1_Scroll(object sender,
System.Windows.Forms.ScrollEventArgs e)

{

ScrollXpos = - hScrollBar1.Value;

panel1.Invalidate(true);

}

private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs
e)

{

// Draw the background grid

using (Pen pen = new Pen(Color.FromArgb(255 * 3 / 4, 255, 255, 255), 1))

{

e.Graphics.DrawLine(pen, pts_vleft[4], pts_vright[4]);

e.Graphics.DrawLine(pen, pts_htop[4], pts_hbot[4]);

pen.DashStyle = DashStyle.Custom;

pen.DashPattern = new float[] {3f, 4f, };

for (int i = 0; i < numpts; i++)

{

e.Graphics.DrawLine(pen, pts_vleft[i], pts_vright[i]);

e.Graphics.DrawLine(pen, pts_htop[i], pts_hbot[i]);

}

}

// Now draw a circle that can be scrolled independently of the grid


Matrix mx=new Matrix(1,0,0,1,ScrollXpos, (panel1.DisplayRectangle.Height -
hScrollBar1.Height) / 2);

e.Graphics.Transform=mx;

Rectangle rect = new Rectangle(250,-50,100,100);

using (Brush brush = new SolidBrush(Color.Green))

{

e.Graphics.FillEllipse(brush, rect);

}

}

private void panel1_Resize(object sender, System.EventArgs e)

{

// Do the calcs for the background grid for each resize before it gets to
Paint

float vertstep = ((float) (panel1.DisplayRectangle.Height -
hScrollBar1.Height)) / 10f;

float horzstep = ((float) panel1.DisplayRectangle.Width) / 10f;

for (int i = 0; i < numpts; i++)

{

pts_vleft[i] = new PointF (0, (i + 1) * vertstep);

pts_vright[i] = new PointF(panel1.DisplayRectangle.Width, (i + 1) *
vertstep);

pts_htop[i] = new PointF((i + 1) * horzstep, 0);

pts_hbot[i] = new PointF((i + 1) * horzstep,
panel1.DisplayRectangle.Height - hScrollBar1.Height);

}

panel1.Invalidate(true);

}

}

}


.



Relevant Pages

  • Re: Help with drawing?
    ... content of the panel to a bitmap (for double buffering so it doesn't ... > I have the following code which is supposed to draw a grid in a panel ... > and activate the autoscroll feature, however I cant get it to scroll ... > Public Sub New ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Column locking pleasant side-effect
    ... LockColumns properties. ... > What exactly is the grid or column property that locks the columns? ... >> that column in the middle of the panel. ... >> if I locked all of the columns, VFP will not scroll ...
    (microsoft.public.fox.programmer.exchange)
  • Quick Gridview question
    ... I have a gridview bound to a SQLDataConnector. ... I have the grid in a panel so it shows the top 15 records and ... then they have vertical scrollbars to scroll down the grid. ... scroll down the panel to find, after the round trip the record is ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Column locking pleasant side-effect
    ... What exactly is the grid or column property that locks the columns? ... > and VFP perceives that any portion of the column is ... > that column in the middle of the panel. ... > if I locked all of the columns, VFP will not scroll ...
    (microsoft.public.fox.programmer.exchange)
  • Re: Fetching more records for a grid
    ... However in all the chatter on this thread there were several issues overlooked, What if a record is added that logically becomes the next record when the user arrows down, because you have an extract of the real table, the user will not see this unless they drop out and go back in again or scroll past the current 100 and back again. ... Codewise to achieve what you want is not onerous, in clipper the TBROWSE function was only about 50 lines of code and catered for drawing the boxes (grid) and maintaining the scrollbar on the side etc. ... The philosophy of the code was to read the first page full of records into an array and display the array. ... I use the comma to concatenate fields, so Smi,joh might find John Smith where Smi finds Smith, the comma says pad out the key with the contents of the current record then look for the contents of the next key ie John. ...
    (microsoft.public.fox.programmer.exchange)