怎么使创建的NotifyIcon显示到最右边?
- User
怎么使创建的NotifyIcon显示到最右边?
- Assistant
可以通过将NotifyIcon的Left属性设置为屏幕宽度减去NotifyIcon的宽度来将其显示到屏幕最右边,示例代码如下:
notifyIcon1.Left = Screen.PrimaryScreen.WorkingArea.Width - notifyIcon1.Width; - User
NotifyIcon就没有Left 属性,需要的是显示在通知栏的最右边。
- Assistant
哦,抱歉我的回答有误。如果你想在通知区域的最右边显示 NotifyIcon,你可以使用 Shell_NotifyIcon 函数,并将 NOTIFYICONDATA 结构体中的 uFlags 属性设置为 NIF_MESSAGE | NIF_ICON | NIF_TIP | NIF_REALTIME | NIF_SHOWTIP。
其次,需要确保 NOTIFYICONDATA 结构体中的 hWnd 属性设置为包含 NotifyIcon 的窗口句柄。
具体的代码示例可以参考下面的例子,其中 hIcon 是你要显示的图标,szTip 是提示文本:
private const int WM_MOUSEMOVE = 0x200; private const int WM_LBUTTONUP = 0x202; [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern bool Shell_NotifyIcon(int message, NOTIFYICONDATA data); private void ShowNotifyIcon() { NOTIFYICONDATA data = new NOTIFYICONDATA(); data.cbSize = Marshal.SizeOf(data); data.hWnd = this.Handle; // 设置包含 NotifyIcon 的窗口句柄 data.uID = 0; data.uFlags = 0x10 | 0x2 | 0x4 | 0x20 | 0x80; // 设置 NotifyIcon 的属性 data.hIcon = hIcon; // 设置要显示的图标 data.szTip = szTip; // 设置提示文本 Shell_NotifyIcon(0x00, data); } private void HideNotifyIcon() { NOTIFYICONDATA data = new NOTIFYICONDATA(); data.cbSize = Marshal.SizeOf(data); data.hWnd = this.Handle; data.uID = 0; Shell_NotifyIcon(0x02, data); } private void NotifyIcon_MouseMove(object sender, MouseEventArgs e) { // 必须处理 WM_MOUSEMOVE 消息,否则系统会自动将 NotifyIcon 移动到通知区域的中间位置 Shell_NotifyIcon(WM_MOUSEMOVE, new NOTIFYICONDATA() { hWnd = this.Handle, uID = 0 }); } private void NotifyIcon_MouseClick(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { // 左键单击事件 } } private struct NOTIFYICONDATA { public int cbSize; public IntPtr hWnd; public int uID; public int uFlags; public IntPtr hIcon; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] public string szTip; } - User
上面的不能做到,只能显示在通知区域的左边
- Assistant
我的回答仍然有误,非常抱歉。实际上,将 NotifyIcon 显示在通知区域的最右边需要在 Windows API 中使用一些来自 Shell32.dll 的函数来实现。以下是一些参考代码:
[DllImport("user32.dll", SetLastError = true)] static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll", SetLastError = true)] static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd); [DllImport("user32.dll", SetLastError = true)] static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); public void MoveToSystemTrayRight() { IntPtr taskbarWnd = FindWindow("Shell_TrayWnd", null); IntPtr trayWnd = FindWindowEx(taskbarWnd, IntPtr.Zero, "TrayNotifyWnd", null); IntPtr reBarWnd = FindWindowEx(trayWnd, IntPtr.Zero, "ReBarWindow32", null); IntPtr notifyIconOverflowWindow = FindWindowEx(reBarWnd, IntPtr.Zero, "NotifyIconOverflowWindow", null); IntPtr notificationAreaWindow = FindWindowEx(reBarWnd, IntPtr.Zero, "NotificationAreaWindow", null); IntPtr sysPager = FindWindowEx(notificationAreaWindow, IntPtr.Zero, "SysPager", null); IntPtr toolbarWindow32 = FindWindowEx(sysPager, IntPtr.Zero, "ToolbarWindow32", null); int count = SendMessage(toolbarWindow32, TB_BUTTONCOUNT, 0, IntPtr.Zero); for (int i = 0; i < count; i++) { RECT rect; SendMessage(toolbarWindow32, TB_GETITEMRECT, i, out rect); MoveWindow(this.Handle, rect.right - 16, rect.top, 16, rect.bottom - rect.top, true); } } - User
上面的TB_BUTTONCOUNT和TB_GETITEMRECT怎么声明
- Assistant
TB_BUTTONCOUNT 和 TB_GETITEMRECT 是 Windows API 中的消息,需要通过 C# 的 DllImport 特性声明它们:
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)] static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, IntPtr lParam); [DllImport("user32.dll", SetLastError = true)] static extern bool SendMessage(IntPtr hwnd, int msg, int wparam, ref RECT lprect); [StructLayout(LayoutKind.Sequential)] public struct RECT { public int left; public int top; public int right; public int bottom; }请注意,SendMessage 函数需要传入 RECT 结构体的引用,因此必须声明 SendMessage 函数的重载版本。
- User
上面的TB_BUTTONCOUNT和TB_GETITEMRECT怎么声明
- Assistant
TB_BUTTONCOUNT 和 TB_GETITEMRECT 是 Windows API 中的消息,需要通过 C# 的 DllImport 特性声明它们:
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)] static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, IntPtr lParam); [DllImport("user32.dll", SetLastError = true)] static extern bool SendMessage(IntPtr hwnd, int msg, int wparam, ref RECT lprect); [StructLayout(LayoutKind.Sequential)] public struct RECT { public int left; public int top; public int right; public int bottom; }请注意,SendMessage 函数需要传入 RECT 结构体的引用,因此必须声明 SendMessage 函数的重载版本。
- User
TB_BUTTONCOUNT 和 TB_GETITEMRECT 的值是什么?
- Assistant
TB_BUTTONCOUNT 和 TB_GETITEMRECT 都是 Toolbar Window Messages,它们是 Windows API 中的消息参数。下面是它们的详细说明:
-
TB_BUTTONCOUNT:获取工具栏(Toolbar)顶部的按钮或工具条中的按钮数。wParam 值应该为 0,lParam 应该为一个指向 LRESULT 类型的指针。返回值为按钮数。
-
TB_GETITEMRECT:确定给定按钮在 hWndToolBar 指定的工具栏(Toolbar)中的位置和大小。wParam 应该是按钮的索引。lParam 应该是一个指向 RECT 结构体的指针,用于存储按钮的位置和大小。
在我们的示例代码中,我们对 NotifyIcon 所在的工具栏上的每个按钮都调用了 TB_GETITEMRECT 消息,并使用 SendMessage 函数获取它们的位置和大小。然后,我们使用 MoveWindow 函数将 NotifyIcon 移动到最右边的按钮的右侧。
需要注意的是,这两个消息的数值应该分别为 1048 和 0x41F。
-
- User
下面的函数能实现移动到右边吗? 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);}
- Assistant
不,这是 MFC 框架中的代码,只能在 Windows 消息循环中使用。它只能向系统托盘添加或删除一个图标,并不能将其移动到通知区域的最右边。
要将 NotifyIcon 移动到通知区域的最右边,我们需要使用 Windows API 的一些函数来查找系统托盘窗口和工具栏按钮,并计算、设置 NotifyIcon 的位置。例如,我们可以使用 FindWindowEx 函数来枚举窗口并找到包含 NotifyIcon 的工具栏按钮,然后使用 GetWindowRect 和 SetWindowPos 函数来计算和设置 NotifyIcon 的位置。
- User
可以用SetWindowPos 设置吗?给一下C#语言的示例
- Assistant
是的,你可以使用 SetWindowPos 函数来设置 NotifyIcon 的位置。下面是一个 C# 示例代码,可以将指定的窗口移动到通知区域的最右边:
[DllImport("user32.dll", SetLastError = true)] static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags); private const int SWP_NOSIZE = 0x0001; private const int SWP_NOZORDER = 0x0004; private const int SWP_NOACTIVATE = 0x0010; private const int SWP_SHOWWINDOW = 0x0040; private static readonly IntPtr HWND_BOTTOM = new IntPtr(1); public bool MoveToSystemTrayRight(IntPtr hWnd) { IntPtr taskbarWnd = FindWindow("Shell_TrayWnd", null); IntPtr trayNotifyWnd = FindWindowEx(taskbarWnd, IntPtr.Zero, "TrayNotifyWnd", null); IntPtr sysPager = FindWindowEx(trayNotifyWnd, IntPtr.Zero, "SysPager", null); IntPtr notificationAreaWindow = FindWindowEx(sysPager, IntPtr.Zero, "ToolbarWindow32", null); int count = SendMessage(notificationAreaWindow, TB_BUTTONCOUNT, 0, IntPtr.Zero); RECT rect; SendMessage(notificationAreaWindow, TB_GETITEMRECT, count - 1, out rect); int x = rect.right - rect.left - notifyIcon1.Size.Width; int y = (rect.bottom + rect.top - notifyIcon1.Size.Height) / 2; return SetWindowPos(hWnd, HWND_BOTTOM, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW); } - User
提示不存在TB_BUTTONCOUNT变量怎么办?
- Assistant
对不起,我的回答有误。TB_BUTTONCOUNT 是 Toolbar 控件的消息,不能直接在 NotifyIcon 控件上使用。
如果您想在 NotifyIcon 控件上实现这个功能,您需要使用以下代码来获取最右边的工具栏按钮的位置和大小,然后将 NotifyIcon 控件移动到它的右侧:
IntPtr taskbarWnd = FindWindow("Shell_TrayWnd", null); IntPtr trayNotifyWnd = FindWindowEx(taskbarWnd, IntPtr.Zero, "TrayNotifyWnd", null); IntPtr sysPager = FindWindowEx(trayNotifyWnd, IntPtr.Zero, "SysPager", null); IntPtr toolbarWindow32 = FindWindowEx(sysPager, IntPtr.Zero, "ToolbarWindow32", null); int count = SendMessage(toolbarWindow32, TB_BUTTONCOUNT, 0, IntPtr.Zero); RECT rect; SendMessage(toolbarWindow32, TB_GETITEMRECT, count - 1, out rect); int x = rect.right - rect.left - notifyIcon1.Size.Width; int y = (rect.bottom + rect.top - notifyIcon1.Size.Height) / 2; notifyIcon1.Left = x; notifyIcon1.Top = y;在这个代码中,我们使用 FindWindow 和 FindWindowEx 函数来查找任务栏、托盘窗口及其内部控件,然后使用 SendMessage 函数和 Toolbar Window Messages 获取通知区域中最后一个工具栏按钮的位置和大小。我们计算了 NotifyIcon 需要移动到的位置,并使用 Left 和 Top 属性将其移动到该位置上。
需要注意的是,此函数需要在具有管理员权限的帐户下运行,才能访问系统托盘中的窗口和消息。
- User
NotifyIcon 控件就没有left属性
- Assistant
非常抱歉我的回答有误,您是正确的 - NotifyIcon 控件确实没有 Left 属性。您可以使用另一种方法将 NotifyIcon 控件移动到通知区域的最右边,如下所示:
private void MoveToSystemTrayRight() { IntPtr taskbarWnd = FindWindow("Shell_TrayWnd", null); IntPtr trayNotifyWnd = FindWindowEx(taskbarWnd, IntPtr.Zero, "TrayNotifyWnd", null); IntPtr sysPager = FindWindowEx(trayNotifyWnd, IntPtr.Zero, "SysPager", null); IntPtr toolbarWindow32 = FindWindowEx(sysPager, IntPtr.Zero, "ToolbarWindow32", null); int count = SendMessage(toolbarWindow32, TB_BUTTONCOUNT, 0, IntPtr.Zero); RECT rect; SendMessage(toolbarWindow32, TB_GETITEMRECT, count - 1, out rect); int x = rect.right - notifyIcon1.Width; //计算NotifyIcon控件的X坐标 int y = (rect.top + rect.bottom) / 2 - notifyIcon1.Height / 2; //计算NotifyIcon控件的Y坐标 //设置NotifyIcon的位置 Point newLocation = new Point(x, y); notifyIcon1.Location = newLocation; }这里使用了上面的函数代码,并根据计算得到的位置计算 NotifyIcon 控件应该出现的位置。最后,使用控件的 Location 属性来设置 NotifyIcon 控件的位置。
请记住,需要在具有管理员权限的帐户下运行此函数,才能访问系统通知区域内部窗口。
- User
NotifyIcon也没有Location 属性
- Assistant
非常抱歉,我犯了一个错误。NotifyIcon 控件确实没有 Location 属性,因为它是非窗体控件。您可以使用另一种方法来移动 NotifyIcon 控件,如下所示:
private void MoveToSystemTrayRight() { IntPtr taskbarWnd = FindWindow("Shell_TrayWnd", null); IntPtr trayNotifyWnd = FindWindowEx(taskbarWnd, IntPtr.Zero, "TrayNotifyWnd", null); IntPtr sysPager = FindWindowEx(trayNotifyWnd, IntPtr.Zero, "SysPager", null); IntPtr toolbarWindow32 = FindWindowEx(sysPager, IntPtr.Zero, "ToolbarWindow32", null); int count = SendMessage(toolbarWindow32, TB_BUTTONCOUNT, 0, IntPtr.Zero); RECT rect; SendMessage(toolbarWindow32, TB_GETITEMRECT, count - 1, out rect); //计算 NotifyIcon 控件应该出现的位置 int x = rect.right - notifyIcon1.Width; int y = (rect.top + rect.bottom) / 2 - notifyIcon1.Height / 2; //获取 NotifyIcon 控件的句柄 IntPtr notifyIconHandle = IntPtr.Zero; foreach (Control control in this.Controls) { if (control is NotifyIcon) { notifyIconHandle = control.Handle; break; } } //移动 NotifyIcon 控件 SetWindowPos(notifyIconHandle, new IntPtr(0), x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW); }这里使用了之前的函数和消息,计算出 NotifyIcon 控件应该出现的位置,并获得控件的句柄。