문제
의도 한대로 작동하기 위해서만 Notifyon 만 있으면되는 프로그램이 있습니다. 그래서 나는 프로그램이 시작될 때 숨길 주요 양식을 얻으려고 노력했습니다.
frmmain_load에서 나는 둘 다 시도했다
this.Hide();
this.Visible = false;
성공없이.
그들은 Notifyon_mouseclick-Method와 같이 다른 방법으로 작동하지만 부하시 숨기기를 원합니다.
나는 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가 생성 된 프로그램이있는 경우 쉬운 방법이 있습니다 .CS 파일 :
[STAThread]
static void Main()
{
Application.EnableVisualStyles ();
Application.SetCompatibleTextRenderingDefault (false);
Application.Run (new MainForm ());
}
전화의 단순한 사실 Run
실제로 양식을 볼 수 있습니다. 양식의 속성에서 다음을 시도하십시오.
- 세트
WindowState
에게Minimized
- 세트
ShowInTaskbar
에게false
이것은 트릭을해야합니다!
양식에 Show 또는 ShowDialog를 호출하지 마십시오. 응용 프로그램을 가질 수 있습니다. 런에 맞는 클래스를 대상으로 한 다음 양식을 인스턴스화하고 Notifyicon 인스턴스를 표시하거나 생성하지 않고 모든 것을 처리하지 않습니다.
form_shown 이벤트에서 hide = true를 넣을 수도 있습니다. 이벤트는로드 이벤트 후에 한 번만 해고된다고 생각합니다. 양식에 많은 컨트롤이 있거나 컴퓨터가 느려지면 Alittle Flicker를 볼 수 있습니다.
나는 단지이 속성을 바꾸었다.Application.OpenForms["Form1"].Opacity = 0;
프로그램이 실행할 양식이 필요하지 않은 경우 가장 좋은 방법은 양식이 전혀없는 것입니다.
프로그램 코드에서 Notifyicon을 설정하고 일부 값을 설정하거나 메소드를 호출하여 프로그램을 종료 할 때까지 루프를 입력하십시오.
이 예에서 설정에서 UserExitCalled
참으로 (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
}
}