Making progress on scrolling?
- From: "Steve Magoon" <nospamowkmann@xxxxxxxxxxxxxxx>
- Date: Thu, 16 Jun 2005 19:39:07 GMT
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);
}
}
}
.
- Follow-Ups:
- Re: Making progress on scrolling?
- From: Bob Powell [MVP]
- Re: Making progress on scrolling?
- From: Rachel Suddeth
- Re: Making progress on scrolling?
- From: Rachel Suddeth
- Re: Making progress on scrolling?
- Prev by Date: Re: Inverted bitmap and negative/positive stride.
- Next by Date: Re: Inverted bitmap and negative/positive stride.
- Previous by thread: icons in dialogs
- Next by thread: Re: Making progress on scrolling?
- Index(es):
Relevant Pages
|