如何最小化C#中的表单? [英] how to minimize the form in C#?

查看:77
本文介绍了如何最小化C#中的表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行Windows应用程序。在那个,基于条件我的Windows应用程序将自动最小化。例如,我想在当前时间达到4P.M时自动最小化Windows应用程序。如何通过c#代码最小化它。注意:只有最小化不能关闭我的应用程序。

I am running windows application. In that, based on condition my windows application will go to minimize automatically. For instance, i would like to minimize windows application automatically when current time reach 4P.M. How to minimize it by c# code. Note: only minimize not close my application.

推荐答案

您的问题是跨线程操作。事件处理程序中发生的事情可能发生在其他线程上,然后发生在UI上。



您可以使用不同的计时器,这不是问题所在: System.Windows.Forms.Timer

http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx [ ^ ]。



但请注意:此计时器非常糟糕,它保证没有合理的计时精度。为了你的目的,它可能足够好,因为你几乎不需要任何严格的计时精度。



在所有其他情况下(线程和其他计时器),你应该解决这个问题交叉线程问题,这是一件非常简单的事情。您不能从非UI线程调用与UI相关的任何内容。相反,您需要使用 Invoke System.Windows.Threading的方法。 Dispatcher (对于Forms或WPF)或 System.Windows.Forms.Control (仅限表单)。



您将在我过去的答案中找到有关其工作原理和代码示例的详细说明:

Control.Invoke()与Control.BeginInvoke() [ ^ ],

使用Treeview扫描仪和MD5的问题 [ ^ ]。



另请参阅有关线程的更多参考资料:

如何让keydown事件在不同的操作上运行vb.net中的线程 [ ^ ],

在启用禁用+多线程后控制事件未触发 [ ^ ]。



-SA
Your problem is the cross-thread operation. What happens in the event handler, can happen on other thread then the UI one.

You can use a different timer, for which this is not a problem: System.Windows.Forms.Timer:
http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx[^].

Be warned though: this timer is very bad, it guarantees no reasonable timing precision. For your purpose it could be good enough though, as you hardly require any serious timing precision.

In all other cases (threading and other timers), you should solve this cross-threading problem, which is a pretty simple thing. You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


一个例子:



An exemple :

public Form1()
        {
            InitializeComponent();
            Timer CurrentTime = new Timer();
            CurrentTime.Tick += new EventHandler(CurrentTime_Tick);
            CurrentTime.Interval = 1000; //check every second
            CurrentTime.Start();
        }

        void CurrentTime_Tick(object sender, EventArgs e)
        {
            if (DateTime.Now.Hour == 16 /*add here more condition*/)
            {
                this.WindowState = FormWindowState.Minimized;
            }
        }





如果当前时间是下午4点,则强制窗口最小化



Force the windows to be minimized if current time is 4pm


如果你想跟踪剩余的时间,你也可以这样做:

If you want to keep track of time remaining aswell you could do something like this:
private static DateTime endDate,startDate;
public DateTime StartDate
        {
            get
            {
                return startDate;
            }
            set
            {
                startDate = value;
            }
        }
        public DateTime EndDate
        {
            get
            {
                return endDate;
            }
            set
            {
                endDate = value;
            }
        }

public int DaysRemaining
        {
                get
                {
                    if (startDate != null && endDate != null)
                        return endDate.Subtract(startDate).Hours;//or days, you would need to change the rest of the code to use Hours and not Days.
                    else
                        return 0;
                }
                set
                {
                    endDate = startDate.Add(new TimeSpan(value, 0, 0));
                }
        }





然后检查剩余时间属性是否为< = 0然后做什么Viswanathan Ramamoorthy上面说过使用Form的WindowState。



Then check if the time remaining property is <= 0 and then do what Viswanathan Ramamoorthy said above with using the Form's WindowState.


这篇关于如何最小化C#中的表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆