On Nov 12, 11:37*pm, "Bob Powell [MVP]" <b...@spamkillerbobpowell.net>
wrote:
If you look at the canvas code I sent you, the control is quite happy to
deal with large images.
No, it did not work, though I finally got it to work past just
compilation. Reproduced below. It did give a nice white screen with
a grey border that can be resized to any arbitrary size, using the
_zoom, but if you set _zoom to a very large number, say 2.0F, then no
scroll bars show up (despite, using the Designer Wizard, I did check
the scrollbars property to True).
>
The scroll bars are set using the info from my "understanding autoscroll"
article.
Is that in your canvas code?
>
Centering on the screen is easy. Just set the origin of the picture to:
center of screen - width/height of picture /2
--
Where do I add this? In what function call / event handler?
--
Bob Powell [MVP]
Visual C#, System.Drawing
Con permission, I have a few more optional questions (you can feel
free to answer as many or few as you please; I appreciate your time):
1) below is my code. I notice that the Color.White background seems
to not allow a very bright overlay of say a red rectangle (see the
rectangle I draw below). It comes out (or so it seems) a bit pink,
like the white background is bleeding through.
2) How to use scrollbars, as I mentioned above. AutoScrollPosition is
shown throughout the code, but, unlike my working example in the
original thread (which was inspired by your website examples), the one
that uses hidden tiny buttons, this time the form doesn't scroll. If
you can point out in your code where this feature is, maybe I can
check that I didn't copy and paste wrong--since to make the program
work past the compile stage I had to create a form from scratch and
copy and paste selectively, until everything (I hope) was copied from
your code.
3). Minor but important for me: this line made things work without
the invalidation problem: "this.SetStyle
( ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint |
ControlStyles.ResizeRedraw ,true); "
How to set this using the Wizard (I like to use the wizard whenever
possible)?
Thank you.
I like your "canvas" example for my library because, though the
scrollbars don't work, for _zoom < 1.0F you can resize the window by
grapping a corner, and the white canvas with grey border will
automatically stay in the middle of the resized window, and, of
course, the BackTrackMouse function and Matrix will correctly adjust
your mouse coordinates (I added a function for right-button down to
test this, as you can see below). Very nice, but I do like the idea of
scrollbars and want to add them.
RL
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace Canvas2
{
public partial class Canvas : Form
{
private Size _pageSize; // = new Size(640, 480);
private bool _clipToPage;
private float _zoom;
private Color _pageColor; // = Color.White;
public Canvas()
{
InitializeComponent();
_pageSize = new Size(640, 480);
_zoom = 0.5F;
_pageColor = Color.White; //Color.Transparent //
Color.Empty; //do not work, give dark background, use Color.White
//
this.SetStyle( ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint | ControlStyles.ResizeRedraw ,true);
}
// private Color _pageColor = Color.White;
[
Category("Appearance"),
Description("The base color of the page")
]
public Color PageColor
{
get { return _pageColor; }
set
{
_pageColor = value;
Invalidate();
}
}
// private bool _clipToPage;
[
Category("Behavior"),
Description("Gets or sets the clipping flag. When true no
drawing is allowed outside page boundaries")
]
public bool ClipToPage
{
get { return _clipToPage; }
set
{
_clipToPage = value;
Invalidate();
}
}
// private Size _pageSize = new Size(640, 480);
[
Category("Appearance"),
Description("The size of the virtual page")
]
public Size PageSize
{
get { return _pageSize; }
set
{
_pageSize = value;
OnPageSizeChanged(EventArgs.Empty);
}
}
protected virtual void OnPageSizeChanged(EventArgs e)
{
CalcScroll();
if (PageSizeChanged != null)
PageSizeChanged(this, e);
}
public event EventHandler PageSizeChanged;
void CalcScroll()
{
Size cs = new Size((int)(this._pageSize.Width * _zoom),
(int)(this._pageSize.Height * _zoom));
this.AutoScrollMinSize = cs;
Invalidate();
}
[Category("Behavior"),
Description("The multiplying factor for the zoom level"),
DefaultValue(1.0f)]
public float Zoom
{
get { return _zoom; }
set
{
_zoom = value;
//A zoom may not be negative
if (_zoom < 0)
_zoom = Math.Abs(_zoom);
//a zoom may be very small but never zero.
if (_zoom == 0)
_zoom = 0.000001f;
//The scrollbars should be recalculated
CalcScroll();
//and the host application code informed if needs be
OnZoomChanged(EventArgs.Empty);
}
}
public event EventHandler ZoomChanged;
protected virtual void OnZoomChanged(EventArgs e)
{
if (this.ZoomChanged != null)
ZoomChanged(this, e);
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaintBackground(e);
Matrix mx = new Matrix(_zoom, 0, 0, _zoom, 0, 0);
Size s = new Size((int)(this.ClientSize.Width * (1f /
_zoom)), (int)(this.ClientSize.Height * (1f / _zoom)));
if (s.Width PageSize.Width)
mx.Translate((s.Width / 2) - (_pageSize.Width / 2),
0);
else
mx.Translate((float)this.AutoScrollPosition.X * (1f /
_zoom), 0);
if (s.Height PageSize.Height)
mx.Translate(0, (s.Height / 2) -
(this._pageSize.Height / 2) + (this.AutoScrollPosition.Y));
else
mx.Translate(0, (float)this.AutoScrollPosition.Y *
(1f / _zoom));
e.Graphics.Transform = mx;
SolidBrush b = new SolidBrush(Color.FromArgb(64,
Color.Black));
e.Graphics.FillRectangle(b, new Rectangle(new Point(20,
20), PageSize));
b.Color = PageColor;
e.Graphics.FillRectangle(b, new Rectangle(new Point(0, 0),
PageSize));
if (ClipToPage)
e.Graphics.SetClip(new Rectangle(0, 0, PageSize.Width,
PageSize.Height));
//SolidBrush b1 = new SolidBrush(Color.FromArgb(64,
Color.Red));
//e.Graphics.FillRectangle(b1, new Rectangle(new Point(75,
75), new Size(20, 30)));
base.OnPaint(e);
}
protected virtual MouseEventArgs BacktrackMouse(MouseEventArgs e)
{
Matrix mx=new Matrix(_zoom,0,0,_zoom,0,0);
Size s=new Size( (int)(this.ClientSize.Width*(1f/_zoom)),
(int)(this.ClientSize.Height*(1f/_zoom)));
if(s.Width>PageSize.Width)
mx.Translate((s.Width/2)-(_pageSize.Width/2),0);
else
mx.Translate((float)this.AutoScrollPosition.X*(1f/_zoom),0);
if(s.Height>PageSize.Height)
mx.Translate(0,(s.Height/2)-(this._pageSize.Height/2)+
(this.AutoScrollPosition.Y));
else
mx.Translate(0,(float)this.AutoScrollPosition.Y*(1 f/_zoom));
mx.Invert();
Point[] px=new Point[]{new Point(e.X,e.Y)};
mx.TransformPoints(px);
MouseEventArgs et=new
MouseEventArgs(e.Button,e.Clicks,px[0].X,px[0].Y,e.Delta);
return et;
}
protected override void OnMouseMove(MouseEventArgs e)
{
MouseEventArgs et=this.BacktrackMouse(e);
base.OnMouseMove(et);
}
protected override void OnMouseUp(MouseEventArgs e)
{
MouseEventArgs et=this.BacktrackMouse(e);
base.OnMouseUp(et);
}
protected override void OnMouseDown(MouseEventArgs e)
{
MouseEventArgs et=this.BacktrackMouse(e);
base.OnMouseDown(et);
}
private void Canvas_Paint(object sender, PaintEventArgs e)
{
SolidBrush b1 = new SolidBrush(Color.FromArgb(64, Color.Red));
e.Graphics.FillRectangle(b1, new Rectangle(new Point(75, 75),
new Size(20, 30)));
}
private void Canvas_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
MessageBox.Show("pt.X,Y = " + e.X.ToString() + "," +
e.Y.ToString());
}
}
}
}