我正在开发一个项目,需要弹出窗口。但问题是我希望通过表单设计器在这个弹出窗口中添加文本框等。
基本上我有一个按钮,当你点击它时,会打开另一个我在表单设计器中设计的窗口。
我已经做了一些谷歌搜索,但还没有找到我需要的东西,所以我希望你们能帮助我!
在 Visual Studio 中创建另一个表单(我们称之为 formPopup
)。在按钮处理程序中编写以下代码:
var formPopup = new Form();
formPopup.Show(this); // if you need non-modal window
如果你需要一个非模态窗口,请使用:formPopup.Show();
。如果您需要一个对话框(因此,您的代码将在调用此方法时挂起,直到您关闭打开的窗体),请使用:formPopup.ShowDialog()
。这并不容易,因为基本上Windows Forms不支持弹出窗口。虽然Windows Forms是基于Win32的,在Win32中支持弹出窗口。 如果您接受一些技巧,下面的代码将帮助您设置弹出窗口。您可以决定是否要好好利用它:
class PopupWindow : Control
{
private const int WM_ACTIVATE = 0x0006;
private const int WM_MOUSEACTIVATE = 0x0021;
private Control ownerControl;
public PopupWindow(Control ownerControl)
:base()
{
this.ownerControl = ownerControl;
base.SetTopLevel(true);
}
public Control OwnerControl
{
get
{
return (this.ownerControl as Control);
}
set
{
this.ownerControl = value;
}
}
protected override CreateParams CreateParams
{
get
{
CreateParams createParams = base.CreateParams;
createParams.Style = WindowStyles.WS_POPUP |
WindowStyles.WS_VISIBLE |
WindowStyles.WS_CLIPSIBLINGS |
WindowStyles.WS_CLIPCHILDREN |
WindowStyles.WS_MAXIMIZEBOX |
WindowStyles.WS_BORDER;
createParams.ExStyle = WindowsExtendedStyles.WS_EX_LEFT |
WindowsExtendedStyles.WS_EX_LTRREADING |
WindowsExtendedStyles.WS_EX_RIGHTSCROLLBAR |
WindowsExtendedStyles.WS_EX_TOPMOST;
createParams.Parent = (this.ownerControl != null) ? this.ownerControl.Handle : IntPtr.Zero;
return createParams;
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr SetActiveWindow(HandleRef hWnd);
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_ACTIVATE:
{
if ((int)m.WParam == 1)
{
//window is being activated
if (ownerControl != null)
{
SetActiveWindow(new HandleRef(this, ownerControl.FindForm().Handle));
}
}
break;
}
case WM_MOUSEACTIVATE:
{
m.Result = new IntPtr(MouseActivate.MA_NOACTIVATE);
return;
//break;
}
}
base.WndProc(ref m);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.FillRectangle(SystemBrushes.Info, 0, 0, Width, Height);
e.Graphics.DrawString((ownerControl as VerticalDateScrollBar).FirstVisibleDate.ToLongDateString(), this.Font, SystemBrushes.InfoText, 2, 2);
}
}
多试试,你需要调整它的位置和大小。如果使用不当,则不会显示任何内容。
C#中的表单是继承Form
基类的类。
通过创建该类的实例并调用ShowDialog()
方法,您可以显示一个弹出窗口。
如果你想在点击按钮时创建一个新的表单,下面的代码可能对你有些帮助:
private void settingsButton_Click(Object sender, EventArgs e)
{
// Create a new instance of the Form2 class
Form2 settingsForm = new Form2();
// Show the settings form
settingsForm.Show();
}
从这里开始,您还可以使用“Show Dialog”方法
来进行操作。/* create new form instance. i am overriding constructor to allow the caller form to set the form header */
var t = new TextPrompt("Insert your message and click Send button");
// pop up the form
t.Show();
if (t.DialogResult == System.Windows.Forms.DialogResult.OK)
{
MessageBox.Show("RTP", "Message sent to user");
}