Click here to Skip to main content
15,888,461 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: How do I Rectify the Closure of a Form Pin
Member 107017019-Apr-14 23:21
Member 107017019-Apr-14 23:21 
AnswerRe: How do I Rectify the Closure of a Form Pin
Alan N10-Apr-14 2:07
Alan N10-Apr-14 2:07 
GeneralRe: How do I Rectify the Closure of a Form Pin
Member 1070170110-Apr-14 2:49
Member 1070170110-Apr-14 2:49 
Question[vb2008] Chart from CSV file Pin
Member 107354929-Apr-14 5:40
Member 107354929-Apr-14 5:40 
AnswerRe: [vb2008] Chart from CSV file Pin
Richard MacCutchan9-Apr-14 6:29
mveRichard MacCutchan9-Apr-14 6:29 
AnswerRe: [vb2008] Chart from CSV file Pin
Eddy Vluggen9-Apr-14 6:31
professionalEddy Vluggen9-Apr-14 6:31 
QuestionGetting error 'AddressOf' expression cannot be converted to 'Integer' Pin
Ankush Seth7-Apr-14 23:27
Ankush Seth7-Apr-14 23:27 
AnswerRe: Getting error 'AddressOf' expression cannot be converted to 'Integer' Pin
Richard Deeming8-Apr-14 2:25
mveRichard Deeming8-Apr-14 2:25 
That's some really old code - I'm guessing it came from this[^], which says it was written for VB5.

The immediate problem is that you're trying to pass a callback delegate as a Integer. You can't do that in VB.NET; you have to pass it as a delegate instead.

A lot of the parameters that you're passing as Integer need to be IntPtr to work properly in 64-bit applications. You can usually get a good declaration for the P/Invoke methods from pinvoke.net[^].

The code also never calls CallNextHookEx, which you're supposed to call from the hook callback method.

The following code should work:
VB
Imports System.Runtime.InteropServices

Module MsgBoxHelper
    <StructLayout(LayoutKind.Sequential)> _
    Private Structure RECT
        Public ReadOnly Left As Integer
        Public ReadOnly Top As Integer
        Public ReadOnly Right As Integer
        Public ReadOnly Bottom As Integer
    End Structure

    Private Delegate Function HookProc(ByVal code As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr

    <DllImport("user32.dll", SetLastError:=True)> _
    Private Function SetWindowsHookEx(ByVal hookType As Integer, ByVal lpfn As HookProc, ByVal hMod As IntPtr, ByVal dwThreadId As Integer) As IntPtr
    End Function

    <DllImport("user32.dll")> _
    Private Function CallNextHookEx(ByVal hhk As IntPtr, ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
    End Function

    <DllImport("user32.dll", SetLastError:=True)> _
    Private Function UnhookWindowsHookEx(ByVal hhk As IntPtr) As Boolean
    End Function

    <DllImport("user32.dll", SetLastError:=True)> _
    Private Function GetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As IntPtr
    End Function

    <DllImport("kernel32.dll")> _
    Private Function GetCurrentThreadId() As Integer
    End Function

    <DllImport("user32.dll")> _
    Private Function GetWindowRect(ByVal hWnd As IntPtr, ByRef lpRect As RECT) As Boolean
    End Function

    <DllImport("user32.dll", SetLastError:=True)> _
    Private Function SetWindowPos(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 uFlags As Integer) As Boolean
    End Function


    Private Const GWL_HINSTANCE As Integer = -6
    Private Const HCBT_ACTIVATE As Integer = 5
    Private Const SWP_NOSIZE As Integer = &H1
    Private Const SWP_NOZORDER As Integer = &H4
    Private Const SWP_NOACTIVATE As Integer = &H10
    Private Const WH_CBT As Integer = 5

    Private theForm As IWin32Window
    Private plMsgHook As IntPtr

    Public Sub CenterMessageBox(ByVal parentForm As IWin32Window)
        If parentForm Is Nothing Then
            Throw New ArgumentNullException("parentForm")
        End If

        If plMsgHook <> IntPtr.Zero Then
            Throw New InvalidOperationException("The hook is already installed.")
        End If

        theForm = parentForm

        Dim lInstance As IntPtr = GetWindowLong(parentForm.Handle, GWL_HINSTANCE)
        Dim lThreadID As Integer = GetCurrentThreadId()

        plMsgHook = SetWindowsHookEx(WH_CBT, AddressOf CenterMsgCallBack, lInstance, lThreadID)
    End Sub

    Private Function CenterMsgCallBack(ByVal code As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
        If code = HCBT_ACTIVATE Then
            Dim typFormRect As RECT
            Dim typMsgRect As RECT
            Dim lxPos As Integer, lyPos As Integer

            GetWindowRect(theForm.Handle, typFormRect)
            GetWindowRect(wParam, typMsgRect)

            lxPos = CInt((typFormRect.Left + (typFormRect.Right - typFormRect.Left) / 2) - ((typMsgRect.Right - typMsgRect.Left) / 2))
            lyPos = CInt((typFormRect.Top + (typFormRect.Bottom - typFormRect.Top) / 2) - ((typMsgRect.Bottom - typMsgRect.Top) / 2))

            SetWindowPos(wParam, IntPtr.Zero, lxPos, lyPos, 0, 0, SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE)

            UnhookWindowsHookEx(plMsgHook)
            plMsgHook = IntPtr.Zero
            theForm = Nothing
        End If

        Return CallNextHookEx(IntPtr.Zero, code, wParam, lParam)
    End Function
End Module

To call it:
VB
CenterMessageBox(Me)
MessageBox.Show("This is a centered Message Box")




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


QuestionError : Property is readonly Pin
dilkonika6-Apr-14 8:42
dilkonika6-Apr-14 8:42 
AnswerRe: Error : Property is readonly Pin
Bernhard Hiller6-Apr-14 21:24
Bernhard Hiller6-Apr-14 21:24 
GeneralRe: Error : Property is readonly Pin
dilkonika7-Apr-14 2:50
dilkonika7-Apr-14 2:50 
GeneralRe: Error : Property is readonly Pin
Eddy Vluggen7-Apr-14 7:22
professionalEddy Vluggen7-Apr-14 7:22 
GeneralRe: Error : Property is readonly Pin
dilkonika7-Apr-14 7:49
dilkonika7-Apr-14 7:49 
GeneralRe: Error : Property is readonly Pin
Eddy Vluggen7-Apr-14 8:53
professionalEddy Vluggen7-Apr-14 8:53 
GeneralRe: Error : Property is readonly Pin
dilkonika7-Apr-14 9:44
dilkonika7-Apr-14 9:44 
GeneralRe: Error : Property is readonly Pin
Eddy Vluggen8-Apr-14 8:01
professionalEddy Vluggen8-Apr-14 8:01 
QuestionCalculations Help! Pin
Member 107286676-Apr-14 8:33
Member 107286676-Apr-14 8:33 
AnswerRe: Calculations Help! Pin
Dave Kreskowiak6-Apr-14 8:37
mveDave Kreskowiak6-Apr-14 8:37 
GeneralRe: Calculations Help! Pin
Member 107286676-Apr-14 8:48
Member 107286676-Apr-14 8:48 
GeneralRe: Calculations Help! Pin
Member 107286676-Apr-14 8:49
Member 107286676-Apr-14 8:49 
GeneralRe: Calculations Help! Pin
Member 107286676-Apr-14 8:50
Member 107286676-Apr-14 8:50 
GeneralRe: Calculations Help! Pin
Mycroft Holmes6-Apr-14 12:55
professionalMycroft Holmes6-Apr-14 12:55 
GeneralRe: Calculations Help! Pin
Eddy Vluggen7-Apr-14 7:46
professionalEddy Vluggen7-Apr-14 7:46 
AnswerRe: Calculations Help! Pin
ZurdoDev7-Apr-14 8:46
professionalZurdoDev7-Apr-14 8:46 
QuestionRefresh Devexpress Master-Detail gridcontrol Pin
dilkonika5-Apr-14 6:04
dilkonika5-Apr-14 6:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.