using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace ScrollImage
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
Image img;
int xx,
yy; // current
position of image
int xCSize, yCSize; //
current width/height of client area
private
System.Windows.Forms.HScrollBar hScrollBar1;
private
System.Windows.Forms.VScrollBar vScrollBar1;
private
System.Windows.Forms.Label label1;
private
System.Windows.Forms.Label label2;
/// <summary>
/// Required designer variable.
/// </summary>
private
System.ComponentModel.Container components = null;
public Form1()
{
//
// Required
for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add
any constructor code after InitializeComponent call
//
xCSize =
this.ClientSize.Width;
yCSize =
this.ClientSize.Height;
xx = 0;
yy = 0;
img =
Image.FromFile("spaceship.bmp");
}
/// <summary>
/// Clean up any resources being
used.
/// </summary>
protected override void Dispose(
bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(
disposing );
}
#region Windows Form Designer
generated code
/// <summary>
/// Required method for Designer
support - do not modify
/// the contents of this method
with the code editor.
/// </summary>
private void InitializeComponent()
{
this.hScrollBar1 = new System.Windows.Forms.HScrollBar();
this.vScrollBar1 = new System.Windows.Forms.VScrollBar();
this.label1 =
new System.Windows.Forms.Label();
this.label2 =
new System.Windows.Forms.Label();
this.SuspendLayout();
//
// hScrollBar1
//
this.hScrollBar1.Location = new System.Drawing.Point(8, 280);
this.hScrollBar1.Maximum = 368;
this.hScrollBar1.Name = "hScrollBar1";
this.hScrollBar1.Size = new System.Drawing.Size(360, 16);
this.hScrollBar1.TabIndex = 0;
this.hScrollBar1.Scroll += new
System.Windows.Forms.ScrollEventHandler(this.hScrollBar1_Scroll);
//
// vScrollBar1
//
this.vScrollBar1.Location = new System.Drawing.Point(368, 0);
this.vScrollBar1.Maximum = 280;
this.vScrollBar1.Name = "vScrollBar1";
this.vScrollBar1.Size = new System.Drawing.Size(16, 272);
this.vScrollBar1.TabIndex = 1;
this.vScrollBar1.Scroll += new
System.Windows.Forms.ScrollEventHandler(this.vScrollBar1_Scroll);
//
// label1
//
this.label1.Location = new System.Drawing.Point(392, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(48, 23);
this.label1.TabIndex = 2;
this.label1.Text = "X = ";
//
// label2
//
this.label2.Location = new System.Drawing.Point(392, 88);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(48, 23);
this.label2.TabIndex = 3;
this.label2.Text = "Y = ";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(432, 317);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label2,
this.label1,
this.vScrollBar1,
this.hScrollBar1});
this.Name =
"Form1";
this.Text =
"Form1";
this.Resize +=
new System.EventHandler(this.Form1_Resize);
this.Paint +=
new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the
application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void
hScrollBar1_Scroll(object sender, System.Windows.Forms.ScrollEventArgs
e)
{
xx =
hScrollBar1.Value;
Invalidate();
}
private void
vScrollBar1_Scroll(object sender, System.Windows.Forms.ScrollEventArgs
e)
{
yy =
vScrollBar1.Value;
Invalidate();
}
private void Form1_Paint(object
sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g =
e.Graphics;
g.DrawImage(img,xx,yy);
label1.Text =
"X= " + xx.ToString();
label2.Text =
"Y = " + yy.ToString();
}
private void Form1_Resize(object
sender, System.EventArgs e)
{
xCSize =
this.ClientSize.Width;
yCSize =
this.ClientSize.Height;
label1.Location = new Point(xCSize - 50, 24);
label2.Location = new Point(xCSize - 50, 88);
hScrollBar1.Maximum = xCSize - 100;
hScrollBar1.Size = new Size(xCSize - 100, 16);
hScrollBar1.Location = new Point(0, yCSize-16);
vScrollBar1.Maximum = yCSize;
vScrollBar1.Size = new Size(16, yCSize - 10);
vScrollBar1.Location = new Point(xCSize - 100, 0);
Invalidate();
}
}
}