1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
/* $Id: ScrollableImageControl.cs,v 1.4 2009/06/03 01:10:59 ellson Exp $ $Revision: 1.4 $ */
/* vim:set shiftwidth=4 ts=8: */
/**********************************************************
* This software is part of the graphviz package *
* http://www.graphviz.org/ *
* *
* Copyright (c) 1994-2008 AT&T Corp. *
* and is licensed under the *
* Common Public License, Version 1.0 *
* by AT&T Corp. *
* *
* Information and Software Systems Research *
* AT&T Research, Florham Park NJ *
**********************************************************/
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace Graphviz
{
public partial class ScrollableImageControl : ScrollableControl
{
public Image Image
{
get
{
return _image;
}
set
{
_image = value;
UpdateAutoScroll();
}
}
public float Zoom
{
get
{
return _zoom;
}
set
{
_zoom = value;
UpdateAutoScroll();
}
}
public ScrollableImageControl()
{
InitializeComponent();
SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.DoubleBuffer |
ControlStyles.ResizeRedraw |
ControlStyles.UserMouse |
ControlStyles.UserPaint,
true);
_image = null;
_zoom = 1.0f;
_lastScroll = new Point(0, 0);
}
public void ActualSize()
{
Zoom = 1.0f;
}
public void ZoomToFit()
{
Size imageSize = Image.Size;
Size controlSize = Size;
Zoom = Math.Min((float)controlSize.Width / (float)imageSize.Width, (float)controlSize.Height / (float)imageSize.Height);
}
public void ZoomIn()
{
Zoom *= _squareRootOfTwo;
}
public void ZoomOut()
{
Zoom /= _squareRootOfTwo;
}
protected override void OnMouseDown(MouseEventArgs e)
{
/* if potentially panning with the mouse, record last scroll position */
/* NOTE: Autoscroll should be set to +ve values but always returns -ve values */
if (e.Button == MouseButtons.Left)
_lastScroll = new Point(
e.X - AutoScrollPosition.X,
e.Y - AutoScrollPosition.Y);
base.OnMouseDown(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
/* if panning with the mouse, scroll to position */
/* NOTE: Autoscroll should be set to +ve values but always returns -ve values */
if (e.Button == MouseButtons.Left)
AutoScrollPosition = new Point(
_lastScroll.X - e.X,
_lastScroll.Y - e.Y);
base.OnMouseMove(e);
}
protected override void OnPaintBackground(PaintEventArgs e)
{
/* fill clip rectangle with background color */
e.Graphics.FillRectangle(new SolidBrush(BackColor), e.ClipRectangle);
}
protected override void OnPaint(PaintEventArgs pe)
{
if (_image != null) {
Matrix oldMatrix = pe.Graphics.Transform;
/* if the zoomed image size is smaller than the control size, center the display */
/* adjust by the scroll position, then scale by the zoom factor */
pe.Graphics.TranslateTransform(
Math.Max((Width - _image.Width * _zoom) / 2.0f, 0.0f) + AutoScrollPosition.X,
Math.Max((Height - _image.Height * _zoom) / 2.0f, 0.0f) + AutoScrollPosition.Y);
pe.Graphics.ScaleTransform(_zoom, _zoom);
/* blit the image onto the graphics */
pe.Graphics.DrawImage(_image, 0.0f, 0.0f);
pe.Graphics.Transform = oldMatrix;
}
base.OnPaint(pe);
}
private void UpdateAutoScroll()
{
if (_image == null)
AutoScrollMinSize = Size;
else
AutoScrollMinSize = new Size(
(int)(_image.Width * _zoom + 0.5f),
(int)(_image.Height * _zoom + 0.5f));
Invalidate();
}
private readonly float _squareRootOfTwo = (float)Math.Sqrt(2.0);
private Image _image;
private float _zoom;
private Point _lastScroll;
}
}
|