PDA

View Full Version : حرفه ای: آیا میتوان برای کد اسکی دو عدد را در یک لحظه تعریف کرد؟؟



sagggad
چهارشنبه 13 آذر 1392, 12:21 عصر
سلام دوستان.من یه پیکچرباکس دارم ومیخوام بازدن دکمه جلوحرکت کنه و اینکار رو باکد زیر انجام میدم:
If e.KeyValue = 38 Then
( PictureBox2.Location = New Point(PictureBox2.Location.X, PictureBox2.Location.Y - 5
End If
وهمینطور دکمه های راست وچپ وعقب روهم تعریف کردم.حالا میخوام وقتی کاربر دو دکمه ی جلو وراست رو نگه میداره پیکچرباکس هم به جلو وهم به راست حرکت کنه .(یعنی همX اون کم شه وهم Y اون)
لطفا راهنماییم کنین.:متفکر::متفکر::تشویق::تش ویق:

Hossis
پنج شنبه 14 آذر 1392, 22:28 عصر
( PictureBox2.Location = New Point(PictureBox2.Location.X-5, PictureBox2.Location.Y - 5

این که خیلی ساده بود

sagggad
پنج شنبه 14 آذر 1392, 22:30 عصر
( PictureBox2.Location = New Point(PictureBox2.Location.X-5, PictureBox2.Location.Y - 5

این که خیلی ساده بود
نه دوست من منظورم این نبود من میخوام اگه هم دکمه جلو و هم چپ گرفته شد اینکار انجام شه.اونقدر هم ساده نیست!!!!!!

habibb
پنج شنبه 14 آذر 1392, 23:06 عصر
namespace moraba{public partial class Form1 : Form
{
enum Direction { Up, Down, Right, Left, UpRight,UpLeft, DownLeft, DownRight};
Direction Dir;
Graphics G;
Point Po;
Size Si;
Rectangle Re;
Pen Pen_Draw;
Pen Pen_Clear;
public Form1()
{InitializeComponent();}
private void Form1_Load(object sender, EventArgs e)
{
Dir = Direction.Right;
G = CreateGraphics();
Po = new Point(300, 300);
Si = new Size(10, 10);
Re = new Rectangle(Po, Si);
Pen_Draw = new Pen(Color.Red,10);
Pen_Clear = new Pen(BackColor,10);
}
private void timer1_Tick(object sender, EventArgs e)
{
if (Dir == Direction.Right)
{
G.DrawRectangle(Pen_Clear, Re);
Po.X += 2;
Re.Location = Po;
G.DrawRectangle(Pen_Draw, Re);
}
if (Dir == Direction.Left)
{
G.DrawRectangle(Pen_Clear, Re);
Po.X -= 2;
Re.Location = Po;
G.DrawRectangle(Pen_Draw, Re);
}
if (Dir == Direction.Up)
{
G.DrawRectangle(Pen_Clear, Re);
Po.Y -= 2;
Re.Location = Po;
G.DrawRectangle(Pen_Draw, Re);
}
if (Dir == Direction.Down)
{
G.DrawRectangle(Pen_Clear, Re);
Po.Y += 2;
Re.Location = Po;
G.DrawRectangle(Pen_Draw, Re);
}
if (Dir == Direction.UpRight)
{
G.DrawRectangle(Pen_Clear, Re);
Po.Y -= 2;
Po.X += 2;
Re.Location = Po;
G.DrawRectangle(Pen_Draw, Re);
}
if (Dir == Direction.UpLeft)
{
G.DrawRectangle(Pen_Clear, Re);
Po.Y -= 2;
Po.X -= 2;
Re.Location = Po;
G.DrawRectangle(Pen_Draw, Re);
}
if (Dir == Direction.DownLeft)
{
G.DrawRectangle(Pen_Clear, Re);
Po.Y += 2;
Po.X -= 2;
Re.Location = Po;
G.DrawRectangle(Pen_Draw, Re);
}
if (Dir == Direction.DownRight)
{
G.DrawRectangle(Pen_Clear, Re);
Po.Y += 2;
Po.X += 2;
Re.Location = Po;
G.DrawRectangle(Pen_Draw, Re);}}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Right)
{
if (e.Shift)
Dir = Direction.UpRight;
else
Dir = Direction.Right;
}
if (e.KeyCode == Keys.Left)
{
if (e.Shift)
Dir = Direction.DownLeft;
else
Dir = Direction.Left;
}
if (e.KeyCode == Keys.Up)
{
if (e.Shift)
Dir = Direction.UpLeft;
else
Dir = Direction.Up;
}
if (e.KeyCode == Keys.Down)
{
if (e.Shift)
Dir = Direction.DownRight;
else
Dir = Direction.Down;}}}}

rahnema1
جمعه 15 آذر 1392, 01:44 صبح
ابتدا به دو اسمبلی یا dll به نام PresetationCore و WindowsBase رفرنس بدهید
سپس در بالای کد این را بگذارید:

Imports System.Windows.Input

بعد یک رویداد KeyDown برای فرم ایجاد کنید به این صورت:

Sub MainFormKeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs)
If Keyboard.IsKeyDown(Key.Up) And Keyboard.IsKeyDown(Key.Right) Then
PictureBox2.Location = New Point(PictureBox2.Location.X+5, PictureBox2.Location.Y - 5 )
ElseIf Keyboard.IsKeyDown(Key.Down) And Keyboard.IsKeyDown(Key.Right) Then
PictureBox2.Location = New Point(PictureBox2.Location.X+5, PictureBox2.Location.Y + 5 )
ElseIf Keyboard.IsKeyDown(Key.Down) And Keyboard.IsKeyDown(Key.Left) Then
PictureBox2.Location = New Point(PictureBox2.Location.X-5, PictureBox2.Location.Y + 5 )
ElseIf Keyboard.IsKeyDown(Key.Up) And Keyboard.IsKeyDown(Key.Left) Then
PictureBox2.Location = New Point(PictureBox2.Location.X-5, PictureBox2.Location.Y - 5 )
ElseIf Keyboard.IsKeyDown(Key.Up) Then
PictureBox2.Location = New Point(PictureBox2.Location.X, PictureBox2.Location.Y - 5 )
ElseIf Keyboard.IsKeyDown(Key.Right) Then
PictureBox2.Location = New Point(PictureBox2.Location.X+5, PictureBox2.Location.Y )
ElseIf Keyboard.IsKeyDown(Key.Down) Then
PictureBox2.Location = New Point(PictureBox2.Location.X, PictureBox2.Location.Y + 5 )
ElseIf Keyboard.IsKeyDown(Key.Left) Then
PictureBox2.Location = New Point(PictureBox2.Location.X-5, PictureBox2.Location.Y )
End If
End Sub