DJeX,
In future, can you say what purpose this code has? Or at least link to the exact source from where you got it? It's a bitch to find out what a program does by only looking at the (buggy) code.
Anyways...
- What's the use of a seperate module with just simple declarations?
- What's the use of global variables if you only have 1 form?
- Always use "Option Explicit" in ALL modules, forms, etc at the beginning of your code.
In short: the code you've downloaded is made by someone who don't know how to properly code stuff, and has all the markings of copy/pasting... The code is altered from the original by some stupid kid (he did a "replace all" of all "Long"'s with "Integer"'s, proof: see the many Integer declarations and especially the comment:
' Find out how Integer the text in this window is'). Not to mention the original code was most likely made for VB3.0 or 4.0...
After all these years, that crappy code has spread all over the place (as such code usually do):
http://www.vbexplorer.com/VBExplorer/tips/src09.htm
http://www.andreavb.com/forum/viewtopic.php?TopicID=1014
http://spazioinwind.libero.it/vbprogzone/articles/art006.html
etc...
etc...
Instead of copy/pasting, try to understand what the code does (or tries to do) and rewrite the whole thing from scratch.
quote:
Originally posted by IKillThings
Change 2 a double?
If you get overflow problems, changing to Double is not the proper thing to do, rather fix the bug itself instead of applying crappy workarounds (which wouldn't work anyways).
This is the proper code:
- Note that everything is put in the form, you don't need any module.
- Note that the code works, but if you're using VB6, you will most likely not see anything happening as VB6 doesn't have a button with "View Code" in it's main parent window.
In other words, this code of yours will not work afterall, but the method behind it will work. Thus the code needs serious adaptations or you need to be specific in what you want.
code:
'C' Always use "Option Explicit" to avoid undeclared variable errors
Option Explicit
Private Declare Function GetDesktopWindow Lib "User32" () As Long
Private Declare Function GetParent Lib "User32" (ByVal hWnd As Long) As Long
Private Declare Function GetWindow Lib "User32" (ByVal hWnd As Long, ByVal uCmd As Long) As Long
Private Declare Function GetWindowText Lib "User32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowTextLength Lib "User32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
'C' Note that the GetWindowWord() function is obsolete and should be replaced
'C' with GetWindowLong() function. See the MSDN Library for more details.
'C' EDIT: I've replaced it for you...
Private Declare Function GetWindowLong Lib "User32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SendMessageByNum Lib "User32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wparam As Long, ByVal lparam As Long) As Long
Private Const WM_COMMAND As Long = &H111
Private Const GW_HWNDFIRST As Long = 0
Private Const GW_HWNDLAST As Long = 1
Private Const GW_HWNDNEXT As Long = 2
Private Const GW_HWNDPREV As Long = 3
Private Const GW_OWNER As Long = 4
Private Const GW_CHILD As Long = 5
Private Const GWL_ID As Long = -12
Private Sub Click(BtnhWnd As Long)
'C' This will send the command id associated with the button to the parent
'C' of the button. In other words: it 'clicks' that button...
SendMessageByNum GetParent(BtnhWnd), WM_COMMAND, GetWindowLong(BtnhWnd, GWL_ID), BtnhWnd
MsgBox "done"
End Sub
Private Sub Command1_Click()
'C' The method used here to find the handle to a button works (almost),
'C' but can greatly be optimized using the proper API's.
'C'
'C' Using the method depict here, has his flaws. eg: if a (child) window
'C' is moved in the Z-order (which can happen all the time!!) it will not
'C' be properly enumerated and even some errors may occur.
'C'
'C' To properly enumerate all (child) windows use the proper API's instead
'C' of a half-baked method. eg: use the EnumWindows() and especially
'C' EnumChildWindows() functions. See the MSDN Library for more details.
Dim T As String
Dim Length As Long
Dim CurHwnd As Long
' Trying to find the handle of the View Code Button so that
' by clicking this program's button, we can see the code
' window for this form.
CurHwnd = GetDesktopWindow() 'Get Desktop handle
CurHwnd = GetWindow(CurHwnd, GW_CHILD) 'Find Child Windows of Desktop
Do Until CurHwnd = 0
' Find out how long the text in this window is
Length = GetWindowTextLength(CurHwnd) 'Get buffer length
T = Space$(Length) 'Allocate buffer space
GetWindowText CurHwnd, T, Length + 1 'Fill buffer
If InStr(UCase$(T), "PROJECT") Then
' The word "Project" was found in this Window's text
' so this is likely VB's "Project" window
' Looking now for the Project Window's child windows/controls
CurHwnd = GetWindow(CurHwnd, GW_CHILD)
Do Until CurHwnd = 0
' Find out how long the text in this window is
Length = GetWindowTextLength(CurHwnd)
T = Space$(Length)
GetWindowText CurHwnd, T, Length + 1
If InStr(UCase$(T), "VIEW CODE") Then
' This is the handle we want
Click CurHwnd 'Click the View Code Button
Exit Sub 'Exit the Sub
End If
CurHwnd = GetWindow(CurHwnd, GW_HWNDNEXT) 'Keep looking
Loop
End If
CurHwnd = GetWindow(CurHwnd, GW_HWNDNEXT) 'Keep looking
Loop
End Sub
Lines starting with 'C' are my added comments
Thus, the important thing about this is to understand the method behind this and write your own code based on that (and using the proper API's instead). The important thing is not to get this code to work (as it is only a dodgy example anyways)...
If you want to press on a button from another program automatically:
- You need to find the control ID of that button
- Send that control ID to the parent of the button
To do this:
- You need to enumerate all child windows of the parent window
- In most cases (but which is not done in this code) you need to enumerate all child windows of all the child windows also, etc...
- Find the button on the current (child) window you're at (the method used here is to search for its caption text, but there are many methods and ways and all depend on what you exactly want)
But, judging from your questions, you might to program/try much easier and basic stuff first before attempting something like this as you don't seem to understand how such stuff works (no offense though! everything needs to be learned and we all need to start somewhere. But starting with something like this, without understanding it isn't the proper way of learning something...
"don't run, if you can't walk")
----------------
As a "bonus", I've added a module to this post which shows a better way of doing this, but the principle stays the same. All you need is have MSN Messenger 6.x English running an execute the module (no form needed). It will click the "Add a contact" button.