2015-05-21 178 views
0

我已閱讀,可能將UnityEngineWinforms融合,所以這就是我所要做的。我正在嘗試使用正弦波來控制Winform中按鈕上的顏色之間的閃爍。但是我得到一個SecurityException,我不知道如何解決。發生安全異常

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

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

     //Application.EnableVisualStyles(); 
     //Application.SetCompatibleTextRenderingDefault(false); 
     //Application.Idle += new EventHandler(Application_Idle); 
     //Application.Run(form1); 
      BCI1 form1 = new BCI1(); 
      //Application.Run(form1); 
      form1.ShowDialog(); 
      //stopwatch.Start(); 
      SPELL.Text = SPELL.Text + form1.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)); 
     } 

在這裏,我打開了新的形式,而形式母雞diplays SOM字符,可以選擇,以下簡稱窗體關閉agian,並返回到主,形式1.

回答

1

完整的程序:

using System; 
using System.Drawing; 
using System.Runtime.InteropServices; 
using System.Security; 
using System.Windows.Forms; 

namespace Flicker 
{ 
    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); 

     static Form1 form; 

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

     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.Diagnostics; 
using System.Windows.Forms; 

namespace Flicker 
{ 
    public partial class Form1 : Form 
    { 
     Stopwatch stopwatch = new Stopwatch(); 

     public Form1() 
     { 
      InitializeComponent(); 

      stopwatch.Start(); 
     } 

     public void UpdateFrame() 
     { 
      double cycleHz = 0.001; 

      double wave = Math.Sin((stopwatch.ElapsedMilliseconds * 2.0 * Math.PI) * cycleHz); 

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

只需添加pictureBox1到您的形式,你的設計師。

我建議寫在微軟XNA整個應用程序 - 這是更容易學習比Unity3D :)

+0

好,再次感謝@JinJi –

+0

我沒有看到你狀態doublebuffering在你的代碼? –

+0

默認情況下,Picturebox具有啓用doublebuffering的功能。你不需要改變任何東西。 – JinJi