显示以表单为中心的MessageBox

时间:2013-04-09 14:24:52

标签: vb.net visual-studio position messagebox

有一种方法可以使MessageBox居中而不进行子类化或挂钩?

我正在寻找VB.NET代码。

4 个答案:

答案 0 :(得分:8)

VB.NET的解决方案:

此代码摘自@Hans Passant asnwer:Winforms-How can I make MessageBox appear centered on MainForm?

Centered_MessageBox.vb

Imports System.Text
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Runtime.InteropServices

Class Centered_MessageBox
    Implements IDisposable
    Private mTries As Integer = 0
    Private mOwner As Form

    Public Sub New(owner As Form)
        mOwner = owner
        owner.BeginInvoke(New MethodInvoker(AddressOf findDialog))
    End Sub

    Private Sub findDialog()
        ' Enumerate windows to find the message box
        If mTries < 0 Then
            Return
        End If
        Dim callback As New EnumThreadWndProc(AddressOf checkWindow)
        If EnumThreadWindows(GetCurrentThreadId(), callback, IntPtr.Zero) Then
            If System.Threading.Interlocked.Increment(mTries) < 10 Then
                mOwner.BeginInvoke(New MethodInvoker(AddressOf findDialog))
            End If
        End If
    End Sub
    Private Function checkWindow(hWnd As IntPtr, lp As IntPtr) As Boolean
        ' Checks if <hWnd> is a dialog
        Dim sb As New StringBuilder(260)
        GetClassName(hWnd, sb, sb.Capacity)
        If sb.ToString() <> "#32770" Then
            Return True
        End If
        ' Got it
        Dim frmRect As New Rectangle(mOwner.Location, mOwner.Size)
        Dim dlgRect As RECT
        GetWindowRect(hWnd, dlgRect)
        MoveWindow(hWnd, frmRect.Left + (frmRect.Width - dlgRect.Right + dlgRect.Left) \ 2, frmRect.Top + (frmRect.Height - dlgRect.Bottom + dlgRect.Top) \ 2, dlgRect.Right - dlgRect.Left, dlgRect.Bottom - dlgRect.Top, True)
        Return False
    End Function
    Public Sub Dispose() Implements IDisposable.Dispose
        mTries = -1
    End Sub

    ' P/Invoke declarations
    Private Delegate Function EnumThreadWndProc(hWnd As IntPtr, lp As IntPtr) As Boolean
    <DllImport("user32.dll")> _
    Private Shared Function EnumThreadWindows(tid As Integer, callback As EnumThreadWndProc, lp As IntPtr) As Boolean
    End Function
    <DllImport("kernel32.dll")> _
    Private Shared Function GetCurrentThreadId() As Integer
    End Function
    <DllImport("user32.dll")> _
    Private Shared Function GetClassName(hWnd As IntPtr, buffer As StringBuilder, buflen As Integer) As Integer
    End Function
    <DllImport("user32.dll")> _
    Private Shared Function GetWindowRect(hWnd As IntPtr, ByRef rc As RECT) As Boolean
    End Function
    <DllImport("user32.dll")> _
    Private Shared Function MoveWindow(hWnd As IntPtr, x As Integer, y As Integer, w As Integer, h As Integer, repaint As Boolean) As Boolean
    End Function
    Private Structure RECT
        Public Left As Integer
        Public Top As Integer
        Public Right As Integer
        Public Bottom As Integer
    End Structure
End Class
  

用法:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Using New Centered_MessageBox(Me)
        MessageBox.Show("Test Text", "Test Title", MessageBoxButtons.OK)
    End Using
End Sub

答案 1 :(得分:1)

可悲的是,没有办法将MessageBox置于父母的中心。默认情况下,它以屏幕为中心,无法更改。

答案 2 :(得分:1)

创建自己的-轻松创建表单(摆脱属性设置为false的控制箱) 放置文本框(称为TextBox_Prompt)并将其设置为多行属性 在文本框下方添加3个按钮(宽度/高度足以舒适地按住“ CANCEL”) 在表单中添加以下代码(我使用|字符表示换行符):

Public Class frmMsgBox
 Private mName As String = "Message Box"           ' default name for form
 Private mLocation As Point = New Point(400, 400)  ' default location in case user does set 
 Private mStyle As MsgBoxStyle
 Private mPrompt As String
 Private mResult As MsgBoxResult
 Private b1Result As MsgBoxResult
 Private b2Result As MsgBoxResult
 Private b3Result As MsgBoxResult
 Public WriteOnly Property Style As MsgBoxStyle
    Set(value As MsgBoxStyle)
        mStyle = value
    End Set
 End Property
 Public WriteOnly Property Prompt As String
    Set(value As String)
        mPrompt = value
    End Set
 End Property
 Public ReadOnly Property Result As MsgBoxResult
    Get
        Return mResult
    End Get
 End Property
 Public WriteOnly Property pLocation As Point
    Set(value As Point)
        mLocation = value
    End Set
 End Property
 Public WriteOnly Property sName As String
    Set(value As String)
        mName = value
    End Set
 End Property

 Private Sub frmMsgBox_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim strPrompt() As String = mPrompt.Split("|")   ' use | for splitting lines
    Dim sWidth As Integer = 0
    Dim sHeight As String = ""
    Me.Text = mName
    For Each sLine As String In strPrompt   ' get maximum width and height necessary for Prompt TextBox
        sWidth = Math.Max(sWidth, TextRenderer.MeasureText(sLine, TextBox_Prompt.Font).Width)
        sHeight += "@" + vbCrLf ' TextRenderer.MeasureText("@", TextBox_Prompt.Font).Height
        TextBox_Prompt.Text += sLine + vbCrLf
    Next
    TextBox_Prompt.Width = Math.Min(800, sWidth + 5)   ' set max width arbitrarily at 800
    TextBox_Prompt.Height = Math.Min(600, TextRenderer.MeasureText(sHeight, TextBox_Prompt.Font).Height)     ' set max height to 600 pixels
    Me.Width = Math.Max(Me.Width, TextBox_Prompt.Width + Me.Width - Me.ClientRectangle.Width + 20)
    TextBox_Prompt.Left = Math.Max(10, (Me.ClientRectangle.Width - TextBox_Prompt.Width) \ 2)
    Button1.Top = TextBox_Prompt.Top + TextBox_Prompt.Height + 20
    Button2.Top = Button1.Top : Button3.Top = Button1.Top
    Me.Height = Me.Height - Me.ClientRectangle.Height + 2 * TextBox_Prompt.Top + TextBox_Prompt.Height + Button1.Height + 20

    Dim Space2 As Integer = (Me.ClientRectangle.Width - 2 * Button1.Width) / 3
    Dim Space3 As Integer = (Me.ClientRectangle.Width - 3 * Button1.Width) / 4
    Select Case mStyle
        Case MsgBoxStyle.AbortRetryIgnore
            Button1.Text = "Abort" : Button2.Text = "Retry" : Button3.Text = "Ignore"
            Button1.Left = Space3
            Button2.Left = 2 * Space3 + Button1.Width
            Button3.Left = 3 * Space3 + 2 * Button1.Width
            b1Result = MsgBoxResult.Abort : b2Result = MsgBoxResult.Retry : b3Result = MsgBoxResult.Ignore
        Case MsgBoxStyle.YesNoCancel
            Button1.Text = "Yes" : Button2.Text = "No" : Button3.Text = "Cancel"
            Button1.Left = Space3
            Button2.Left = 2 * Space3 + Button1.Width
            Button3.Left = 3 * Space3 + 2 * Button1.Width
            b1Result = MsgBoxResult.Yes : b2Result = MsgBoxResult.No : b3Result = MsgBoxResult.Cancel
        Case MsgBoxStyle.YesNo
            Button1.Text = "Yes" : Button2.Text = "No" : Button3.Visible = False
            Button1.Left = Space2
            Button2.Left = 2 * Space2 + Button1.Width
            b1Result = MsgBoxResult.Yes : b2Result = MsgBoxResult.No
        Case MsgBoxStyle.OkCancel
            Button1.Text = "Ok" : Button2.Text = "Cancel" : Button3.Visible = False
            Button1.Left = Space2
            Button2.Left = 2 * Space2 + Button1.Width
            b1Result = MsgBoxResult.Ok : b2Result = MsgBoxResult.Cancel
        Case MsgBoxStyle.RetryCancel
            Button1.Text = "Retry" : Button2.Text = "Cancel" : Button3.Visible = False
            Button1.Left = Space2
            Button2.Left = 2 * Space2 + Button1.Width
            b1Result = MsgBoxResult.Retry : b2Result = MsgBoxResult.Cancel
        Case MsgBoxStyle.OkOnly
            Button1.Visible = False : Button2.Text = "Ok" : Button3.Visible = False
            Button1.Left -= Space2 : Button2.Width += 2 * Space2
            b2Result = MsgBoxResult.Ok
    End Select
    Me.Location = New Point(mLocation.X - Me.Width \ 2, mLocation.Y - Me.Height \ 2)
 End Sub
 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    mResult = b1Result
    Me.Close()
 End Sub
 Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    mResult = b2Result
    Me.Close()
 End Sub
 Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    mResult = b3Result
    Me.Close()
 End Sub
End Class

要使用您的程序,您可以执行以下操作

Dim ans as MsgBoxResult
Using f As New frmMsgBox With {
            .sName = "form tile goes here ",
            .Style = MsgBoxStyle.YesNoCancel,            ' or whatever style
            .Prompt = "Your prompt|2nd line||4th line",
            .pLocation = New Point(Me.Left + Me.Width \ 2, Me.Top + Me.Height \ 2)
        }                            ' this location will center MsgBox on form
            f.ShowDialog()
            ans = f.Result
        End Using
        If ans = MsgBoxResult.Yes Then
           'do whatever
        ElseIf ans = MsgBoxResult.No then
            'do not whatever
        Else  ' was cancel
            ' do cancel
        End If

我一直在使用此表格 您还可以将图片属性/框以及其他内容添加到表单中。 乔治

答案 3 :(得分:1)

这是我自己的消息框使用C#winform,除了在中心实现父窗体外,还可以自定义按钮文字和图标。您可以自行将其转换为 VB 代码。

using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace BluePointLilac.Methods
{
    /// <summary>在窗体居中显示的MessageBox</summary>
    public class MessageBoxEx
    {
        public static DialogResult Show(string text, string caption = null,
            MessageBoxButtons buttons = MessageBoxButtons.OK,
            MessageBoxIcon boxIcon = MessageBoxIcon.None)
        {
            using(MessageBoxForm frm = new MessageBoxForm(text, caption, buttons, boxIcon))
            {
                return frm.ShowDialog();
            }
        }

        public static string Show(string text, string caption,
            string[] buttonTexts, Image boxImaage, bool cancelBox = false)
        {
            using(MessageBoxForm frm = new MessageBoxForm(text, caption, buttonTexts, boxImaage, cancelBox))
            {
                frm.ShowDialog();
                return frm.Tag?.ToString();
            }
        }

        sealed class MessageBoxForm : Form
        {
            private MessageBoxForm(string text, string caption)
            {
                lblText.Text = text;
                this.Text = caption;
                this.Font = SystemFonts.MessageBoxFont;
                this.ShowIcon = this.ShowInTaskbar = false;
                this.MaximizeBox = this.MinimizeBox = false;
                this.FormBorderStyle = FormBorderStyle.FixedSingle;
                this.StartPosition = FormStartPosition.CenterParent;
            }

            public MessageBoxForm(string text, string caption,
                string[] buttonTexts, Image boxImage, bool cancelBox) : this(text, caption)
            {
                this.CancelBox = cancelBox;
                this.InitializeComponents(buttonTexts, boxImage);
                foreach(Button button in flpButtons.Controls)
                {
                    button.Click += (sender, e) =>
                    {
                        this.Tag = button.Text;
                        this.Close();
                    };
                }
            }

            public MessageBoxForm(string text, string caption,
                MessageBoxButtons buttons, MessageBoxIcon boxIcon) : this(text, caption)
            {
                string[] buttonTexts = null;
                Image boxImage = null;
                switch(buttons)
                {
                    case MessageBoxButtons.OK:
                        buttonTexts = new[] { "OK" }; break;
                    case MessageBoxButtons.OKCancel:
                        buttonTexts = new[] { "Cancel", "OK" }; break;
                    case MessageBoxButtons.AbortRetryIgnore:
                        buttonTexts = new[] { "&Ignore", "&Retry", "&Abort" }; break;
                    case MessageBoxButtons.YesNoCancel:
                        buttonTexts = new[] { "Cancel", "&No", "&Yes" }; break;
                    case MessageBoxButtons.YesNo:
                        buttonTexts = new[] { "&No", "&Yes" }; break;
                    case MessageBoxButtons.RetryCancel:
                        buttonTexts = new[] { "Cancel", "&Retry" }; break;
                }
                switch(boxIcon)
                {
                    case MessageBoxIcon.Question:
                        boxImage = SystemIcons.Question.ToBitmap(); break;
                    case MessageBoxIcon.Error:
                        boxImage = SystemIcons.Error.ToBitmap(); break;
                    case MessageBoxIcon.Warning:
                        boxImage = SystemIcons.Warning.ToBitmap(); break;
                    case MessageBoxIcon.Information:
                        boxImage = SystemIcons.Information.ToBitmap(); break;
                }
                this.CancelBox = !buttonTexts.Contains("Cancel");
                this.InitializeComponents(buttonTexts, boxImage);
                foreach(Button button in flpButtons.Controls)
                {
                    button.Click += (sender, e) =>
                    {
                        switch(button.Text)
                        {
                            case "OK":
                                this.DialogResult = DialogResult.OK; break;
                            case "Cancel":
                                this.DialogResult = DialogResult.Cancel; break;
                            case "&Yes":
                                this.DialogResult = DialogResult.Yes; break;
                            case "&No":
                                this.DialogResult = DialogResult.No; break;
                            case "&Abort":
                                this.DialogResult = DialogResult.Abort; break;
                            case "&Retry":
                                this.DialogResult = DialogResult.Retry; break;
                            case "&Ignore":
                                this.DialogResult = DialogResult.Ignore; break;
                        }
                    };
                }
            }

            private void InitializeComponents(string[] buttonTexts, Image boxImage)
            {
                int w1 = 0;
                Size buttonSize = new Size(75, 27);
                for(int i = 0; i < buttonTexts.Length; i++)
                {
                    Button button = new Button
                    {
                        Margin = new Padding(0, 12, 12, 12),
                        Parent = flpButtons,
                        AutoSize = true,
                        Text = buttonTexts[i],
                    };
                    button.Width = Math.Max(buttonSize.Width, button.Width);
                    button.Height = Math.Max(buttonSize.Height, button.Height);
                    buttonSize = button.Size;
                    w1 += button.Width + button.Margin.Horizontal;
                }
                picIcon.Image = boxImage;
                if(boxImage == null)
                {
                    picIcon.Visible = false;
                    lblText.Left = 35;
                }
                pnlInfo.Controls.AddRange(new Control[] { picIcon, lblText });
                this.Controls.AddRange(new Control[] { pnlInfo, flpButtons });
                pnlInfo.Height = lblText.Height + lblText.Top * 2;
                int w2 = lblText.Right + picIcon.Left;
                int w = Math.Max(w1, w2);
                int h = pnlInfo.Height + flpButtons.Height;
                this.ClientSize = new Size(w, h);
            }

            readonly FlowLayoutPanel flpButtons = new FlowLayoutPanel
            {
                Padding = new Padding(0, 0, 12, 0),
                FlowDirection = FlowDirection.RightToLeft,
                BackColor = default,
                Dock = DockStyle.Bottom,
                Height = 50
            };
            readonly Panel pnlInfo = new Panel
            {
                BackColor = Color.White,
                Dock = DockStyle.Top,
            };
            readonly PictureBox picIcon = new PictureBox
            {
                SizeMode = PictureBoxSizeMode.AutoSize,
                Location = new Point(30, 30)
            };
            readonly Label lblText = new Label
            {
                Location = new Point(67, 32),
                AutoSize = true,
            };

            readonly bool CancelBox = false;

            protected override CreateParams CreateParams
            {
                get
                {
                    const int CP_NOCLOSE_BUTTON = 0x200;
                    CreateParams cp = base.CreateParams;
                    if(CancelBox) cp.ClassStyle |= CP_NOCLOSE_BUTTON; //禁用关闭按钮
                    return cp;
                }
            }

            protected override bool ProcessDialogKey(Keys keyData)
            {
                if(ModifierKeys == Keys.None)
                {
                    if(!CancelBox && keyData == Keys.Escape)
                    {
                        this.DialogResult = DialogResult.Cancel; //ESC键关闭窗体并返回“取消”
                        return true;
                    }
                }
                return base.ProcessDialogKey(keyData);
            }

            protected override void OnLoad(EventArgs e)
            {
                this.Owner = Form.ActiveForm;
                if(this.Owner == null) this.StartPosition = FormStartPosition.CenterScreen;
                base.OnLoad(e);
            }
        }
    }
}