题
我有一个程序,它仅需要一个NotifyIcon的按预期方式工作。所以我一直试图让主要形式隐藏在程序启动时。
在frmMain_Load,我试图既
this.Hide();
this.Visible = false;
没有成功。
他们其他方法的工作,就像在NotifyIcon_MouseClick法,但我想它隐藏在负载。
我在另外一个问题在这里看到的,在SO其中Matias的建议这样的:
BeginInvoke(new MethodInvoker(delegate
{
Hide();
}));
这个工作,但是当我启动该程序,我可以看到闪烁的形式实快。这是聊胜于无,但我不知道是否有任何更好的解决了这一点。
感谢。
解决方案
// In Your Program.cs Convert This
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
// To This
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 TheForm = new Form1();
Application.Run();
}
// Call Application.Exit() From Anywhere To Stop Application.Run() Message Pump and Exit Application
其他提示
有一个简单的方法,如果你的程序的Visual Studio生成的Program.cs文件默认:
[STAThread]
static void Main()
{
Application.EnableVisualStyles ();
Application.SetCompatibleTextRenderingDefault (false);
Application.Run (new MainForm ());
}
调用Run
会,确实使窗体可见的简单的事实。试着做你的窗体的属性如下:
- 设置
WindowState
到Minimized
- 设置
ShowInTaskbar
到false
醇>
这应该做的伎俩!
不要调用显示或ShowDialog的表单上,你可以有你Application.Run自订目标类,然后实例化一个形式,并且不显示或创建一个NotifyIcon的实例,并从那里处理一切。
您也可以把this.hide = true在form_shown事件。我认为,事件一度仅和负载事件之后被解雇。您可能会看到小有一点闪烁但如果您的形式有很多的控制和/或电脑很慢。
我所做的只是更改此属性:
Application.OpenForms["Form1"].Opacity = 0;
如果你的程序不需要的形式来运行,那么最好的方法就是不要有一个表格都没有。结果
直到要通过设置一定的价值,或者调用一些方法来退出程序设置你的NotifyIcon的程序代码,并进入一个循环。结果
在这个例子中设置UserExitCalled
为true(Program.UserExitCalled = true
)将导致程序退出。
下面是一个简单的例子:
static class Program {
internal static Boolean UserExitCalled;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// Setup your tray icon here
while (!UserExitCalled) {
Application.DoEvents(); // Process windows messages
Thread.Sleep(1);
}
return;
}
}
下面从我的系统托盘应用程序作为一个工作示例一个完整的程序类。
// *********************************************************************
// [DCOM Productions .NET]
// [DPDN], [Visual Studio Launcher]
//
// THIS FILE IS PROVIDED "AS-IS" WITHOUT ANY WARRANTY OF ANY KIND. ANY
// MODIFICATIONS TO THIS FILE IN ANY WAY ARE YOUR SOLE RESPONSIBILITY.
//
// [Copyright (C) DCOM Productions .NET All rights reserved.]
// *********************************************************************
namespace VisualStudioLauncher
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading;
using VisualStudioLauncher.Common.Objects;
using VisualStudioLauncher.Forms;
using System.Drawing;
using VisualStudioLauncher.Common.Data;
using System.IO;
static class Program
{
#region Properties
private static ProjectLocationList m_ProjectLocationList;
/// <summary>
/// Gets or Sets the ProjectsLocationList
/// </summary>
public static ProjectLocationList ProjectLocationList
{
get
{
return m_ProjectLocationList;
}
set
{
m_ProjectLocationList = value;
}
}
private static ShellProcessList m_ShellProcessList = null;
/// <summary>
/// Gets or Sets the ShellProcessList
/// </summary>
public static ShellProcessList ShellProcessList
{
get
{
return m_ShellProcessList;
}
set
{
m_ShellProcessList = value;
}
}
private static NotifyIcon m_TrayIcon;
/// <summary>
/// Gets the programs tray application.
/// </summary>
public static NotifyIcon TrayIcon
{
get
{
return m_TrayIcon;
}
}
private static bool m_UserExitCalled;
/// <summary>
/// Gets a value indicating whether the user has called for an Application.Exit
/// </summary>
public static bool UserExitCalled
{
get
{
return m_UserExitCalled;
}
set
{
m_UserExitCalled = value;
}
}
// TODO: Finish implementation, then use this for real.
private static ApplicationConfiguration m_ApplicationConfiguration = null;
/// <summary>
/// Gets the application configuration
/// </summary>
public static ApplicationConfiguration ApplicationConfiguration
{
get
{
if (m_ApplicationConfiguration == null)
m_ApplicationConfiguration = ApplicationConfiguration.LoadConfigSection(@"./settings.config");
return m_ApplicationConfiguration;
}
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
if (args.Length > 0)
{
if (args[0].ToLower() == "-rmvptr")
{
for (int i = 1; i < args.Length; i++) {
try {
if (File.Exists(Application.StartupPath + @"\\" + args[i])) {
File.Delete(Application.StartupPath + @"\\" + args[i]);
}
}
catch { /* this isn't critical, just convenient */ }
}
}
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
SplashForm splashForm = new SplashForm();
splashForm.Show();
while (!UserExitCalled)
{
Application.DoEvents();
Thread.Sleep(1);
}
if (m_TrayIcon != null)
{
m_TrayIcon.Icon = null;
m_TrayIcon.Visible = false;
m_TrayIcon.Dispose();
GC.Collect();
}
}
#region System Tray Management
public static void SetupTrayIcon()
{
m_TrayIcon = new NotifyIcon();
m_TrayIcon.Text = Resources.UserInterfaceStrings.ApplicationName;
m_TrayIcon.Visible = false; // This will be set visible when the context menu is generated
m_TrayIcon.MouseDoubleClick += new MouseEventHandler(m_TrayIcon_MouseDoubleClick);
if (Orcas.IsInstalled)
{
m_TrayIcon.Icon = Orcas.Icon;
}
else if (Whidbey.IsInstalled) {
m_TrayIcon.Icon = Whidbey.Icon;
}
else {
m_TrayIcon.Icon = SystemIcons.Warning;
m_TrayIcon.Text = "Visual Studio is not installed. VSL cannot run properly.";
}
}
static void m_TrayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
{
return;
}
SettingsForm settingsForm = new SettingsForm();
settingsForm.Show();
}
#endregion
}
}