首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >InputBox位置

InputBox位置
EN

Stack Overflow用户
提问于 2013-08-25 02:24:20
回答 5查看 5.5K关注 0票数 1

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

代码语言:javascript
复制
Interaction.InputBox("Question?", "Title", "Default Text", x,y);

我将以不同大小的不同形式使用此InputBox。有没有办法在表单中心打开InputBox?或者我必须在每个表单上分别定位它们?

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

EN

回答 5

Stack Overflow用户

发布于 2019-10-11 03:28:58

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

代码语言:javascript
复制
{
    int x = this.Left + (this.Width / 2) - 200;
    int y = this.Top + (this.Height / 2) - 100;
}

将这些内容传递到x和y的输入框中

票数 3
EN

Stack Overflow用户

发布于 2013-08-25 03:34:34

如果你想要完全定制,那么创建你自己的表单是最好的方式,正如Fabio的评论所指出的那样。

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

代码语言:javascript
复制
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);
    }
}

表单中的用法:

代码语言:javascript
复制
this.CentredInputBox("MyPrompt", "MyTitle", "MyDefaultResponse");

它不是完美的,因为如果由于某种原因,框比正常情况下更大,那么它就不会完全在中心,我认为它的大小是可变的,这取决于其中有多少文本。然而,它在正常使用中应该不会太远。

票数 2
EN

Stack Overflow用户

发布于 2013-08-25 03:37:51

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

代码语言:javascript
复制
[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上改变按钮,label和TextBox的位置,但是这非常麻烦和棘手,我们可以说它不是简单的。向您推荐的解决方案是在System.Windows.Forms.Form**,中创建新的标准窗体,向其中添加控件,并使用** ShowDialog() 方法显示form.。当然,它需要更多的代码来完成,但它允许您完全定制外观和行为。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18421740

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档