我想完成一个可以控制的实时更新的正弦波形,现在的问题是使用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);
}
}
}
...全文
890 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
  • 打赏
  • 举报
回复

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

这个是不平移静态的曲线图,可以实时调整参数,就是无法让他平移
串联谐振双有源桥(SR-DAB)DC-DC变换器闭环仿真研究内容概要:本文围绕串联谐振双有源桥(SR-DAB)DC-DC变换器的闭环仿真展开研究,重点探讨其在电力电子系统中的动态响应特性与控制策略。通过对SR-DAB变换器建立数学模型,并结合Matlab/Simulink进行闭环仿真,分析系统在不同工作条件下的电压、电流波及功率传输效率,验证控制算法的有效性与稳定性。研究涵盖系统建模、控制器设计(如PI控制)、软开关实现条件、抗干扰能力以及动态调节性能,旨在提升变换器在新能源、储能、数据中心供电等场景中的可靠性和能效水平。; 适合人群:具备电力电子、自动控制理论基础,从事新能源、微电网、电力系统仿真等相关领域的科研人员与工程技术人员。; 使用场景及目标:①用于高校及科研机构开展DC-DC变换器控制策略的教学与实验;②为工业界在高效率隔离型功率变换系统的设计与优化提供仿真依据和技术支撑;③服务于数据中心、电动汽车、可再生能源并网等场景下的电源系统研发。; 阅读建议:建议读者结合Matlab代码实践仿真流程,重点关注系统建模与控制器参数整定部分,通过调整负载、输入电压等条件观察系统响应,深入理解闭环控制对变换器性能的影响机制。

111,129

社区成员

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

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

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