By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,458 Members | 1,252 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,458 IT Pros & Developers. It's quick & easy.

SetWindowsHookEx API -- PLEASE HELP!

krungkrung
P: 18
Hi everyone! Im new to vb.net. I'm currently recoding my vb6.0 codes to vb.net(for upgrading purposes), unfortunately some of the codes dont work/execute the way it executes in vb6.0, though I never get any error. That is, the program runs without error but the function being called dont execute. Can anybody help me figure out this matter??

***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
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.  
  3. Call Sys_WindowAdjustment(Me)
  4. MsgBox("THIS IS A TEST")
  5.  
  6. End Sub
  7.  
**** "THIS IS A TEST" message box should always appear at the center of the owner form. even if i move the owner form anywhere in the screen, the msgbox shuld always at the center of it..


HERE'S THE ENTIRE CODE RELATED TO Sys_WindowAdjustment


Expand|Select|Wrap|Line Numbers
  1.     Public lngHookProcHandle As Long
  2.  
  3.     'API CONSTANTS
  4.     Public Const GWL_HINSTANCE = (-6)
  5.     Public Const WH_CBT = 5
  6.     Public Const HCBT_ACTIVATE = 5
  7.     Public Const SWP_NOZORDER = &H4
  8.     Public Const SWP_NOSIZE = &H1
  9.     Public Const SWP_NOACTIVATE = &H10
  10.  
  11.  
  12.     Public Declare Function UnhookWindowsHookEx Lib "user32.dll" _
  13.                 (ByVal hhk As Long) As Long
  14.     Public Declare Function SetWindowPos Lib "user32" _
  15.                 (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
  16.     Public Declare Function GetWindowPlacement Lib "user32.dll" _
  17.                 (ByVal HWND As Long, ByVal lpwndpl As WINDOWPLACEMENT) As Long
  18.     Public Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" _
  19.                 (ByVal HWND As Long, ByVal nIndex As Long) As Long
  20.  
  21.     Public Declare Function GetCurrentThreadId Lib "kernel32.dll" () As Long
  22.     Public Declare Function SetWindowsHookEx Lib "user32.dll" Alias "SetWindowsHookExA" _
  23.                 (ByVal idHook As Long, ByVal lpfn As Sys_WindowHookProcDelegate, ByVal hMod As Long, ByVal dwThreadId As Long) As Long
  24.  
  25.     Public Delegate Function Sys_WindowHookProcDelegate(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  26.  
  27.     Public Structure WINDOWRECT
  28.         Dim LEFT As Long
  29.         Dim TOP As Long
  30.         Dim RIGHT As Long
  31.         Dim BOTTOM As Long
  32.     End Structure
  33.  
  34.     Public Structure POINTCOORDINATES
  35.         Dim X_POINT As Long
  36.         Dim Y_POINT As Long
  37.     End Structure
  38.  
  39.  
  40.     Public Structure WINDOWPLACEMENT
  41.         Dim length As Long
  42.         Dim Flags As Long
  43.         Dim showCmd As Long
  44.         Dim ptMinPosition As POINTCOORDINATES
  45.         Dim ptMaxPosition As POINTCOORDINATES
  46.         Dim rcNormalPosition As WINDOWRECT
  47.     End Structure
  48.  
  49.     Public sysWindowPlacement As WINDOWPLACEMENT
  50.  
  51.     Public Structure WINDOWCONTAINER
  52.         Dim W_TOP As Double
  53.         Dim W_LEFT As Double
  54.         Dim W_WIDTH As Double
  55.         Dim W_HEIGHT As Double
  56.     End Structure
  57.  
  58.     Public sysWindowContainer As WINDOWCONTAINER
  59.  
  60.  
  61.  
  62.     Public Function Sys_WindowAdjustment(ByVal objOwnerForm As Form) As Long
  63.  
  64.         Dim dlgWinHookDel As Sys_WindowHookProcDelegate
  65.         Dim strErrMsg As String
  66.         Dim strErrNum As String
  67.         Dim strErrorDesc As String
  68.         Dim strErrLocNum As String
  69.         Dim lngInstanceHandle As Long
  70.         Dim lngThreadId As Long
  71.  
  72.         On Error GoTo Sys_WindowAdjustment_Err
  73.  
  74.         strErrMsg = "CommonTools -- Sys_WindowAdjustment"
  75.  
  76.         'Initialize window adjustment to 0
  77.         Sys_WindowAdjustment = 0
  78.         'Get ownerform's top,left,height, width values
  79.         With sysWindowContainer
  80.             .W_TOP = objOwnerForm.Top
  81.             .W_LEFT = objOwnerForm.Left
  82.             .W_HEIGHT = objOwnerForm.Height
  83.             .W_WIDTH = objOwnerForm.Width
  84.         End With
  85.  
  86.         'Get ownerform's instance handle
  87.         lngInstanceHandle = GetWindowLong(objOwnerForm.Handle, GWL_HINSTANCE)
  88.  
  89.         'Get current thread ID
  90.         lngThreadId = GetCurrentThreadId
  91.         'Instantiate new window hook delegate
  92.         dlgWinHookDel = New Sys_WindowHookProcDelegate(AddressOf Sys_WindowHookProc)
  93.         'Set windows hook (set position of window to be loaded)
  94.         lngHookProcHandle = SetWindowsHookEx(WH_CBT, _
  95.                                              dlgWinHookDel, _
  96.                                              lngInstanceHandle, _
  97.                                              lngThreadId)
  98.  
  99.         Sys_WindowAdjustment = lngHookProcHandle
  100. End Function
  101.  
  102.    Public Function Sys_WindowHookProc(ByVal nCode As Long, _
  103.                                        ByVal wParam As Long, _
  104.                                        ByVal lParam As Long) As Long
  105.  
  106.         Dim lngXPos As Long     'Window's Left position
  107.         Dim lngYPos As Long     'Window's Top Position
  108.         Dim dblXPos As Double
  109.         Dim dblYPos As Double
  110.         Dim lngPlacement As Long     'Window Placement
  111.  
  112.         On Error Resume Next
  113.  
  114.         'If window is to be activated
  115.         If nCode = HCBT_ACTIVATE Then
  116.  
  117.             sysWindowPlacement.length = Len(sysWindowPlacement)
  118.             'Get window placement
  119.             lngPlacement = GetWindowPlacement(wParam, sysWindowPlacement)
  120.             'Set Top and Left position of window to be activated
  121.             With sysWindowPlacement.rcNormalPosition
  122.                 dblYPos = sysWindowContainer.W_TOP + (sysWindowContainer.W_HEIGHT / 2 - (.BOTTOM - .TOP) * 15 / 2)
  123.                 dblXPos = sysWindowContainer.W_LEFT + (sysWindowContainer.W_WIDTH / 2 - (.RIGHT - .LEFT) * 15 / 2)
  124.             End With
  125.  
  126.             'Convert to pixels
  127.             lngXPos = CLng(dblXPos / 15)
  128.             lngYPos = CLng(dblYPos / 15)
  129.  
  130.             'Set window position
  131.             SetWindowPos(wParam, _
  132.                          0, _
  133.                          lngXPos, _
  134.                          lngYPos, _
  135.                          0, _
  136.                          0, _
  137.                          SWP_NOSIZE Or _
  138.                          SWP_NOZORDER Or _
  139.                          SWP_NOACTIVATE)
  140.             'After setting window position, remove window's hook
  141.             UnhookWindowsHookEx(lngHookProcHandle)
  142.         End If
  143.  
  144.         'Set Sys_WindowHookProc to false to continue normal execution
  145.         Sys_WindowHookProc = False
  146.  
  147.     End Function
  148.  
****EVERY TIME THE PROGRAM EXECUTES THE Sys_WindowHookProc FUNCTION IS NEVER EXECUTED OR BEING BY-PASSED...WHAT SHOULD BE THE PROBLEM OF THIS CODE?....I BELIEVE I PRETTY CODED IT WELL(MORE READABLE AND UNDERSTANDABLE I THINK)...BTW I JUST WROTE THIS IN CAPITALS SO THAT VB CODES CAN BE RECOGNIZE OR EMPHASIZE..TNX A LOT.
Nov 6 '08 #1
Share this Question
Share on Google+
5 Replies


Plater
Expert 5K+
P: 7,872
MsgBox is not .NET code. Try using MessageBox.Show() and going through the overloads to specify who the "owner form" is?
Nov 6 '08 #2

Curtis Rutland
Expert 2.5K+
P: 3,256
Please enclose your posted code in [CODE] [/CODE] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [CODE] [/CODE] tags in future.

MODERATOR
Nov 6 '08 #3

krungkrung
P: 18
oh my apology....

hi Plater...I used MessageBox.Show() and it did not make any difference....the message box appeared but its not at the center of the owner form(as what im expecting)....as what ive said..Im new to vb.net. Just started using this like 2 days ago..;-)
Nov 7 '08 #4

Plater
Expert 5K+
P: 7,872
Hmm I guess it's not available.
The only other thing I can think of is to make your own custom popup form.
You can either set its start position to "manual" and compute where it goes...
Or you can set TopMost =false (manually in code when you create an instance of it), give its Parent property that of the form you want it centered on, then attempt to set the StartPosition to "CenterParent"
Nov 7 '08 #5

krungkrung
P: 18
tnx for that bright idea Plater...maybe i should try it then...(",)
Nov 10 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.