Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WinC窗体中的弹出窗口#_C#_Winforms_Popup_Window - Fatal编程技术网

C# WinC窗体中的弹出窗口#

C# WinC窗体中的弹出窗口#,c#,winforms,popup,window,C#,Winforms,Popup,Window,我正在做一个需要弹出窗口的项目。但问题是,我还希望能够通过表单设计器在这个弹出窗口中添加文本框等 基本上我有一个按钮,当你点击它时,它会打开另一个我在表单设计器中设计的窗口 我一直在用谷歌搜索,但我还没有找到我需要的东西,所以我希望你们能帮我 C#中的表单是继承表单基类的类 您可以通过创建类的实例并调用ShowDialog()来显示弹出窗口。只需使用Visual Studio创建另一个表单(我们称之为formPopup)。在按钮处理程序中,编写以下代码: var formPopup = new

我正在做一个需要弹出窗口的项目。但问题是,我还希望能够通过表单设计器在这个弹出窗口中添加文本框等

基本上我有一个按钮,当你点击它时,它会打开另一个我在表单设计器中设计的窗口

我一直在用谷歌搜索,但我还没有找到我需要的东西,所以我希望你们能帮我

C#中的表单是继承
表单
基类的类

您可以通过创建类的实例并调用
ShowDialog()

来显示弹出窗口。只需使用Visual Studio创建另一个表单(我们称之为
formPopup
)。在按钮处理程序中,编写以下代码:

var formPopup = new Form();
formPopup.Show(this); // if you need non-modal window

如果需要非模式窗口,请使用:
formPopup.Show()。如果您需要一个对话框(因此在关闭打开的表单之前,您的代码将挂起此调用),请使用:
formPopup.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();
}
从这里,您也可以使用

“但问题是,我还希望能够通过表单设计器在此弹出窗口中添加文本框等。”

从您的描述中不清楚您处于开发过程的哪个阶段。如果您还没有弄清楚,要创建一个新表单,请单击项目-->添加Windows表单,然后键入表单名称并点击“添加”按钮。现在,您可以按预期将控件添加到表单中


当需要显示时,按照其他帖子的建议创建一个实例,并根据需要调用Show()或ShowDialog()。

这并不容易,因为windows窗体基本上不支持弹出窗口。虽然windows窗体基于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);
    }
}

对它进行一点实验,你必须摆弄它的位置和大小。用错了,什么也看不出来。

我用的是这种方法

添加要弹出的控件,添加所需的所有控件。 在代码中,您可以处理用户输入并将结果返回给调用者。 对于弹出窗体,只需创建窗体和show方法的新实例

/* 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"); 
}

你可以发布一些示例代码让我们解密吗?你最好创建你自己的表单来做你想做的事情。很好,这是从UserControl继承的还是会产生问题?重写wm_mouseactivate的目的是什么?最后一个问题,我可以使用这个.Activate()来保持与mono的兼容性,还是因为这不是一个表单,所以它不起作用?我不知道用户控件,它本身就是从控件派生出来的,所以它比控件有其他专门的行为,但它可能会工作。也从未在mono上测试过,我不确定dll导入是否更具体。简言之,它利用了底层的win32 api系统。我希望没有更多(它们几乎同时发布,这是可疑的)。通常情况下,你必须对这个问题进行近距离投票,作为这个问题的副本,而不是复制/粘贴你的答案。我不确定这是否是op想要的。由于这个答案不被接受,我认为我的假设是正确的。如果需要非模态窗口,代码段中的op与popup.comment的意思不同