0

EDIT:

I'm using this custom messagebox, the messagebox can receive a custom font, I'm looking for the way to be able to use big font sizes niside the default messagebox window by resizing the messagebox window.

But following the @endofzero instructions I've found a problem when resizing the window it is not sufficient to solve the custom font size problem, if I use a big size font then the font will be displayed 'truncated', I mean cropped, even If I resize the messagebox window using a high resolution, no way, the text stills alligned to top-left and is displayed cropped like in a little box.

So I imagine that I will need to resize also the control who contains the text message inside the messagebox form, or something else that I totally miss, but this is possibly to resize? and how?

( This custom messagebox is written by Hans Passant and modified a little by me: 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

Usage example:

 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
Community
  • 1
  • 1
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
  • 2
    You are already calling `MoveWindow` which can do this. Change the 4th and 5th arguments to the desired width and height. – endofzero Oct 14 '13 at 18:18
  • @endofzero Thankyou so much, but I've found a problem, resizing the window will not solve the font size problem, the font will be displayed 'truncated', I mean cropped, even If I use a high resolution. then what is the thing that I need to resize inside the messagebox window and how? – ElektroStudios Oct 15 '13 at 03:50
  • Your question was answered. Please start a new thread for any new questions. – Steve Oct 15 '13 at 17:46
  • No body answered the question, only exist comments but not answers, but anyway I've made a bounty to get help to learn from answers in this question, I've edited the question to make it more consistent to don't make a new answer, please see my edits if can, thanks for comment. – ElektroStudios Oct 15 '13 at 17:56
  • @Steve Here is my new question about this if someone could help me: http://stackoverflow.com/questions/19397549/which-is-the-mathematic-formula-to-calculate-the-size-and-positions-for-this-win – ElektroStudios Oct 16 '13 at 07:44

1 Answers1

3

You will need to add one of these:

 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

and in your checkwindow code, right after the send message code, something like this

 SetWindowPos(hText, 0, 70, 30, 170, 30, 0)
Rob
  • 3,488
  • 3
  • 32
  • 27
  • Thankyou for the info, it is very useful answer. Only one thing to say: Also I needed to move the button controls with GetDlgItem function. – ElektroStudios Oct 16 '13 at 05:38
  • Here is my new question about this if someone could help me: http://stackoverflow.com/questions/19397549/which-is-the-mathematic-formula-to-calculate-the-size-and-positions-for-this-win – ElektroStudios Oct 16 '13 at 07:44