简体   繁体   English

如何从任务栏最小化表单?

[英]How to minimize form from taskbar?

I have developed winform application and I have set formborderstyle=none.我开发了 winform 应用程序,并设置了 formborderstyle=none。 Thatz why when I am running application I can't minimize it through taskbar.这就是为什么当我运行应用程序时,我无法通过任务栏将其最小化。 Does any body knows solution for this?有没有人知道解决方案?

I tried following code.. adding it in my form.我尝试了以下代码.. 在我的表单中添加它。

    const int WS_CLIPCHILDREN = 0x2000000;
    const int WS_MINIMIZEBOX = 0x20000;
    const int WS_MAXIMIZEBOX = 0x10000;
    const int WS_SYSMENU = 0x80000;
    const int CS_DBLCLKS = 0x8;
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.Style = WS_CLIPCHILDREN | WS_MINIMIZEBOX | WS_SYSMENU;
            cp.ClassStyle = CS_DBLCLKS;
            return cp;
        }
    }

I am now able to minimize the application from taskbar.我现在可以从任务栏中最小化应用程序。 But the problem is it is creating two intances of my application one which I need and the other which is unneccessary.但问题是它正在创建我的应用程序的两个实例,一个是我需要的,另一个是不必要的。

Does any body knows solution for this.. or does anyone has some other solution which works ?有没有人知道这个问题的解决方案......或者有没有其他人有其他可行的解决方案?

A borderless form should always be one that the user is not expected to minimize.无边界表单应始终是用户不希望最小化的表单。 The discoverability principle starts to apply here: most users don't know that you can minimize a window by clicking on its taskbar icon.可发现性原则在这里开始适用:大多数用户不知道您可以通过单击任务栏图标来最小化窗口。 They're going to expect to be able to do it by clicking the button next to the big red x .他们希望能够通过单击大红色x旁边的按钮来完成此操作。

The right solution is to choose a different border style for your form, one that includes the title bar and the minimize box.正确的解决方案是为表单选择不同的边框样式,其中包括标题栏和最小化框。 Windows will automatically behave as expected. Windows 将自动按预期运行。 Things are much easier when you follow the standard conventions of your platform, not only for you as a programmer, but for your users.当您遵循平台的标准约定时,事情会容易得多,不仅对于您作为程序员,而且对于您的用户。 It also fixes that nasty flickering effect when your form is created or restored where I can see the standard caption bar for a few seconds.它还修复了在创建或恢复表单时令人讨厌的闪烁效果,我可以在几秒钟内看到标准标题栏。

Of course, you'll inevitably want to do this anyway, so despite my better judgement, I'll try to provide a solution.当然,无论如何你都不可避免地想要这样做,所以尽管我有更好的判断,我还是会尝试提供一个解决方案。 The first problem is I can't reproduce the behavior you describe (Windows Server 2008 R2, .NET 4.0).第一个问题是我无法重现您描述的行为(Windows Server 2008 R2、.NET 4.0)。 Adding exactly the code shown to a new WinForms project, and setting the form's FormBorderStyle property to "None", there's no way I can get two windows to show up.将显示的代码准确添加到新的 WinForms 项目,并将表单的FormBorderStyle属性设置为“无”,我无法显示两个窗口。 Clicking on the taskbar icon causes the form to minimize, clicking it again restores it.单击任务栏图标会导致表单最小化,再次单击它会恢复它。

But there is a way to simplify your code.但是有一种方法可以简化您的代码。 And you should probably be OR-ing the style flags that you're adding with the existing style flags, rather than replacing the existing flags.并且您可能应该将要添加的样式标志与现有样式标志进行 OR 运算,而不是替换现有标志。 Replace your code with this:用这个替换你的代码:

const int WS_MINIMIZEBOX = 0x20000;
const int CS_DBLCLKS = 0x8;
protected override CreateParams CreateParams
{
    get
    {
        CreateParams cp = base.CreateParams;
        cp.Style |= WS_MINIMIZEBOX;
        cp.ClassStyle |= CS_DBLCLKS;
        return cp;
    }
}

If that doesn't fix your problem (and I'm skeptical that it will), then as I suspected, there's something else wrong in your code that you haven't shown us .如果这不能解决您的问题(我怀疑它会解决),那么正如我怀疑的那样,您的代码中还有其他错误,您没有向我们展示 Just because you can comment out a few lines of code and your program works as expected doesn't necessarily imply that the problem lies in those lines of code.仅仅因为您可以注释掉几行代码并且您的程序按预期工作并不一定意味着问题出在这些代码行中。 They can be perfectly correct, but interfering with a hack you've used elsewhere.它们可以是完全正确的,但会干扰您在其他地方使用的 hack。

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

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