C# 自訂圖示顯示在右下角工作列

  • 556
  • 0
  • C#
  • 2015-12-01

摘要:C# 自訂圖示顯示在右下角工作列

<<Program.cs>>

    static class Program
    {
        static Mutex mutex = new Mutex(true, "{8F6F0AC4-B9A1-45fd-A8CF-72F04E6BDE8F}");

        /// 
/// The main entry point for the application. ///

 

[STAThread] static void Main() { if (mutex.WaitOne(TimeSpan.Zero, true)) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MDIMain()); mutex.ReleaseMutex(); } else { // send our Win32 message to make the currently running instance // jump on top of all the other windows NativeMethods.PostMessage( (IntPtr)NativeMethods.HWND_BROADCAST, NativeMethods.WM_SHOWME, IntPtr.Zero, IntPtr.Zero); } } internal class NativeMethods { public const int HWND_BROADCAST = 0xffff; public static readonly int WM_SHOWME = RegisterWindowMessage("WM_SHOWME"); [DllImport("user32")] public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam); [DllImport("user32")] public static extern int RegisterWindowMessage(string message); } }

 

<<MDIMain.cs>>

 

 

 

 

 

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace notifyIcon_Sample
{
    public partial class MDIMain : Form
    {
        public MDIMain()
        {
            InitializeComponent();
        }

        #region "在畫面上最前面顯示"
        protected override void WndProc(ref Message m)
        {
            if (m.Msg ==Program.NativeMethods.WM_SHOWME)
            {
                ShowMe();
            }
            base.WndProc(ref m);
        }
        private void ShowMe()
        {
            if (WindowState == FormWindowState.Minimized)
            {
                WindowState = FormWindowState.Normal;
            }
            // get our current "TopMost" value (ours will always be false though)
            bool top = TopMost;
            // make our form jump to the top of everything
            TopMost = true;
            // set it back to whatever it was
            TopMost = top;
        }
        #endregion

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

        private void MDIMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.WindowState != FormWindowState.Minimized)
            {
                e.Cancel = true;
                this.WindowState = FormWindowState.Minimized;                
                notifyIcon1.Tag = string.Empty;
                notifyIcon1.ShowBalloonTip(1500, this.Text, "程式並未結束,要結束請在圖示上按右鍵,選取結束功能", ToolTipIcon.Info);
            }
        }


        /// 

 

/// 顯示出主畫面 ///

 

private void ShowForm() { if (this.WindowState == FormWindowState.Minimized) { //如果目前是縮小狀態,才要回覆成一般大小的視窗 this.Show(); this.WindowState = FormWindowState.Normal; } // Activate the form. this.Activate(); this.Focus(); } private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) { ShowForm(); } private void notifyIcon1_BalloonTipClicked(object sender, EventArgs e) { string notifyTag = (string)notifyIcon1.Tag; if (string.IsNullOrEmpty(notifyTag)) { //只是關閉form的訊息 ShowForm(); } } private void tsmOpen_Click(object sender, EventArgs e) { this.Show(); this.WindowState = FormWindowState.Normal; } private void tsmExit_Click(object sender, EventArgs e) { DialogResult ext = MessageBox.Show("您確定要結束系統嗎?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (ext == DialogResult.Yes) { GC.Collect(); Application.Exit(); } } private void tsmAboutMigo_Click(object sender, EventArgs e) { ProcessStartInfo sInfo = new ProcessStartInfo("http://www.dotblogs.com.tw/jean/Default.aspx"); Process.Start(sInfo); } } }

 

notifyIcon_Sample.zip