简体   繁体   English

在 Window Forms 之间切换似乎不起作用

[英]Switching between Window Forms does not seem to work

Hey there StackOverflow community!嘿,StackOverflow 社区!

So I've been working on an application that checks if the user has entered valid credentials in a Login() form, then it switches over to an Intro_Sequence() form (where a.mp4 file is played in fullscreen mode) as a sort of aesthetic addition to the app.因此,我一直在开发一个应用程序,该应用程序检查用户是否在Login()表单中输入了有效凭据,然后它切换到Intro_Sequence()表单(其中以全屏模式播放 .mp4 文件)作为排序应用程序的美学补充。 So far so good, no problems whatsoever.到目前为止一切顺利,没有任何问题。

The problem comes right after the Intro ends, where supposedly the application should switch over to a third form, called Main() .问题出现在 Intro 结束后,据说应用程序应该切换到第三种形式,称为Main()

I have implemented a check whenever Windows Media Player (aka axWMPLib ) changes its PlayState to see whether it has finished the playback.每当 Windows 媒体播放器(又名axWMPLib )更改其PlayState以查看它是否已完成播放时,我都会进行检查。

If it has, then the Hide() event is called to conceal the current Form's window, then main.ShowDialog() should open the third form.如果有,则调用Hide()事件来隐藏当前 Form 的 window,然后main.ShowDialog()应该打开第三个表单。

Afterwards, I call the Close() event to close the previous Form's window entirely.之后,我调用Close()事件来完全关闭前一个 Form 的 window。

Here is the code so far:这是到目前为止的代码:

    public partial class Intro_Sequence : Form
    {
        public static string Username;
        public Intro_Sequence(string username)
        {
            InitializeComponent();
            Username = username;
            FormBorderStyle = FormBorderStyle.None;
            Bounds = Screen.PrimaryScreen.Bounds;
            TopMost = true;
            intro.uiMode = "none";
            intro.URL = AppDomain.CurrentDomain.BaseDirectory + "\\Intro.mp4";
            intro.enableContextMenu = false;
            DisableMouseClicks();
        }
        private void DisableMouseClicks()
        {
            if (this.Filter == null)
            {
                this.Filter = new MouseClickMessageFilter();
                Application.AddMessageFilter(this.Filter);
            }
        }

        private MouseClickMessageFilter Filter;

        private const int LButtonDown = 0x201;
        private const int LButtonUp = 0x202;
        private const int LButtonDoubleClick = 0x203;

        public class MouseClickMessageFilter : IMessageFilter
        {

            public bool PreFilterMessage(ref System.Windows.Forms.Message m)
            {
                switch (m.Msg)
                {
                    case LButtonDown:
                    case LButtonUp:
                    case LButtonDoubleClick:
                        return true;
                }
                return false;
            }
        }

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

        private void intro_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
        {
            if(intro.playState == WMPLib.WMPPlayState.wmppsMediaEnded)
            {
                Main main = new Main(Username);
                this.Hide();
                main.ShowDialog();
                this.Close();
            }
        }
    }

As you can see I have also added a filter to block clicks during playback, so as not to allow the user to pause it.如您所见,我还添加了一个过滤器来阻止播放期间的点击,以免用户暂停它。

However, when I execute this code, it works perfectly fine until it finishes the video and then closes abruptly.但是,当我执行此代码时,它工作得非常好,直到它完成视频然后突然关闭。

I tried putting breakpoints and everything seems to be fine.我尝试设置断点,一切似乎都很好。

It does call everything I tell it to call, yet the form doesn't even appear.它确实调用了我告诉它调用的所有内容,但表单甚至没有出现。

I have also tried several other alternatives, like not closing the Form at all, calling Show() instead of ShowDialog() and even not Hiding it at all.我还尝试了其他几种选择,例如根本不关闭表单,调用Show()而不是ShowDialog()甚至根本不隐藏它。

It is as if it either freezes there or closes instantly without any sign of the Main form showing.就好像它要么冻结在那里,要么立即关闭,而没有任何主窗体显示的迹象。

I also tried calling the Main() form from the Login() and it works perfectly from there.我还尝试从Login()调用Main()表单,它从那里完美运行。

I really don't know what is going on.我真的不知道发生了什么事。

Any help would be appreciated.任何帮助,将不胜感激。

How about something like this?这样的事情怎么样?

There are three forms. forms 共有三个。 There's a Login form (in this case, it's just an empty form - you close it by clicking on the red X).有一个登录表单(在这种情况下,它只是一个空表单 - 您可以通过单击红色 X 将其关闭)。 It is popped up modally from within the Main form (while the main form is hidden).它从窗体中以模态方式弹出(而主窗体被隐藏)。

There's a Splash screen on which your video is to play.您的视频将在启动画面上播放。 I fake out the video by using await Task.Delay(4000);我使用await Task.Delay(4000); to get a pause.暂停一下。 After the 4 second delay, I raise an event (equivalent to your media player event).在 4 秒延迟后,我提出了一个事件(相当于您的媒体播放器事件)。 What I do is show this modally from the main form.我所做的是从主要形式中以模态方式显示这一点。 I put the event handler in this form;我把事件处理程序放在这种形式; when the event is raised, I close the splash screen modal.引发事件时,我关闭启动画面模式。 The entire (non-designer) code for that form looks like (and, since there are no controls on this form, the designer code is pretty lean):该表单的整个(非设计器)代码看起来像(并且,由于此表单上没有控件,设计器代码非常精简):

public partial class SplashScreen : Form
{
    public event EventHandler SplashFinished;
    public SplashScreen()
    {
        InitializeComponent();
        this.SplashFinished += SplashScreen_SplashFinished;
    }

    private async void SplashScreen_Load(object sender, EventArgs e)
    {
        await Task.Delay(4000);
        SplashFinished?.Invoke(this, new EventArgs());
    }

    private void SplashScreen_SplashFinished(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.OK;
        this.Close();
    }
}

Then there's the Main form.然后是窗体。 It gets fired up in the normal way from Program.cs :它从Program.cs以正常方式启动:

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}

The only thing that I added to that form (from the out-of-the-box code) is:我添加到该表单中的唯一内容(来自开箱即用的代码)是:

private void Form1_Load(object sender, EventArgs e)
{
    this.Hide();
    var login = new LoginForm();
    //should really check this, but for now
    login.ShowDialog(this);
    var splash = new SplashScreen();
    splash.ShowDialog(this);
    this.Show();
}

So, when the app starts, the user is shown the login form (the main form is hidden).因此,当应用程序启动时,用户会看到登录表单(主表单被隐藏)。 He does what is needed to do (and the result is checked in the main form's Form1_Load handler.他做了需要做的事情(并在主窗体的Form1_Load处理程序中检查结果。

If everything is cool, a new SplashScreen form is created and shown modally.如果一切正常,就会创建一个新的SplashScreen表单并以模态方式显示。 When it pops up, the video starts (in this case, the video is simply an asynchronous timer).当它弹出时,视频开始(在这种情况下,视频只是一个异步计时器)。 When the video ends, the SplashScreen handles the finished event, and uses it to close itself.当视频结束时, SplashScreen 处理完成的事件,并使用它来关闭自己。

Once control returns to the main form, it displays itself.一旦控件返回到主窗体,它就会显示自己。

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

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