首页 新闻 会员 周边 捐助

pictureBox如何改变控件位置

0
悬赏园豆:80 [已解决问题] 解决于 2016-04-15 20:48

界面:button,timer,picturebox

代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace PictureSport;

{

public partial class Test_Picture_Yuan : Form
{
public Test_Picture_Yuan()
{
InitializeComponent();
}

private Graphics g;
private Bitmap bmp;
private Pen pen;
private Point start;
private Point stop;

Bitmap image1;
public Point P_lo;
private void Test_Picture_Yuan_Load(object sender, EventArgs e)
{

timer1.Enabled = false;
bmp = new Bitmap(this.Width, this.Height);
g = Graphics.FromImage(bmp);
//g = this.CreateGraphics();
pen = new Pen(Color.Red);

}
int i = 1;
private void Test_Picture_Yuan_MouseClick(object sender, MouseEventArgs e)
{
if (i == 1)
{
start = e.Location;
i++;
}
else
{
if (i == 2)
{
stop = e.Location;
g.DrawLine(pen, start, stop);
i++;
this.BackgroundImage = bmp;

}
}
}

private void button1_Click(object sender, EventArgs e)
{

// g.DrawLine(pen, start, stop);
//直线的坐标 横轴坐标不变, 纵轴坐标+ - 50
//(pen,start,stop) (pen,new Point (start.X,start.Y-50),new Point (stop.X,stop.Y-50)) (pen, new Point(start.X, start.Y +50), new Point(stop.X, stop.Y +50))
//圆的坐标 横轴坐标不变, 纵轴坐标+ - 50
//(pen,start.X-15,start.Y-15,30,30); (pen, start.X - 15, start.Y - 65, 30, 30); (pen, start.X - 15, start.Y + 35, 30, 30);


g.DrawLine(pen,new Point (start.X,start.Y-50),new Point (stop.X,stop.Y-50));
g.DrawLine(pen, new Point(start.X, start.Y +50), new Point(stop.X, stop.Y +50));

timer1.Enabled = true;


try
{
// Retrieve the image.

image1 = new Bitmap(Application.StartupPath + @"/images/black.png", true);

int x, y;

// Loop through the images pixels to reset color.
for (x = 0; x < image1.Width; x++)
{
for (y = 0; y < image1.Height; y++)
{
Color pixelColor = image1.GetPixel(x, y);
Color newColor = Color.FromArgb(pixelColor.B, 0, 0);
image1.SetPixel(x, y, newColor);
}
}

// Set the PictureBox to display the image.
pictureBox1.Image = image1;

}
catch (ArgumentException)
{
MessageBox.Show("There was an error." +
"Check the path to the image file.");
}


this.BackgroundImage = null;
this.BackgroundImage = bmp;

}

private void timer1_Tick(object sender, EventArgs e)
{
P_lo = pictureBox1.Location;
P_lo = new Point(start.X, start.Y);
P_lo.X ++;
P_lo.Y ++;
pictureBox1.Location = P_lo;
}
}
}

随风起舞的主页 随风起舞 | 初学一级 | 园豆:19
提问于:2016-04-14 17:06
< >
分享
最佳答案
0

pictureBox1.Location = new Point(pictureBox1.Location.X + 1, pictureBox1.Location.Y + 1)

收获园豆:80
刘宏玺 | 专家六级 |园豆:14020 | 2016-04-14 18:48

你说的方案的确是改变picturebox的位置了,但是图片的起始位置定位不到第一条直线的起始位置了。

随风起舞 | 园豆:19 (初学一级) | 2016-04-14 18:54

@随风起舞: 你在load事件中加上这句话

pictureBox1.Location = new Point(start.X, start.Y);

刘宏玺 | 园豆:14020 (专家六级) | 2016-04-14 18:57

@刘宏玺: 试过了,没有效果,图片的起始位置还是定位不到第一条直线的起始位置。

随风起舞 | 园豆:19 (初学一级) | 2016-04-14 18:59

@随风起舞: 在你改变start的地方加上

pictureBox1.Location = new Point(start.X, start.Y);

刘宏玺 | 园豆:14020 (专家六级) | 2016-04-14 19:03

@刘宏玺: 图片定位到了直线的起始位置,又不能运动了

随风起舞 | 园豆:19 (初学一级) | 2016-04-14 19:06

@随风起舞: 现在我修改了一下代码,可以运动了。

随风起舞 | 园豆:19 (初学一级) | 2016-04-14 19:07

@刘宏玺: 但是不是沿着直线运动的,求教怎么更改代码,追加20园豆

随风起舞 | 园豆:19 (初学一级) | 2016-04-14 19:09

@随风起舞: 

float yAdd = (start.X - stop.X) / (start.Y - stop.Y) 

pictureBox1.Location = new Point(pictureBox1.Location.X + 1, pictureBox1.Location.Y + yAdd );

刘宏玺 | 园豆:14020 (专家六级) | 2016-04-14 19:15

@刘宏玺: 我不知道你后来说的两行代码应该写在哪里

随风起舞 | 园豆:19 (初学一级) | 2016-04-14 19:19

@随风起舞: 就写在timer的事件里面

那两句代码有点问题,忘记判断x的正负了,改成这样


float xAdd = start.X > stop.X ? -1 : 1;

float yAdd = (start.X - stop.X) / (start.Y - stop.Y) ;

pictureBox1.Location = new Point(pictureBox1.Location.X + xAdd , pictureBox1.Location.Y + yAdd );

刘宏玺 | 园豆:14020 (专家六级) | 2016-04-14 19:22

@刘宏玺: 

随风起舞 | 园豆:19 (初学一级) | 2016-04-14 19:28

@随风起舞: 我记得有个类叫PointF

float xAdd = start.X > stop.X ? -1 : 1;

float yAdd = (start.X - stop.X) / (start.Y - stop.Y) ;

pictureBox1.Location = new PointF(pictureBox1.Location.X + xAdd , pictureBox1.Location.Y + yAdd );

 

你先试试,如果不行就转换成int类型吧,不过这样也不会按照直线运动

int xAdd = start.X > stop.X ? -1 : 1;

int yAdd = (start.X - stop.X) / (start.Y - stop.Y) ;

pictureBox1.Location = new Point(pictureBox1.Location.X + xAdd , pictureBox1.Location.Y + yAdd );

刘宏玺 | 园豆:14020 (专家六级) | 2016-04-14 19:32

@刘宏玺: 还是存在转换问题啊,结果就是不能按照直线运动?怎么办?

随风起舞 | 园豆:19 (初学一级) | 2016-04-14 19:38

@随风起舞: 

如果走直线,那就需要使用PointF

你这样改造,代码还是写在timer里面

 

int xAdd = start.X > stop.X ? -1 : 1;

float yAdd = (start.X - stop.X) / (start.Y - stop.Y) ;

if(pictureBox1.Tag == null)

{

  pictureBox1.Tag = (float)pictureBox1.Location.Y;

}

pictureBox1.Tag = ((float)pictureBox1.Tag) + yAdd;

pictureBox1.Location = new Point(pictureBox1.Location.X + xAdd , (int)pictureBox1.Tag);

刘宏玺 | 园豆:14020 (专家六级) | 2016-04-14 19:46

@刘宏玺: 

随风起舞 | 园豆:19 (初学一级) | 2016-04-14 19:53

@随风起舞: 

如果直接报错就这样改
pictureBox1.Location = new Point(pictureBox1.Location.X + xAdd , (int)((float)pictureBox1.Tag));

 

如果是可以执行,但是过会才报错,那就在网面加一个try catch

刘宏玺 | 园豆:14020 (专家六级) | 2016-04-14 19:59

加了try catch 后,图片不能沿着直线动

随风起舞 | 园豆:19 (初学一级) | 2016-04-14 20:04

@随风起舞: 那看来不能把中间值放在tag里面

这样弄

float pictureBox1_Y = -1;//这句话写在timer外面,就是全局变量

 

int xAdd = start.X > stop.X ? -1 : 1;

float yAdd = (start.X - stop.X) / (start.Y - stop.Y) ;

if(pictureBox1_Y  = -1)

{

  pictureBox1_Y  = (float)pictureBox1.Location.Y;

}

pictureBox1_Y += yAdd;

pictureBox1.Location = new Point(pictureBox1.Location.X + xAdd , (int)pictureBox1_Y);

刘宏玺 | 园豆:14020 (专家六级) | 2016-04-14 20:08

@刘宏玺: 还是不能沿着直线运动,而且鼠标点击后形成的直线与显示出来的直线长度不一致

随风起舞 | 园豆:19 (初学一级) | 2016-04-14 20:12

@随风起舞: 你把项目代码发出来吧,我帮你调试一下,不然我也不知道你说的效果是啥样的

刘宏玺 | 园豆:14020 (专家六级) | 2016-04-14 20:14

@刘宏玺: 好的。

随风起舞 | 园豆:19 (初学一级) | 2016-04-14 20:15

@刘宏玺: 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace PictureSport
{
public partial class Test_Picture_Yuan : Form
{
public Test_Picture_Yuan()
{
InitializeComponent();
}

private Graphics g;
private Bitmap bmp;
private Pen pen;
private Point start;
private Point stop;

Bitmap image1;

public Point P_lo;
private void Test_Picture_Yuan_Load(object sender, EventArgs e)
{

timer1.Enabled = false;
bmp = new Bitmap(this.Width, this.Height);
g = Graphics.FromImage(bmp);
//g = this.CreateGraphics();
pen = new Pen(Color.Red);

}
int i = 1;
private void Test_Picture_Yuan_MouseClick(object sender, MouseEventArgs e)
{
if (i == 1)
{
start = e.Location;
i++;
}
else
{
if (i == 2)
{
stop = e.Location;
g.DrawLine(pen, start, stop);
i++;
this.BackgroundImage = bmp;
pictureBox1.Location = new Point(start.X, start.Y);
}
}
}

private void button1_Click(object sender, EventArgs e)
{

// g.DrawLine(pen, start, stop);
//直线的坐标 横轴坐标不变, 纵轴坐标+ - 50
//(pen,start,stop) (pen,new Point (start.X,start.Y-50),new Point (stop.X,stop.Y-50)) (pen, new Point(start.X, start.Y +50), new Point(stop.X, stop.Y +50))
//圆的坐标 横轴坐标不变, 纵轴坐标+ - 50
//(pen,start.X-15,start.Y-15,30,30); (pen, start.X - 15, start.Y - 65, 30, 30); (pen, start.X - 15, start.Y + 35, 30, 30);


g.DrawLine(pen,new Point (start.X,start.Y-50),new Point (stop.X,stop.Y-50));
g.DrawLine(pen, new Point(start.X, start.Y +50), new Point(stop.X, stop.Y +50));

timer1.Enabled = true;


try
{
// Retrieve the image.
// image1 = new Bitmap(@"C:/Users/Administrator/Desktop/black.png", true);
image1 = new Bitmap(Application.StartupPath + @"/images/black.png", true);

int x, y;

// Loop through the images pixels to reset color.
for (x = 0; x < image1.Width; x++)
{
for (y = 0; y < image1.Height; y++)
{
Color pixelColor = image1.GetPixel(x, y);
Color newColor = Color.FromArgb(pixelColor.B, 0, 0);
image1.SetPixel(x, y, newColor);
}
}

// Set the PictureBox to display the image.
pictureBox1.Image = image1;

}
catch (ArgumentException)
{
MessageBox.Show("There was an error." +
"Check the path to the image file.");
}


this.BackgroundImage = null;
this.BackgroundImage = bmp;

}
public float pictureBox1_Y = -1;
private void timer1_Tick(object sender, EventArgs e)
{
//
//P_lo = new Point(pictureBox1.Location.X + 1, pictureBox1.Location.Y + 1);
// pictureBox1.Location = P_lo;
// pictureBox1.Location = new Point(start.X, start.Y);
// float xAdd = start.X > stop.X ? -1 : 1;
//float yAdd = (start.X - stop.X) / (start.Y - stop.Y);

//PointF pf=(PointF)(pictureBox1.Location.X + xAdd);
// pictureBox1.Location = new Point(, pictureBox1.Location.Y + yAdd);

// float xAdd = start.X > stop.X ? -1 : 1;

// float yAdd = (start.X - stop.X) / (start.Y - stop.Y);

// //pictureBox1.Location = new PointF(pictureBox1.Location.X + xAdd, pictureBox1.Location.Y + yAdd);
// Point p = new Point(50, 50);
// PointF pf = (PointF)p;
//try
//{
// int xAdd = start.X > stop.X ? -1 : 1;
// float yAdd = (start.X - stop.X) / (start.Y - stop.Y);
// if (pictureBox1.Tag == null)
// {
// pictureBox1.Tag = (float)pictureBox1.Location.Y;
// }
// pictureBox1.Tag = ((float)pictureBox1.Tag) + yAdd;
// pictureBox1.Location = new Point(pictureBox1.Location.X + xAdd, (int)(float)pictureBox1.Tag);
//}

//catch (ArgumentException)
//{
// MessageBox.Show("111");
//}
int xAdd = start.X > stop.X ? -1 : 1;

float yAdd = (start.X - stop.X) / (start.Y - stop.Y);

if (pictureBox1_Y ==-1)
{

pictureBox1_Y = (float)pictureBox1.Location.Y;

}

pictureBox1_Y += yAdd;

pictureBox1.Location = new Point(pictureBox1.Location.X + xAdd, (int)pictureBox1_Y);

}
}
}

随风起舞 | 园豆:19 (初学一级) | 2016-04-14 20:16

@随风起舞: 改成这样


public float pictureBox1_X = -1;
public float pictureBox1_Y = -1;
private void timer1_Tick(object sender, EventArgs e)
{
int xAdd = start.X > stop.X ? -1 : 1;
float yAdd = (float)(stop.Y - start.Y) / (float)(stop.X - start.X);
yAdd *= xAdd;

if (pictureBox1_X == -1)
{
pictureBox1_X = (float)pictureBox1.Location.X;
}
if (pictureBox1_Y == -1)
{
pictureBox1_Y = (float)pictureBox1.Location.Y;
}
pictureBox1_X += xAdd;
pictureBox1_Y += yAdd;
pictureBox1.Location = new Point((int)pictureBox1_X, (int)pictureBox1_Y);

}

刘宏玺 | 园豆:14020 (专家六级) | 2016-04-14 20:44

@刘宏玺: 圆的确是沿着直线动的,请问当点移动到直线终点时如何使圆停止运动?

随风起舞 | 园豆:19 (初学一级) | 2016-04-15 13:58

@随风起舞: 加判断呗!

private void timer1_Tick(object sender, EventArgs e)
{

if(pictureBox1_X == stop.X)

{

timer1.Enabled = false;

return;

}

...

刘宏玺 | 园豆:14020 (专家六级) | 2016-04-15 15:03

@刘宏玺: 问题已经解决了,只是个别代码不晓得啥意思,要仔细琢磨一下。谢谢啦!等下就结贴,然后把答应送你的20园豆给你

随风起舞 | 园豆:19 (初学一级) | 2016-04-15 20:43

@随风起舞: 剩下的豆你留着吧,这个只是象征,没有什么用的,我也不缺豆!

刘宏玺 | 园豆:14020 (专家六级) | 2016-04-15 20:56

@刘宏玺: 额,我还没找到怎么赠送别人园豆!那谢谢啦!请问你收徒么?

随风起舞 | 园豆:19 (初学一级) | 2016-04-15 20:58

@随风起舞: 有问题你就问呗,我有时间的话都会帮你解决的!

刘宏玺 | 园豆:14020 (专家六级) | 2016-04-15 21:01

@刘宏玺: 好啊!那我还有个问题,在博问里,等下发链接,求解决!

随风起舞 | 园豆:19 (初学一级) | 2016-04-15 21:06

@刘宏玺: 问题链接:http://q.cnblogs.com/q/81393/

随风起舞 | 园豆:19 (初学一级) | 2016-04-15 21:08
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册