***HERES WHAT SHOULD THE PROGRAM DO....
message box should always display at the center of the owner form and not at the center of the screen(monitor) upon invoking it.
I used the SetWindowsHookEx API in vb6.0 and it executes perfectly. but when i recoded it in vb.net it doesnt execute the it did in vb6...
[HERES MY CODE IN VB.NET]
by simply calling the code inside button1 click event:
Expand|Select|Wrap|Line Numbers
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- Call Sys_WindowAdjustment(Me)
- MsgBox("THIS IS A TEST")
- End Sub
HERE'S THE ENTIRE CODE RELATED TO Sys_WindowAdjustment
Expand|Select|Wrap|Line Numbers
- Public lngHookProcHandle As Long
- 'API CONSTANTS
- Public Const GWL_HINSTANCE = (-6)
- Public Const WH_CBT = 5
- Public Const HCBT_ACTIVATE = 5
- Public Const SWP_NOZORDER = &H4
- Public Const SWP_NOSIZE = &H1
- Public Const SWP_NOACTIVATE = &H10
- Public Declare Function UnhookWindowsHookEx Lib "user32.dll" _
- (ByVal hhk As Long) As Long
- Public Declare Function SetWindowPos Lib "user32" _
- (ByVal HWND As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
- Public Declare Function GetWindowPlacement Lib "user32.dll" _
- (ByVal HWND As Long, ByVal lpwndpl As WINDOWPLACEMENT) As Long
- Public Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" _
- (ByVal HWND As Long, ByVal nIndex As Long) As Long
- Public Declare Function GetCurrentThreadId Lib "kernel32.dll" () As Long
- Public Declare Function SetWindowsHookEx Lib "user32.dll" Alias "SetWindowsHookExA" _
- (ByVal idHook As Long, ByVal lpfn As Sys_WindowHookProcDelegate, ByVal hMod As Long, ByVal dwThreadId As Long) As Long
- Public Delegate Function Sys_WindowHookProcDelegate(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
- Public Structure WINDOWRECT
- Dim LEFT As Long
- Dim TOP As Long
- Dim RIGHT As Long
- Dim BOTTOM As Long
- End Structure
- Public Structure POINTCOORDINATES
- Dim X_POINT As Long
- Dim Y_POINT As Long
- End Structure
- Public Structure WINDOWPLACEMENT
- Dim length As Long
- Dim Flags As Long
- Dim showCmd As Long
- Dim ptMinPosition As POINTCOORDINATES
- Dim ptMaxPosition As POINTCOORDINATES
- Dim rcNormalPosition As WINDOWRECT
- End Structure
- Public sysWindowPlacement As WINDOWPLACEMENT
- Public Structure WINDOWCONTAINER
- Dim W_TOP As Double
- Dim W_LEFT As Double
- Dim W_WIDTH As Double
- Dim W_HEIGHT As Double
- End Structure
- Public sysWindowContainer As WINDOWCONTAINER
- Public Function Sys_WindowAdjustment(ByVal objOwnerForm As Form) As Long
- Dim dlgWinHookDel As Sys_WindowHookProcDelegate
- Dim strErrMsg As String
- Dim strErrNum As String
- Dim strErrorDesc As String
- Dim strErrLocNum As String
- Dim lngInstanceHandle As Long
- Dim lngThreadId As Long
- On Error GoTo Sys_WindowAdjustment_Err
- strErrMsg = "CommonTools -- Sys_WindowAdjustment"
- 'Initialize window adjustment to 0
- Sys_WindowAdjustment = 0
- 'Get ownerform's top,left,height, width values
- With sysWindowContainer
- .W_TOP = objOwnerForm.Top
- .W_LEFT = objOwnerForm.Left
- .W_HEIGHT = objOwnerForm.Height
- .W_WIDTH = objOwnerForm.Width
- End With
- 'Get ownerform's instance handle
- lngInstanceHandle = GetWindowLong(objOwnerForm.Handle, GWL_HINSTANCE)
- 'Get current thread ID
- lngThreadId = GetCurrentThreadId
- 'Instantiate new window hook delegate
- dlgWinHookDel = New Sys_WindowHookProcDelegate(AddressOf Sys_WindowHookProc)
- 'Set windows hook (set position of window to be loaded)
- lngHookProcHandle = SetWindowsHookEx(WH_CBT, _
- dlgWinHookDel, _
- lngInstanceHandle, _
- lngThreadId)
- Sys_WindowAdjustment = lngHookProcHandle
- End Function
- Public Function Sys_WindowHookProc(ByVal nCode As Long, _
- ByVal wParam As Long, _
- ByVal lParam As Long) As Long
- Dim lngXPos As Long 'Window's Left position
- Dim lngYPos As Long 'Window's Top Position
- Dim dblXPos As Double
- Dim dblYPos As Double
- Dim lngPlacement As Long 'Window Placement
- On Error Resume Next
- 'If window is to be activated
- If nCode = HCBT_ACTIVATE Then
- sysWindowPlacement.length = Len(sysWindowPlacement)
- 'Get window placement
- lngPlacement = GetWindowPlacement(wParam, sysWindowPlacement)
- 'Set Top and Left position of window to be activated
- With sysWindowPlacement.rcNormalPosition
- dblYPos = sysWindowContainer.W_TOP + (sysWindowContainer.W_HEIGHT / 2 - (.BOTTOM - .TOP) * 15 / 2)
- dblXPos = sysWindowContainer.W_LEFT + (sysWindowContainer.W_WIDTH / 2 - (.RIGHT - .LEFT) * 15 / 2)
- End With
- 'Convert to pixels
- lngXPos = CLng(dblXPos / 15)
- lngYPos = CLng(dblYPos / 15)
- 'Set window position
- SetWindowPos(wParam, _
- 0, _
- lngXPos, _
- lngYPos, _
- 0, _
- 0, _
- SWP_NOSIZE Or _
- SWP_NOZORDER Or _
- SWP_NOACTIVATE)
- 'After setting window position, remove window's hook
- UnhookWindowsHookEx(lngHookProcHandle)
- End If
- 'Set Sys_WindowHookProc to false to continue normal execution
- Sys_WindowHookProc = False
- End Function