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

将以下C++代码用C#编写: CSystemTray::CSystemTray(HINSTANCE hInst, // Handle to application…

  1. User

    将以下C++代码用C#编写:

    CSystemTray::CSystemTray(HINSTANCE hInst, // Handle to application instance HWND hParent, // The window that will recieve tray notifications UINT uCallbackMessage, // the callback message to send to parent LPCTSTR szToolTip, // tray icon tooltip HICON icon, // Handle to icon UINT uID, // Identifier of tray icon BOOL bHidden /=FALSE/, // Hidden on creation?
    LPCTSTR szBalloonTip /=NULL/, // Ballon tip (w2k only) LPCTSTR szBalloonTitle /=NULL/, // Balloon tip title (w2k) DWORD dwBalloonIcon /=NIIF_NONE/,// Ballon tip icon (w2k) UINT uBalloonTimeout /=10/) // Balloon timeout (w2k) { Initialise(); Create(hInst, hParent, uCallbackMessage, szToolTip, icon, uID, bHidden, szBalloonTip, szBalloonTitle, dwBalloonIcon, uBalloonTimeout); }

    void CSystemTray::Initialise() { // If maintaining a list of all TrayIcon windows (instead of // only allowing a single TrayIcon per application) then add // this TrayIcon to the list m_pThis = this;

    memset(&m_tnd, 0, sizeof(m_tnd));
    m_bEnabled = FALSE;
    m_bHidden  = TRUE;
    m_bRemoved = TRUE;
    
    m_DefaultMenuItemID    = 0;
    m_DefaultMenuItemByPos = TRUE;
    
    m_bShowIconPending = FALSE;
    
    m_uIDTimer   = 0;
    m_hSavedIcon = NULL;
    
    m_hTargetWnd = NULL;
    m_uCreationFlags = 0;
    

    #ifdef SYSTEMTRAY_USEW2K OSVERSIONINFO os = { sizeof(os) }; GetVersionEx(&os); m_bWin2K = ( VER_PLATFORM_WIN32_NT == os.dwPlatformId && os.dwMajorVersion >= 5 ); #else m_bWin2K = FALSE; #endif }

    ATOM CSystemTray::RegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX); 
    
    wcex.style			= CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
    wcex.lpfnWndProc	= (WNDPROC)WindowProc;
    wcex.cbClsExtra		= 0;
    wcex.cbWndExtra		= 0;
    wcex.hInstance		= hInstance;
    wcex.hIcon			= 0;
    wcex.hCursor		= 0;
    wcex.hbrBackground	= 0;
    wcex.lpszMenuName	= 0;
    wcex.lpszClassName	= TRAYICON_CLASS;
    wcex.hIconSm		= 0;
    
    return RegisterClassEx(&wcex);
    

    }

    BOOL CSystemTray::Create(HINSTANCE hInst, HWND hParent, UINT uCallbackMessage, LPCTSTR szToolTip, HICON icon, UINT uID, BOOL bHidden /=FALSE/, LPCTSTR szBalloonTip /=NULL/, LPCTSTR szBalloonTitle /=NULL/,
    DWORD dwBalloonIcon /=NIIF_NONE/, UINT uBalloonTimeout /=10/) { #ifdef _WIN32_WCE m_bEnabled = TRUE; #else // this is only for Windows 95 (or higher) m_bEnabled = (GetVersion() & 0xff) >= 4; if (!m_bEnabled) { ASSERT(FALSE); return FALSE; } #endif

    m_nMaxTooltipLength = _countof(m_tnd.szTip);
    
    // Make sure we avoid conflict with other messages
    ASSERT(uCallbackMessage >= WM_APP);
    
    // Tray only supports tooltip text up to m_nMaxTooltipLength) characters
    ASSERT(_tcslen(szToolTip) <= m_nMaxTooltipLength);
    
    m_hInstance = hInst;
    
    RegisterClass(hInst);
    
    // Create an invisible window
    m_hWnd = ::CreateWindow(TRAYICON_CLASS, _T(""), WS_POPUP, 
                            CW_USEDEFAULT,CW_USEDEFAULT, 
                            CW_USEDEFAULT,CW_USEDEFAULT, 
                            NULL, 0,
                            hInst, 0);
    
    // load up the NOTIFYICONDATA structure
    //m_tnd.cbSize = sizeof(NOTIFYICONDATA);
    m_tnd.cbSize = NOTIFYICONDATA_V2_SIZE;  // 2012-01-05 GONG Chen, XP compatibility
    m_tnd.hWnd   = (hParent)? hParent : m_hWnd;
    m_tnd.uID    = uID;
    m_tnd.hIcon  = icon;
    m_tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
    m_tnd.uCallbackMessage = uCallbackMessage;
    

    // 2007-11-14 GONG _tcsncpy_s(m_tnd.szTip, _countof(m_tnd.szTip), szToolTip, m_nMaxTooltipLength);

    #ifdef SYSTEMTRAY_USEW2K if (m_bWin2K && szBalloonTip) { #if _MSC_VER < 0x1000 // The balloon tooltip text can be up to 255 chars long. // ASSERT(AfxIsValidString(szBalloonTip)); ASSERT(lstrlen(szBalloonTip) < 256); #endif

        // The balloon title text can be up to 63 chars long.
        if (szBalloonTitle)
        {
    

    // ASSERT(AfxIsValidString(szBalloonTitle)); ASSERT(lstrlen(szBalloonTitle) < 64); }

        // dwBalloonIcon must be valid.
        ASSERT(NIIF_NONE == dwBalloonIcon    || NIIF_INFO == dwBalloonIcon ||
               NIIF_WARNING == dwBalloonIcon || NIIF_ERROR == dwBalloonIcon);
    
        // The timeout must be between 10 and 30 seconds.
        ASSERT(uBalloonTimeout >= 10 && uBalloonTimeout <= 30);
    
        m_tnd.uFlags |= NIF_INFO;
    
        _tcsncpy_s(m_tnd.szInfo, _countof(m_tnd.szInfo), szBalloonTip, 255);
        if (szBalloonTitle)
            _tcsncpy_s(m_tnd.szInfoTitle, _countof(m_tnd.szInfoTitle), szBalloonTitle, 63);
        else
            m_tnd.szInfoTitle[0] = _T('\0');
        m_tnd.uTimeout    = uBalloonTimeout * 1000; // convert time to ms
        m_tnd.dwInfoFlags = dwBalloonIcon;
    }
    

    #endif

    m_bHidden = bHidden;
    m_hTargetWnd = m_tnd.hWnd;
    

    #ifdef SYSTEMTRAY_USEW2K
    if (m_bWin2K && m_bHidden) { m_tnd.uFlags = NIF_STATE; m_tnd.dwState = NIS_HIDDEN; m_tnd.dwStateMask = NIS_HIDDEN; } #endif

    m_uCreationFlags = m_tnd.uFlags;	// Store in case we need to recreate in OnTaskBarCreate
    
    BOOL bResult = TRUE;
    if (!m_bHidden || m_bWin2K)
    {
        bResult = Shell_NotifyIcon(NIM_ADD, &m_tnd);
        m_bShowIconPending = m_bHidden = m_bRemoved = !bResult;
    }
    

    #ifdef SYSTEMTRAY_USEW2K
    if (m_bWin2K && szBalloonTip) { // Zero out the balloon text string so that later operations won't redisplay // the balloon. m_tnd.szInfo[0] = _T('\0'); } #endif

    return bResult;
    

    }

    CSystemTray::~CSystemTray() { RemoveIcon(); m_IconList.clear(); if (m_hWnd) ::DestroyWindow(m_hWnd); }

    ///////////////////////////////////////////////////////////////////////////// // CSystemTray icon manipulation

    void CSystemTray::SetFocus() { #ifdef SYSTEMTRAY_USEW2K Shell_NotifyIcon ( NIM_SETFOCUS, &m_tnd ); #endif }

    BOOL CSystemTray::MoveToRight() { RemoveIcon(); return AddIcon(); }

    BOOL CSystemTray::AddIcon() { if (!m_bRemoved) RemoveIcon();

    if (m_bEnabled)
    {
        m_tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
        if (!Shell_NotifyIcon(NIM_ADD, &m_tnd))
            m_bShowIconPending = TRUE;
        else
            m_bRemoved = m_bHidden = FALSE;
    }
    return (m_bRemoved == FALSE);
    

    }

    BOOL CSystemTray::RemoveIcon() { m_bShowIconPending = FALSE;

    if (!m_bEnabled || m_bRemoved)
        return TRUE;
    
    m_tnd.uFlags = 0;
    if (Shell_NotifyIcon(NIM_DELETE, &m_tnd))
        m_bRemoved = m_bHidden = TRUE;
    
    return (m_bRemoved == TRUE);
    

    }

  2. Assistant

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Runtime.InteropServices; using System.Windows.Forms;

    public class CSystemTray { private const string TRAYICON_CLASS = "CSystemTray"; private const int NOTIFYICONDATA_V2_SIZE = 936;

    private struct NOTIFYICONDATA
    {
        public int cbSize;
        public IntPtr hWnd;
        public int uID;
        public int uFlags;
        public int uCallbackMessage;
        public IntPtr hIcon;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
        public string szTip;
        public int dwState;
        public int dwStateMask;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
        public string szInfo;
        public int uTimeoutOrVersion;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
        public string szInfoTitle;
        public int dwInfoFlags;
        public Guid guidItem;
        public IntPtr hBalloonIcon;
    }
    
    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool Shell_NotifyIcon(uint dwMessage, ref NOTIFYICONDATA pnid);
    
    private delegate IntPtr WndProcDelegate(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
    private static Dictionary<IntPtr, CSystemTray> trayInstances = new Dictionary<IntPtr, CSystemTray>();
    private NOTIFYICONDATA m_tnd;
    private bool m_bEnabled;
    private bool m_bHidden;
    private bool m_bRemoved;
    private int m_nMaxTooltipLength;
    private Icon m_hSavedIcon;
    private IntPtr m_hWnd;
    private uint m_uIDTimer;
    private int m_DefaultMenuItemID;
    private bool m_DefaultMenuItemByPos;
    private bool m_bShowIconPending;
    private List<Icon> m_IconList = new List<Icon>();
    
    public CSystemTray(IntPtr hInst, IntPtr hParent, uint uCallbackMessage, string szToolTip, 
            Icon icon, int uID, bool bHidden=false, string szBalloonTip=null, 
            string szBalloonTitle=null, uint dwBalloonIcon=0, uint uBalloonTimeout=10)
    {
        Initialise();
        Create(hInst, hParent, uCallbackMessage, szToolTip, icon, uID, bHidden, szBalloonTip, 
            szBalloonTitle, dwBalloonIcon, uBalloonTimeout);
    }
    
    private void Initialise()
    {
        m_bEnabled = false;
        m_bHidden = true;
        m_bRemoved = true;
        m_DefaultMenuItemID = 0;
        m_DefaultMenuItemByPos = true;
        m_bShowIconPending = false;
        m_uIDTimer = 0;
        m_hSavedIcon = null;
        m_tnd = new NOTIFYICONDATA();
        m_tnd.cbSize = NOTIFYICONDATA_V2_SIZE;
        m_IconList.Clear();
    }
    
    private static IntPtr WindowProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
    {
        if (trayInstances.ContainsKey(hWnd))
        {
            return trayInstances[hWnd].HandleTrayMessage(msg, wParam, lParam);
        }
        return DefWindowProc(hWnd, msg, wParam, lParam);
    }
    
    private IntPtr HandleTrayMessage(uint msg, IntPtr wParam, IntPtr lParam)
    {
        switch (msg)
        {
            case 0x800: // WM_MOUSEMOVE
                break;
            case 0x201: // WM_LBUTTONDOWN
                break;
        }
        return IntPtr.Zero;
    }
    
    private static IntPtr DefWindowProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam)
    {
        if (hWnd == null)
        {
            return IntPtr.Zero;
        }
        return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }
    
    private static IntPtr DefWindowProc(IntPtr hWnd, uint uMsg, IntPtr wParam, ref COPYDATASTRUCT lParam)
    {
        if (hWnd == null)
        {
            return IntPtr.Zero;
        }
        return DefWindowProc(hWnd, uMsg, wParam, ref lParam);
    }
    
    class COPYDATASTRUCT
    {
        public IntPtr dwData = IntPtr.Zero;
        public int cbData = 0;
        public IntPtr lpData = IntPtr.Zero;
    
        public string GetString()
        {
            byte[] ba = new byte[cbData];
            Marshal.Copy(lpData, ba, 0, cbData);
            return System.Text.Encoding.UTF8.GetString(ba);
        }
    }
    
    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool DefWindowProc(IntPtr hWnd, uint uMsg, IntPtr wParam, ref COPYDATASTRUCT lParam);
    
    [DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
    
    [DllImport("user32.dll", SetLastError = true)]
    private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
    
    [DllImport("user32.dll", SetLastError = true)]
    private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int diNewLong);
    
    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool DestroyWindow(IntPtr hWnd);
    
    [DllImport("user32.dll", SetLastError = true)]
    private static extern int GetVersion();
    
    public bool AddIcon()
    {
        if (!m_bRemoved)
            RemoveIcon();
    
        if (m_bEnabled)
        {
            m_tnd.uFlags = 0;
            if (Shell_NotifyIcon(0, ref m_tnd))
                m_bRemoved = m_bHidden = true;
    
            m_tnd.uFlags = 0x03;
            if (!Shell_NotifyIcon(1, ref m_tnd))
                m_bShowIconPending = true;
            else
                m_bRemoved = m_bHidden = false;
        }
        return !m_bRemoved;
    }
    
    public bool RemoveIcon()
    {
        m_bShowIconPending = false;
    
        if (!m_bEnabled || m_bRemoved)
            return true;
    
        m_tnd.uFlags = 0;
        if (Shell_NotifyIcon(2, ref m_tnd))
            m_bRemoved = m_bHidden = true;
    
        return m_bRemoved;
    }
    
    private bool Create(IntPtr hInst, IntPtr hParent, uint uCallbackMessage, string szToolTip, 
        Icon icon, int uID, bool bHidden, string szBalloonTip, string szBalloonTitle, 
        uint dwBalloonIcon, uint uBalloonTimeout)
    {
        if (GetVersion() < 4)
            return false;
    
        try
        {
            if (hParent == null)
                hParent = m_hWnd;
    
            if (m_hWnd == null)
                RegClass(hInst);
    
            // load up the NOTIFYICONDATA structure
            m_tnd.hWnd = hParent;
            m_tnd.uID = uID;
            m_tnd.hIcon = icon.Handle;
            m_tnd.uFlags = 0x03;
            m_tnd.uCallbackMessage = uCallbackMessage;
            m_tnd.cbSize = NOTIFYICONDATA_V2_SIZE;
            m_tnd.szTip = szToolTip;
    
            if (!string.IsNullOrEmpty(szBalloonTip))
            {
                m_tnd.uFlags |= 0x40; //NIF_INFO
                m_tnd.szInfo = szBalloonTip;
                m_tnd.uTimeoutOrVersion = (int)uBalloonTimeout;
                m_tnd.szInfoTitle = szBalloonTitle;
                m_tnd.dwInfoFlags = (int)dwBalloonIcon;
            }
    
            m_bHidden = bHidden;
            trayInstances[m_hWnd] = this;
    
            if (m_bHidden)
            {
                m_tnd.uFlags = 0x01; //NIF_STATE
                m_tnd.dwState = 0x02; //NIS_HIDDEN
                m_tnd.dwStateMask = 0x02; //NIS_HIDDEN
            }
    
            m_bEnabled = true;
            bool result = Shell_NotifyIcon(0, ref m_tnd);
            m_bShowIconPending = m_bHidden = m_bRemoved = !result;
            return result;
        }
        catch
        {
            return false;
        }
    }
    
    private bool RegClass(IntPtr hInst)
    {
        WndProcDelegate d = WindowProc;
        WndProcDelegate[] singleDelegateArray = { d };
        var singleDelegateFunc = Marshal.GetFunctionPointerForDelegate(d);
        var pWndProc = Marshal.AllocHGlobal(Marshal.SizeOf(singleDelegateFunc));
        Marshal.WriteIntPtr(pWndProc, singleDelegateFunc);
        var wcex = new WNDCLASSEX
        {
            cbSize = Marshal.SizeOf(typeof(WNDCLASSEX)),
            lpfnWndProc = pWndProc,
            hInstance = hInst,
            lpszClassName = TRAYICON_CLASS,
            hIcon = IntPtr.Zero,
            hbrBackground = IntPtr.Zero,
            style = 0,
            cbClsExtra = 0,
            cbWndExtra = 0,
            hIconSm = IntPtr.Zero
        };
        bool res = RegisterClassEx(ref wcex) != 0;
        Marshal.FreeHGlobal(pWndProc);
        return res;
    }
    
    [StructLayout(LayoutKind.Sequential)]
    private struct WNDCLASSEX
    {
        public int cbSize;
        public uint style;
        public IntPtr lpfnWndProc;
        public int cbClsExtra;
        public int cbWndExtra;
        public IntPtr hInstance;
        public IntPtr hIcon;
        public IntPtr hCursor;
        public IntPtr hbrBackground;
        public IntPtr lpszMenuName;
        public IntPtr lpszClassName;
        public IntPtr hIconSm;
    }
    
    [DllImport("user32.dll")]
    private static extern short RegisterClassEx(ref WNDCLASSEX lpwcx);
    
    public void