首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SecurityException发生

SecurityException发生
EN

Stack Overflow用户
提问于 2015-05-21 14:28:54
回答 1查看 238关注 0票数 1

我读过,可以将UnityEngineWinforms融合,所以这就是我想要做的。我试图使用正弦波来控制Winform按钮上颜色之间的闪烁。然而,我得到了一个SecurityException,我不知道如何解决。

代码语言:javascript
复制
    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));
        }

在这里,我打开了新的表单,表单中包含可以选择的字符,然后表单关闭,然后返回主表,表单1。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-23 12:24:54

完整程序:

代码语言:javascript
复制
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();
            }
        }
    }
}

以及表格:

代码语言:javascript
复制
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添加到表单中即可。

我建议用Microsoft编写整个应用程序--它比Unity3D更容易学习:)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30376740

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档