【问题标题】:Auto shuffle between windows forms every after 5 min每隔 5 分钟在窗体之间自动随机播放一次
【发布时间】:2018-09-14 11:31:33
【问题描述】:

专家

我想每隔 5 分钟自动洗牌一次窗体。 windows 窗体包含多个查询、多个视频、多个 powerpoint。

我有三个窗体,如下所示。

表格1代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Daily_System {
  public partial class Form1: Form {
    public Form1() {
      InitializeComponent();
      timer1.Enabled = true;
      timer1.Interval = 5000;
      timer1.Tick += timer1_Tick;
      timer1.Start();
    }

    private void Form1_Load(object sender, EventArgs e) {
      this.WindowState = FormWindowState.Maximized;

      CenterToScreen();
    }

    private Timer timer1 = new Timer();


    private void button1_Click_1(object sender, EventArgs e) {
      this.WindowState = FormWindowState.Minimized;
      Form2 f = new Form2(); // This is bad
      timer2.Enabled = true;
    }
    private void timer2_Tick(object sender, EventArgs e) {
      button1.PerformClick();
    }
  }
}

表格 2:Microsoft Powerpoint 文件

来自网络文件夹(路径)的多个 powerpoint 文件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Core = Microsoft.Office.Core;

namespace Daily_System {

  public partial class Form2: Form {


    public Form2() {
      InitializeComponent();


      this.WindowState = FormWindowState.Minimized;
      timer1.Enabled = true;
      timer1.Interval = 15000;
      timer1.Start();

    }


    private void Tick(object sender, EventArgs e) {
      Form3 Next = new Form3();
      Next.Show();
      this.Hide();
      timer1.Stop(); //Stop timer after tick once
    }




    protected override void OnLoad(EventArgs e)

    {

      base.OnLoad(e);

      this.BeginInvoke(new MethodInvoker(delegate() {
        button1.PerformClick();
      }));


    }



    private void button1_Click(object sender, EventArgs e) {
      Microsoft.Office.Interop.PowerPoint.Application pptApp = new Microsoft.Office.Interop.PowerPoint.Application();
      Microsoft.Office.Core.MsoTriState ofalse = Microsoft.Office.Core.MsoTriState.msoFalse;
      Microsoft.Office.Core.MsoTriState otrue = Microsoft.Office.Core.MsoTriState.msoTrue;

      pptApp.Visible = otrue;
      pptApp.Activate();
      Microsoft.Office.Interop.PowerPoint.Presentations ps = pptApp.Presentations;


      var opApp = new Microsoft.Office.Interop.PowerPoint.Application();
      pptApp.SlideShowEnd += PpApp_SlideShowEnd;
      var ppPresentation = ps.Open(@ "C:\Users\ok\Downloads\Parks-WASD2017.pptx", ofalse, ofalse, otrue);
      var settings = ppPresentation.SlideShowSettings;
      settings.Run();
    }



    private void PpApp_SlideShowEnd(Microsoft.Office.Interop.PowerPoint.Presentation Pres) {
      Pres.Saved = Microsoft.Office.Core.MsoTriState.msoTrue;
      Pres.Close();

    }

    private void Form2_Load(object sender, EventArgs e) {

    }

    private void button2_Click(object sender, EventArgs e) {
      this.WindowState = FormWindowState.Minimized;
      Form3 f = new Form3(); // This is bad
      f.Show(); /// f.Show();
      timer1.Enabled = true;
      this.Hide();
      timer1.Stop(); //Stop timer after tick once

    }

    private void timer1_Tick_1(object sender, EventArgs e) {
      button2.PerformClick();
    }
  }
}

形式 3:多个视频文件(MP4、FLV、MOV 等)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Daily_System {

  public partial class Form3: Form {

    public Form3() {
      InitializeComponent();
      timer1.Enabled = true;
      timer1.Interval = 15000;
      timer1.Start();

    }

    private void Form3_Load(object sender, EventArgs e) {
      axWindowsMediaPlayer1.settings.autoStart = true;
    }



    private void axWindowsMediaPlayer1_Enter_1(object sender, EventArgs e) {

      axWindowsMediaPlayer1.URL = @ "C:\Users\ok\Downloads\ok.mp4";
    }





    private void button1_Click(object sender, EventArgs e) {


      this.WindowState = FormWindowState.Minimized;
      Form1 f = new Form1(); // This is bad
      f.Show(); /// f.Show();
      timer1.Enabled = true;
      this.Hide();
      timer1.Stop(); //Stop timer after tick once

    }

    private void timer1_Tick_1(object sender, EventArgs e) {
      button1.PerformClick();
    }
  }
}

网络文件夹中的多个视频文件(路径)

要求:

每个表单应每 5 分钟更改一次并显示一次。

示例:首先 form1 应该显示,然后 5 分钟后 form1 应该最小化,form2 应该显示幻灯片,然后 5 分钟后 form2 应该最小化,form3 应该播放视频,然后 5 分钟后 form3 应该最小化并暂停视频,然后 form1应该显示。

它应该继续执行与上述相同的步骤。

最终条件:所有表单应在下午 6 点(每天)准确停止,并应在早上 7 点(每天)自动开始。

请指教...

【问题讨论】:

  • 洗牌是什么意思?带到前面?改变他们的内容?
  • 我的意思是首先 5 分钟 form1 应该显示,然后 form1 应该最小化,然后 form2 应该显示,然后 form2 应该最小化,然后 form3 应该打开。这一切都应该每 5 分钟发生一次。
  • @Leace 您没有发布任何相关代码。当您询问 Windows 窗体 时,这是一个 SQL 查询。你尝试过什么吗?您是否知道如何播放视频、显示嵌入的 powerpoint 幻灯片等,或者您是否也在问这个问题?
  • @PanagiotisKanavos 很抱歉,我缺少与 powerpoint 幻灯片和视频显示有关的 form2 和 fom3 代码。如果您有相同的编码,请告诉我..
  • @PanagiotisKanavos 找到附加的编码

标签: c# winforms visual-studio-2012 powerpoint windows-forms-designer


【解决方案1】:

一种方法是为表单创建基类以控制它们的最小化和最大化,并通过覆盖OnStart()OnStop() 方法找出特定表单何时最小化或最大化。这可以按如下方式完成:

定义名为CustomForm的新基类:

public class CustomForm : Form
{
    public static List<CustomForm> AllForms = new List<CustomForm>();
    private static int CurrentFormIndex = 0;
    private static Timer SliderTimer = new Timer() { Interval = 5000 }; // { Interval = 5 * 60000 };

    public static void Start(params CustomForm[] forms)
    {
        AllForms.AddRange(forms);

        forms[0].Show();
        forms[0].WindowState = FormWindowState.Maximized;
        AllForms[0].OnStart(AllForms[0]);

        SliderTimer.Tick += SliderTimer_Tick;
        SliderTimer.Start();
    }

    private static void SliderTimer_Tick(object sender, EventArgs e)
    {
        SliderTimer.Stop();

        // Minimizing current form
        AllForms[CurrentFormIndex].OnStop(AllForms[CurrentFormIndex]);
        AllForms[CurrentFormIndex].WindowState = FormWindowState.Minimized;

        // Maximizing next form
        int NextFormIndex = (CurrentFormIndex + 1) % AllForms.Count;
        if (!AllForms[NextFormIndex].Visible)
            AllForms[NextFormIndex].Show();
        AllForms[NextFormIndex].WindowState = FormWindowState.Maximized;
        AllForms[NextFormIndex].OnStart(AllForms[NextFormIndex]);

        CurrentFormIndex = NextFormIndex;

        SliderTimer.Start();
    }

    // Application will exits when one of forms being closed
    protected override void OnFormClosed(FormClosedEventArgs e)
    {
        base.OnFormClosed(e);
        Application.Exit();
    }

    // For overriding in forms to Start something such as playing or etc
    protected virtual void OnStart(CustomForm Sender)
    {
    }

    // For overriding in forms to Stop something such as playing or etc
    protected virtual void OnStop(CustomForm Sender)
    {
    }
}

更改Program 类如下:

static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        CustomForm.Start(new Form1(), new Form2(), new Form3());
        Application.Run();
    }
}

将您的表单更改为继承CustomForm 而不是Form,如下所示:

public partial class Form1 : CustomForm
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Shown(object sender, EventArgs e)
    {
        // axWindowsMediaPlayer1.URL = @"C:\Users\ok\Downloads\ok.mp4";
        WMPLib.IWMPMedia v1 = axWindowsMediaPlayer1.newMedia(@"d:\1.mp4");
        axWindowsMediaPlayer1.currentPlaylist.appendItem(v1);
        WMPLib.IWMPMedia v2 = axWindowsMediaPlayer1.newMedia(@"d:\2.mp4");
        axWindowsMediaPlayer1.currentPlaylist.appendItem(v2);
        WMPLib.IWMPMedia v3 = axWindowsMediaPlayer1.newMedia(@"d:\3.mp4");
        axWindowsMediaPlayer1.currentPlaylist.appendItem(v3);
    }

    // To start playing video and etc when form being maximized
    protected override void OnStart(CustomForm Sender)
    {
        axWindowsMediaPlayer1.Ctlcontrols.play();
    }

    // To stop playing video and etc when form being minimized
    protected override void OnStop(CustomForm Sender)
    {
        axWindowsMediaPlayer1.Ctlcontrols.pause();
    }
}

表格2:

public partial class Form2 : CustomForm
{
    Microsoft.Office.Interop.PowerPoint.Presentation ppPresentation;
    Microsoft.Office.Interop.PowerPoint.SlideShowSettings settings;
    Microsoft.Office.Interop.PowerPoint.Application opApp;
    int StartingSlide = 1;

    public Form2()
    {
        InitializeComponent();
    }

    protected override void OnStart(CustomForm Sender)
    {
        Microsoft.Office.Interop.PowerPoint.Application pptApp = new Microsoft.Office.Interop.PowerPoint.Application();
        Microsoft.Office.Core.MsoTriState ofalse = Microsoft.Office.Core.MsoTriState.msoFalse;
        Microsoft.Office.Core.MsoTriState otrue = Microsoft.Office.Core.MsoTriState.msoTrue;

        pptApp.Visible = otrue;
        pptApp.Activate();
        Microsoft.Office.Interop.PowerPoint.Presentations ps = pptApp.Presentations;

        opApp = new Microsoft.Office.Interop.PowerPoint.Application();
        opApp.SlideShowNextSlide += OpApp_SlideShowNextSlide;
        ppPresentation = ps.Open(@"c:\a.pptx", ofalse, ofalse, otrue);
        settings = ppPresentation.SlideShowSettings;

        settings.RangeType = Microsoft.Office.Interop.PowerPoint.PpSlideShowRangeType.ppShowSlideRange;
        settings.StartingSlide = StartingSlide;

        settings.Run();
    }

    private void OpApp_SlideShowNextSlide(Microsoft.Office.Interop.PowerPoint.SlideShowWindow Wn)
    {
        StartingSlide = Wn.View.CurrentShowPosition;
    }

    protected override void OnStop(CustomForm Sender)
    {
        ppPresentation.Close();
        //opApp.Quit();
        Process.Start("cmd", "/c taskkill /im POWERPNT.EXE");
    }
}

【讨论】:

  • 谢谢,但视频也没有暂停,也有 powerpoint。请建议
  • @Leace - 要为视频加注星标,请使用 axWindowsMediaPlayer1.URL = @"C:\ok.mp4"; 并停止它,只需执行 axWindowsMediaPlayer1.URL = "";。检查更新的答案。
  • :我们快完成任务了。让我知道您调整错误的方便时间。 “没有什么是不可能的”
  • 对不起。我参与了其他问题。我随时为您服务。
  • 你好,可以点击上方聊天
【解决方案2】:

有很多方法可以做到这一点。 Winforms 是一个乐高盒子,可让您以任何您想要的方式将这些部件拼凑在一起。从一个内置的 winforms 类派生您自己的类是一种基本策略。你需要的是一个处理表单切换的小控制器。最好的覆盖类是ApplicationContext。您获得的默认设置是一个非常简单的设置,它仅确保显示主窗体并在您关闭应用程序时终止它。

让我们自己推导出来。这是一个潜在的重量级应用程序,这些不是廉价的形式。所以我们想通过它们的类型而不是它们的实例来指定要切换的表单,当表单被切换时创建和销毁它们。您希望应用程序在当前应用程序被用户关闭时终止。将此代码复制/粘贴到 Program.cs 文件中:

class FormSwitcher : ApplicationContext {
    Timer switcher;
    Type[] forms;
    int formIndex;
    Form currentForm;
    bool switching;

    public FormSwitcher(params Type[] forms) {
        this.forms = forms;
        switcher = new Timer() { Enabled = true };
        switcher.Interval = System.Diagnostics.Debugger.IsAttached ? 3000 : 5 * 60000;
        switcher.Tick += SwitchForm;
        formIndex = -1;
        SwitchForm(this, EventArgs.Empty);
    }
    private void SwitchForm(object sender, EventArgs e) {
        switching = true;
        formIndex += 1;
        if (formIndex >= forms.Length) formIndex = 0;
        var newform = (Form)Activator.CreateInstance(forms[formIndex]);
        newform.FormClosed += delegate { if (!switching) this.ExitThread(); };
        if (currentForm != null) {
            newform.StartPosition = FormStartPosition.Manual;
            newform.Bounds = currentForm.Bounds;
        }
        newform.Show();
        if (currentForm != null) currentForm.Close();
        currentForm = newform;
        switching = false;
    }
}

希望它的作用很明显,如果没有,请告诉我,我会添加 cmets。现在您可以在同一个文件中修改 Main() 方法,将此类的一个实例传递给 Application.Run() 方法。我将复制/粘贴我测试过的代码:

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

【讨论】:

  • 感谢您的帮助。有没有可能以这种方式。 Form1 不应该关闭,而是它可以最小化,然后在轮到它时最大化。并且 form2 视频应该暂停,然后在恢复时最大化。同样的方式 powerpoint 应该暂停,然后在轮到时恢复最大化。
  • 按照你想要的方式修改它。我确实试图强调保持这些表单对象活着并不是一个好主意,因为它们非常昂贵。事实上,我建议您在 Close() 调用之后添加 GC.Collect(),这对于驯服 PowerPoint 非常重要。
  • 我无法以我的方式做事。你能帮我解决以上问题吗?
【解决方案3】:

这里是创建 3 个表单的示例代码,然后每 5 秒切换一次最大化(其他被最小化)。当任何表单关闭时,应用程序都会退出。我已经把 cmets 放在了整个地方,下面是你可以用来暂停表单播放的代码:

internal static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    private static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        //here we create our 3 forms.  note, you can create and show as many as you want here
        //the application will automatically loop through them
        new Form1().Show();
        new Form2().Show();
        new Form().Show();

        //minimize all forms, and set a close handler
        foreach (Form form in Application.OpenForms)
        {
            form.WindowState = FormWindowState.Minimized;
            form.FormClosed += Form_FormClosed;
        }

        //start a thread to manage switching them
        Task.Run((Action)Go);

        //start the main UI thread loop
        Application.Run();
    }

    private static void Go()
    {
        while (true)
        {
            //loop through all forms
            foreach (Form form in Application.OpenForms)
            {
                //show it (send execution to UI thread)
                form.Invoke(new MethodInvoker(() =>
                {
                    form.Show();
                    form.WindowState = FormWindowState.Maximized;
                }));

                //wait 5 seconds
                Thread.Sleep(5000);

                //minimize it (send execution to UI thread)
                form.Invoke(new MethodInvoker(() =>
                {
                    form.WindowState = FormWindowState.Minimized;
                }));
            }
        }
    }

    private static void Form_FormClosed(object sender, FormClosedEventArgs e)
    {
        Application.Exit();
    }
}

现在对于需要在最小化/最大化时采取行动的表单,将这样的 Resize 处理程序添加到表单上的代码中:

    private void Form1_Resize(object sender, EventArgs e)
    {
        if (WindowState == FormWindowState.Minimized)
        {
            //stop any playback
        } else
        {
            //start any playback
        }
    }

【讨论】:

    猜你喜欢
    • 2013-11-17
    • 1970-01-01
    • 2020-08-09
    • 2022-06-28
    • 1970-01-01
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    • 2019-05-02
    相关资源
    最近更新 更多