【问题标题】:InputBox Position输入框位置
【发布时间】:2013-08-24 18:24:19
【问题描述】:

如何将 Interaction.InputBox 打开到窗体的中心?我知道有一个输入框位置的代码

Interaction.InputBox("Question?", "Title", "Default Text", x,y);

我将使用这个不同大小的不同形式的 InputBox。有没有办法在表单的中心打开 InputBox?还是我必须在每个表单上单独放置它们?

是否也可以重新定位 InputBox 的 OKbutton 和 Cancelbutton?

【问题讨论】:

  • 创建您自己的表单并根据需要放置TextBoxLabelButtons。然后你可以随时以inputForm.StartPosition = FormStartPosition.CenterParent;inputForm.ShowDialog(this); 的形式调用它

标签: c# winforms position inputbox


【解决方案1】:

这里有一个简单的计算表格中心的东西,额外的偏移量是输入框的大小。

{
    int x = this.Left + (this.Width / 2) - 200;
    int y = this.Top + (this.Height / 2) - 100;
}

将这些输入到 x 和 y 的输入框中

【讨论】:

    【解决方案2】:

    如果您想要完全自定义,那么创建自己的表单是最好的方法,如 Fabio 的评论所示。

    但是,如果您只想使框大致居中并且您将多次这样做,那么您可以编写自己的扩展方法来为您显示和定位输入框:

    public static class FormExtensions
    {
        public static string CentredInputBox(this Form form, string prompt, string title = "", string defaultResponse = "")
        {
            const int approxInputBoxWidth = 370;
            const int approxInputBoxHeight = 158;
    
            int left = form.Left + (form.Width / 2) - (approxInputBoxWidth / 2);
            left = left < 0 ? 0 : left;
            int top = form.Top + (form.Height / 2) - (approxInputBoxHeight / 2);
            top = top < 0 ? 0 : top;
    
            return Microsoft.VisualBasic.Interaction.InputBox(prompt, title, defaultResponse, left, top);
        }
    }
    

    在表单中使用:

    this.CentredInputBox("MyPrompt", "MyTitle", "MyDefaultResponse");
    

    这并不完美,因为如果由于某种原因盒子比正常的大,那么它就不会完全位于中心,而且我认为它的大小是可变的,具体取决于其中的文本量。但是,在正常使用中应该不会太远。

    【讨论】:

      【解决方案3】:

      要将InputBox 居中,您可以尝试使用Win32 函数来处理它。此代码适用于您:

      [DllImport("user32")]
      private static extern int SetWindowPos(IntPtr hwnd, IntPtr afterHwnd, int x, int y, int cx, int cy, int flag);
      [DllImport("user32")]
      private static extern IntPtr FindWindow(string className, string caption);        
      [DllImport("user32")]
      private static extern int GetWindowRect(IntPtr hwnd, out RECT rect);
      //RECT structure
      public struct RECT {
         public int left, top, right, bottom;
      }
      public void ShowCenteredInputBox(string prompt, string title, string defaultReponse){
         BeginInvoke((Action)(() => {           
             while (true) {               
                 IntPtr hwnd = FindWindow(null, title + "\n\n\n");//this is just a trick to identify your InputBox from other window with the same caption
                 if (hwnd != IntPtr.Zero) {
                     RECT rect;
                     GetWindowRect(hwnd, out rect);
                     int w = rect.right - rect.left;
                     int h = rect.bottom - rect.top;
                     int x = Left + (Width - w) / 2;
                     int y = Top + (Height - h) / 2;
                     SetWindowPos(hwnd, IntPtr.Zero, x, y, w, h, 0x40);//SWP_SHOWWINDOW = 0x40
                     break;
                 }
             };
         }));
         Microsoft.VisualBasic.Interaction.InputBox(prompt, title + "\n\n\n", defaultResponse,0,0);
      }
      

      当然你也可以改变InputBox上的按钮、标签和TextBox的位置,但是这很麻烦也很棘手,可以说不简单。为您推荐的解决方案是System.Windows.Forms.Form 中创建新的标准表单,为其添加控件并使用ShowDialog() 方法来显示您的表单。。当然,它需要更多代码来完成,但它允许您完全自定义外观和行为。

      【讨论】:

        【解决方案4】:

        您可以设置 InputBox 的起始位置。有一个属性

        InputBox ib = new InputBox();
        
        ib.StartPosition = FormStartPosition.CenterParent;
        

        FormStartPosition 是一个枚举,您可以从中选择所需的位置!

        【讨论】:

          【解决方案5】:

          您可以简单地将 -1 用于 x 和 y: Interaction.InputBox("问题?", "标题", "默认文本", -1,-1);

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-01-09
            • 1970-01-01
            • 1970-01-01
            • 2018-02-23
            • 2020-04-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多