/*★ 矢印キーで画像を操作 前田 稔 ★*/
#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>
using namespace System;
using namespace System::Windows::Forms;
using namespace System::Drawing;
//画像を表示するクラス
ref class IMAGE
{
public:
Bitmap^ bmp;
IMAGE(String^ file)
{
try
{ bmp = gcnew Bitmap(file); }
catch(Exception^ e)
{ Console::WriteLine(e->Message);
MessageBox::Show("イメージが取得できません", "Error");
}
}
void View(Graphics^ g, int x, int y)
{
if (bmp!=nullptr) g->DrawImage(bmp,x,y);
}
};
ref class FormClass : public System::Windows::Forms::Form
{
private:
IMAGE^ App;
int xp,yp;
public:
FormClass()
{
this->Width = 600;
this->Height = 400;
App = gcnew IMAGE("c:\\data\\test\\kishi.gif");
xp = yp = 100;
Paint += gcnew PaintEventHandler(this, &FormClass::MyHandler);
}
void MyHandler(Object^ sender, PaintEventArgs^ e)
{
Graphics^ g = e->Graphics;
if (App->bmp == nullptr) Application::Exit();
App->View(g,xp,yp);
}
virtual void OnKeyDown(KeyEventArgs^ e) override
{
switch(e->KeyCode)
{ case Keys::Escape:
Application::Exit();
break;
case Keys::Down:
yp += 2;
break;
case Keys::Up:
yp -= 2;
break;
case Keys::Right:
xp += 2;
break;
case Keys::Left:
xp -= 2;
break;
}
Invalidate();
}
};
int main()
{
Application::Run(gcnew FormClass());
return 0;
}
|