我怎样才能在 WinForms 中创建一个“弹出”窗口?
由于我使用了我自己编造的词“popup”,让我举例说明这个所谓的“popup”窗口:
![enter image description here](https://i.sstatic.net/Ta51q.png)
![enter image description here](https://i.sstatic.net/jzde4.png)
![enter image description here](https://i.sstatic.net/sWvtk.png)
![enter image description here](https://i.sstatic.net/NKrpF.png)
![enter image description here](https://i.sstatic.net/bN6sY.png)
我在这个神话般的“弹出窗口”中寻找的属性是:
Windows 应用程序已经在设法创建这样的窗口。我如何在 WinForms 应用程序中做到这一点?
相关问题
Control
那可以作为我的弹出窗口吗? 尝试#1
我试过
Show(onwer) + ShowWithoutActivation
方法:PopupForm dd = new PopupForm ();
dd.Show(this);
使用 PopupForm:
public class PopupForm: Form
{
public PopupForm()
{
InitilizeComponent();
}
private void InitilizeComponent()
{
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = false;
}
protected override bool ShowWithoutActivation
{ get { return true; } }
}
几乎解决了这个问题,但后来我发现提醒了“弹出”窗口的另一个属性:当通过鼠标或键盘进行交互时,它们不会从“所有者”表单中获取焦点。
最佳答案
你想要一个拥有的 window 。在您的主要形式中:
private void showPopup_Click(object sender, EventArgs e)
{
PopupForm popupForm = new PopupForm();
// Make "this" the owner of form2
popupForm.Show(this);
}
PopupForm 应如下所示:
public partial class PopupForm : Form
{
private bool _activating = false;
public PopupForm()
{
InitializeComponent();
}
// Ensure the popup isn't activated when it is first shown
protected override bool ShowWithoutActivation
{
get
{
return true;
}
}
private const int WM_NCACTIVATE = 0x86;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
protected override void WndProc(ref Message m)
{
// The popup needs to be activated for the user to interact with it,
// but we want to keep the owner window's appearance the same.
if ((m.Msg == WM_NCACTIVATE) && !_activating && (m.WParam != IntPtr.Zero))
{
// The popup is being activated, ensure parent keeps activated appearance
_activating = true;
SendMessage(this.Owner.Handle, WM_NCACTIVATE, (IntPtr) 1, IntPtr.Zero);
_activating = false;
// Call base.WndProc here if you want the appearance of the popup to change
}
else
{
base.WndProc(ref m);
}
}
}
并确保
PopupForm.ShowInTaskbar = false
.
关于winforms - 如何在 Winforms 中制作 "popup"(提示、下拉、弹出)窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8669368/