|
-
Apr 27th, 2005, 12:19 PM
#1
Thread Starter
Frenzied Member
Module Help...[resolved]
Hey, thanks for stopping by, i have a slight problem with my module, i cannot get my forms to get the resulting string from it, how would i go about doing it ?
Here is my Forms code -
VB Code:
Private Sub Command1_Click()
Call getWinampWindow
MsgBox NowPlayingStr
End Sub
And here is my Module code (Feel free to re-use this code if needbe, its fully fnctioning)
VB Code:
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Dim lHandle As Long
Dim NowPlayingStr As String
Function getWinampWindow()
lHandle = FindWindow("Winamp v1.x", vbNullString)
If lHandle > 0 Then
Dim lRet As Long
Dim sTitle As String * 256
Dim sCaption As String
lRet = GetWindowText(lHandle, sTitle, Len(sTitle))
sCaption = Left(sTitle, InStr(1, sTitle, vbNullChar, vbTextCompare) - 10)
Do
If IsNumeric(Left(sCaption, 1)) = True Or Left(sCaption, 1) = "." Or Left(sCaption, 1) = " " Then
sCaption = Right(sCaption, Len(sCaption) - 1)
Else
Exit Do
End If
Loop
Do
If IsNumeric(Right(sCaption, 1)) = True Or Right(sCaption, 1) = ")" Or Right(sCaption, 1) = " " Or Right(sCaption, 1) = ":" Then
sCaption = Left(sCaption, Len(sCaption) - 1)
ElseIf Right(sCaption, 1) = "(" Then
sCaption = Left(sCaption, Len(sCaption) - 1)
Exit Do
Else
Exit Do
End If
Loop
NowPlayingStr = sCaption
Else
NowPlayingStr = "Winamp 6 Not Found"
End If
End Function
Any suggestions as to how to get this to work ?
EDIT : Final Code in Last post
Last edited by thegreatone; Apr 27th, 2005 at 12:44 PM.
Zeegnahtuer?
-
Apr 27th, 2005, 12:22 PM
#2
Re: Module Help...(i feel dumb....)
Try changing Dim to Public.
When declaring variables in an event, use Dim.
When declaring variables at the form level, use Private
When declaring variables at the module level, use Public
-
Apr 27th, 2005, 12:27 PM
#3
Thread Starter
Frenzied Member
Re: Module Help...(i feel dumb....)
Ok, that didn't work, but i have found a workaround, by simpling making the MsgBox pop-up from the module itself... a little un-orthodox but still, it works.
here is the code i am now using, anymore answers are welcome 
VB Code:
'Form1
Private Sub Command1_Click()
Call getWinampWindow
End Sub
and the module -
VB Code:
'Module1
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public lHandle As Long
Public NowPlayingStr As String
Function getWinampWindow()
lHandle = FindWindow("Winamp v1.x", vbNullString)
If lHandle > 0 Then
Dim lRet As Long
Dim sTitle As String * 256
Dim sCaption As String
lRet = GetWindowText(lHandle, sTitle, Len(sTitle))
sCaption = Left(sTitle, InStr(1, sTitle, vbNullChar, vbTextCompare) - 10)
Do
If IsNumeric(Left(sCaption, 1)) = True Or Left(sCaption, 1) = "." Or Left(sCaption, 1) = " " Then
sCaption = Right(sCaption, Len(sCaption) - 1)
Else
Exit Do
End If
Loop
Do
If IsNumeric(Right(sCaption, 1)) = True Or Right(sCaption, 1) = ")" Or Right(sCaption, 1) = " " Or Right(sCaption, 1) = ":" Then
sCaption = Left(sCaption, Len(sCaption) - 1)
ElseIf Right(sCaption, 1) = "(" Then
sCaption = Left(sCaption, Len(sCaption) - 1)
Exit Do
Else
Exit Do
End If
Loop
NowPlayingStr = sCaption
MsgBox NowPlayingStr
Else
NowPlayingStr = "Winamp 6 Not Found"
MsgBox NowPlayingStr
End If
End Function
-
Apr 27th, 2005, 12:34 PM
#4
Hyperactive Member
Re: Module Help...(i feel dumb....)
as the code you have in your module is a Funtion, it needs to return a value, but to do that you have to set the function to have a value within the function, so what you should do is get rid of the variable NowPlayingStr and wherever you mention it in the code (like NowPlayingStr = sCaption), replace it with getWinampWindow (so it would say getWinampWindow = sCaption) and then in your code instead of saying
VB Code:
Call getWinampWindow
msgbox NowPlayingStr
just use:
Do you wake up in the morning feeling sleepy and grumpy? Then you must be Snow White
-
Apr 27th, 2005, 12:39 PM
#5
Re: Module Help...(i feel dumb....)
 Originally Posted by Arachnid13
as the code you have in your module is a Funtion, it needs to return a value,
Good point Arachnid13. I missed that.
thegreatone: Change your function to Public Function getWinampWindow() As String and you should get your return string.
-
Apr 27th, 2005, 12:43 PM
#6
Thread Starter
Frenzied Member
Re: Module Help...(i feel dumb....)
Finished, your both getting good rep. thanks so much.
Final Code-
VB Code:
'in a module
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public lHandle As Long
Function getWinampWindow()
lHandle = FindWindow("Winamp v1.x", vbNullString)
If lHandle > 0 Then
Dim lRet As Long
Dim sTitle As String * 256
Dim sCaption As String
lRet = GetWindowText(lHandle, sTitle, Len(sTitle))
sCaption = Left(sTitle, InStr(1, sTitle, vbNullChar, vbTextCompare) - 10)
Do
If IsNumeric(Left(sCaption, 1)) = True Or Left(sCaption, 1) = "." Or Left(sCaption, 1) = " " Then
sCaption = Right(sCaption, Len(sCaption) - 1)
Else
Exit Do
End If
Loop
Do
If IsNumeric(Right(sCaption, 1)) = True Or Right(sCaption, 1) = ")" Or Right(sCaption, 1) = " " Or Right(sCaption, 1) = ":" Then
sCaption = Left(sCaption, Len(sCaption) - 1)
ElseIf Right(sCaption, 1) = "(" Then
sCaption = Left(sCaption, Len(sCaption) - 1)
Exit Do
Else
Exit Do
End If
Loop
getWinampWindow = sCaption
Else
getWinampWindow = "Winamp 6 Not Found"
End If
End Function
It is simply accessed like this -
Do you think this is worth posting in the codebank ?
-
Apr 27th, 2005, 12:59 PM
#7
Re: Module Help...(i feel dumb....)
 Originally Posted by thegreatone
Do you think this is worth posting in the codebank ?
If you are going to do that, make sure you include a detailed explanation of what your code does, and how it does it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|