有什么方法可以在 C# 中创建隐藏的主窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/683896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Any way to create a hidden main window in C#?
提问by
I just want a c# application with a hidden main window that will process and respond to window messages.
我只想要带有隐藏主窗口的 ac# 应用程序,它将处理和响应窗口消息。
I can create a form without showing it, and can then call Application.Run() without passing in a form, but how can I hook the created form into the message loop?
我可以创建一个表单而不显示它,然后可以在不传递表单的情况下调用 Application.Run(),但是如何将创建的表单挂接到消息循环中?
Is there another way to go about this?
有没有其他方法可以解决这个问题?
Thanks in advance for any tips!
提前感谢您的任何提示!
采纳答案by Noldorin
Excellent! That link pointed me in the right direction. This seems to work:
优秀!该链接为我指明了正确的方向。这似乎有效:
Form f = new Form1();
f.FormBorderStyle = FormBorderStyle.FixedToolWindow;
f.ShowInTaskbar = false;
f.StartPosition = FormStartPosition.Manual;
f.Location = new System.Drawing.Point(-2000, -2000);
f.Size = new System.Drawing.Size(1, 1);
Application.Run(f);
To keep it from showing up in Alt-Tab, you need it to be a tool window. Unfortunately, this prevents it from starting minimized. But setting the start position to Manual and positioning it offscreen does the trick!
为了防止它出现在 Alt-Tab 中,您需要将它作为一个工具窗口。不幸的是,这会阻止它最小化启动。但是将开始位置设置为手动并将其定位在屏幕外就可以了!
回答by Sruly
in the Form1 code file add this.Visible = false; to the constructor.
在 Form1 代码文件中添加 this.Visible = false; 到构造函数。
This will hide the window but it will flash for a sec before it is hidden. Alternatively you can write your own Application.Run command.
这将隐藏窗口,但它会在隐藏之前闪烁一秒钟。或者,您可以编写自己的 Application.Run 命令。
for more info http://social.msdn.microsoft.com/forums/en-US/winforms/thread/dece45c8-9076-497e-9414-8cd9b34f572f/
更多信息http://social.msdn.microsoft.com/forums/en-US/winforms/thread/dece45c8-9076-497e-9414-8cd9b34f572f/
also you may want to set the this.ShowInTaskbar to false.
您也可能希望将 this.ShowInTaskbar 设置为 false。
回答by Fraser
You should look at creating a 'service' as this is an application without a form. See http://support.microsoft.com/kb/816169
您应该考虑创建一个“服务”,因为这是一个没有表单的应用程序。请参阅http://support.microsoft.com/kb/816169
回答by Noldorin
Why can't you just pass the form when you call Application.Run
? Given that it's clearly a blocking call, on what event do you want to show the form? Just calling form.Show()
should be enough.
为什么你不能在你打电话时传递表格Application.Run
?鉴于这显然是一个阻塞调用,您想在什么事件上显示表单?打电话form.Show()
应该就够了。
回答by Zach Johnson
You can create a class that inherits from System.Windows.Forms.NativeWindow
(which provides basic message loop capability) and reference the Handle
property in its constructor to create its handle and hook it into the message loop. Once you call Application.Run
, you will be able to process messages from it.
您可以创建一个继承自的类System.Windows.Forms.NativeWindow
(它提供基本的消息循环功能)并Handle
在其构造函数中引用该属性以创建其句柄并将其挂接到消息循环中。一旦您调用Application.Run
,您将能够处理来自它的消息。
回答by Tarion
The best way is to use the following 1-2 lines in the constuctor:
最好的方法是在构造函数中使用以下 1-2 行:
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false; // This is optional
You can even set the Minimized property in the VS Property window.
您甚至可以在 VS 属性窗口中设置最小化属性。
回答by Tarion
public partial class Form1 : Form
{
private bool _isApplicationRun;
public Form1(bool applicationRun)
{
InitializeComponent();
_isApplicationRun = applicationRun;
}
protected override void SetVisibleCore(bool value)
{
if (_isApplicationRun)
{
_isApplicationRun = false;
base.SetVisibleCore(false);
return;
}
base.SetVisibleCore(value);
}
}
static class Program
{
[STAThread]
static void Main()
{
Application.Run(new Form1(true));
}
}
回答by kami
I solved the problem like this:
我解决了这样的问题:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Main main = new Main();
Application.Run();
//Application.Run(new Main());
}
This code resides in the Program.cs file, and you can see the original Application.Run method call commented out. I just create a Main class object (my main form class is named Main) and start application message loop w/o any parameters. This starts the application, initializes any form components but doesn't show the form.
此代码位于 Program.cs 文件中,您可以看到注释掉的原始 Application.Run 方法调用。我只是创建一个 Main 类对象(我的主窗体类名为 Main)并启动不带任何参数的应用程序消息循环。这将启动应用程序,初始化任何表单组件但不显示表单。
Note: you have to have some method to get your window showing (like system tray icon, hotkey or timer or anything you might like).
注意:你必须有一些方法来显示你的窗口(比如系统托盘图标、热键或计时器或任何你可能喜欢的东西)。
回答by Paul Keister
Using Kami's answer as an inspiration, I created a more complete concept. If you use this solution, don't ever show the hidden window. If you do, the user might close it and then you've lost the ability to control the application exit in an orderly way. This approach can be used to manage a Timer, NotifyIcon, or any other component that is happy living on an invisible form.
以 Kami 的回答为灵感,我创建了一个更完整的概念。如果您使用此解决方案,请不要显示隐藏窗口。如果这样做,用户可能会关闭它,然后您就失去了以有序方式控制应用程序退出的能力。这种方法可用于管理 Timer、NotifyIcon 或任何其他以隐形形式生活的组件。
using System;
using System.Windows.Forms;
namespace SimpleHiddenWinform
{
internal class HiddenForm : Form
{
private Timer _timer;
private ApplicationContext _ctx;
public HiddenForm(ApplicationContext ctx)
{
_ctx = ctx;
_timer = new Timer()
{
Interval = 5000, //5 second delay
Enabled = true
};
_timer.Tick += new EventHandler(_timer_Tick);
}
void _timer_Tick(object sender, EventArgs e)
{
//tell the main message loop to quit
_ctx.ExitThread();
}
}
static class Program
{
[STAThread]
static void Main()
{
var ctx = new ApplicationContext();
var frmHidden = new HiddenForm(ctx);
//pass the application context, not the form
Application.Run(ctx);
}
}
}
回答by Maris B.
I know this is old question, but it ranks well in google, so I will provide my solution anyway.
我知道这是个老问题,但它在谷歌中排名很好,所以无论如何我都会提供我的解决方案。
I do two things:
我做两件事:
private void Form_Load(object sender, EventArgs e)
{
Opacity = 0;
}
private void Form_Shown(object sender, EventArgs e)
{
Visible = false;
Opacity = 100;
}