• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3077
  • Last Modified:

msgbox VB.NET - set position screen centre

Hi, how to set msgbox position at screen centre or form ... in VB.NET? I have to use msgbox and not messagebox (due to VB6 conversion project). Does it work with messagebox?

In dual screen or large resolution ... the msgbox goes crazy. Any code sample and help is much appreciated. Cheers.

0
tjoseph833
Asked:
tjoseph833
4 Solutions
 
John (Yiannis) ToutountzoglouCommented:
Here is a complete code how to center "messaageBox"...
I am trying to find out about MsgBox
http://www.freevbcode.com/ShowCode.asp?ID=952
0
 
John (Yiannis) ToutountzoglouCommented:
Another Option is make you "Own" MessageBox Creating a new Form and Open it as ShowDialog whenever you want
0
 
Éric MoreauSenior .Net ConsultantCommented:
if you want to build your own messagebox, chech http://www.emoreau.com/Entries/Articles/2006/01/A-custom-MessageBox.aspx
0
 
Meir RivkinFull stack Software EngineerCommented:
this is what you do.

1. add the following declarations:

Private Const MSG_BOX_TITLE As String = "MsgBoxTitle"

    Structure RECT
        Public Left As Integer
        Public Top As Integer
        Public Right As Integer
        Public Bottom As Integer
    End Structure

    Declare Function GetWindowRect Lib "user32" Alias "GetWindowRect" _
    (ByVal hwnd As Integer, ByRef lpRect As RECT) As Integer

    <DllImport("user32.dll")> _
    Public Function MoveWindow(ByVal hWnd As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal bRepaint As Boolean) As Boolean
    End Function

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Public Function FindWindow( _
     ByVal lpClassName As String, _
     ByVal lpWindowName As String) As IntPtr
    End Function


2. add the following handler:

 Sub BackgroundProcess()
        Dim h As IntPtr = Nothing
        Dim rct As RECT
        Dim res As Int32 = 0
        While (res <> 1)
            h = FindWindow(Nothing, MSG_BOX_TITLE )
            res = GetWindowRect(h, rct)
        End While

        Dim x As Int32 = (Screen.PrimaryScreen.Bounds.Width - (rct.Right - rct.Left)) / 2
        Dim y As Int32 = (Screen.PrimaryScreen.Bounds.Height - (rct.Bottom - rct.Top)) / 2

        MoveWindow(h, x, y, rct.Right - rct.Left, rct.Bottom - rct.Top, True)
    End Sub

3. just before calling you MsgBox line, add the following:

  Dim t As Thread = New Thread(AddressOf BackgroundProcess)
        t.Start()

        Microsoft.VisualBasic.MsgBox("MsgBox Body...", MsgBoxStyle.ApplicationModal, MSG_BOX_TITLE )

and that's it.

the thread will wait till your msg box is display and then it will move it with a blink of an eye to the center of the screen.
0
 
tjoseph833Author Commented:
thanks everyone ...
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now