调整显示自定义字体大小的 MessageBox 窗口的大小?

Posted

技术标签:

【中文标题】调整显示自定义字体大小的 MessageBox 窗口的大小?【英文标题】:Resize MessageBox window which displays a custom font size? 【发布时间】:2013-10-13 11:41:05 【问题描述】:

编辑:

我正在使用这个自定义消息框,消息框可以接收自定义字体,我正在寻找能够通过调整消息框窗口大小在默认消息框窗口旁边使用大字体大小的方法。

但是按照@endofzero 的说明,我在调整窗口大小时发现了一个问题,它不足以解决自定义字体大小问题,如果我使用大尺寸字体,那么字体将是显示“截断”,我的意思是裁剪,即使我使用高分辨率调整消息框窗口的大小,也没办法,文本仍然对齐到左上角,并且像在一个小框中一样显示裁剪。

所以我想我还需要调整包含消息框表单内的文本消息的控件的大小,或者我完全想念的其他东西,但这可能会调整大小?以及如何?

(这个自定义消息框由 Hans Passant 编写,我稍作修改:MessageBox with custom font?)

#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

    ' 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", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function EnumThreadWindows(tid As Integer, callback As EnumThreadWndProc, lp As IntPtr) As Boolean
    End Function

    <DllImport("kernel32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function GetCurrentThreadId() As Integer
    End Function

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function GetClassName(hWnd As IntPtr, buffer As StringBuilder, buflen As Integer) As Integer
    End Function

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function GetDlgItem(hWnd As IntPtr, item As Integer) As IntPtr
    End Function

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function SendMessage(hWnd As IntPtr, msg As Integer, wp As IntPtr, lp As IntPtr) As IntPtr
    End Function

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Shared Function GetWindowRect(hWnd As IntPtr, ByRef rc As RECT) As Boolean
    End Function

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    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

    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)

        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)
        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))

        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

使用示例:

 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

【问题讨论】:

你已经打电话给MoveWindow,它可以做到这一点。将第 4 和第 5 个参数更改为所需的宽度和高度。 @endofzero 非常感谢,但是我发现了一个问题,调整窗口大小并不能解决字体大小问题,字体会显示'截断',我的意思是裁剪,即使我使用高分辨率。那么我需要在消息框窗口内调整大小以及如何调整大小? 您的问题已得到解答。如有任何新问题,请创建一个新线程。 没有人回答这个问题,只存在 cmets 但没有答案,但无论如何我已经获得了帮助以从这个问题的答案中学习,我已经编辑了这个问题以使其更多一致不要做出新的答案,如果可以,请查看我的编辑,感谢您的评论。 @Steve 如果有人可以帮助我,这是我的新问题:***.com/questions/19397549/… 【参考方案1】:

您需要添加以下之一:

 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

在您的检查窗口代码中,就在发送消息代码之后,像这样

 SetWindowPos(hText, 0, 70, 30, 170, 30, 0)

【讨论】:

感谢您提供的信息,这是非常有用的答案。只有一件事要说:我还需要使用 GetDlgItem 函数移动按钮控件。 如果有人可以帮助我,这是我的新问题:***.com/questions/19397549/…

以上是关于调整显示自定义字体大小的 MessageBox 窗口的大小?的主要内容,如果未能解决你的问题,请参考以下文章

怎么设置字体的大小?

为啥“自动调整字体”不起作用?

Help-IntelliJIDEA-2019-基础设置:8.调整字体类型和字体大小

Android开发 如何重写SimpleAdapter,自定义字体的大小?

急求一段JS,让网页字体大小随分辨率变化而自适应

windows10字体小模糊怎么调