gpt4 book ai didi

c# - 多种形式的 UpdateFrame

转载 作者:太空宇宙 更新时间:2023-11-03 13:07:00 25 4
gpt4 key购买 nike

我用 C# 做了一个脑机接口(interface) (BCI),每个表单上都有闪烁的 pictureboxes。但是,只有在主窗体中才会出现闪烁。有谁知道如何让 updateframe 也能在其他表单上工作吗?

程序.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Security;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DTU_BCI_SPELLER_BioRadio_Interface
{
static class Program
{
[StructLayout(LayoutKind.Sequential)]
private struct Message
{
public IntPtr hWnd;
public int msg;
public IntPtr wParam;
public IntPtr lParam;
public uint time;
public Point p;
}

[return: MarshalAs(UnmanagedType.Bool)]
[SuppressUnmanagedCodeSecurity, DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool PeekMessage(out Message msg, IntPtr hWnd, uint messageFilterMin, uint messageFilterMax, uint flags);

public static ABCDE form;


[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
form = new ABCDE();

Application.Idle += new EventHandler(Application_Idle);
Application.Run(form);


//udpsock2.Close();


}

static void Application_Idle(object sender, EventArgs e)
{
Message message;
while (!PeekMessage(out message, IntPtr.Zero, 0, 0, 0))
{
form.UpdateFrame();

}
}
}

}

主窗体:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Windows.Threading;
using System.Runtime.InteropServices;
using System.Security;
//using DTU_BCI_SPELLER_BioRadio_Interface.FlickerTimer;

namespace DTU_BCI_SPELLER_BioRadio_Interface
{
public partial class ABCDE : Form
{


Stopwatch stopwatch = new Stopwatch();
private byte[] dataStream = new byte[1024];
private Socket udpSock1;

private byte[] buffer;

public delegate void DisplayMessageDelegate(string message);
private DisplayMessageDelegate displayMessageDelegate = null;
public EndPoint newClientEP = null;


private string Avalue;
public string AVALUE
{
get { return Avalue; }
set { Avalue = value; }
}


public ABCDE()
{
InitializeComponent();
stopwatch.Start();
this.displayMessageDelegate = new DisplayMessageDelegate(this.DisplayMessage);
udpSock1 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
udpSock1.Bind(new IPEndPoint(IPAddress.Any, 8050));
buffer = new byte[1024];

newClientEP = new IPEndPoint(IPAddress.Any, 0);
udpSock1.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock1);


}

private void DoReceiveFrom(IAsyncResult iar)
{
try
{
Socket recvSock = (Socket)iar.AsyncState;
EndPoint clientEP = new IPEndPoint(IPAddress.Any, 0);
int msgLen = recvSock.EndReceiveFrom(iar, ref clientEP);
byte[] localMsg = new byte[msgLen];
Array.Copy(buffer, localMsg, msgLen);

EndPoint newClientEP = new IPEndPoint(IPAddress.Any, 0);
udpSock1.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock1);

if (msgLen > 0)
{
string textToprint = Encoding.UTF8.GetString(localMsg, 0, msgLen);
this.Invoke(this.displayMessageDelegate, new object[] { textToprint });
}
}
catch (ObjectDisposedException)
{
}

}
private void DisplayMessage(string messge)
{
int numVal;
//SPELL.Text += messge + Environment.NewLine;

numVal = Convert.ToInt32(messge);
udpSock1.Close();
if (numVal == 83)
{

//Application.SetSuspendState();
BCI1 form = new BCI1();
//Application.Run(form);
//form.ShowDialog();
//stopwatch.Start();
SPELL.Text = SPELL.Text + form.LET;

udpSock1 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
udpSock1.Bind(new IPEndPoint(IPAddress.Any, 8050));
buffer = new byte[1024];

newClientEP = new IPEndPoint(IPAddress.Any, 0);
udpSock1.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock1);

//udpSock.Disconnect(true);
//udpSock.Connect(new IPEndPoint(IPAddress.Any, 8050));
}
else if (numVal == 76)
{
stopwatch.Stop();
//pictureBox2.PerformClick();
BCI2 l = new BCI2();
l.ShowDialog();
stopwatch.Start();
SPELL.Text = SPELL.Text + l.LET;
udpSock1 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
udpSock1.Bind(new IPEndPoint(IPAddress.Any, 8050));
buffer = new byte[1024];

newClientEP = new IPEndPoint(IPAddress.Any, 0);
udpSock1.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock1);
//udpSock.Close();
}
else if (numVal == 70)
{
//pictureBox3.PerformClick();
BCI3 l = new BCI3();
l.ShowDialog();
SPELL.Text = SPELL.Text + l.LET;
udpSock1 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
udpSock1.Bind(new IPEndPoint(IPAddress.Any, 8050));
buffer = new byte[1024];

newClientEP = new IPEndPoint(IPAddress.Any, 0);
udpSock1.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock1);
//udpSock.Close();
}
else if (numVal == 65)
{
//pictureBox4.PerformClick();
BCI4 l = new BCI4();
l.ShowDialog();
SPELL.Text = SPELL.Text + l.LET;
udpSock1 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
udpSock1.Bind(new IPEndPoint(IPAddress.Any, 8050));
buffer = new byte[1024];

newClientEP = new IPEndPoint(IPAddress.Any, 0);
udpSock1.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock1);
//udpSock.Close();
}
else if (numVal == 61)
{
//pictureBox5.PerformClick();
BCI5 l = new BCI5();
l.ShowDialog();
SPELL.Text = SPELL.Text + l.LET;
udpSock1 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
udpSock1.Bind(new IPEndPoint(IPAddress.Any, 8050));
buffer = new byte[1024];

newClientEP = new IPEndPoint(IPAddress.Any, 0);
udpSock1.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock1);
//udpSock.Close();
}
else if (numVal == 55)
{
//pictureBox6.PerformClick();
BCI6 l = new BCI6();
l.ShowDialog();
SPELL.Text = SPELL.Text + l.LET;
udpSock1 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
udpSock1.Bind(new IPEndPoint(IPAddress.Any, 8050));
buffer = new byte[1024];

newClientEP = new IPEndPoint(IPAddress.Any, 0);
udpSock1.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock1);
//udpSock.Close();
}
else if (numVal == 48)
{
//pictureBox7.PerformClick();
BCI7 l = new BCI7();
l.ShowDialog();
SPELL.Text = SPELL.Text + l.LET;
udpSock1 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
udpSock1.Bind(new IPEndPoint(IPAddress.Any, 8050));
buffer = new byte[1024];

newClientEP = new IPEndPoint(IPAddress.Any, 0);
udpSock1.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock1);
//udpSock.Close();
}
else if (numVal == 45)
{
//pictureBox8.PerformClick();
String text = SPELL.Text;
if (!String.IsNullOrEmpty(text)) SPELL.Text = text.Substring(0, text.Length - 1);
//SPELL.Text = SPELL.Text;
udpSock1 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
udpSock1.Bind(new IPEndPoint(IPAddress.Any, 8050));
buffer = new byte[1024];

newClientEP = new IPEndPoint(IPAddress.Any, 0);
udpSock1.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock1);
//udpSock.Close();
}
else if (numVal == 40)
{
//pictureBox9.PerformClick();
this.Close();
//udpSock.Close();
}
}
public void UpdateFrame()
{
double cycleHz1 = 0.006;

double wave1 = Math.Sin((stopwatch.ElapsedMilliseconds * 2.0 * Math.PI) * cycleHz1);

if (wave1 > 0.0)
{
pictureBox1.BackColor = Color.Black;
}
else
{
pictureBox1.BackColor = Color.White;
}
double cycleHz2 = 0.0065;

double wave2 = Math.Sin((stopwatch.ElapsedMilliseconds * 2.0 * Math.PI) * cycleHz2);

if (wave2 > 0.0)
{
pictureBox2.BackColor = Color.Black;
}
else
{
pictureBox2.BackColor = Color.White;
}
double cycleHz3 = 0.007;

double wave3 = Math.Sin((stopwatch.ElapsedMilliseconds * 2.0 * Math.PI) * cycleHz3);

if (wave3 > 0.0)
{
pictureBox3.BackColor = Color.Black;
}
else
{
pictureBox3.BackColor = Color.White;
}
double cycleHz4 = 0.0075;

double wave4 = Math.Sin((stopwatch.ElapsedMilliseconds * 2.0 * Math.PI) * cycleHz4);

if (wave4 > 0.0)
{
pictureBox4.BackColor = Color.Black;
}
else
{
pictureBox4.BackColor = Color.White;
}
double cycleHz5 = 0.0082;

double wave5 = Math.Sin((stopwatch.ElapsedMilliseconds * 2.0 * Math.PI) * cycleHz5);

if (wave5 > 0.0)
{
pictureBox5.BackColor = Color.Black;
}
else
{
pictureBox5.BackColor = Color.White;
}
double cycleHz6 = 0.0093;

double wave6 = Math.Sin((stopwatch.ElapsedMilliseconds * 2.0 * Math.PI) * cycleHz6);

if (wave6 > 0.0)
{
pictureBox6.BackColor = Color.Black;
}
else
{
pictureBox6.BackColor = Color.White;
}
double cycleHz7 = 0.010;

double wave7 = Math.Sin((stopwatch.ElapsedMilliseconds * 2.0 * Math.PI) * cycleHz7);

if (wave7 > 0.0)
{
pictureBox7.BackColor = Color.Black;
}
else
{
pictureBox7.BackColor = Color.White;
}
double cycleHz8 = 0.011;

double wave8 = Math.Sin((stopwatch.ElapsedMilliseconds * 2.0 * Math.PI) * cycleHz8);

if (wave8 > 0.0)
{
pictureBox8.BackColor = Color.Black;
}
else
{
pictureBox8.BackColor = Color.White;
}
double cycleHz9 = 0.0125;

double wave9 = Math.Sin((stopwatch.ElapsedMilliseconds * 2.0 * Math.PI) * cycleHz9);

if (wave9 > 0.0)
{
pictureBox9.BackColor = Color.Black;
}
else
{
pictureBox9.BackColor = Color.White;
}

}
private void ABCDE_Load(object sender, EventArgs e)
{

}

}
}

表格 2:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Windows.Threading;
//using DTU_BCI_SPELLER_BioRadio_Interface.FlickerTimer;


namespace DTU_BCI_SPELLER_BioRadio_Interface
{
public partial class BCI1 : Form
{


Stopwatch stopwatch2 = new Stopwatch();
private byte[] dataStream = new byte[1024];
private Socket udpSock2;

private byte[] buffer;

public delegate void DisplayMessageDelegate(string message);
private DisplayMessageDelegate displayMessageDelegate = null;
private string letter;
private bool BlinkOn = false;
public string LET
{
get { return letter; }
set { letter = value; }
}

public BCI1()
{
InitializeComponent();

this.displayMessageDelegate = new DisplayMessageDelegate(this.DisplayMessage);
stopwatch2.Start();
udpSock2 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
udpSock2.Bind(new IPEndPoint(IPAddress.Any, 8050));
buffer = new byte[1024];

EndPoint newClientEP = new IPEndPoint(IPAddress.Any, 0);
udpSock2.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock2);
}
private void DoReceiveFrom(IAsyncResult iar)
{
try
{
Socket recvSock = (Socket)iar.AsyncState;
EndPoint clientEP = new IPEndPoint(IPAddress.Any, 0);
int msgLen = recvSock.EndReceiveFrom(iar, ref clientEP);
byte[] localMsg = new byte[msgLen];
Array.Copy(buffer, localMsg, msgLen);

EndPoint newClientEP = new IPEndPoint(IPAddress.Any, 0);
udpSock2.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock2);

if (msgLen > 0)
{
string textToprint = Encoding.UTF8.GetString(localMsg, 0, msgLen);
this.Invoke(this.displayMessageDelegate, new object[] { textToprint });
}
}
catch (ObjectDisposedException)
{
}

}
public void UpdateFrame()
{
double cycleHz1 = 0.006;

double wave1 = Math.Sin((stopwatch2.ElapsedMilliseconds * 2.0 * Math.PI) * cycleHz1);

if (wave1 > 0.0)
{
pictureBox1.BackColor = Color.Black;
}
else
{
pictureBox1.BackColor = Color.White;
}
double cycleHz2 = 0.0065;

double wave2 = Math.Sin((stopwatch2.ElapsedMilliseconds * 2.0 * Math.PI) * cycleHz2);

if (wave2 > 0.0)
{
pictureBox2.BackColor = Color.Black;
}
else
{
pictureBox2.BackColor = Color.White;
}
double cycleHz3 = 0.007;

double wave3 = Math.Sin((stopwatch2.ElapsedMilliseconds * 2.0 * Math.PI) * cycleHz3);

if (wave3 > 0.0)
{
pictureBox3.BackColor = Color.Black;
}
else
{
pictureBox3.BackColor = Color.White;
}
double cycleHz4 = 0.0075;

double wave4 = Math.Sin((stopwatch2.ElapsedMilliseconds * 2.0 * Math.PI) * cycleHz4);

if (wave4 > 0.0)
{
pictureBox4.BackColor = Color.Black;
}
else
{
pictureBox4.BackColor = Color.White;
}
double cycleHz5 = 0.0082;

double wave5 = Math.Sin((stopwatch2.ElapsedMilliseconds * 2.0 * Math.PI) * cycleHz5);

if (wave5 > 0.0)
{
pictureBox5.BackColor = Color.Black;
}
else
{
pictureBox5.BackColor = Color.White;
}
double cycleHz6 = 0.0093;

double wave6 = Math.Sin((stopwatch2.ElapsedMilliseconds * 2.0 * Math.PI) * cycleHz6);

if (wave6 > 0.0)
{
pictureBox6.BackColor = Color.Black;
}
else
{
pictureBox6.BackColor = Color.White;
}
}
private void DisplayMessage(string messge)
{
int numVal;
//SPELL.Text += messge + Environment.NewLine;

numVal = Convert.ToInt32(messge);
udpSock2.Close();
if (numVal == 83)
{

//AB.PerformClick();
//this.Hide();
LET = label1.Text;
stopwatch2.Stop();
this.Hide();
//udpSock.Disconnect(true);
//udpSock.Connect(new IPEndPoint(IPAddress.Any, 8051));
}
else if (numVal == 76)
{
//BB.PerformClick();
LET = label2.Text;
stopwatch2.Stop();
this.Hide();
//udpSock2.Close();
}
else if (numVal == 70)
{
//C.PerformClick();
LET = label3.Text;
stopwatch2.Stop();
this.Hide();
//udpSock2.Close();
}
else if (numVal == 65)
{
//D.PerformClick();
LET = label4.Text;
stopwatch2.Stop();
this.Hide();
//udpSock2.Close();
}
else if (numVal == 61)
{
//E.PerformClick();
LET = label5.Text;
stopwatch2.Stop();
this.Hide();
//udpSock2.Close();
}
else if (numVal == 55)
{
//button1.PerformClick();
this.Hide();
//udpSock2.Close();
}
}
}
}

我有 7 个表格,在表格中选择一个字符后应该可以打开和隐藏这些表格。

最佳答案

像这样尝试:

    public static ABCDE form;
public static BCI1 form1;

[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
form = new ABCDE();
form1 = new BCI1();
form.Show();
form1.Show();
Application.Idle += new EventHandler(Application_Idle);
Application.Run();
}

static void Application_Idle(object sender, EventArgs e)
{
Message message;
while (!PeekMessage(out message, IntPtr.Zero, 0, 0, 0))
{
form.UpdateFrame();
form1.UpdateFrame();
}
}

将其设置为绿色:

long timeToBeGreenInMiliseconds = 10;
long timeWhenValue83WasSent;
bool shouldBeGreen;

void CallThisFunctionWhenValue83IsSent()
{
timeWhenValue83WasSent = stopwatch.ElapsedMilliseconds;
shouldBeGreen = true;
}

public void UpdateFrame()
{
if( shouldBeGreen )
{
pictureBox1.BackColor = Color.Green;
if( stopwatch.ElapsedMilliseconds - timeWhenValue83WasSent > timeToBeGreenInMiliseconds )
{
shouldBeGreen = false;
}
}
else
{
double cycleHz1 = 0.006;

double wave1 = Math.Sin((stopwatch.ElapsedMilliseconds * 2.0 * Math.PI) * cycleHz1);

if (wave1 > 0.0)
{
pictureBox1.BackColor = Color.Black;
}
else
{
pictureBox1.BackColor = Color.White;
}
}
}

关于c# - 多种形式的 UpdateFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30439465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com