当使用图表到系统托盘最小化表单时出现问题, [英] Issue when minimizing forms with Charts to System Tray,

查看:226
本文介绍了当使用图表到系统托盘最小化表单时出现问题,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,通过挂接到form_resize事件,我最小化到系统托盘。一切顺利,直到我添加一个图表的形式。当图表在表单上时,我会得到以下异常:

I have a form that I minimize to the system tray, by hooking-in to the form_resize event. All worked well, until I added a Chart to the form. When the chart is on the form, I would get the following exception:


宽度必须大于0px。

Width must be greater than 0px.

突出显示的行是:

   this.ShowInTaskbar = false;

我花了一点时间来解决问题(包括它正在添加导致它的图表),但设法从很长的堆栈跟踪消息中推断出它。无论如何,我设法在隐藏窗体之前将图表的可见性设置为false。

It took me a bit of time frigging around to work-out what the problem was (including that it was adding the Chart that caused it), but managed to deduce it from very long stack trace messages. Anyway, I managed to solve it by setting the chart visibility to false before hiding the form.

private void Form1_Resize(object sender, EventArgs e)
    {
        if (FormWindowState.Minimized == this.WindowState)
        {
            //Hide chart to prevent exception
            chtCompliance.Visible = false;
            chtHistory.Visible = false;

            icoTrayIcon.Visible = true;
            icoTrayIcon.ShowBalloonTip(500);
            this.ShowInTaskbar = false;
            this.Hide();
        }
        else if (FormWindowState.Normal == this.WindowState)
        {
            icoTrayIcon.Visible = false;
            this.ShowInTaskbar = true;

            //Restore chart visibility
            chtCompliance.Visible = true;
            chtHistory.Visible = true;
        }
    }

现在...这似乎解决了问题,但在我看来,这比一个坚实的解决方案更容易解决。任何人都知道一个更优雅的方式来解决这个问题? (我猜想一开始我可以动态地查找所有的图表,所以如果我添加更多,我不需要将这个代码重新编入例程中。)

Now... this appears to resolve the problem, but seems to me that this is more of a workaround than a solid solution. Anyone know of a more elegant way of addressing this problem? (I guess for a start I could dynamically look for all charts, so I don't have to hard-code this into the routine if I add more!)

推荐答案

更改ShowInTaskbar属性具有许多副作用。它是一个困难的属性,它强制本地窗口被重新创建,因为该属性影响使用CreateWindowEx()创建窗口时指定的窗口样式标志。底层样式标志是WS_EX_APPWINDOW,它必须被关闭才能摆脱任务栏按钮。 Winforms会破坏并重新创建窗口。

Changing the ShowInTaskbar property has many side effects. It is a "difficult" property, it forces the native window to be recreated since the property affects a window style flag that is specified when the window is created with CreateWindowEx(). The underlying style flag is WS_EX_APPWINDOW, it has to be turned off to get rid of the taskbar button. Which forces Winforms to destroy and re-create the window.

这有一个引起麻烦的诀窍,特别是对于尚未被调试的控件来说,这是非常好的。而您 在最糟糕的时候,当窗口最小化并且没有有意义的大小时。可以肯定的是,这样的图表控件可能反对被重新托管到一个0x0大小的新窗口。

That has a knack for causing trouble, particularly for controls that haven't been debugged well enough. And you are doing it at the worst possible time, when the window is minimized and doesn't have a meaningful size. So sure, such a chart control might well object against being re-hosted to a new window with a 0x0 size.

从尝试让这个笨拙的控件开心,更简单的解决方法是只是更改ShowInTaskbar属性。没有任何意义,当您调用Hide()时,任务栏按钮不可见。

Short from trying to keep that balky control happy, the much simpler workaround is to just not change the ShowInTaskbar property. There is no point to it, the taskbar button is invisible anyway when you call Hide().

删除ShowInTaskbar属性分配以解决您的问题。

Remove the ShowInTaskbar property assignments to fix your problem.

这篇关于当使用图表到系统托盘最小化表单时出现问题,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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