using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 小影像動畫1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int x=300, y=300; //x,y為小影像中心
int r=0, c=0; //小圖列數行數
int w, h; //大影像寬高
Image im1 = Properties.Resources.wGirl;
Rectangle[,] im = new Rectangle[4, 4];
//設定小影像,im二維陣列,型態先給,四層樓同一個門牌號碼,
//陣列從0開始,第0,第一,第二,第三...
//影像變數為屬性.資源.檔案名稱
private void Form1_Load(object sender, EventArgs e)
{
this.Width = 960; this.Height = 640;
this.DesktopLocation = new Point(5, 10);
//設定在桌面位置
w = im1.Width / 4; h = im1.Height / 4;
//小圖寬高為大圖寬高/4
for (int i=0;i<4;i++)
for(int j=0;j<4;j++)
im[i,j]=new Rectangle(j*w,i*h,w,h);
//i為列,j為行,跑兩個for迴圈
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
this.DoubleBuffered = true;
e.Graphics.DrawImage(im1, 64, 64);
//顯示影像局部矩形區域
//e.Graphics.DrawImage(im, 左上x,左上y,矩形區域,GraphicsUnit.Pixel)
e.Graphics.DrawImage(im1, x-r/2, y-c/2,im[r,c],GraphicsUnit.Pixel);
//e表繪圖畫布變數的指令,
}
private void timer1_Tick(object sender, EventArgs e)
{
c++;
c=c%4;
//c為0,1,2,3,0,1,2,3
this.Refresh();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{ r = 1; x = x - 3; }
else if (e.KeyCode == Keys.Right)
{ r = 2; x = x + 3; }
if (e.KeyCode == Keys.Up)
{ r = 3; y = y - 3; }
else if (e.KeyCode == Keys.Down)
{ r = 0; y = y + 3; }
this.Refresh();
}
}
}
沒有留言:
張貼留言