邮箱定制未正确显示

时间:2014-06-26 17:50:29

标签: .net vb.net winforms winapi messagebox

以前我已经@Hans Passant的this code进行了一些修改,一切正常,但现在我注意到自定义消息框没有正确显示。

这是我用自定义Windows主题显示正常消息框的方式: enter image description here

这就是它显示自定义消息框的方式: enter image description here

正如您所看到的,自定义消息框中的黑暗底层未正确分配或调整大小,或者我不知道。

我如何解决这个问题?,这是完整的代码:

' [ 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

    ' 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

    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

    <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

    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

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

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

            ' 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), _
                       (dlgRect.Bottom - dlgRect.Top), True)

            ' Resize the MessageBox Text Label
            If mFont IsNot Nothing Then
                SetWindowPos(hText, 0, 70, 35, 1920, 1080, 0)
            End If

        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

1 个答案:

答案 0 :(得分:2)

看起来调整文本标签大小的代码导致了问题

        ' Resize the MessageBox Text Label
        If mFont IsNot Nothing Then
            SetWindowPos(hText, 0, 70, 35, 1920, 1080, 0)
        End If

此代码将文本标签的大小调整为非常大的尺寸,并覆盖较低的区域。如果将高度从1080更改为更小的值(如30),问题就会消失。

我还能通过完全删除这个If语句来使它工作。 MessageBox仍然以父窗体为中心,看起来没有它就能正常工作。