using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Windows.Forms;
namespace TimerEx {
public partial class TimerForm : Form {
//イメージ
private Bitmap image; //イメージ
private ImageAttributes imageAttr;//イメージ属性
private int x=100;//X座標
private int y=100;//Y座標
private int dx=10; //X速度
private int dy=10; //Y速度
//ダブルバッファリング
private Image offImg;//オフイメージ
private Graphics g; //オフグラフィックス
//コンストラクタ
public TimerForm() {
InitializeComponent();
//イメージの読み込み
try {
image=LoadImage("TimerEx.sorami.gif");
} catch (Exception exc) {
MessageBox.Show(exc.ToString(),"エラー");
}
//イメージ属性の生成
imageAttr=new ImageAttributes();
imageAttr.SetColorKey(
Color.FromArgb(255,0,255),
Color.FromArgb(255,0,255));
//ダブルバッファリングの生成
offImg=new Bitmap(ClientSize.Width, ClientSize.Height);
g=Graphics.FromImage(offImg);
//タイマーの開始
Timer timer=new Timer();
timer.Interval=100;
timer.Tick+=new EventHandler(TimerOnTick);
timer.Enabled=true;
}
//イメージの読み込み
private Bitmap LoadImage(String name) {
return new Bitmap(Assembly.GetExecutingAssembly().
GetManifestResourceStream(name));
}
//イメージの描画
private void DrawImage(Graphics g,Bitmap image,int x,int y) {
g.DrawImage(image,
new Rectangle(x,y,image.Size.Width,image.Size.Height),
0,0,image.Size.Width,image.Size.Height,
GraphicsUnit.Pixel,imageAttr);
}
//タイマーイベントの処理
protected virtual void TimerOnTick(Object obj,EventArgs ea) {
if (image==null || offImg==null) return;
//移動
x+=dx;
y+=dy;
if (x<0 || x>ClientSize.Width) dx=-dx;
if (y<0 || y>ClientSize.Height) dy=-dy;
//オフイメージの描画
g.Clear(Color.White);
DrawImage(g,image,x-image.Width/2,y-image.Height/2);
//画面の描画
Graphics gra=CreateGraphics();
gra.DrawImage(offImg,0,0);
gra.Dispose();
}
}
}
|