我现在只能够获取鼠标移动到该图标上的提示文字
 
网上也搜过了需要使用Shell_NotifyIcon来对托盘区图标进行操作
 
问题是我如何能获取我要操作的图标,怎么写遍历方法?
 
我用的是C#

解决方案 »

  1.   

    用FindWindow函数获取ToolbarWindow32的句柄即可
      

  2.   


    using System.Runtime.InteropServices; [DllImport("user32.DLL")]public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);[DllImport("user32.DLL")]public static extern IntPtr FindWindow(string lpszClass, string lpszWindow);[DllImport("user32.DLL")]public static extern IntPtr FindWindowEx(IntPtr hwndParent,    IntPtr hwndChildAfter, string lpszClass, string lpszWindow);[DllImport("user32.dll")]public static extern uint GetWindowThreadProcessId(IntPtr hWnd,    out uint dwProcessId); public const uint PROCESS_VM_OPERATION = 0x0008;public const uint PROCESS_VM_READ = 0x0010;public const uint PROCESS_VM_WRITE = 0x0020; [DllImport("kernel32.dll")]public static extern IntPtr OpenProcess(uint dwDesiredAccess,    bool bInheritHandle, uint dwProcessId);public const uint MEM_COMMIT = 0x1000;public const uint MEM_RELEASE = 0x8000; public const uint MEM_RESERVE = 0x2000;public const uint PAGE_READWRITE = 4; [DllImport("kernel32.dll")]public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress,    uint dwSize, uint flAllocationType, uint flProtect); [DllImport("kernel32.dll")]public static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress,   uint dwSize, uint dwFreeType); [DllImport("kernel32.dll")]public static extern bool CloseHandle(IntPtr handle); [DllImport("kernel32.dll")]public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,   IntPtr lpBuffer, int nSize, ref uint vNumberOfBytesRead); public const int WM_USER = 0x0400;public const int TB_BUTTONCOUNT = WM_USER + 24;public const int TB_GETBUTTONTEXTW = WM_USER + 75; private void button1_Click(object sender, EventArgs e){    IntPtr vHandle = FindWindow("Shell_TrayWnd", null);    vHandle = FindWindowEx(vHandle, IntPtr.Zero, "ReBarWindow32", null);    vHandle = FindWindowEx(vHandle, IntPtr.Zero, "MSTaskSwWClass", null);    vHandle = FindWindowEx(vHandle, IntPtr.Zero, "ToolbarWindow32", null);    int vCount = SendMessage(vHandle, TB_BUTTONCOUNT, 0, 0);    uint vProcessId;    GetWindowThreadProcessId(vHandle, out vProcessId);    IntPtr vProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ |        PROCESS_VM_WRITE, false, vProcessId);    IntPtr vPointer = VirtualAllocEx(vProcess, IntPtr.Zero, 0x1000,        MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);    char[] vBuffer = new char[256];    uint vNumberOfBytesRead = 0;    try    {        for (int i = 0; i < vCount; i++)        {            SendMessage(vHandle, TB_GETBUTTONTEXTW, i, vPointer.ToInt32());            ReadProcessMemory(vProcess, vPointer,                Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer, 0),                vBuffer.Length * sizeof(char), ref vNumberOfBytesRead);            int l = 0;            for (int j = 0; j < vBuffer.Length; j++)            {                if (vBuffer[j] == (char)0)                {                    l = j;                    break;                }            }            Console.WriteLine(new string(vBuffer, 0, l));        }    }    finally    {        VirtualFreeEx(vProcess, vPointer, 0, MEM_RELEASE);        CloseHandle(vProcess);    }}
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;namespace 遍历托盘区图标
    {
        public partial class Form1 : Form
        {
            [DllImport("user32.DLL")]        public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);        [DllImport("user32.DLL")]        public static extern IntPtr FindWindow(string lpszClass, string lpszWindow);        [DllImport("user32.DLL")]        public static extern IntPtr FindWindowEx(IntPtr hwndParent,            IntPtr hwndChildAfter, string lpszClass, string lpszWindow);        [DllImport("user32.dll")]        public static extern uint GetWindowThreadProcessId(IntPtr hWnd,            out uint dwProcessId);        public const uint PROCESS_VM_OPERATION = 0x0008;        public const uint PROCESS_VM_READ = 0x0010;        public const uint PROCESS_VM_WRITE = 0x0020;        [DllImport("kernel32.dll")]        public static extern IntPtr OpenProcess(uint dwDesiredAccess,            bool bInheritHandle, uint dwProcessId);        public const uint MEM_COMMIT = 0x1000;        public const uint MEM_RELEASE = 0x8000;        public const uint MEM_RESERVE = 0x2000;        public const uint PAGE_READWRITE = 4;        [DllImport("kernel32.dll")]        public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress,            uint dwSize, uint flAllocationType, uint flProtect);        [DllImport("kernel32.dll")]        public static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress,           uint dwSize, uint dwFreeType);        [DllImport("kernel32.dll")]        public static extern bool CloseHandle(IntPtr handle);        [DllImport("kernel32.dll")]        public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,           IntPtr lpBuffer, int nSize, ref uint vNumberOfBytesRead);        public const int WM_USER = 0x0400;        public const int TB_BUTTONCOUNT = WM_USER + 24;        public const int TB_GETBUTTONTEXTW = WM_USER + 75;        public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                IntPtr vHandle = FindWindow("Shell_TrayWnd", null);            vHandle = FindWindowEx(vHandle, IntPtr.Zero, "ReBarWindow32", null);            vHandle = FindWindowEx(vHandle, IntPtr.Zero, "MSTaskSwWClass", null);            vHandle = FindWindowEx(vHandle, IntPtr.Zero, "ToolbarWindow32", null);            int vCount = SendMessage(vHandle, TB_BUTTONCOUNT, 0, 0);            uint vProcessId;            GetWindowThreadProcessId(vHandle, out vProcessId);            IntPtr vProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ |                PROCESS_VM_WRITE, false, vProcessId);            IntPtr vPointer = VirtualAllocEx(vProcess, IntPtr.Zero, 0x1000,                MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);            char[] vBuffer = new char[256];            uint vNumberOfBytesRead = 0;            try
                {                for (int i = 0; i < vCount; i++)
                    {                    SendMessage(vHandle, TB_GETBUTTONTEXTW, i, vPointer.ToInt32());                    ReadProcessMemory(vProcess, vPointer,                        Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer, 0),                        vBuffer.Length * sizeof(char), ref vNumberOfBytesRead);                    int l = 0;                    for (int j = 0; j < vBuffer.Length; j++)
                        {                        if (vBuffer[j] == (char)0)
                            {                            l = j;                            break;                        }                    }                    Console.WriteLine(new string(vBuffer, 0, l));                }            }            finally
                {                VirtualFreeEx(vProcess, vPointer, 0, MEM_RELEASE);                CloseHandle(vProcess);            }
            }
        }
    }vHandle为0,在第一步查找shell_TrayWnd时,找不到