我使用它最初是由@Hans帕桑特这里写了一个自定义的MessageBox类:Winforms-How can I make MessageBox appear centered on MainForm?
(我使用修改后的版本是在这个问题的底部)
自定义消息框可以接收自定义文本字体以显示它。
这是一个使用例:
Using New CenteredMessageBox(Me, _
New Font(New FontFamily("Lucida Console"), 16, FontStyle.Bold))
MessageBox.Show("Test Text", "Test Title", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
End Using
该问题:
所有窗口的内容需要被调整大小/手动定位,如果不是,文本将DE显示像这样裁剪:
这是结果如果我用更大的字体(我已经调整了文本控件):
http://img545.imageshack.us/img545/3462/lloi.png
所以我需要计算其位置和大小将有realatively元素的字体大小,调整窗口大小并定位按钮,这就是我所要求的。我如何做到这一点?我需要的不仅仅是一个公式,因为数学我做得不好。
的元素:
我心里有以下几个要素:
- MessageBox的文本字体大小。
- 包含并显示文本的Messagebox静态控件。 (可以是1个按钮,2个按钮或3个按钮的组合,具有不同的默认位置)
- 消息框窗口包含所有这些元素的窗体。
按钮(一个或多个)变量:
- 如果仅仅是1个布顿在消息框窗口(例如:“好吧”),它会被神韵:中等,但如果是3个按钮(例如:“ AbortRetryIgnore'),那么第一个按钮将被分配到左下角,第二个按钮到中间,最后一个按钮到右下角,我认为没有必要解释这个原因,因为它们都知道msgbox如何显示它们的按钮。
Dim Text_width As Integer = 0 Dim Text_height As Integer = 0 Dim Text_Container_width As Integer = 0 Dim Text_Container_height As Integer = 0 ' Don't calculate the exact size of messagebox window, ' just I think only needs to sum an extra size to a default messagebox rectangle Dim Messagebox_window_extra_width As Integer = 0 Dim Messageboxwindow_extra_height As Integer = 0 ' This represents the first button, ' in a messagebox button combination of 1, 2, or 3 buttons, ' this button could be displayed alligned at bottom-middle (as unique button), ' or could be alligned at bottom-left (as the first of 2 of 3 button combination). Dim Button_1_Pos_X As Integer = 0 Dim Button_1_Pos_Y As Integer = 0 ' This button represents the second button ' in a messagebox button combination of 2 or 3 buttons, ' the buton could exist or could not ' and could be displayed alligned at bottom-middle (in the middle of 3 buttons), ' this button could be displayed alligned at bottom-right (as second and last button). Dim Button_2_Pos_X As Integer = 0 Dim Button_2_Pos_Y As Integer = 0 ' This button represents the third button ' a messagebox button combination of 2 or 3 buttons, ' the buton could exist or could not ' and could be displayed alligned at bottom-right (as third and last button). Dim Button_3_Pos_X As Integer = 0 Dim Button_3_Pos_Y As Integer = 0
:
我不是一个算术大师,所以请原谅我,如果我错过了一些必要的值,但我认为这些将是必要的值/代码中的变量与合作这就是我可以自己做,我一直在测试存储这样的消息框按钮元素:
Dim button_Ok As IntPtr = GetDlgItem(hWnd, 1) ' The 'Ok' button.
Dim button_Cancel As IntPtr = GetDlgItem(hWnd, 2) ' the 'Cancel' button.
Dim button_Abort As IntPtr = GetDlgItem(hWnd, 3) ' the 'Abort' button.
Dim button_Retry As IntPtr = GetDlgItem(hWnd, 4) ' the 'Retry' button.
Dim button_Ignore As IntPtr = GetDlgItem(hWnd, 5) ' the 'Ignore' button.
Dim button_Yes As IntPtr = GetDlgItem(hWnd, 6) ' the 'Yes' button.
Dim button_No As IntPtr = GetDlgItem(hWnd, 7) ' the 'NO' button.
(他们总是有相同的项目指标无论在MessageBox布敦组合)
要调整在MessageBox窗口我可以使用的MoveWindow API这样的:
' This is to resize and positionate the messagebox window
MoveWindow(hWnd, _
frmRect.Left + (frmRect.Width - dlgRect.Right + dlgRect.Left) \ 2, _
frmRect.Top + (frmRect.Height - dlgRect.Bottom + dlgRect.Top) \ 2, _
(dlgRect.Right - dlgRect.Left) + Messagebox_window_extra_width, _
(dlgRect.Bottom - dlgRect.Top) + Messagebox_window_extra_height, True)
而调整大小的其余元素(文本容器,按钮),调整在MessageBox形式然后我可以使用SetWindowPos API后像这样的:
' Text container:
SetWindowPos(hText, 0, 70, 30, 1920, 1080, 0) ' Values are not perfect calculated but works fine
' Messagebox buttons:
' SetWindowPos(button_Ok, 0, 0, 0, 0, 0, 0)
' SetWindowPos(button_Cancel, 0, 0, 0, 0, 0, 0)
' SetWindowPos(button_Abort, 0, 0, 0, 0, 0, 0)
' SetWindowPos(button_Retry, 0, 0, 0, 0, 0, 0)
' SetWindowPos(button_Ignore, 0, 0, 0, 0, 0, 0)
' SetWindowPos(button_Yes, 0, 0, 0, 0, 0, 0)
' SetWindowPos(button_No, 0, 0, 0, 0, 0, 0)
MessageBox类
这里被搞砸了一切我之前提到过的事情:
' [ Centered MessageBox ]
'
' The author of the original code is Hans Passant: https://stackoverflow.com/questions/2576156/winforms-how-can-i-make-messagebox-appear-centered-on-mainform
'
' Examples :
'
' Using New CenteredMessageBox(Me, New Font(New FontFamily("Lucida Console"), Font.SizeInPoints, FontStyle.Bold))
' MessageBox.Show("Test Text", "Test Title", MessageBoxButtons.OK, MessageBoxIcon.Information)
' End Using
#Region " Centered MessageBox Class"
Imports System.Drawing
Imports System.Runtime.InteropServices
Imports System.Text
Imports System.Windows.Forms
Class CenteredMessageBox : Implements IDisposable
Private mTries As Integer = 0
Private mOwner As Form
Private mFont As Font
Dim Text_width As Integer = 0
Dim Text_height As Integer = 0
Dim Text_Container_width As Integer = 0
Dim Text_Container_height As Integer = 0
Dim Messagebox_window_extra_width As Integer = 0
Dim Messagebox_window_extra_height As Integer = 0
Dim Button_1_Pos_X As Integer = 0 ' "OK" Button
Dim Button_1_Pos_Y As Integer = 0 ' "OK" Button
Dim Button_2_Pos_X As Integer = 0 ' This button could not exist
Dim Button_2_Pos_Y As Integer = 0 ' This button could not exist
Dim Button_3_Pos_X As Integer = 0 ' This button could not exist
Dim Button_3_Pos_Y As Integer = 0 ' This button could not exist
' P/Invoke declarations
Private Const WM_SETFONT As Integer = &H30
Private Const WM_GETFONT As Integer = &H31
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 GetDlgItem(hWnd As IntPtr, item As Integer) As IntPtr
End Function
<DllImport("user32.dll")> _
Private Shared Function SendMessage(hWnd As IntPtr, msg As Integer, wp As IntPtr, lp As IntPtr) As IntPtr
End Function
<DllImport("user32.dll")> _
Shared Function GetWindowRect(hWnd As IntPtr, ByRef rc As RECT) As Boolean
End Function
<DllImport("user32.dll")> _
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
Structure RECT
Public Left As Integer
Public Top As Integer
Public Right As Integer
Public Bottom As Integer
End Structure
Friend Declare Function SetWindowPos Lib "user32" (ByVal hwnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As UInt32) As Boolean
Public Sub New(owner As Form, Optional Custom_Font As Font = Nothing)
mOwner = owner
mFont = Custom_Font
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
' Got it, get the STATIC control that displays the text
Dim hText As IntPtr = GetDlgItem(hWnd, &HFFFF)
' Get the messagebox button elements
Dim button_Ok As IntPtr = GetDlgItem(hWnd, 1) ' The 'Ok' button.
Dim button_Cancel As IntPtr = GetDlgItem(hWnd, 2) ' the 'Cancel' button.
Dim button_Abort As IntPtr = GetDlgItem(hWnd, 3) ' the 'Abort' button.
Dim button_Retry As IntPtr = GetDlgItem(hWnd, 4) ' the 'Retry' button.
Dim button_Ignore As IntPtr = GetDlgItem(hWnd, 5) ' the 'Ignore' button.
Dim button_Yes As IntPtr = GetDlgItem(hWnd, 6) ' the 'Yes' button.
Dim button_No As IntPtr = GetDlgItem(hWnd, 7) ' the 'NO' button.
Dim frmRect As New Rectangle(mOwner.Location, mOwner.Size)
Dim dlgRect As RECT
GetWindowRect(hWnd, dlgRect)
If hText <> IntPtr.Zero Then
If mFont Is Nothing Then
' Get the current font
mFont = Font.FromHfont(SendMessage(hText, WM_GETFONT, IntPtr.Zero, IntPtr.Zero))
End If
SendMessage(hText, WM_SETFONT, mFont.ToHfont(), New IntPtr(1))
' Just here is an empty space where I can test some operations:
'
' Messagebox_window_extra_width = (mFont.Height \ mFont.Size) + (dlgRect.Right)
' Messagebox_window_extra_height = mFont.Height +
' This is to resize and positionate the messagebox window:
MoveWindow(hWnd, _
frmRect.Left + (frmRect.Width - dlgRect.Right + dlgRect.Left) \ 2, _
frmRect.Top + (frmRect.Height - dlgRect.Bottom + dlgRect.Top) \ 2, _
(dlgRect.Right - dlgRect.Left) + Messagebox_window_extra_width, _
(dlgRect.Bottom - dlgRect.Top) + Messagebox_window_extra_height, True)
' And this is to resize and positionate the rest elements:
'
' Text container:
SetWindowPos(hText, 0, 70, 30, 1920, 1080, 0)
'
' Messagebox buttons:
' SetWindowPos(button_Ok, 0, 0, 0, 0, 0, 0)
' SetWindowPos(button_Cancel, 0, 0, 0, 0, 0, 0)
' SetWindowPos(button_Abort, 0, 0, 0, 0, 0, 0)
' SetWindowPos(button_Retry, 0, 0, 0, 0, 0, 0)
' SetWindowPos(button_Ignore, 0, 0, 0, 0, 0, 0)
' SetWindowPos(button_Yes, 0, 0, 0, 0, 0, 0)
' SetWindowPos(button_No, 0, 0, 0, 0, 0, 0)
End If
' Done
Return False
End Function
Public Sub Dispose() Implements IDisposable.Dispose
mTries = -1
mOwner = Nothing
If mFont IsNot Nothing Then mFont.Dispose()
End Sub
End Class
#End Region
UPDATE
这是我在尝试使用@Grahamvs解决方案:
Using New CenteredMessageBox(Me, New Font(New FontFamily("Lucida Console"), Font.SizeInPoints, FontStyle.Bold))
MessageBox.Show("Test Text", "Test Title", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Using
使用Win32函数GetTextExtent确定要衡量任何文本的高x宽。 NET等价物是'graphics.MeasureString',在这里可能更合适。 (没读过整个WOT)。您是否尝试过AutoSize Label? – Plutonix
@Plutonix嗨,谢谢API函数信息,我会尝试一下当我有时间测试它,默认的messagebox标签没有autosize,或者至少如果我改变它不会调整大小的文本,但那么你的意思是我可以改变其他外部公式的autosize属性?如果是的话,怎么样?,也许这会简化很多事情,谢谢你。 – ElektroStudios
@Plutonix没有办法看到如何使用GetTextExtent,该函数是不是pinvoke.net也我读过,该功能是C/C + +,我无法找到任何relatod该功能在VB.NET和图形.MeasureString我不知道如何使用它 – ElektroStudios