最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C# WinForm調(diào)用Shell_NotifyIcon的示例代碼

 更新時間:2020年11月21日 17:13:42   作者:正在緩沖  
這篇文章主要介紹了C# WinForm調(diào)用Shell_NotifyIcon的示例代碼,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
public class InnerClass: Form
 {
  private Shell_NotifyIconEx servicesClass = null; // 接受主CLASS 的實例句柄
  internal InnerClass(Shell_NotifyIconEx _servicesClass)
  {
   servicesClass = _servicesClass;
  }

  private const int WM_LBUTTONDOWN = 0x0201; // 左鍵
  private const int WM_RBUTTONDOWN = 0x204; // 右鍵
  private const int WM_MBUTTONDOWN = 0x207; // 中鍵

  [DllImport("user32.dll", EntryPoint = "TrackPopupMenu")]
  private static extern int TrackPopupMenu( // c# 和vb.net 好象沒有了隨地popup 了,只要請它老人家出馬了
   IntPtr hMenu,
   int wFlags,
   int x,
   int y,
   int nReserved,
   IntPtr hwnd,
   ref RECT lprc
   );

  [StructLayout(LayoutKind.Sequential)]
  private struct RECT
  { // 上面那位用的結(jié)構(gòu),表示前彈出菜單可用的一個范圍大?。ㄒ话闶侨聊欢甲屗?,留著搞游戲或視頻對話之類的朋友指定菜單可用的范圍)
   internal int Left;
   internal int Top;
   internal int Right;
   internal int Bottom;
  }

  protected override void WndProc(ref Message msg)
  {
   if (msg.Msg == servicesClass.WM_NOTIFY_TRAY)
   { // 如果消息相符
    if ((int)msg.WParam == servicesClass.uID)
    { // 并且消息的WParam 相符
     MouseButtons mb =MouseButtons.None;
     if ((int)msg.LParam == WM_LBUTTONDOWN)
     { //如果點擊的是左鍵
      mb =MouseButtons.Left;
     }
     else if ((int)msg.LParam == WM_MBUTTONDOWN)
     { //中鍵
      mb =MouseButtons.Middle;
     }
     else if ((int)msg.LParam == WM_RBUTTONDOWN)
     { //右鍵
      if (servicesClass.contextMenuHwnd != IntPtr.Zero)
      { //如果有定義過菜單關(guān)聯(lián)
       RECT r = new RECT();
       r.Left = Screen.PrimaryScreen.WorkingArea.Left;
       r.Right =Screen.PrimaryScreen.WorkingArea.Right;
       r.Top =Screen.PrimaryScreen.WorkingArea.Top;
       r.Bottom =Screen.PrimaryScreen.WorkingArea.Right;

       TrackPopupMenu(
        servicesClass.contextMenuHwnd,
        2,
       Cursor.Position.X,
       Cursor.Position.Y,
        0,
        servicesClass.formHwnd,
        ref r
        );
      }
      else
      { //如果沒有定義過菜單關(guān)聯(lián)
       mb =MouseButtons.Right;
      }
     }

     if (mb !=MouseButtons.None && servicesClass._delegateOfCallBack != null)
     {
      servicesClass._delegateOfCallBack(mb); // 執(zhí)行回調(diào)
      return;
     }
    }
   }

   base.WndProc(ref msg);
  }
 }
public class Shell_NotifyIconEx
 {
  /// <summary>
  /// ArLi, last fix: 2003.9.12, reference: ArLi.CommonPrj Lib @ http://zpcity.com/arli/
  /// </summary>
  public static readonly System.Version myVersion = new System.Version(1, 2); //版本聲明

  private readonly InnerClass formTmp = null; // 這個很重要,不能放在構(gòu)造里,因為它必須和此實例同等生存期才不會被中止消息循環(huán)
  private readonly IntPtr formTmpHwnd = IntPtr.Zero; // 這是上一行的句柄
  private readonly bool VersionOk = false; // 這是一個由VersionPass 返回的屬性,它允許開發(fā)者檢測當前機子的Shell32.dll(可能在win95 或未知平臺上版本) 合適此組,不符則用.net 自己的notifyicon
  private bool forgetDelNotifyBox = false; // 這是一個私有標志,它允許開發(fā)者在程序退出時忘記調(diào)用DelNotifyBox 來清除圖標時會自動在析構(gòu)里清掉它。

  internal IntPtr formHwnd = IntPtr.Zero; // 這是調(diào)用此組件的主窗口句柄(當前實例有效,可多個icon 不沖突)
  internal IntPtr contextMenuHwnd = IntPtr.Zero; // 這是菜單的句柄(當前實例有效,可多個icon 不沖突)

  internal delegate void delegateOfCallBack(System.Windows.Forms.MouseButtons mb);
  internal delegateOfCallBack _delegateOfCallBack = null;

  public Shell_NotifyIconEx() // 構(gòu)造
  {
   WM_NOTIFY_TRAY += 1; // 消息ID +1,避免多個ICON 消息處理沖突
   uID += 1; // 同上
   formTmp = new InnerClass(this); // 新實例一個消息循環(huán)
   formTmpHwnd = formTmp.Handle; // 新實例句柄
   VersionOk = this.GetShell32VersionInfo() >= 5; // 版本是否合適,此組件由于重點在氣泡提示,它要求Shell32.dll 5.0(ie 5.0) 以上
  }

  ~Shell_NotifyIconEx()
  { // 析構(gòu)
   if (forgetDelNotifyBox) this.DelNotifyBox(); //如果開發(fā)者忘記則清理icon
  }

  #region API_Consts
  internal readonly int WM_NOTIFY_TRAY = 0x0400 + 2001; //readonly 表示只在構(gòu)造可付值
  internal readonly int uID = 5000;

  // 常數(shù)定義,有VC 的可以參見 shellapi.h
  private const int NIIF_NONE = 0x00;
  private const int NIIF_INFO = 0x01;
  private const int NIIF_WARNING = 0x02;
  private const int NIIF_ERROR = 0x03;

  private const int NIF_MESSAGE = 0x01;
  private const int NIF_ICON = 0x02;
  private const int NIF_TIP = 0x04;
  private const int NIF_STATE = 0x08;
  private const int NIF_INFO = 0x10;

  private const int NIM_ADD = 0x00;
  private const int NIM_MODIFY = 0x01;
  private const int NIM_DELETE = 0x02;
  private const int NIM_SETFOCUS = 0x03;
  private const int NIM_SETVERSION = 0x04;

  private const int NIS_HIDDEN = 0x01;
  private const int NIS_SHAREDICON = 0x02;

  private const int NOTIFYICON_OLDVERSION = 0x00;
  private const int NOTIFYICON_VERSION = 0x03;

  [DllImport("shell32.dll", EntryPoint = "Shell_NotifyIcon")]
  private static extern bool Shell_NotifyIcon( // 這位是主角
   int dwMessage,
   ref NOTIFYICONDATA lpData
  );

  /// <summary>
  /// 此API 的作用是當 this.focus() 無效時可以考慮使用,效果很好
  /// </summary>
  /// <param name="hwnd">this.Handle, 當前窗體句柄</param>
  [DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
  public static extern int SetForegroundWindow(
   IntPtr hwnd
  );

  [StructLayout(LayoutKind.Sequential)]
  private struct NOTIFYICONDATA
  { // 主角用的結(jié)構(gòu)
   internal int cbSize;
   internal IntPtr hwnd;
   internal int uID;
   internal int uFlags;
   internal int uCallbackMessage;
   internal IntPtr hIcon;
   [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x80)]
   internal string szTip;
   internal int dwState; // 這里往下幾個是 5.0 的精華
   internal int dwStateMask;
   [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0xFF)]
   internal string szInfo;
   internal int uTimeoutAndVersion;
   [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x40)]
   internal string szInfoTitle;
   internal int dwInfoFlags;
  }
  #endregion

  /// <summary>
  /// 建一個結(jié)構(gòu)
  /// </summary>
  private NOTIFYICONDATA GetNOTIFYICONDATA(IntPtr iconHwnd, string sTip, string boxTitle, string boxText)
  {
   NOTIFYICONDATA nData = new NOTIFYICONDATA();

   nData.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(nData); // 結(jié)構(gòu)的大小
   nData.hwnd = formTmpHwnd; // 處理消息循環(huán)的窗體句柄,可以移成主窗體
   nData.uID = uID; // 消息的 WParam,回調(diào)時用
   nData.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP | NIF_INFO; // 標志,表示由消息、圖標、提示、信息組成
   nData.uCallbackMessage = WM_NOTIFY_TRAY; // 消息ID,回調(diào)用
   nData.hIcon = iconHwnd; // 圖標的句柄,有興趣的話可以定時改變它變成動畫ICON
   nData.uTimeoutAndVersion = 10 * 1000 | NOTIFYICON_VERSION; // 提示的超時值(幾秒后自動消失)和版本
   nData.dwInfoFlags = NIIF_INFO; // 類型標志,有INFO、WARNING、ERROR,更改此值將影響氣泡提示框的圖標類型

   nData.szTip = sTip; // 圖標的提示信息
   nData.szInfoTitle = boxTitle; // 氣泡提示框的標題
   nData.szInfo = boxText; // 氣泡提示框的提示內(nèi)容

   return nData; // 這個嘛。。。
  }

  private int GetShell32VersionInfo()
  { // 返回shell32 的版本
   FileInfo fi = new FileInfo(Path.Combine(System.Environment.SystemDirectory, "shell32.dll")); //將來的平臺shell32 放哪目前不得而知,碰到再改
   if (fi.Exists)
   {
    FileVersionInfo theVersion = FileVersionInfo.GetVersionInfo(fi.FullName);
    int i = theVersion.FileVersion.IndexOf('.');
    if (i > 0)
    {
     try
     {
      return int.Parse(theVersion.FileVersion.Substring(0, i));
     }
     catch { }
    }
   }
   return 0;
  }

  /// <summary>
  /// 加一個新圖標
  /// </summary>
  /// <param name="iconHwnd">圖標句柄</param>
  /// <param name="sTip">提示, 5.0 最大: 128 char</param>
  /// <param name="boxTitle">氣泡標題, 最大: 64 char</param>
  /// <param name="boxText">氣泡內(nèi)容, 最大: 256 char</param>
  /// <returns>成功、失敗或錯誤(-1)</returns>
  public int AddNotifyBox(IntPtr iconHwnd, string sTip, string boxTitle, string boxText)
  {
   if (!this.VersionOk) return -1;

   NOTIFYICONDATA nData = GetNOTIFYICONDATA(iconHwnd, sTip, boxTitle, boxText);
   if (Shell_NotifyIcon(NIM_ADD, ref nData))
   {
    this.forgetDelNotifyBox = true;
    return 1;
   }
   else
   {
    return 0;
   }
  }

  /// <summary>
  /// 和add 差不多,不重復了
  /// </summary>
  public int DelNotifyBox()
  {
   if (!this.VersionOk) return -1;

   NOTIFYICONDATA nData = GetNOTIFYICONDATA(IntPtr.Zero, null, null, null);
   if (Shell_NotifyIcon(NIM_DELETE, ref nData))
   {
    this.forgetDelNotifyBox = false;
    return 1;
   }
   else
   {
    return 0;
   }
  }

  public int ModiNotifyBox(IntPtr iconHwnd, string sTip, string boxTitle, string boxText)
  {
   if (!this.VersionOk) return -1;

   NOTIFYICONDATA nData = GetNOTIFYICONDATA(iconHwnd, sTip, boxTitle, boxText);
   return Shell_NotifyIcon(NIM_MODIFY, ref nData) ? 1 : 0;
  }

  #region Optional Module //這里是可選方法
  /// <summary>
  /// 連接一個已存在的 contextMenu
  /// </summary>
  /// <param name="_formHwnd">窗體句柄,用來處理菜單的消息</param>
  /// <param name="_contextMenuHwnd">菜單的句柄</param>
  public void ConnectMyMenu(IntPtr _formHwnd, IntPtr _contextMenuHwnd)
  {
   formHwnd = _formHwnd;
   contextMenuHwnd = _contextMenuHwnd;
  }

  /// <summary>
  /// 立即清理掉圖標、委托和formtmp 資源(好象沒什么資源,考慮到可能二次開發(fā)掛接就開了這個東東)
  /// </summary>
  public void Dispose()
  {
   _delegateOfCallBack = null;
   this.formTmp.Dispose();
  }

  /// <summary>
  /// 版本適合
  /// </summary>
  public bool VersionPass
  {
   get
   {
    return this.VersionOk;
   }
  }
  #endregion
 }

用法示例:

 private void button2_Click (object sender, System.EventArgs e) {
  Shell_NotifyIconEx ().AddNotifyBox (this.Icon.Handle, this.Text, "這是標題", "單擊這里開始,我將帶你暢游API 世界");
 }
private void GetPoc1 (MouseButtons mb) { // 回調(diào)處理
 if (mb == MouseButtons.Left) {
  MessageBox.Show ("來自菜單1");
 }
}
privateShell_NotifyIconEx o1 = newShell_NotifyIconEx (); //這個放外面是用在 o.DelNotifyBox
private void button1_Click (object sender, System.EventArgs e) {
 o1.AddNotifyBox (this.Icon.Handle, this.Text, "菜單1", "單擊這里開始,我將帶你暢游API 世界");
 o1.ConnectMyMenu (this.Handle, this.contextMenu1.Handle); // 掛上菜單,可選
 o1._delegateOfCallBack = newShell_NotifyIconEx.delegateOfCallBack (GetPoc1); //定義回調(diào)
}
private void GetPoc1(MouseButtons mb) { // 回調(diào)處理
 if (mb == MouseButtons.Left) {
 MessageBox.Show("來自菜單1");
 }
 }
 private Shell_NotifyIconEx o1 = new Shell_NotifyIconEx(); //這個放外面是用在 o.DelNotifyBox
 private void button1_Click(object sender, System.EventArgs e) {
 o1.AddNotifyBox(this.Icon.Handle,this.Text,"菜單1","單擊這里開始,我將帶你暢游API 世界"); 
 o1.ConnectMyMenu(this.Handle,this.contextMenu1.Handle); // 掛上菜單,可選
 o1._delegateOfCallBack = new Shell_NotifyIconEx.delegateOfCallBack(GetPoc1); //定義回調(diào)
 }
 private void GetPoc2(MouseButtons mb) {
 if (mb == MouseButtons.Left) {
 MessageBox.Show("來自菜單2");
 }
 }
 private Shell_NotifyIconEx o2 = new Shell_NotifyIconEx(); //第二個nofityicon 和上面一樣
 private void button2_Click(object sender, System.EventArgs e) {
 o2.AddNotifyBox(this.Icon.Handle,this.Text,"菜單2","單擊這里開始,我將帶你暢游API 世界"); 
 o2.ConnectMyMenu(this.Handle,this.contextMenu2.Handle);
 o2._delegateOfCallBack = new Shell_NotifyIconEx.delegateOfCallBack(GetPoc2);
 }

以上就是C# WinForm調(diào)用Shell_NotifyIcon的示例代碼的詳細內(nèi)容,更多關(guān)于C# WinForm調(diào)用Shell_NotifyIcon的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C#多線程的ResetAbort()方法

    C#多線程的ResetAbort()方法

    這篇文章介紹了C#多線程的ResetAbort()方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • c#中Linq to Sql 增刪除的實例

    c#中Linq to Sql 增刪除的實例

    c#中Linq to Sql 增刪除的實例,需要的朋友可以參考一下
    2013-05-05
  • 基于WPF實現(xiàn)用戶頭像選擇器的示例代碼

    基于WPF實現(xiàn)用戶頭像選擇器的示例代碼

    這篇文章主要為大家詳細介紹了如何基于WPF實現(xiàn)用戶頭像選擇器,文中的示例代碼簡潔易懂,對我們學習WPF有一定幫助,感興趣的可以了解一下
    2022-07-07
  • C#使用ADO.Net連接數(shù)據(jù)庫與DbProviderFactory實現(xiàn)多數(shù)據(jù)庫訪問

    C#使用ADO.Net連接數(shù)據(jù)庫與DbProviderFactory實現(xiàn)多數(shù)據(jù)庫訪問

    這篇文章介紹了C#使用ADO.Net連接數(shù)據(jù)庫與DbProviderFactory實現(xiàn)多數(shù)據(jù)庫訪問的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05
  • Unity實現(xiàn)相機截圖功能

    Unity實現(xiàn)相機截圖功能

    這篇文章主要為大家詳細介紹了Unity實現(xiàn)相機截圖功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • C#導入導出Excel數(shù)據(jù)的兩種方法

    C#導入導出Excel數(shù)據(jù)的兩種方法

    這篇文章主要為大家詳細介紹了C#導入導出Excel數(shù)據(jù)的兩種方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • C#文字識別API場景解析、表格識別提取功能實現(xiàn)

    C#文字識別API場景解析、表格識別提取功能實現(xiàn)

    文章介紹了基于深度學習算法和自主OCR核心技術(shù)提供的文字識別API,該API能夠適應(yīng)各種復雜場景,高效、精準地識別印刷體和手寫體文字,并支持批量識別,文章以身份證識別接口為例,介紹C#文字識別API場景解析、表格識別提取功能,感興趣的朋友一起看看吧
    2024-11-11
  • C#實現(xiàn)List.Sort()使用小計

    C#實現(xiàn)List.Sort()使用小計

    在C#開發(fā)中,List是常見的一種集合類型,其提供了一個 Sort() 方法來實現(xiàn)對集合的排序,本文主要介紹了C#實現(xiàn)List.Sort()使用小計,具有一定的參考價值,感興趣的可以了解一下
    2023-12-12
  • 一道關(guān)于C#參數(shù)傳遞的面試題分析

    一道關(guān)于C#參數(shù)傳遞的面試題分析

    這篇文章主要介紹了一道關(guān)于C#參數(shù)傳遞的面試題,實例分析了C#參數(shù)傳遞的相關(guān)使用技巧,需要的朋友可以參考下
    2015-05-05
  • 基于C#實現(xiàn)文件偽裝技術(shù)

    基于C#實現(xiàn)文件偽裝技術(shù)

    這篇文章主要為大家詳細介紹了如何基于C#實現(xiàn)文件偽裝功能,將一般文件夾偽裝成計算機,控制面板,打印機等,感興趣的小伙伴可以跟隨小編一起學習一下
    2024-02-02

最新評論

桃园市| 萝北县| 黄石市| 中阳县| 重庆市| 澎湖县| 宜丰县| 津市市| 闽侯县| 星座| 东乌珠穆沁旗| 内江市| 綦江县| 遵义县| 罗甸县| 九龙坡区| 甘孜| 沈丘县| 磴口县| 隆回县| 丰镇市| 揭西县| 台南市| 阳春市| 禄丰县| 镇江市| 英德市| 陆丰市| 永德县| 满城县| 牡丹江市| 普宁市| 武定县| 子洲县| 柳河县| 衡东县| 永川市| 英山县| 博爱县| 兴宁市| 仙桃市|