简体   繁体   English

按下按钮时如何弹出课程

[英]How do you make a class pop up when a button is pressed

private void buttonPlay_Click(object sender, EventArgs e)//This is the the button.
{
    //This is the class that I want to throw to another window and run at the same time.
    //I want this class to pop up in another window.  How would I do this?
    Matrix.MatrixEffect();    
}

This is the matrix class: 这是矩阵类:

using System;

namespace JasonsMatrix
{
    class Matrix
    {
        internal static void MatrixEffect()
        {

            Console.Title = "The Matrix (Remember It's a Secret)";
            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.WindowLeft = Console.WindowTop = 0;
            Console.WindowHeight = Console.BufferHeight = Console.LargestWindowHeight;
            Console.WindowWidth = Console.BufferWidth = Console.LargestWindowWidth;

#if readkey
            Console.WriteLine("H1T 7NY K3Y T0 C0NT1NU3 =/");
            Console.ReadKey();
#endif

            Console.CursorVisible = false;
            int width, height;
            int[] y;
            int[] l;
            Initialize(out width, out height, out y, out l);
            int ms;

            while (true)
            {
                DateTime t1 = DateTime.Now;
                MatrixStep(width, height, y, l);
                ms = 10 - (int)((TimeSpan)(DateTime.Now - t1)).TotalMilliseconds;

                if (ms > 0)
                    System.Threading.Thread.Sleep(ms);

                if (Console.KeyAvailable)
                    if (Console.ReadKey().Key == ConsoleKey.F5)
                        Initialize(out width, out height, out y, out l);
            }
        }

        static bool thistime = false;

        private static void MatrixStep(int width, int height, int[] y, int[] l)
        {
            int x;
            thistime = !thistime;

            for (x = 0; x < width; ++x)
            {
                if (x % 11 == 10)
                {
                    if (!thistime)
                        continue;

                    Console.ForegroundColor = ConsoleColor.White;
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.DarkGreen;
                    Console.SetCursorPosition(x, inBoxY(y[x] - 2 - (l[x] / 40 * 2), height));
                    Console.Write(R);
                    Console.ForegroundColor = ConsoleColor.Green;
                }
                Console.SetCursorPosition(x, y[x]);
                Console.Write(R);
                y[x] = inBoxY(y[x] + 1, height);
                Console.SetCursorPosition(x, inBoxY(y[x] - l[x], height));
                Console.Write(' ');
            }
        }

        private static void Initialize(out int width, out int height, out int[] y, out int[] l)
        {
            int h1;
            int h2 = (h1 = (height = Console.WindowHeight) / 2) / 2;
            width = Console.WindowWidth - 1;
            y = new int[width];
            l = new int[width];
            int x;
            Console.Clear();
            for (x = 0; x < width; ++x)
            {
                y[x] = r.Next(height);
                l[x] = r.Next(h2 * ((x % 11 != 10) ? 2 : 1), h1 * ((x % 11 != 10) ? 2 : 1));
            }
        }

        static Random r = new Random();

        static char R
        {
            get
            {
                int t = r.Next(10);
                if (t <= 2)
                    return (char)('0' + r.Next(10));
                else if (t <= 4)
                    return (char)('a' + r.Next(27));
                else if (t <= 6)
                    return (char)('A' + r.Next(27));
                else
                    return (char)(r.Next(32, 255));
            }
        }

        public static int inBoxY(int n, int height)
        {
            n = n % height;
            if (n < 0)
                return n + height;
            else
                return n;
        }



    }
}

(I've assumed WinForms, but for no particular reason - things would be completely different in ASP.NET for example.) (我假设使用WinForms,但没有特殊原因-例如,ASP.NET中的情况将完全不同。)

You should create a new form to host the matrix effect, and show the matrix effect in that form. 您应该创建一个新的窗体来承载矩阵效果,并以该窗体显示矩阵效果。 It's hard to say more about the details without knowing what Matrix.MatrixEffect actually does. 在不知道Matrix.MatrixEffect实际作用的情况下很难说出更多细节。 You don't "throw" a class to another window... 您无需将课程“扔”到另一个窗口...

You should also consider whether by "pop up" you mean a modal dialog, or just an extra window which will "run" at the same time as your existing one. 您还应该考虑“弹出”是一个模态对话框,还是只是一个与现有对话框同时“运行”的额外窗口。

It looks like this is a console application. 看起来这是一个控制台应用程序。 If you want to open a new console window, you'll have to spawn a new process using the System.Diagnostics.Process class. 如果要打开新的控制台窗口,则必须使用System.Diagnostics.Process类生成新进程。 You can pass data to the new application using command line parameters or environment variables. 您可以使用命令行参数或环境变量将数据传递给新应用程序。 For a more advanced approach, you can pass data back and forth using standard input and output. 对于更高级的方法,您可以使用标准输入和输出来回传递数据。

You won't be able to do this with a single class, these changes will mean a new architecture for your solution. 您将无法使用单个类来完成此操作,这些更改将为您的解决方案提供新的体系结构。

process.start(@"something.exe");

//所有需要说的就是

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM