C#Winform控件问题

sbc0519 2019-09-13 04:44:44
在form里定义两个控件,如picturebox1和picturebox2 我要让它们同时运动,每个有每个的运动方式(如picturebox1:x=sin t,y=t;picturebox2:x=t,y=480-t) 并用button1来控制picturebox1的停止运动,用button2来控制picturebox2的停止运动 请问大佬们该如何实现
...全文
580 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
nicholasvb 2019-09-16
  • 打赏
  • 举报
回复
学习了,很好的例子!
xiaoxiangqing 2019-09-16
  • 打赏
  • 举报
回复
lidengyin 2019-09-16
  • 打赏
  • 举报
回复
学习学习。
xiaozhao127 2019-09-16
  • 打赏
  • 举报
回复
添加一个timer控件,在函数体里对button控件的.Location属性(x,y)同时增减x,y的值,x,y到最大或最小时改变一下,反向操作(最好创建两个全局int变量来控制增减多少,反向时乘以-1方向就可以改变方向),创建两个全局bool变量IsMove1和IsMove2在改变button控件时用来判断是否增减值;同时如果是true就把变量变false,反之则异然。
货郎大叔 2019-09-16
  • 打赏
  • 举报
回复
自己绘制坐标系和函数图像,重写它的onpaint方法或是用w p f
weixin_40094055 2019-09-16
  • 打赏
  • 举报
回复

学习了,很好的例子!
  • 打赏
  • 举报
回复
学习了,很好的例子56666666
sbc0519 2019-09-15
  • 打赏
  • 举报
回复
引用 7 楼 desperaso 的回复:
在线程里面加控制标记,button控制标记来暂停或什么的,轨迹自行设置,这里随意设的

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Demo_1
{
public partial class Form1 : Form
{
public class PanelEx : Panel
{
public PanelEx()
{
SetStyle(
ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.ResizeRedraw |
ControlStyles.SupportsTransparentBackColor |
ControlStyles.UserPaint,
true
);
UpdateStyles();
}
}

public abstract class DraggableObject
{
public abstract Rectangle Region { get; set; }
public abstract Bitmap Setimage { get; set; }
public abstract void OnPaint(PaintEventArgs e);
}

public class Draggable : DraggableObject
{
private Rectangle m_Region;
private Bitmap m_SetImage;

public Draggable(string id, Rectangle regin, Bitmap image)
{
m_Region = regin;
m_SetImage = image;
}
public override Rectangle Region { get => m_Region; set => m_Region = value; }
public override Bitmap Setimage { get => m_SetImage; set => m_SetImage = value; }
public override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawImage(m_SetImage, m_Region);
}
}
public List<DraggableObject> DesignObjects = new List<DraggableObject>();

public PanelEx panelContent;

public Form1()
{
InitializeComponent();

panelContent = new PanelEx()
{
Dock = DockStyle.Fill,
BackgroundImageLayout = ImageLayout.Stretch,
BackgroundImage = Image.FromFile("back.jpg")
};
panelContent.Paint += PanelContent_Paint;
this.Controls.Add(panelContent);

string[] files = new string[] { "01.png", "02.png", "03.png" };
for (int i = 0; i < 3; i++)
{
Bitmap bmp = Image.FromFile(files[i]) as Bitmap;
Draggable draggableBlock = new Draggable(DateTime.Now.ToString(), new Rectangle(0, 0, bmp.Width, bmp.Height), bmp);
DesignObjects.Add(draggableBlock);
}
Task.Run(async () =>
{
int set_x = 0;
while (true)
{
DesignObjects[0].Region = new Rectangle(set_x, 100, DesignObjects[0].Region.Width, DesignObjects[0].Region.Height);
panelContent.Invalidate();
await Task.Delay(5);
if (set_x < this.Width) set_x += 1;
else set_x = 0;
}
});
Task.Run(async () =>
{
int set_y = 0;
while (true)
{
DesignObjects[1].Region = new Rectangle(100, set_y, DesignObjects[1].Region.Width, DesignObjects[1].Region.Height);
panelContent.Invalidate();
await Task.Delay(5);
if (set_y < this.Height) set_y += 1;
else set_y = 0;
}
});
Task.Run(async () =>
{
int set_x = 0, set_y = 0;
while (true)
{
DesignObjects[2].Region = new Rectangle(set_x, set_y, DesignObjects[2].Region.Width, DesignObjects[2].Region.Height);
panelContent.Invalidate();
await Task.Delay(5);
if ((set_y + 1 + DesignObjects[2].Region.Height) < this.Height)
{
set_x += 1;
set_y += 1;
}
else
{
set_x = 0;
set_y = 0;
}
}
});
}

private void PanelContent_Paint(object sender, PaintEventArgs e)
{
foreach (DraggableObject item in DesignObjects)
{
item.OnPaint(e);
}
}

}
}
谢谢🙏…………
desperaso 2019-09-15
  • 打赏
  • 举报
回复
在线程里面加控制标记,button控制标记来暂停或什么的,轨迹自行设置,这里随意设的

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Demo_1
{
public partial class Form1 : Form
{
public class PanelEx : Panel
{
public PanelEx()
{
SetStyle(
ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.ResizeRedraw |
ControlStyles.SupportsTransparentBackColor |
ControlStyles.UserPaint,
true
);
UpdateStyles();
}
}

public abstract class DraggableObject
{
public abstract Rectangle Region { get; set; }
public abstract Bitmap Setimage { get; set; }
public abstract void OnPaint(PaintEventArgs e);
}

public class Draggable : DraggableObject
{
private Rectangle m_Region;
private Bitmap m_SetImage;

public Draggable(string id, Rectangle regin, Bitmap image)
{
m_Region = regin;
m_SetImage = image;
}
public override Rectangle Region { get => m_Region; set => m_Region = value; }
public override Bitmap Setimage { get => m_SetImage; set => m_SetImage = value; }
public override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawImage(m_SetImage, m_Region);
}
}
public List<DraggableObject> DesignObjects = new List<DraggableObject>();

public PanelEx panelContent;

public Form1()
{
InitializeComponent();

panelContent = new PanelEx()
{
Dock = DockStyle.Fill,
BackgroundImageLayout = ImageLayout.Stretch,
BackgroundImage = Image.FromFile("back.jpg")
};
panelContent.Paint += PanelContent_Paint;
this.Controls.Add(panelContent);

string[] files = new string[] { "01.png", "02.png", "03.png" };
for (int i = 0; i < 3; i++)
{
Bitmap bmp = Image.FromFile(files[i]) as Bitmap;
Draggable draggableBlock = new Draggable(DateTime.Now.ToString(), new Rectangle(0, 0, bmp.Width, bmp.Height), bmp);
DesignObjects.Add(draggableBlock);
}
Task.Run(async () =>
{
int set_x = 0;
while (true)
{
DesignObjects[0].Region = new Rectangle(set_x, 100, DesignObjects[0].Region.Width, DesignObjects[0].Region.Height);
panelContent.Invalidate();
await Task.Delay(5);
if (set_x < this.Width) set_x += 1;
else set_x = 0;
}
});
Task.Run(async () =>
{
int set_y = 0;
while (true)
{
DesignObjects[1].Region = new Rectangle(100, set_y, DesignObjects[1].Region.Width, DesignObjects[1].Region.Height);
panelContent.Invalidate();
await Task.Delay(5);
if (set_y < this.Height) set_y += 1;
else set_y = 0;
}
});
Task.Run(async () =>
{
int set_x = 0, set_y = 0;
while (true)
{
DesignObjects[2].Region = new Rectangle(set_x, set_y, DesignObjects[2].Region.Width, DesignObjects[2].Region.Height);
panelContent.Invalidate();
await Task.Delay(5);
if ((set_y + 1 + DesignObjects[2].Region.Height) < this.Height)
{
set_x += 1;
set_y += 1;
}
else
{
set_x = 0;
set_y = 0;
}
}
});
}

private void PanelContent_Paint(object sender, PaintEventArgs e)
{
foreach (DraggableObject item in DesignObjects)
{
item.OnPaint(e);
}
}

}
}
sbc0519 2019-09-14
  • 打赏
  • 举报
回复
像flash一样动
sbc0519 2019-09-14
  • 打赏
  • 举报
回复
引用 4 楼 XBodhi. 的回复:
首先这个控件不支持动态图像,除非你自己绘制坐标系和函数图像,重写它的onpaint方法或是用w p f
不是,这里的x、y为left与top
XBodhi. 2019-09-14
  • 打赏
  • 举报
回复
首先这个控件不支持动态图像,除非你自己绘制坐标系和函数图像,重写它的onpaint方法或是用w p f
sbc0519 2019-09-13
  • 打赏
  • 举报
回复
我觉得很有用啊
sbc0519 2019-09-13
  • 打赏
  • 举报
回复
引用 1 楼 LSJlsj1234567 的回复:
多线程控制窗体,另有什么用吗
还请大师指教
LSJlsj1234567 2019-09-13
  • 打赏
  • 举报
回复
多线程控制窗体,另有什么用吗

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧