2008-11-12 35 views
16

Tôi đã làm theo mẹo thường được liên kết để giảm ứng dụng vào khay hệ thống: http://www.developer.com/net/csharp/article.php/3336751 Bây giờ nó hoạt động, nhưng vẫn còn một vấn đề: ứng dụng của tôi được hiển thị khi nó bắt đầu; Tôi muốn nó bắt đầu trực tiếp trong systray. Tôi cố gắng giảm thiểu và ẩn nó trong sự kiện Load, nhưng nó không có gì.Đặt chương trình vào khay hệ thống khi khởi động

Chỉnh sửa: Tôi có thể, như áp phích được đề xuất, sửa đổi thuộc tính phím tắt, nhưng tôi muốn sử dụng mã: Tôi không có toàn quyền kiểm soát mọi máy tính được cài đặt mềm.

Tôi không muốn xóa hoàn toàn từ mọi nơi ngoại trừ systray, tôi chỉ muốn nó bắt đầu thu nhỏ.

Bất kỳ ý tưởng nào?

Cảm ơn

Trả lời

25

Trong chương trình chính của bạn, bạn có thể có một dòng biểu mẫu:

Application.Run(new Form1()); 

Điều này sẽ buộc biểu mẫu được hiển thị. Bạn sẽ cần phải tạo ra các hình thức nhưng không vượt qua nó để Application.Run:

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

Lưu ý rằng chương trình sẽ bây giờ không chấm dứt cho đến khi bạn gọi Application.ExitThread(). Tốt nhất nên làm điều này từ một người xử lý cho sự kiện FormClosed.

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

Nếu bạn đang sử dụng một NotifyIcon, hãy thử thay đổi ShowInTaskbar false.

Để xóa nó khỏi màn hình Alt + Tab, hãy thử thay đổi kiểu viền cửa sổ của bạn; Tôi tin rằng một số các công cụ của cửa sổ phong cách không xuất hiện ...

cái gì đó như:

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()); 
    } 
} 

Nếu nó vẫn xuất hiện trong Alt + Tab, bạn có thể thay đổi phong cách cửa sổ thông qua p/gọi (một chút tin tặc):

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

đây là cách bạn làm điều đó

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

Vì đây đã được gắn thẻ với vb.net, đây là những gì tôi đã làm trong một ứng dụng Windows Service và điều khiển Tôi vừa hoàn thành, Thêm một mô-đun mã để các dự án, Thiết lập các NotifyIcon và nó Menu nội dung liên quan trong Sub Main(), rồi đặt đối tượng khởi động của ứng dụng thành Sub Main() thay cho 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 

Trong Config(), tôi tạo một thể hiện biểu mẫu của tôi và hiển thị nó.

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

End Sub 
0

Ở đây bạn đi:

Tạo 2 lớp, 1 mà được thừa hưởng từ ApplicationContext. Cái kia chỉ chứa một thói quen chính. Tôi đã thực hiện một ví dụ mà có một hình thức và một informicon rằng khi nhấp đúp mang lên các hình thức và trở lại một lần nữa.

Hãy nhớ để thiết lập "Sub Main" như đối tượng khởi động của bạn trong môi trường Dự án của tôi và trỏ đến một thực file * .ico thay vì f: \ TP.ico .. :)

Mã nên dĩ nhiên được nhồi với mã xử lý lỗi thích hợp.

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 

Class2:

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 
Các vấn đề liên quan