我想完成一个可以控制的实时更新的正弦波形,现在的问题是使用for循环平移造成无法操作,我纯属菜鸟

qq_14821757 2016-09-10 08:30:44
for循环进行中无法操作combobox,和button,请大神指点如何能达成我的目的。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
bool b = false;

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
b = true;
pictureBox1.Refresh();

}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{


SolidBrush sb = new SolidBrush(Color.Black);//话刷
Graphics g = e.Graphics;
Pen p = new Pen(Color.Black, 1);
Point[] xpts = new Point[3] {
new Point(410 - 35, 108),
new Point(410 - 35, 112),
new Point(410 - 30, 110)
};

g.DrawLine(p, 30, 110, 410 - 30, 110);
g.DrawPolygon(p, xpts);
g.DrawString("X", new Font("宋体", 9), sb, 410 - 25, 105);

Point[] ypts = new Point[3] {
new Point(28, 25),
new Point(30, 20),
new Point(32, 25) };
g.DrawLine(p, 30, 232 - 30, 30, 20);
g.DrawPolygon(p, ypts);
g.DrawString("Y", new Font("宋体", 9), sb, 15, 20);
Graphics g1 = CreateGraphics();
int o = int.Parse(comboBox2.Text);
int n = int.Parse(comboBox3.Text);
int m = int.Parse(comboBox1.Text);
if (b != true) return;
for (int l = 0; l <= 230; l++)
{
Refresh();
double X = 30;
double Y = 0;
for (double x = 0; x <= 510; x += o)
{
double y = Math.Sin((x + n + l) / 180 * Math.PI) * m;
g1.DrawLine(p, (float)X, (float)Y + 110, (float)(x / 1.8) + 30, (float)y + 110);
X = x / 1.8 + 30;
Y = y;

}
Thread.Sleep(100);

}

}

private void button2_Click(object sender, EventArgs e)
{
b = false;
pictureBox1.Refresh();

}

private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox3.Items.Add(comboBox3.Text);
}

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox2.Items.Add(comboBox2.Text);
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox1.Items.Add(comboBox1.Text);
}
}
}
...全文
891 8 打赏 收藏 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Forty2 2016-09-12
  • 打赏
  • 举报
回复
引用 7 楼 qq_14821757 的回复:
... 嗯,改变我的n的话,让n自加可以平移了,但是n自加的话去掉for循环还有其他方法嘛?可以用while或者ifelse嘛?
可以用定时器(Timer)。
qq_14821757 2016-09-12
  • 打赏
  • 举报
回复
引用 6 楼 xuzuning 的回复:
1、g1 改为 g 2、外层循环去掉,包括 Thread.Sleep(100); 3、改变 初始相位 (你的 n)就可平移图象
嗯,改变我的n的话,让n自加可以平移了,但是n自加的话去掉for循环还有其他方法嘛?可以用while或者ifelse嘛?
xuzuning 2016-09-11
  • 打赏
  • 举报
回复
1、g1 改为 g 2、外层循环去掉,包括 Thread.Sleep(100); 3、改变 初始相位 (你的 n)就可平移图象
qq_14821757 2016-09-11
  • 打赏
  • 举报
回复
琢磨了半天有点进步,这是改后的,效果如图

那个refresh好像不起作用,不加外面的for循环可以清除画的,为什么加了平移的循环就不能刷新掉了呢?
 private void pictureBox1_Paint(object sender, PaintEventArgs e)
{


SolidBrush sb = new SolidBrush(Color.Black);//话刷
Graphics g = e.Graphics;
Pen p = new Pen(Color.Black, 1);
Point[] xpts = new Point[3] {
new Point(410 - 35, 108),
new Point(410 - 35, 112),
new Point(410 - 30, 110)
};

g.DrawLine(p, 30, 110, 410 - 30, 110);
g.DrawPolygon(p, xpts);
g.DrawString("X", new Font("宋体", 9), sb, 410 - 25, 105);

Point[] ypts = new Point[3] {
new Point(28, 25),
new Point(30, 20),
new Point(32, 25) };
g.DrawLine(p, 30, 232 - 30, 30, 20);
g.DrawPolygon(p, ypts);
g.DrawString("Y", new Font("宋体", 9), sb, 15, 20);
Graphics g1 = this.pictureBox1.CreateGraphics(); //创建画板,这里的画板是由pictureBox提供的.
int o = int.Parse(comboBox2.Text);
int n = int.Parse(comboBox3.Text);
int m = int.Parse(comboBox1.Text);
if (b != true) return;
double X = 30;
double x = 0;
double Y = 0;
for (int l = 0; l <= 720; l++)
{
if (b ==true)
this.Refresh();//这里我一直搞不懂
for (x = 0; x <= 510; x += o)
{
double y = Math.Sin((x+l + n) / 180 * Math.PI) * m;
g1.DrawLine(p, (float)X, (float)Y + 110, (float)(x / 1.8) + 30, (float)y + 110);
X = x / 1.8 + 30;
Y = y;

}

Thread.Sleep(10);
}

}
qq_14821757 2016-09-11
  • 打赏
  • 举报
回复
没有人来了吗?
qq_14821757 2016-09-10
  • 打赏
  • 举报
回复
引用 2 楼 xuzuning 的回复:
Graphics g1 = CreateGraphics(); 这就画到 Form1 上了,不是在 pictureBox1 中
对的,我第二张图片是正确的,但是他不能平移,之间的区别就是一个是e.Graphics一个事CreateGraphics,请问您指点应该怎么实现吗?
xuzuning 2016-09-10
  • 打赏
  • 举报
回复
Graphics g1 = CreateGraphics(); 这就画到 Form1 上了,不是在 pictureBox1 中
qq_14821757 2016-09-10
  • 打赏
  • 举报
回复

这个是贴出来的程序的界面,问题不少

这个是不平移静态的曲线图,可以实时调整参数,就是无法让他平移
【期刊论文复现】多元宇宙算法求解电力系统多目标优化问题(Matlab实现)内容概要:本文复现了期刊论文中提出的多元宇宙算法(Multi-Verse Optimizer, MVO)用于求解电力系统中的多目标优化调度问题,采用Matlab进行代码实现。该算法受宇宙中黑洞、白洞和虫洞等天文现象启发,通过数学建模模拟多目标搜索过程,具有较强的全局寻优能力和收敛性能。文中将其应用于典型的电力系统经济调度场景,如考虑燃料成本、排放量等多个优化目标的微电网或多能源系统调度问题,通过构建合理的数学模型和约束条件,实现对多个冲突目标的协同优化。同时,文章提供了完整的代码框架与仿真结果,便于读者理解和复现。; 适合人群:具备一定Matlab编程基础和优化算法基础知识的研究生、科研人员及从事电力系统优化调度工作的工程师。; 使用场景及目标:①学习和掌握新型智能优化算法——多元宇宙算法的基本原理与实现方法;②将其应用于电力系统经济调度、微电网优化、多目标决策等领域的模型求解;③为科研工作提供可复现的代码参考和技术支持。; 阅读建议:建议读者结合Matlab代码逐行理解算法实现细节,重点关注目标函数建模、约束处理及MVO算法核心迭代逻辑,并尝试在不同测试系统中进行参数调优与性能对比,以深入掌握其应用技巧。

111,128

社区成员

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

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

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