Program Club

C # WinForms 앱 트레이를 최소화하는 적절한 방법은 무엇입니까?

proclub 2020. 12. 5. 10:50
반응형

C # WinForms 앱 트레이를 최소화하는 적절한 방법은 무엇입니까?


WinForms 앱을 시스템 트레이로 최소화하는 적절한 방법은 무엇입니까?

참고 : 시스템 트레이로 최소화하십시오 . 작업 표시 줄의 오른쪽에 시계 옆에 있습니다. 창에서 "빼기"버튼을 눌렀을 때 발생하는 작업 표시 줄 최소화에 대해 묻는 것이 아닙니다.

"최소화, ShowInTaskbar = false로 설정 한 다음 NotifyIcon 표시"와 같은 해커 솔루션을 보았습니다.

앱이 다른 앱과 같이 트레이에 최소화되지 않는 것처럼 보이기 때문에 이와 같은 솔루션은 해커입니다. 코드는 다른 문제 중에서 언제 ShowInTaskbar = true를 설정해야하는지 감지해야합니다.

이를 수행하는 적절한 방법은 무엇입니까?


실제로 네이티브 winform의 트레이에 이러한 형태의 애니메이션을 수행하는 관리되는 방법은 없지만 shell32.dll을 P / Invoke하여 수행 할 수 있습니다.

여기에 몇 가지 좋은 정보가 있습니다 (게시물이 아닌 댓글에 있음).

http://blogs.msdn.com/jfoscoding/archive/2005/10/20/483300.aspx

그리고 여기 C ++에 있습니다.

http://www.codeproject.com/KB/shell/minimizetotray.aspx

이를 사용하여 C # 버전에 대해 Pinvoke 할 항목을 파악할 수 있습니다.


this.WindowState = FormWindowState.Minimized  

그것은 그것을 수행하는 방식이며 대부분의 경우 나에게 괜찮아 보입니다. 유일한 시간은 시작시 호출하는 경우가 있습니다. 가끔 이상한 점이 있기 때문에 대부분의 사람들이 ShowInTaskbar = false로 설정하고 양식도 숨길 것입니다.

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.windowstate.aspx


도움이됩니다.

public partial class Form1 : Form
{
    public static bool Close = false;
    Icon[] images;
    int offset = 0;

    public Form1()
    {
        InitializeComponent();

        notifyIcon1.BalloonTipText = "My application still working...";
        notifyIcon1.BalloonTipTitle = "My Sample Application";
        notifyIcon1.BalloonTipIcon = ToolTipIcon.Info; 
    }

    private void Form1_Resize(object sender, EventArgs e)
    {
        if (FormWindowState.Minimized == WindowState)
        {
            this.Hide();
            notifyIcon1.ShowBalloonTip(500);
            //WindowState = FormWindowState.Minimized;
        }
    }

    private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        this.Show();
        notifyIcon1.ShowBalloonTip(1000);
        WindowState = FormWindowState.Normal;
    }

    private void maximizeToolStripMenuItem_Click(object sender, EventArgs e)
    {
        this.Show();
        WindowState = FormWindowState.Normal;
    }

    private void closeToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Close = true;
        this.Close();  
    }

    // Handle Closing of the Form.
    protected override void OnClosing(CancelEventArgs e)
    {
        if (Close)
        {
            e.Cancel = false;
        }
        else
        {
            WindowState = FormWindowState.Minimized;
            e.Cancel = true;
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // Load the basic set of eight icons.
        images = new Icon[5];
        images[0] = new Icon("C:\\icon1.ico");
        images[1] = new Icon("C:\\icon2.ico");
        images[2] = new Icon("C:\\icon3.ico");
        images[3] = new Icon("C:\\icon4.ico");
        images[4] = new Icon("C:\\icon5.ico");
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        // Change the icon.
        // This event handler fires once every second (1000 ms).
        if (offset < 5)
        {
            notifyIcon1.Icon = images[offset];
            offset++;
        }
        else
        {
            offset = 0;
        }
    }
}

이 코드는 테스트되었으며 많은 옵션을 지원합니다.

추가 정보 : http://code.msdn.microsoft.com/TheNotifyIconExample


Update: Looks like I posted too soon. I was also using the below hack for a tool of mine. Waiting for the right solution for this..........

You can use Windows.Forms.NotifyIcon for this. http://msdn.microsoft.com/en-us/library/system.windows.forms.notifyicon.aspx

Add NotifyIcon to the form, set some properties and you are done.

        this.ShowIcon = false;//for the main form
        this.ShowInTaskbar = false;//for the main form
        this.notifyIcon1.Visible = true;//for notify icon
        this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));//set an icon for notifyicon

    private void Form1_Shown(object sender, EventArgs e)
    {
        this.Hide();
    }

Similar as above...

I have a resize event handler that is triggered whenever the window gets resized (Maximized, minimized, etc.)...

    public form1()
    {
       Initialize Component();

       this.Resize += new EventHanlder(form1_Resize);
    } 


    private void form1_Resize(object sender, EventArgs e)
    {
       if (this.WindowState == FormWindowState.Minimized && minimizeToTrayToolStripMenuItem.Checked == true)
       {
             NotificationIcon1.Visible = true;
             NotificationIcon1.BalloonTipText = "Tool Tip Text"
             NotificationIcon1.ShowBalloonTip(2);  //show balloon tip for 2 seconds
             NotificationIcon1.Text = "Balloon Text that shows when minimized to tray for 2 seconds";
             this.WindowState = FormWindowState.Minimized;
             this.ShowInTaskbar = false;
       }
    }

This allows the user to select whether or not they want to minimize to tray via a menubar. They can go to Windows -> and click on Minimize to Tray. If this is checked, and the window is being resized to Minimized, then it will minimize to the tray. Works flawlessly for me.


In the constructor of the Form:

this.Resize += new EventHandler(MainForm_Minimize);

Then use this Event Handler method:

    private void MainForm_Minimize(object sender, EventArgs e)
    {
        if(this.WindowState == FormWindowState.Minimized)
            Hide();
    }

If you are having problems getting this to work, check that you have

this.Resize += new System.EventHandler(this.Form1_Resize);

in the fom1.designer.cs

참고URL : https://stackoverflow.com/questions/46918/whats-the-proper-way-to-minimize-to-tray-a-c-sharp-winforms-app

반응형