Panel.AutoScrollPosition
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
public Form1() {
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
this.panel1.AutoScroll = true;
this.panel1.BackgroundImage = new Bitmap("yourfile.bmp");
this.panel1.Location = new System.Drawing.Point(13, 13);
this.panel1.Size = new System.Drawing.Size(267, 243);
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 268);
this.Controls.Add(this.panel1);
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); this.panel1.AutoScrollMinSize = this.panel1.BackgroundImage.Size;
}
private void Form1_Load(object sender, EventArgs e) {
int midX = this.panel1.AutoScrollMinSize.Width / 2;
int midY = this.panel1.AutoScrollMinSize.Height / 2;
int halfSizeX = this.panel1.Size.Width / 2;
int halfSizeY = this.panel1.Size.Height / 2;
int startPosX = midX - halfSizeX;
if (startPosX < 0) startPosX = 0;
int startPosY = midY - halfSizeY;
if (startPosY < 0) startPosY = 0;
this.panel1.AutoScrollPosition = new Point(startPosX, startPosY);
}
private System.Windows.Forms.Panel panel1;
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
Related examples in the same category