2013-10-13 954 views
0

編輯:調整顯示自定義字體大小的MessageBox窗口?

我使用這個自定義消息框,在MessageBox可以接收自定義字體,我正在尋找的方式,能夠通過調整用大字體大小niside默認的消息框窗口消息框窗口。

但是,如果我使用大尺寸字體,字體會顯示'truncated',那麼在調整窗口大小時,發現問題並不足以解決自定義字體大小問題。 ,我的意思是裁剪,即使如果我使用高分辨率調整消息框窗口大小,沒辦法,文本仍然會左上角,並顯示爲像在一個小框中裁剪。

所以我想我還需要調整包含在messagebox窗體內的文本消息或其他我完全錯過的控件,但這可能是調整大小?如何?

(這種習俗消息框是由Hans帕桑特編寫和修改了一點我: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 
+2

您已經在調用'MoveWindow',它可以做到這一點。將第4個和第5個參數更改爲所需的寬度和高度。 – endofzero

+0

@endofzero謝謝你這麼多,但我發現一個問題,調整窗口大小不會解決字體大小的問題,字體將顯示'截斷',我的意思是裁剪,即使我使用高分辨率。那麼我需要在消息框窗口中調整大小以及如何操作? – ElektroStudios

+0

您的問題已回答。請爲任何新問題啓動一個新線程。 – Steve

回答

3

您將需要添加的其中之一:

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 

並在您的檢查窗口代碼中,在發送消息代碼之後,事端g像這樣

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

Thankyou的信息,這是非常有用的答案。只有一件事要說:另外我需要用GetDlgItem函數來移動按鈕控件。 – ElektroStudios

+0

這是我關於這個新的問題,如果有人可以幫助我:http://stackoverflow.com/questions/19397549/which-is-the-mathematic-formula-to-calculate-the-size-and-positions-for-這種雙贏 – ElektroStudios