关于如何将无模式消息框显示为工具提示的想法

每当用户将鼠标hover在菜单项上时,我都需要显示无模式消息框。 我不能使用messagebox.show(…),因为它是一个模态。 所以我所做的是创建一个单独的窗体,并使用菜单项上的hover事件显示窗体。 我有两个问题:

1)当窗体显示时,菜单失去其可见性。
2)窗体表格不会出现在菜单项旁边,就像工具提示一样。

关于如何保留组件的工具提示以使其外观和行为像窗体的任何想法?

回答你的第二个问题:

如果将form.StartPosition属性设置为FormStartPosition.Manual则可以将表单放在光标处(例如):

 form.StartPosition = FormStartPosition.Manual; form.Location = new Point(Cursor.Position.X - 1, Cursor.Position.Y - 1); 

这也可能有助于解决您的第一个问题。

如果您希望表单的行为类似于工具提示,那么如果您添加以下事件处理程序代码,它可能会让您想要:

  private void Form_MouseLeave(object sender, EventArgs e) { // Only close if cursor actually outside the popup and not over a label if (Cursor.Position.X < Location.X || Cursor.Position.Y < Location.Y || Cursor.Position.X > Location.X + Width - 1 || Cursor.Position.Y > Location.Y + Height - 1) { Close(); } } 

这解释了设置表格位置时的-1 。 它确保光标在首次显示时实际位于表单上。

由于Form类只是本机窗口的包装器,因此您可以使用以下代码段创建自己的弹出窗体,该​​窗体几乎看起来像工具提示窗口:

 public class PopupForm : Form { private const int SWP_NOSIZE = 0x0001; private const int SWP_NOMOVE = 0x0002; private const int SWP_NOACTIVATE = 0x0010; private const int WS_POPUP = unchecked((int)0x80000000); private const int WS_BORDER = 0x00800000; private const int WS_EX_TOPMOST = 0x00000008; private const int WS_EX_NOACTIVATE = 0x08000000; private const int CS_DROPSHADOW = 0x00020000; private static readonly IntPtr HWND_TOPMOST = (IntPtr)(-1); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); public PopupForm() { InitializeComponent(); SetStyle(ControlStyles.Selectable, false); Visible = false; } protected virtual void InitializeComponent() { FormBorderStyle = FormBorderStyle.None; StartPosition = FormStartPosition.Manual; ShowInTaskbar = false; BackColor = SystemColors.Info; // ... } protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.Style |= WS_POPUP; cp.Style |= WS_BORDER; cp.ExStyle |= WS_EX_TOPMOST | WS_EX_NOACTIVATE; //if (Microsoft.OS.IsWinXP && SystemInformation.IsDropShadowEnabled) // cp.ClassStyle |= CS_DROPSHADOW; return cp; } } protected override bool ShowWithoutActivation { get { return true; } } public new void Show() { SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOMOVE); base.Show(); } public void Show(Point p) { Location = p; Show(); } } 

使用外部代码中的Show()和Hide()方法控制此表单。