2008-09-16 15 views
9

कोई भी कृपया अच्छा कोड उदाहरण vb.net/c# कोड का सुझाव दे सकता है ताकि सिस्टम को ट्रे में छोटा कर दिया जा सके।कम से कम सिस्टम ट्रे में .net एप्लिकेशन कैसे डालें?

+2

यह http://stackoverflow.com/questions/46918/whats-the-proper-way-to-minimize-to-tray-ac-winforms-app का डुप्लिकेट प्रतीत होता है। –

उत्तर

18

अपने फ़ॉर्म के एक NotifyIcon नियंत्रण जोड़ें, उसके बाद निम्न कोड का उपयोग करें:

private void frm_main_Resize(object sender, EventArgs e) 
    { 
     if (this.WindowState == FormWindowState.Minimized) 
     { 
      this.ShowInTaskbar = false; 
      this.Hide(); 
      notifyIcon1.Visible = true; 
     } 
    } 

    private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) 
    { 
     this.Show(); 
     this.WindowState = FormWindowState.Normal; 
     this.ShowInTaskbar = true; 
     notifyIcon1.Visible = false; 
    } 

आप ShowInTaskbar गुण सेट करने की जरूरत नहीं हो सकती है।

+0

आपका नमूना काफी अपूर्ण है। – FlySwat

+0

क्या आप विस्तृत कर सकते हैं? –

+0

आप फॉर्म से घोषणाओं को याद करते हैं, अधिसूचना आइकन नियंत्रण, और आपके ओवरराइड निपटान विधि को खोना :) – FlySwat

2

आप NotifyIcon नामक एक अंतर्निहित नियंत्रण का लाभ उठा सकते हैं। दिखाए जाने पर यह एक ट्रे आइकन बनाता है। @ फिलिप में एक कोड उदाहरण है जो कुछ हद तक पूरा हो गया है।

वहाँ एक पकड़ लिया, हालांकि है:

आप NotifyIcon पर निपटान कॉल करने के लिए मुख्य रूप निपटान विधि अपने अनुप्रयोगों को ओवरराइड चाहिए अन्यथा यह आवेदन बाहर निकलता है के बाद अपने ट्रे में रहना होगा।

public void Form_Dispose(object sender, EventArgs e) 
{ 
    if (this.Disposing) 
     notifyIcon1.Dispose(); 
} 

ऐसा कुछ।

+0

भागने के लिए धन्यवाद। – bugBurger

+2

यदि आप डिज़ाइनर का उपयोग करके फॉर्म पर अधिसूचना आइकन डालते हैं तो यह आवश्यक नहीं होना चाहिए - वीएस-जेनरेट डिस्प्ले() घटक को कॉल करेगा।() को() जो कंटेनर के रूप में बनाए गए सभी घटकों को निपटाना चाहिए। –

0

आप अपने फॉर्म में नोटिफ़िकॉन जोड़कर और फ़ॉर्म के आकार बदलने वाले ईवेंट को संभालने के द्वारा ऐसा कर सकते हैं। ट्रे से वापस पाने के लिए NotifyIcon की डबल-क्लिक ईवेंट को संभाल लें।

आप एक छोटे से एनीमेशन जोड़ना चाहते हैं आप यह कर सकते हैं भी ...

1) निम्नलिखित मॉड्यूल जोड़ें:

Module AnimatedMinimizeToTray 
Structure RECT 
    Public left As Integer 
    Public top As Integer 
    Public right As Integer 
    Public bottom As Integer 
End Structure 

Structure APPBARDATA 
    Public cbSize As Integer 
    Public hWnd As IntPtr 
    Public uCallbackMessage As Integer 
    Public uEdge As ABEdge 
    Public rc As RECT 
    Public lParam As IntPtr 
End Structure 

Enum ABMsg 
    ABM_NEW = 0 
    ABM_REMOVE = 1 
    ABM_QUERYPOS = 2 
    ABM_SETPOS = 3 
    ABM_GETSTATE = 4 
    ABM_GETTASKBARPOS = 5 
    ABM_ACTIVATE = 6 
    ABM_GETAUTOHIDEBAR = 7 
    ABM_SETAUTOHIDEBAR = 8 
    ABM_WINDOWPOSCHANGED = 9 
    ABM_SETSTATE = 10 
End Enum 

Enum ABNotify 
    ABN_STATECHANGE = 0 
    ABN_POSCHANGED 
    ABN_FULLSCREENAPP 
    ABN_WINDOWARRANGE 
End Enum 

Enum ABEdge 
    ABE_LEFT = 0 
    ABE_TOP 
    ABE_RIGHT 
    ABE_BOTTOM 
End Enum 

Public Declare Function SHAppBarMessage Lib "shell32.dll" Alias "SHAppBarMessage" (ByVal dwMessage As Integer, ByRef pData As APPBARDATA) As Integer 
Public Const ABM_GETTASKBARPOS As Integer = &H5& 
Public Const WM_SYSCOMMAND As Integer = &H112 
Public Const SC_MINIMIZE As Integer = &HF020 

Public Sub AnimateWindow(ByVal ToTray As Boolean, ByRef frm As Form, ByRef icon As NotifyIcon) 
    ' get the screen dimensions 
    Dim screenRect As Rectangle = Screen.GetBounds(frm.Location) 

    ' figure out where the taskbar is (and consequently the tray) 
    Dim destPoint As Point 
    Dim BarData As APPBARDATA 
    BarData.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(BarData) 
    SHAppBarMessage(ABMsg.ABM_GETTASKBARPOS, BarData) 
    Select Case BarData.uEdge 
     Case ABEdge.ABE_BOTTOM, ABEdge.ABE_RIGHT 
      ' Tray is to the Bottom Right 
      destPoint = New Point(screenRect.Width, screenRect.Height) 

     Case ABEdge.ABE_LEFT 
      ' Tray is to the Bottom Left 
      destPoint = New Point(0, screenRect.Height) 

     Case ABEdge.ABE_TOP 
      ' Tray is to the Top Right 
      destPoint = New Point(screenRect.Width, 0) 

    End Select 

    ' setup our loop based on the direction 
    Dim a, b, s As Single 
    If ToTray Then 
     a = 0 
     b = 1 
     s = 0.05 
    Else 
     a = 1 
     b = 0 
     s = -0.05 
    End If 

    ' "animate" the window 
    Dim curPoint As Point, curSize As Size 
    Dim startPoint As Point = frm.Location 
    Dim dWidth As Integer = destPoint.X - startPoint.X 
    Dim dHeight As Integer = destPoint.Y - startPoint.Y 
    Dim startWidth As Integer = frm.Width 
    Dim startHeight As Integer = frm.Height 
    Dim i As Single 
    For i = a To b Step s 
     curPoint = New Point(startPoint.X + i * dWidth, startPoint.Y + i * dHeight) 
     curSize = New Size((1 - i) * startWidth, (1 - i) * startHeight) 
     ControlPaint.DrawReversibleFrame(New Rectangle(curPoint, curSize), frm.BackColor, FrameStyle.Thick) 
     System.Threading.Thread.Sleep(15) 
     ControlPaint.DrawReversibleFrame(New Rectangle(curPoint, curSize), frm.BackColor, FrameStyle.Thick) 
    Next 


    If ToTray Then 
     ' hide the form and show the notifyicon 
     frm.Hide() 
     icon.Visible = True 
    Else 
     ' hide the notifyicon and show the form 
     icon.Visible = False 
     frm.Show() 
    End If 

End Sub 
End Module 

2) अपने फार्म के लिए एक NotifyIcon जोड़ें निम्नलिखित जोड़ें :

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) 
    If m.Msg = WM_SYSCOMMAND AndAlso m.WParam.ToInt32() = SC_MINIMIZE Then 
     AnimateWindow(True, Me, NotifyIcon1) 
     Exit Sub 
    End If 
    MyBase.WndProc(m) 
End Sub 

Private Sub NotifyIcon1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles NotifyIcon1.DoubleClick 
    AnimateWindow(False, Me, NotifyIcon1) 
End Sub 
संबंधित मुद्दे