473,788 Members | 2,735 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SetWindowsHookE x API -- PLEASE HELP!

krungkrung
18 New Member
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 SetWindowsHookE x 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_WindowAdjus tment


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_WindowHookP roc 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 4560
Plater
7,872 Recognized Expert Expert
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 Recognized Expert Specialist
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
18 New Member
oh my apology....

hi Plater...I used MessageBox.Show () and it did not make any difference....t he message box appeared but its not at the center of the owner form(as what im expecting)....a s what ive said..Im new to vb.net. Just started using this like 2 days ago..;-)
Nov 7 '08 #4
Plater
7,872 Recognized Expert Expert
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 "CenterPare nt"
Nov 7 '08 #5
krungkrung
18 New Member
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
3615
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> </head> <style type="text/css">
23
3287
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 to create certain textboxes, labels, and combo boxes? Any ideas would be appreciated. Thanks
1
3098
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 that is ... Windows uses ShellExecute() to doalmost every task. I need some implementation help. I want to trigger an userdefined function when suppose Notepad is opened or in simplecase a messagebox that says Notepad has started. If anyone has a...
15
11900
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 from MSDN Library: lpfn Pointer to the hook procedure.
1
9654
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 and I was wondering if anyone here would be able to give me some tips for young players such as myself, for learning the language. Is this the best Newsgroup for support with JAVA?
1
8447
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 int InterpretKey(int nCode, IntPtr wParam, IntPtr lParam) { .....
4
6063
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 service on a windows machine. If I am at that windows machine the application works just fine, logging my keystrokes. However, if I use Remote Desktop to connect to the machine and open up say Notepad it doesn't capture the keystrokes. Is this a...
2
3894
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 works all the jni hooks are fine and the program appears to run but the callback (HookKeyboardProc) does not seem to get executed or possibly never returns from execution. The code is as follows. None of the variables being set in the code...
2
2898
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 the the keyboardcallback function declaration line it pauses for seconds before moving to the next line. This directly precedes the new variable declaration of the callback variable While running this leads to only 1 press of the "windows" keys...
0
9656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10373
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10118
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9969
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8995
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5403
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4074
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.