PDA

View Full Version : مبتدی: حرکت دادن یک شی گرافیکی



azita90
یک شنبه 21 اردیبهشت 1393, 15:40 عصر
سلام.من یک دایره رسم کردم و می خوام حرکتش بدم.میشه راهنمایی ام کنید.

javadaskari
یک شنبه 21 اردیبهشت 1393, 16:07 عصر
میتونی مثلا تو رویداد Mousehover فرم مختصات دایره رو جابجا کنی

azita90
یک شنبه 21 اردیبهشت 1393, 16:28 عصر
میتونی مثلا تو رویداد Mousehover فرم مختصات دایره رو جابجا کنی
من میخوام کاربر یک عدد رو توی textbox وارد کنه و یک دکمه رو رو بزنه.با زدن این دکمه interval تایمر مقدار دهی بشه و توی Timer_Tick نحوه ی حرکت دایره رو نوشتم. من همه ی این کارها رو برای یک picturebox نوشتم اما میخوام اون رو برای یک شی انجام بدم.


int dx = 5;
private void timer1_Tick(object sender, EventArgs e)
{
if ((pictureBox1.Location.X +dx > 0) && (pictureBox1.Location.X +dx < (this.Size.Width - pictureBox1.Size.Width)))
{
if(pictureBox1.Location.Y+dx >0 && (pictureBox1.Location.Y +dx < (this.Size.Height-pictureBox1.Size.Height)))
pictureBox1.Location = new Point(pictureBox1.Location.X + dx, pictureBox1.Location.Y-dx);
}
}


private void button2_Click(object sender, EventArgs e)
{
timer1.Interval = Convert.ToInt32(textBox1.Text);
}

rahnema1
یک شنبه 21 اردیبهشت 1393, 17:42 عصر
int dx = 5;
int graphicWidth=50;
int graphicHeight=50;
Point loc= new Point(200,200);
void timer1_Tick(object sender, EventArgs e)
{
if ((loc.X +dx > 0) && (loc.X +dx < (this.Size.Width - graphicWidth)))
{
if(loc.Y+dx >0 && (loc.Y +dx < (this.Size.Height-graphicHeight)))
loc = new Point(loc.X + dx, loc.Y-dx);
this.CreateGraphics().Clear(this.BackColor);
this.CreateGraphics().FillEllipse(Brushes.Red,loc. X,loc.Y,graphicWidth,graphicHeight);
}
}