[.net] Disortion with scrollbars

Started by
5 comments, last by Sijmen 19 years, 6 months ago
Back again, for your help! I am building a map editor (see topic in 'Your Announcements', i posted a prototype there). When I draw all the tiles on a panel, and update the the minimum scroll size of that panel. But when I try to use the scrollbars, I get very weird disortion. When I trigger a resize event (by changing the window size or such), everthing is ok again. I'd like to know two thing: - Did I do something wrong with the drawing? (if yes, what?) - If that doesn't fix it, how to redraw the panel whenever it scrolls? There is no Scrolled event or such which I can hook a handler up! Thanks [Edited by - Sijmen on September 29, 2004 3:47:31 AM]
Advertisement
I got the problem now: when you draw to coordinate (0,0), it means that it will draw it on the upper-left corner of the control on-screen, indipendent of the scrollbar state.

What I ask now, is: how to get the position of the scrollbar?

AutoScrollPosition does not seem to help. If you want, I'll upload the source and binaries somewhere.
Just compare the value property to the minimum and maximum of the scrollbar...

Cheers
I am afraid that I do not really understand you. In what way? Isn't it just in pixels?
No. You set the Minimum and the Maximum properties to, let's say, 10 and 100. Then the Value property will allways have a value between 10 and 100.

They made it like this so you don't have to worry about pixels.

Then again, I could be totally wrong 'cause you might be doing something completely different.

Post some code and I'll take a look.

Cheers
Thanks. Please realize that I'm using the AutoScroll property of ScrollableControl (Panel inherrits from it), they work somewhat different. When I'll get to school tomorrow, I'll post the code. Thanks!
Sorry that I was so infinitely late. Here is the code:

public class TileView : System.Windows.Forms.UserControl{	#region Component Designer generated code	private System.Windows.Forms.Panel panel;	private System.Windows.Forms.ImageList tileList;	private System.ComponentModel.IContainer components;	/// <summary> 	/// Clean up any resources being used.	/// </summary>	protected override void Dispose( bool disposing )	{		if( disposing )		{			if(components != null)			{				components.Dispose();			}		}		base.Dispose( disposing );	}	/// <summary> 	/// Required method for Designer support - do not modify 	/// the contents of this method with the code editor.	/// </summary>	private void InitializeComponent()	{		this.components = new System.ComponentModel.Container();		this.panel = new System.Windows.Forms.Panel();		this.tileList = new System.Windows.Forms.ImageList(this.components);		this.SuspendLayout();		// 		// panel		// 		this.panel.BackColor = System.Drawing.SystemColors.Window;		this.panel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;		this.panel.Dock = System.Windows.Forms.DockStyle.Fill;		this.panel.Location = new System.Drawing.Point(0, 0);		this.panel.Name = "panel";		this.panel.Size = new System.Drawing.Size(150, 150);		this.panel.TabIndex = 0;		this.panel.Resize += new System.EventHandler(this.panel_Resize);		this.panel.Paint += new System.Windows.Forms.PaintEventHandler(this.panel_Paint);		this.panel.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel_MouseDown);		// 		// tileList		// 		this.tileList.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;		this.tileList.ImageSize = new System.Drawing.Size(16, 16);		this.tileList.TransparentColor = System.Drawing.Color.Transparent;		// 		// TileView		// 		this.Controls.Add(this.panel);		this.Name = "TileView";		this.ResumeLayout(false);	}	#endregion	Pen gridPen   = new Pen(new SolidBrush(Color.FromKnownColor(KnownColor.Control)));	Pen selectPen = new Pen(Color.Yellow, 2);	int    tileSize = 32;	Bitmap source;	int    selected = -1;	int tilesPerRow;	Point getPoint(int i)	{			return new Point(i%tilesPerRow*tileSize, i/tilesPerRow*tileSize);	}	Rectangle getRect(int i)	{		return new Rectangle(i%tilesPerRow*tileSize, i/tilesPerRow*tileSize, tileSize, tileSize);	}	Rectangle getRectWithMargin(int i, int margin)	{		return new Rectangle(i%tilesPerRow*tileSize-margin, i/tilesPerRow*tileSize-margin,			tileSize+margin*2, tileSize+margin*2);	}	void redoTiles()	{		selected = -1;		if(source!=null)		{			BusyWindow busy = new BusyWindow();			busy.BusyText   = "Splitting tiles...";			busy.Show();			busy.Update();			tileList.ImageSize = new Size(tileSize, tileSize);			tileList.Images.Clear();			tilesPerRow = panel.ClientSize.Width/tileSize;			for(int y=0; y*tileSize<=source.Width-tileSize; y++)			{				for(int x=0; x*tileSize<=source.Width-tileSize; x++)				{					busy.BusyText = string.Format("Splitting tiles...\r\nTile ({0},{1})", x, y);					busy.Update();					Bitmap thisTile;					thisTile = new Bitmap(tileSize, tileSize);					Graphics.FromImage(thisTile).DrawImage(source, new Rectangle(0, 0, tileSize, tileSize),						new Rectangle(x*tileSize, y*tileSize, tileSize, tileSize), GraphicsUnit.Pixel);					tileList.Images.Add(thisTile);				}			}			busy.Close();		}		panel.Invalidate();	}	public TileView()	{		InitializeComponent();	}	private void panel_Paint(object sender, System.Windows.Forms.PaintEventArgs e)	{		tilesPerRow = panel.ClientSize.Width/tileSize;		Invalidate();		Graphics g = e.Graphics;		for(int x=0; x<panel.ClientSize.Width; x+=tileSize)			g.DrawLine(gridPen, x, 0, x, panel.ClientSize.Height);		for(int y=0; y<panel.ClientSize.Height; y+=tileSize)			g.DrawLine(gridPen, 0, y, panel.ClientSize.Width, y);		if(tileList.Images.Count!=0)		{			for(int i=0; i<tileList.Images.Count; i++)				tileList.Draw(g, getPoint(i), i);			panel.AutoScrollMinSize = new Size(0, tileList.Images.Count/tilesPerRow*tileSize);		}		if(selected!=-1)			g.DrawRectangle(selectPen, getRect(selected));	}	private void panel_Resize(object sender, System.EventArgs e)	{		panel.Invalidate();	}	private void panel_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)	{		int oldSelected = selected;		selected = -1;		if(oldSelected>0)			panel.Invalidate(getRectWithMargin(oldSelected, 1));		if(e.X>tilesPerRow*tileSize)			return;		int row     = e.Y/tileSize;		int numrows = tileList.Images.Count/tilesPerRow;		if(row>numrows || row==numrows && e.X>tileList.Images.Count%tilesPerRow*tileSize)			return;		selected = row*tilesPerRow+e.X/tileSize;		panel.Invalidate(getRectWithMargin(selected, 1));	}	public int TileSize	{		get { return tileSize; }				set 		{ 			tileSize = value;			redoTiles();		}	}	public Bitmap SourceBitmap	{		get { return source; }				set		{			source = value;			redoTiles();		}	}	public Image Selected	{		get { return selected==-1 ? null : tileList.Images[selected]; }	}}


As you cam see, I wrote some helper funtions that should make it easyer to find screen coordinates. But even when I add/substract AutoScrollPosition.X or Y (or something like that) to/from the values, I still get the same problem.

Thanks for the help!

This topic is closed to new replies.

Advertisement