[英]Ideas on how to display a modeless message box as a tooltip
每當用戶將鼠標懸停在菜單項上時,我都需要顯示無模式消息框。 我不能使用messagebox.show(...),因為它是一個模態。 所以我所做的是創建一個單獨的窗體,並使用菜單項上的懸停事件顯示窗體。 我有兩個問題:
1)當窗體顯示時,菜單失去了可見性。
2)窗體表單不會出現在菜單項旁邊,就像工具提示一樣。
關於如何定制組件的工具提示以使其外觀和行為類似於Windows窗體的任何想法?
要回答你的第二個問題:
如果將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()方法控制此表單。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.