[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hWnd,uint Msg,int wParam,int lParam);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, StringBuilder lParam);static void Main(string[] args)
{
 
            IntPtr FWIN1, CWIN1, CWIN2, CWIN3, CWIN4, CWIN5, CWIN6; //窗体句柄
            FWIN1 = FindWindow("Shell_TrayWnd", null); //任务栏
            CWIN1 = FindWindowEx(FWIN1, 0, "TrayNotifyWnd", null);//托盘
            CWIN4 = FindWindowEx(CWIN1, 0, "SysPager", null);
            CWIN6 = FindWindowEx(CWIN4, 0, "ToolbarWindow32", null);      int count = SendMessage(CWIN6, TB_BUTTONCOUNT, 0, 0);
      Console.WriteLine("一共有{0}个图标", count);
      StringBuilder buffer = new StringBuilder(1024);
      for (int i = 1; i < count; i++)
      {
         SendMessage(CWIN6, TB_GETBUTTONTEXTW, (IntPtr)i, buffer);
         Console.WriteLine(buffer.ToString());
      }}为什么我在发送TB_GETBUTTONTEXTW消息的时候explorer就报错。我是想列出目前托盘中的图标信息,在选择其中的一个,进行操作。我要怎么能获得其中一个图标?

解决方案 »

  1.   

    C# 中的NotifyIcon 控件,你应该去看看.
      

  2.   

    我不是控制自己程序的NotifyIcon,而是其他程序的,比如QQ,迅雷等
      

  3.   

    在网上看了下,好多是delphi写的,我改成C#的,现在就不报错了,但是输出结果为空
      int count = SendMessage(CWIN6, TB_BUTTONCOUNT, 0, 0);
     Console.WriteLine("一共有{0}个图标", count);
                            
     uint procID;
     GetWindowThreadProcessId(CWIN6, out procID);
     Console.WriteLine("进程号{0}", procID); int hProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE, false, procID);
     Console.WriteLine("进程号{0}", hProcess); uint pointer  =  VirtualAllocEx(hProcess, 0, 4096, 0x1000 | 0x2000, 0x04);
     Console.WriteLine("地址{0}", pointer.ToString());
     if (pointer != 0)
     {   
        StringBuilder buffer = new StringBuilder(1024);
        TB_BUTTON bt;
        for (int i = 0; i < count; i++)
        {
           SendMessage(CWIN6, TB_GETBUTTON, i, (int)pointer);
           ReadProcessMemory(hProcess, pointer, out bt, 1024, 0);                        
           SendMessage(CWIN6, TB_GETBUTTONTEXTW, bt.idCommand, (int)pointer);
           ReadProcessMemory(hProcess, pointer+1024, buffer, 1024, 0);
           Console.WriteLine("输出");
           Console.WriteLine(buffer.ToString());
         }
    }但输出的buffer为空,也不知道改的对不对。搞不定了
      

  4.   

            struct TB_BUTTON
            {
                public int iBitmap;
                public int idCommand;
                public byte fsState;
                public byte fsStyle;
                public uint dwData;
                public int iString; 
            }