GVKun编程网logo

winapi – 如何Shell_NotifyIcon而不在通知区域中添加图标

2

这篇文章主要围绕winapi–如何Shell_NotifyIcon而不在通知区域中添加图标展开,旨在为您提供一份详细的参考资料。我们将全面介绍winapi–如何Shell_NotifyIcon而不在通

这篇文章主要围绕winapi – 如何Shell_NotifyIcon而不在通知区域中添加图标展开,旨在为您提供一份详细的参考资料。我们将全面介绍winapi – 如何Shell_NotifyIcon而不在通知区域中添加图标,同时也会为您带来c – 为什么Shell_NotifyIcon气球提示不起作用?、C# NotifyIcon 托盘控件、C# WinForm 调用 Shell_NotifyIcon、C# WinForm调用Shell_NotifyIcon的示例代码的实用方法。

本文目录一览:

winapi – 如何Shell_NotifyIcon而不在通知区域中添加图标

winapi – 如何Shell_NotifyIcon而不在通知区域中添加图标

MSDN关于 Notifications and the Notification Area的文档非常清楚,要求在通知区域中显示一个图标以显示通知:

To display a notification,you must
have an icon in the notification
area
. In certain cases,such as
Microsoft Communicator or battery
level,that icon will already be
present. In many other cases,however,
you will add an icon to the
notification area only as long as is
needed to show the notification.

由于我不想在通知区域添加任何图标,我想到的可能是“重用”现有的一个最常见的桌面上的图标.一个好的候选者可能是系统时钟.

我的问题是:

>我如何查找/枚举
NOTIFYICONDATA结构
系统时钟(AKA“日期和时间
属性“恢复时”?
>有没有更好的方法
完成这个(没有添加
一个图标)?

Shell_NotifyIcon在引擎盖下使用IUserNotification.我玩了它并制作了一个 utility out of it.我听说一个视力受损的系统管理员使用它来使他的脚本屏幕阅读器兼容.它是命令行,它没有消息循环.

它是自我意识的,这意味着发送给它的通知将排队(您可以控制它).为此,我提供了一个IQueryContinue实现.该项目是C语言,是开源的,帮助自己.

以下是它的内容:

HRESULT NotifyUser(const NOTIFU_ParaM& params,IQueryContinue *querycontinue,IUserNotificationCallback *notifcallback)
 {
    HRESULT result = E_FAIL;

    IUserNotification *un = 0;
    IUserNotification2 *deux = 0; //french pun : "un" above stands for UserNotification but it also means 1 in french. deux means 2.

    //First try with the Vista/Windows 7 interface
    //(unless /xp flag is specified
    if (!params.mForceXP)
       result = CoCreateInstance(CLSID_UserNotification,CLSCTX_ALL,IID_IUserNotification2,(void**)&deux);

    //Fall back to Windows XP
    if (!SUCCEEDED(result))
    {
       TRACE(eWARN,L"Using Windows XP interface IUserNotification\n");
       result = CoCreateInstance(CLSID_UserNotification,IID_IUserNotification,(void**)&un);
    }
    else
    {
       TRACE(eINFO,L"Using Vista interface IUserNotification2\n");
       un = (IUserNotification*)deux; //Rather ugly cast saves some code...
    }

    if (SUCCEEDED(result))
    {
       const std::basic_string<TCHAR> crlf_text(L"\\n");
       const std::basic_string<TCHAR> crlf(L"\n");
       std::basic_string<TCHAR> text(params.mText);
       size_t look = 0;
       size_t found;

       //Replace \n with actual CRLF pair
       while ((found = text.find(crlf_text,look)) != std::string::npos)
       {
          text.replace(found,crlf_text.size(),crlf);
          look = found+1;
       }

       result = un->SetIconInfo(params.mIcon,params.mTitle.c_str());
       result = un->SetBalloonInfo(params.mTitle.c_str(),text.c_str(),params.mType);

       //Looks like it controls what happends when the X button is
       //clicked on
       result = un->SetBalloonRetry(0,250,0);

       if (deux)
          result = deux->Show(querycontinue,notifcallback);
       else
          result = un->Show(querycontinue,250);

       un->Release();
    }

    return result;
 }

c – 为什么Shell_NotifyIcon气球提示不起作用?

c – 为什么Shell_NotifyIcon气球提示不起作用?

根据我所看到的一切,当我在应用程序窗口中单击鼠标左键时,以下C程序应该从托盘图标显示气球工具提示,但它不起作用.谁能告诉我我错过了什么?

这是在XP的Shell32.dll 6.0版本(使用DllGetVersion验证).

谢谢!

#include "stdafx.h"
    #include "shellapi.h"

    LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WParaM wParam,LParaM lParam);

    int APIENTRY _tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdshow)
    {
        MSG msg;

        WNDCLASS wc;
        memset(&wc,sizeof(wc));
        wc.lpfnWndProc = WndProc;
        wc.hInstance = hInstance;
        wc.hCursor = LoadCursor(NULL,IDC_ARROW);
        wc.hbrBackground = (HBrush)(COLOR_WINDOW+1);
        wc.lpszClassName = "sysTrayTest";
        RegisterClass(&wc);

        HWND hWnd = CreateWindow("sysTrayTest","",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,500,NULL,hInstance,NULL);
        if (hWnd)
        {
            ShowWindow(hWnd,nCmdshow);
            while (GetMessage(&msg,0))
            {
                TranslateMessage(&msg);
                dispatchMessage(&msg);
            }
        }

        return 0;
    }

    LRESULT CALLBACK WndProc(HWND hWnd,LParaM lParam)
    {
        switch (message)
        {
            case WM_DESTROY:
            {
                NOTIFYICONDATA nid;
                memset(&nid,sizeof(NOTIFYICONDATA));
                nid.cbSize = sizeof(NOTIFYICONDATA);
                nid.hWnd = hWnd;
                nid.uID = 1;
                Shell_NotifyIcon(NIM_DELETE,&nid);

                PostQuitMessage(0);
            }
            break;

            case WM_CREATE:
            {
                NOTIFYICONDATA nid;
                memset(&nid,sizeof(NOTIFYICONDATA));
                nid.cbSize = sizeof(NOTIFYICONDATA);
                nid.hWnd = hWnd;
                nid.uID = 1;
                nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
                nid.uCallbackMessage = WM_USER + 200;
                nid.hIcon = LoadIcon(NULL,IDI_informatION);
                lstrcpy (nid.szTip,"Test Tip");
                Shell_NotifyIcon(NIM_ADD,&nid);
            }
            break;

            case WM_LBUTTONDOWN:
            {
                NOTIFYICONDATA nid;
                memset(&nid,sizeof(NOTIFYICONDATA));
                nid.cbSize = sizeof(NOTIFYICONDATA);
                nid.hWnd = hWnd;
                nid.uID = 1;
                nid.uFlags = NIF_INFO;
                lstrcpy(nid.szInfo,"Test balloon tip");
                lstrcpy(nid.szInfoTitle,"Test Title");
                nid.dwInfoFlags = NIIF_INFO;
                nid.uTimeout = 15000;
                Shell_NotifyIcon(NIM_MODIFY,&nid);
            }
            break;

            default:
                return DefWindowProc(hWnd,message,wParam,lParam);
        }
        return 0;
    }

解决方法

呸,我明白了.出于某种原因我有标题…

sizeof(NOTIFYICONDATA)== 508

而…

NOTIFYICONDATA_V3_SIZE == 504
NOTIFYICONDATA_V2_SIZE == 488
NOTIFYICONDATA_V1_SIZE == 88

如果我指定V2或V3而不是sizeof(NOTIFYICONDATA),气球提示就会显示出来.

C# NotifyIcon 托盘控件

C# NotifyIcon 托盘控件

右下角以图标形式显示,双击图标显示界面,最小化界面时不显示在任务栏。

第一步:在界面上添加NotifyIcon控件。

 


 

第二步:设置notifyIcon1的属性,添加图标,将Visible设为true。当然,Visible也可以在代码中设置,true是显示在右下角,false不显示。


第三步:notifyIcon1添加事件,即双击显示界面。

1 private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
2 {
3     this.Show();//显示界面
4     this.WindowState = FormWindowState.Normal;
5     this.ShowInTaskbar = true;//显示在任务栏
6 }

 


第四步:最小化界面时不显示在任务栏。

1 private void FormMain_Resize(object sender, EventArgs e)
2 {
3     if (this.WindowState == FormWindowState.Minimized)
4     {
5         this.Hide();
6         this.ShowInTaskbar = false;
7     }
8 }

 

C# WinForm 调用 Shell_NotifyIcon

C# WinForm 调用 Shell_NotifyIcon

1  public class InnerClass: Form 2  { 3   private Shell_NotifyIconEx servicesClass = null; // 接受主CLASS 的实例句柄 4   internal InnerClass(Shell_NotifyIconEx _servicesClass) 5   { 6    servicesClass = _servicesClass; 7   } 8  9   private const int WM_LBUTTONDOWN = 0x0201; // 左键10   private const int WM_RBUTTONDOWN = 0x204; // 右键11   private const int WM_MBUTTONDOWN = 0x207; // 中键12 13   [DllImport("user32.dll", EntryPoint = "TrackPopupMenu")]14   private static extern int TrackPopupMenu( // c# 和vb.net 好象没有了随地popup 了,只要请它老人家出马了15    IntPtr hMenu,16   int wFlags,17   int x,18   int y,19   int nReserved,20    IntPtr hwnd,21   ref RECT lprc22    );23 24   [StructLayout(LayoutKind.Sequential)]25   private struct RECT26   { // 上面那位用的结构,表示前弹出菜单可用的一个范围大小(一般是全屏幕都让它用,留着搞游戏或视频对话之类的朋友指定菜单可用的范围)27    internal int Left;28    internal int Top;29    internal int Right;30    internal int Bottom;31   }32 33   protected override void WndProc(ref Message msg)34   {35    if (msg.Msg == servicesClass.WM_NOTIFY_TRAY)36    { // 如果消息相符37     if ((int)msg.WParam == servicesClass.uID)38     { // 并且消息的WParam 相符39     MouseButtons mb =MouseButtons.None;40      if ((int)msg.LParam == WM_LBUTTONDOWN)41      { //如果点击的是左键42       mb =MouseButtons.Left;43      }44      else if ((int)msg.LParam == WM_MBUTTONDOWN)45      { //中键46       mb =MouseButtons.Middle;47      }48      else if ((int)msg.LParam == WM_RBUTTONDOWN)49      { //右键50       if (servicesClass.contextMenuHwnd != IntPtr.Zero)51       { //如果有定义过菜单关联52        RECT r = new RECT();53        r.Left = Screen.PrimaryScreen.WorkingArea.Left;54        r.Right =Screen.PrimaryScreen.WorkingArea.Right;55        r.Top =Screen.PrimaryScreen.WorkingArea.Top;56        r.Bottom =Screen.PrimaryScreen.WorkingArea.Right;57 58        TrackPopupMenu(59         servicesClass.contextMenuHwnd,60        2,61        Cursor.Position.X,62        Cursor.Position.Y,63        0,64         servicesClass.formHwnd,65        ref r66         );67       }68       else69       { //如果没有定义过菜单关联70        mb =MouseButtons.Right;71       }72      }73 74      if (mb !=MouseButtons.None && servicesClass._delegateOfCallBack != null)75      {76       servicesClass._delegateOfCallBack(mb); // 执行回调77       return;78      }79     }80    }81 82    base.WndProc(ref msg);83   }84  }
1 public class Shell_NotifyIconEx 2  { 3   /// <summary> 4   /// ArLi, last fix: 2003.9.12, reference: ArLi.CommonPrj Lib @  5   /// </summary> 6   public static readonly System.Version myVersion = new System.Version(1, 2); //版本声明 7  8   private readonly InnerClass formTmp = null; // 这个很重要,不能放在构造里,因为它必须和此实例同等生存期才不会被中止消息循环 9   private readonly IntPtr formTmpHwnd = IntPtr.Zero; // 这是上一行的句柄 10   private readonly bool VersionOk = false; // 这是一个由VersionPass 返回的属性,它允许开发者检测当前机子的Shell32.dll(可能在win95 或未知平台上版本) 合适此组,不符则用.net 自己的notifyicon 11   private bool forgetDelNotifyBox = false; // 这是一个私有标志,它允许开发者在程序退出时忘记调用DelNotifyBox 来清除图标时会自动在析构里清掉它。 12  13   internal IntPtr formHwnd = IntPtr.Zero; // 这是调用此组件的主窗口句柄(当前实例有效,可多个icon 不冲突) 14   internal IntPtr contextMenuHwnd = IntPtr.Zero; // 这是菜单的句柄(当前实例有效,可多个icon 不冲突) 15  16   internal delegate void delegateOfCallBack(System.Windows.Forms.MouseButtons mb); 17   internal delegateOfCallBack _delegateOfCallBack = null; 18  19   public Shell_NotifyIconEx() // 构造 20   { 21    WM_NOTIFY_TRAY += 1; // 消息ID +1,避免多个ICON 消息处理冲突 22    uID += 1; // 同上 23    formTmp = new InnerClass(this); // 新实例一个消息循环 24    formTmpHwnd = formTmp.Handle; // 新实例句柄 25    VersionOk = this.GetShell32VersionInfo() >= 5; // 版本是否合适,此组件由于重点在气泡提示,它要求Shell32.dll 5.0(ie 5.0) 以上 26   } 27  28   ~Shell_NotifyIconEx() 29   { // 析构 30    if (forgetDelNotifyBox) this.DelNotifyBox(); //如果开发者忘记则清理icon 31   } 32  33   #region API_Consts 34   internal readonly int WM_NOTIFY_TRAY = 0x0400 + 2001; //readonly 表示只在构造可付值 35   internal readonly int uID = 5000; 36  37   // 常数定义,有VC 的可以参见 shellapi.h 38   private const int NIIF_NONE = 0x00; 39   private const int NIIF_INFO = 0x01; 40   private const int NIIF_WARNING = 0x02; 41   private const int NIIF_ERROR = 0x03; 42  43   private const int NIF_MESSAGE = 0x01; 44   private const int NIF_ICON = 0x02; 45   private const int NIF_TIP = 0x04; 46   private const int NIF_STATE = 0x08; 47   private const int NIF_INFO = 0x10; 48  49   private const int NIM_ADD = 0x00; 50   private const int NIM_MODIFY = 0x01; 51   private const int NIM_DELETE = 0x02; 52   private const int NIM_SETFOCUS = 0x03; 53   private const int NIM_SETVERSION = 0x04; 54  55   private const int NIS_HIDDEN = 0x01; 56   private const int NIS_SHAREDICON = 0x02; 57  58   private const int NOTIFYICON_OLDVERSION = 0x00; 59   private const int NOTIFYICON_VERSION = 0x03; 60  61   [DllImport("shell32.dll", EntryPoint = "Shell_NotifyIcon")] 62   private static extern bool Shell_NotifyIcon( // 这位是主角 63    int dwMessage, 64    ref NOTIFYICONDATA lpData 65   ); 66  67   /// <summary> 68   /// 此API 的作用是当 this.focus() 无效时可以考虑使用,效果很好 69   /// </summary> 70   /// <param name="hwnd">this.Handle, 当前窗体句柄</param> 71   [DllImport("user32.dll", EntryPoint = "SetForegroundWindow")] 72   public static extern int SetForegroundWindow( 73    IntPtr hwnd 74   ); 75  76   [StructLayout(LayoutKind.Sequential)] 77   private struct NOTIFYICONDATA 78   { // 主角用的结构 79    internal int cbSize; 80    internal IntPtr hwnd; 81    internal int uID; 82    internal int uFlags; 83    internal int uCallbackMessage; 84    internal IntPtr hIcon; 85    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x80)] 86    internal string szTip; 87    internal int dwState; // 这里往下几个是 5.0 的精华 88    internal int dwStateMask; 89    [MarshalAs(UnmanagedType.ByValTStr, SizeConst.........

C# WinForm调用Shell_NotifyIcon的示例代码

C# WinForm调用Shell_NotifyIcon的示例代码

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
  { // 上面那位用的结构,表示前弹出菜单可用的一个范围大小(一般是全屏幕都让它用,留着搞游戏或视频对话之类的朋友指定菜单可用的范围)
   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)
      { //如果有定义过菜单关联
       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
      { //如果没有定义过菜单关联
       mb =MouseButtons.Right;
      }
     }

     if (mb !=MouseButtons.None && servicesClass._delegateOfCallBack != null)
     {
      servicesClass._delegateOfCallBack(mb); // 执行回调
      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; // 这个很重要,不能放在构造里,因为它必须和此实例同等生存期才不会被中止消息循环
  private readonly IntPtr formTmpHwnd = IntPtr.Zero; // 这是上一行的句柄
  private readonly bool VersionOk = false; // 这是一个由VersionPass 返回的属性,它允许开发者检测当前机子的Shell32.dll(可能在win95 或未知平台上版本) 合适此组,不符则用.net 自己的notifyicon
  private bool forgetDelNotifyBox = false; // 这是一个私有标志,它允许开发者在程序退出时忘记调用DelNotifyBox 来清除图标时会自动在析构里清掉它。

  internal IntPtr formHwnd = IntPtr.Zero; // 这是调用此组件的主窗口句柄(当前实例有效,可多个icon 不冲突)
  internal IntPtr contextMenuHwnd = IntPtr.Zero; // 这是菜单的句柄(当前实例有效,可多个icon 不冲突)

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

  public Shell_NotifyIconEx() // 构造
  {
   WM_NOTIFY_TRAY += 1; // 消息ID +1,避免多个ICON 消息处理冲突
   uID += 1; // 同上
   formTmp = new InnerClass(this); // 新实例一个消息循环
   formTmpHwnd = formTmp.Handle; // 新实例句柄
   VersionOk = this.GetShell32VersionInfo() >= 5; // 版本是否合适,此组件由于重点在气泡提示,它要求Shell32.dll 5.0(ie 5.0) 以上
  }

  ~Shell_NotifyIconEx()
  { // 析构
   if (forgetDelNotifyBox) this.DelNotifyBox(); //如果开发者忘记则清理icon
  }

  #region API_Consts
  internal readonly int WM_NOTIFY_TRAY = 0x0400 + 2001; //readonly 表示只在构造可付值
  internal readonly int uID = 5000;

  // 常数定义,有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
  { // 主角用的结构
   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>
  /// 建一个结构
  /// </summary>
  private NOTIFYICONDATA GetNOTIFYICONDATA(IntPtr iconHwnd, string sTip, string boxTitle, string boxText)
  {
   NOTIFYICONDATA nData = new NOTIFYICONDATA();

   nData.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(nData); // 结构的大小
   nData.hwnd = formTmpHwnd; // 处理消息循环的窗体句柄,可以移成主窗体
   nData.uID = uID; // 消息的 WParam,回调时用
   nData.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP | NIF_INFO; // 标志,表示由消息、图标、提示、信息组成
   nData.uCallbackMessage = WM_NOTIFY_TRAY; // 消息ID,回调用
   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; // 气泡提示框的提示内容

   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">气泡内容, 最大: 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 资源(好象没什么资源,考虑到可能二次开发挂接就开了这个东东)
  /// </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) { // 回调处理
 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); //定义回调
}
private void GetPoc1(MouseButtons mb) { // 回调处理
 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); //定义回调
 }
 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调用Shell_NotifyIcon的示例代码的详细内容,更多关于C# WinForm调用Shell_NotifyIcon的资料请关注其它相关文章!

您可能感兴趣的文章:
  • C# winform打印excel的方法
  • C# winform中窗口关闭按钮的隐藏与禁用详解
  • C# WinForm-Timer控件的使用
  • C# Winform实现波浪滚动效果
  • C# Winform按钮中图片实现左图右字的效果实例
  • C# Winform中如何绘制动画示例详解
  • C# Winform调用百度接口实现人脸识别教程(附源码)
  • C# Winform程序实现防止多开的方法总结【亲测】
  • c# winform异步不卡界面的实现方法
  • c# winform 解决PictureBox 无法打印全部图片的问题

关于winapi – 如何Shell_NotifyIcon而不在通知区域中添加图标的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于c – 为什么Shell_NotifyIcon气球提示不起作用?、C# NotifyIcon 托盘控件、C# WinForm 调用 Shell_NotifyIcon、C# WinForm调用Shell_NotifyIcon的示例代码的相关信息,请在本站寻找。

本文标签: