2007/10/29(Mon) 23:22:48 編集(投稿者)
■No9525 (ken さん) に返信
C# よりも、VB の方がいいんじゃない?
簡単だよ。
Public Class Form1
Private pic As New PictureBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
pic.Image = My.Resources.sep_50.ToBitmap
pic.Size = New Size(My.Resources.sep_50.Width, My.Resources.sep_50.Height)
Controls.Add(pic)
AddHandler Me.KeyUp, AddressOf me_Keyup
End Sub
Private Sub me_Keyup(ByVal s As Object, ByVal e As KeyEventArgs)
Select Case e.KeyCode
Case Keys.Left
pic.Location = New Point(pic.Left - 10, pic.Top)
Case Keys.Right
pic.Location = New Point(pic.Left + 10, pic.Top)
Case Keys.Up
pic.Location = New Point(pic.Left, pic.Top - 10)
Case Keys.Down
pic.Location = New Point(pic.Left, pic.Top + 10)
End Select
End Sub
End Class
感じるところがあれば、ご自分のPGへ取り込んで下さい。
上記は、VBですが、上下左右の矢印キーでsep_50.ico が動くことを確認しました。
C# も載せます。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace GameCs
{
public partial class Form1 : Form
{
private PictureBox pic = new PictureBox();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Controls.Add(pic);
pic.Paint += pic_Paint;
this.KeyUp += me_Keyup;
}
private void pic_Paint(Object s, PaintEventArgs e)
{
e.Graphics.DrawRectangle(Pens.AliceBlue,new Rectangle(10,10,40,40));
e.Graphics.FillRectangle(Brushes.Aqua, new Rectangle(10, 10, 40, 40));
}
private void me_Keyup(object s, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Up:
pic.Top = pic.Top - 10;
break;
case Keys.Down:
pic.Top = pic.Top + 10;
break;
case Keys.Left:
pic.Left = pic.Left - 10;
break;
case Keys.Right:
pic.Left = pic.Left + 10;
break;
default:
break;
};
}
}
}
C#はかなり怪しいですが、一応動いているようです。
がんばってください。