473,406 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

SetWindowsHookEx API -- PLEASE HELP!

krungkrung
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
5 4537
Plater
7,872 Expert 4TB
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
3,256 Expert 2GB
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
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
7,872 Expert 4TB
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
tnx for that bright idea Plater...maybe i should try it then...(",)
Nov 10 '08 #6

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

Similar topics

7
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
1
by: bugs bunny via .NET 247 | last post by:
Does anyone have actually used SetWindowsHookEx() to hook upShellExecute() so as to find what application was started bywindows or which folder is currently browsed by explorer. I knowone thing and...
15
by: bruno | last post by:
I need some help on how write SetWindowsHookEx in vb.net 2005 to hook kb input. this is the statement: hHOOKKb = SetWindowsHookEx( _ WH_KEYBOARD, _ lpfn, _ hMod, _ dwThreadId) And this is...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
1
by: Mike Carlisle | last post by:
Hi, Can't seem to find any sensible C# examples of this, struggling to work out the correct way to trap key combinations using SetWindowsHookEx with WH_KEYBOARD_LL, such as ALT-ESC. private...
4
by: abcd | last post by:
I am having trouble with pyHook on python 2.4.1. Basically I have a python app that uses pyHook to capture keyboard events and write them straight to a file. The application is running as a...
2
by: KWhat4 | last post by:
I seem to have an issue that I cant resolve. I have a jni dll that calls SetWindowsHookEx with a callback to HookKeyboardProc (basically a real simple global keyboard hook). Now the dll almost...
2
by: niksoftware | last post by:
I'm making a kiosk style desktop replacement. Im using setwindowshookex to run a callback system to trap the keys just trapping "windows" key atm. if I step through just as the program goes...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.