Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

win api Shell_NotifyIcon用C#语言怎么声明

  1. User

    win api Shell_NotifyIcon用C#语言怎么声明

  2. Assistant

    在C#语言中声明winapi Shell_NotifyIcon可以使用以下代码:

    [DllImport("shell32.dll")]
    public static extern bool Shell_NotifyIcon(uint dwMessage, NOTIFYICONDATA lpData);
    
    [StructLayout(LayoutKind.Sequential)]
    public struct NOTIFYICONDATA
    {
        public uint cbSize;
        public IntPtr hWnd;
        public uint uID;
        public uint uFlags;
        public uint uCallbackMessage;
        public IntPtr hIcon;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
        public string szTip;
    }
    

    其中,Shell_NotifyIcon作为C#语言调用winapi的方法,使用DllImport标识,传入两个参数:dwMessage和lpData。NOTIFYICONDATA结构体作为lpData参数的类型,包含了需要传入的数据,如消息窗口句柄、图标句柄、提示信息等。

  3. User

    LoadIcon函数怎么加载一个图标文件?

  4. Assistant

    在使用LoadIcon函数加载一个图标文件时,需要执行以下步骤:

    1. 定义一个HICON类型的变量,该变量将用于保存加载后的图标句柄。
    HICON hIcon = IntPtr.Zero;
    
    1. 使用LoadImage函数加载图标文件,返回的是一个HANDLE类型的句柄值。
    IntPtr hImage = LoadImage(IntPtr.Zero, "path/to/icon/file", IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
    

    其中,第一个参数为NULL,表示使用当前进程的实例句柄;第二个参数为图标文件的路径;第三个参数为IMAGE_ICON,表示加载的是图标类型;第四个和第五个参数为0,则默认使用图标文件中的大小;最后一个参数为LR_LOADFROMFILE,表示从文件中加载。

    1. 将返回的HANDLE句柄转换为HICON类型的句柄。
    hIcon = (HICON)hImage;
    
    1. 使用加载后的HICON句柄进行其他操作,如将其设置到图标控件或通知区域图标中。
    // 例如将图标设置到通知区域图标中
    NotifyIcon notifyIcon = new NotifyIcon();
    notifyIcon.Icon = Icon.FromHandle(hIcon);
    

    完整代码如下:

    using System;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    public class LoadIconSample
    {
        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr LoadImage(IntPtr hInst, string lpszName, uint uType, int cxDesired, int cyDesired, uint fuLoad);
    
        [DllImport("user32.dll", SetLastError = true)]
        private static extern bool DestroyIcon(IntPtr hIcon);
    
        [StructLayout(LayoutKind.Sequential)]
        private struct NOTIFYICONDATA
        {
            public uint cbSize;
            public IntPtr hWnd;
            public uint uID;
            public uint uFlags;
            public uint uCallbackMessage;
            public IntPtr hIcon;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]
            public byte[] szTip;
            public uint dwState;
            public uint dwStateMask;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
            public byte[] szInfo;
            public uint uTimeoutOrVersion;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
            public byte[] szInfoTitle;
            public uint dwInfoFlags;
        }
    
        private const uint WM_USER = 0x0400;
        private const uint NIM_ADD = 0x00000000;
        private const uint NIM_MODIFY = 0x00000001;
        private const uint NIM_DELETE = 0x00000002;
        private const uint NIF_ICON = 0x00000002;
    
        private const uint IMAGE_ICON = 0x00000001;
        private const uint LR_LOADFROMFILE = 0x00000010;
    
        [STAThread]
        static void Main()
        {
            HICON hIcon = IntPtr.Zero;
            try
            {
                IntPtr hImage = LoadImage(IntPtr.Zero, "path/to/icon/file", IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
                hIcon = (HICON)hImage;
    
                // 将图标设置到通知区域图标中
                NotifyIcon notifyIcon = new NotifyIcon();
                notifyIcon.Icon = Icon.FromHandle(hIcon);
                NOTIFYICONDATA data = new NOTIFYICONDATA();
                data.cbSize = (uint)Marshal.SizeOf(typeof(NOTIFYICONDATA));
                data.hWnd = notifyIcon.Handle;
                data.uID = 1;
                data.uFlags = NIF_ICON;
                data.hIcon = hIcon;
                Shell_NotifyIcon(NIM_ADD, ref data);
                Application.Run();
            }
            finally
            {
                if (hIcon != IntPtr.Zero)
                {
                    DestroyIcon(hIcon);
                }
            }
        }
    
        [DllImport("shell32.dll")]
        public static extern bool Shell_NotifyIcon(uint dwMessage, ref NOTIFYICONDATA pnid);
    
        [StructLayout(LayoutKind.Sequential)]
        public struct HICON
        {
            public IntPtr handle;
        }
    }
    

    注意:在使用LoadImage函数加载图标文件时,需要确保图标文件存在且格式符合要求。