2008-11-12 85 views
16

我遵循常用鏈接提示,以減少應用程序到系統托盤中:http://www.developer.com/net/csharp/article.php/3336751現在可以工作,但仍然存在問題:我的應用程序在啓動時顯示;我希望它直接在系統托盤中啓動。我試圖儘量減少並隱藏它在Load事件中,但它什麼都不做。在啓動時在系統托盤中放入一個程序

編輯:作爲海報建議,我可以修改快捷方式屬性,但我寧願使用代碼:我沒有完全控制安裝軟件的每臺計算機。

我不想從除了系統托盤以外的任何地方完全刪除它,我只是希望它從最小化開始。

任何想法?

感謝

回答

25

在你的主程序你可能有一個線形式:

Application.Run(new Form1()); 

這將強制顯示窗體。您將需要創建的形式,但不Application.Run

Form1 form = new Form1(); 
Application.Run(); 

注意,該方案現在將不會終止,直到你叫Application.ExitThread()。最好從FormClosed事件的處理程序執行此操作。

private void Form1_FormClosed(object sender, FormClosedEventArgs e) 
{ 
    Application.ExitThread(); 
} 
3

如果您使用的是NotifyIcon,嘗試改變ShowInTaskbar爲false。

要從Alt + Tab屏幕中將其刪除,請嘗試更改窗口邊框樣式;我相信一些工具窗口風格不會出現的......

類似:

using System; 
using System.Windows.Forms; 
class MyForm : Form 
{ 
    NotifyIcon sysTray; 

    MyForm() 
    { 
     sysTray = new NotifyIcon(); 
     sysTray.Icon = System.Drawing.SystemIcons.Asterisk; 
     sysTray.Visible = true; 
     sysTray.Text = "Hi there"; 
     sysTray.MouseClick += delegate { MessageBox.Show("Boo!"); }; 

     ShowInTaskbar = false; 
     FormBorderStyle = FormBorderStyle.SizableToolWindow; 
     Opacity = 0; 
     WindowState = FormWindowState.Minimized; 
    } 

    [STAThread] 
    static void Main() 
    { 
     Application.EnableVisualStyles(); 
     Application.Run(new MyForm()); 
    } 
} 

如果仍出現在Alt + Tab鍵,可以通過改變p中的窗口樣式/調用(有點hackier):

protected override void OnLoad(EventArgs e) 
{ 
    base.OnLoad(e); 
    IntPtr handle = this.Handle; 
    int currentStyle = GetWindowLong(handle, GWL_EXSTYLE); 
    SetWindowLong(handle, GWL_EXSTYLE, currentStyle | WS_EX_TOOLWINDOW); 
} 
private const int GWL_EXSTYLE = -20, WS_EX_TOOLWINDOW = 0x00000080; 
[System.Runtime.InteropServices.DllImport("user32.dll")] 
private static extern int SetWindowLong(IntPtr window, int index, int value); 
[System.Runtime.InteropServices.DllImport("user32.dll")] 
private static extern int GetWindowLong(IntPtr window, int index); 
13

你這是怎麼做到這一點

static class Program 
{ 
    [STAThread] 
    static void Main() 
    { 
     NotifyIcon icon = new NotifyIcon(); 
     icon.Icon = System.Drawing.SystemIcons.Application; 
     icon.Click += delegate { MessageBox.Show("Bye!"); icon.Visible = false; Application.Exit(); }; 
     icon.Visible = true; 
     Application.Run(); 
    } 
} 
1

,因爲這是標有vb.net,這裏就是我的Windows服務和控制器應用程序,我剛說完沒有,添加一個代碼模塊的項目,設置NotifyIcon的,它是在子主關聯的上下文菜單(),然後將應用程序的啓動對象設置爲Sub Main()而不是Form。

Public mobNotifyIcon As NotifyIcon 
Public WithEvents mobContextMenu As ContextMenu 

Public Sub Main() 

    mobContextMenu = New ContextMenu 
    SetupMenu() 
    mobNotifyIcon = New NotifyIcon() 
    With mobNotifyIcon 
     .Icon = My.Resources.NotifyIcon 
     .ContextMenu = mobContextMenu 
     .BalloonTipText = String.Concat("Monitor the EDS Transfer Service", vbCrLf, "Right click icon for menu") 
     .BalloonTipIcon = ToolTipIcon.Info 
     .BalloonTipTitle = "EDS Transfer Monitor" 
     .Text = "EDS Transfer Service Monitor" 
     AddHandler .MouseClick, AddressOf showBalloon 
     .Visible = True 
    End With 
    Application.Run() 
End Sub 

Private Sub SetupMenu() 
    With mobContextMenu 

     .MenuItems.Add(New MenuItem("Configure", New EventHandler(AddressOf Config))) 
     .MenuItems.Add("-") 
     .MenuItems.Add(New MenuItem("Start", New EventHandler(AddressOf StartService))) 
     .MenuItems.Add(New MenuItem("Stop", New EventHandler(AddressOf StopService))) 
     .MenuItems.Add("-") 
     .MenuItems.Add(New MenuItem("Exit", New EventHandler(AddressOf ExitController))) 
    End With 
    GetServiceStatus() 
End Sub 

在Config()中,我創建了我的窗體的一個實例並顯示它。

Private Sub Config(ByVal sender As Object, ByVal e As EventArgs) 
    Using cs As New ConfigureService 
     cs.Show() 
    End Using 

End Sub 
0

在這裏你去:

創建2班,1從ApplicationContext中繼承。另一個只包含一個Main例程。我做了一個有一個窗體和一個notifyicon的例子,當雙擊時會彈出窗體並返回。

記得設置「副主」在我的項目設置你的啓動對象,並指向一個真正的* .ico文件,而不是F:\ TP.ico .. :)

代碼,當然應該釀具有適當的錯誤處理代碼。

的Class1:

Imports System.threading 
Imports System.Runtime.InteropServices 
Imports System.Windows.Forms 


Public Class Class1 

    <System.STAThread()> _ 
     Public Shared Sub Main() 

     Try 
      System.Windows.Forms.Application.EnableVisualStyles() 
      System.Windows.Forms.Application.DoEvents() 
      System.Windows.Forms.Application.Run(New Class2) 
     Catch invEx As Exception 

      Application.Exit() 

     End Try 


    End Sub 'Main End Class 

等級2:

Imports System.Windows.Forms 
Imports System.drawing 

Public Class Class2 
    Inherits System.Windows.Forms.ApplicationContext 

    Private WithEvents f As New System.Windows.Forms.Form 
    Private WithEvents nf As New System.Windows.Forms.NotifyIcon 

    Public Sub New() 

     f.Size = New Drawing.Size(50, 50) 
     f.StartPosition = FormStartPosition.CenterScreen 
     f.WindowState = Windows.Forms.FormWindowState.Minimized 
     f.ShowInTaskbar = False 
     nf.Visible = True 
     nf.Icon = New Icon("f:\TP.ico") 
    End Sub 


    Private Sub nf_DoubleClick(ByVal sender As Object, ByVal e As EventArgs) Handles nf.DoubleClick 
     If f.WindowState <> Windows.Forms.FormWindowState.Minimized Then 
      f.WindowState = Windows.Forms.FormWindowState.Minimized 
      f.Hide() 
     Else 
      f.WindowState = Windows.Forms.FormWindowState.Normal 
      f.Show() 
     End If 
    End Sub 

    Private Sub f_FormClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs) Handles f.FormClosed 
     Application.Exit() 
    End Sub End Class 
相關問題