Shoutbox

Timer from a form - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Forum: Plug-Ins (/forumdisplay.php?fid=28)
+------ Thread: Timer from a form (/showthread.php?tid=28462)

Timer from a form by TheCodeSmith on 07-10-2004 at 08:32 PM

I have a form that is called from my plugin in VB6 (thanks guys), how can i put a timer on the form that is able to trigger every 5 seconds, that will be able to use msgplus functions/commands such as /nick or /brb?


RE: Timer from a form by dotNorma on 07-10-2004 at 08:53 PM

Make a timer.

Set the interval to "5000" (Its in miliseconds)

Make sure you enable the timer.

Make the timer code be

code:
Private Sub Timer1_Timer()
sResult = "/nick NoName"
End Sub


RE: Timer from a form by Stigmata on 07-10-2004 at 08:54 PM

code:
private sub form_load()
timer1.invertal = 5000
end sub

private sub timer1_timer ()
send message /nick
timer1.interval = 5000
end sub


or along the lines of

RE: Timer from a form by Millenium_edition on 07-10-2004 at 10:53 PM

noname, read what he asked. Changing a variable sResult will not change a thing
jwb, your code is *excuse me for the word* crap

TheCodeSmith, you can't just trigger commands. You have to wait for msgplus to call events. Only then you can use them, unless you do it manually


RE: Timer from a form by TheCodeSmith on 07-11-2004 at 01:46 AM

quote:
Originally posted by Millenium_edition
TheCodeSmith, you can't just trigger commands. You have to wait for msgplus to call events. Only then you can use them, unless you do it manually
Ah right, so i can't even use an API to trigger something every X seconds?

I'm beginning to think i should just start making my own pure MSN plugin, rather than a plugin that is run through MSN plus events and restrictions.

What i need is, something that you can start by typing for example "/xblah", then at an interval of 5 seconds i'd need to call one of the MSN functions, i.e change nick.

I'll try and find out how to use the MSN API directly from a VB app, i've tried before but haven't ever been able to get it working, not even slightly.
RE: Timer from a form by matty on 07-11-2004 at 02:14 AM

You don't NEED to use the Messenger Plus! API.

Code to call the event in the plugin.

code:
Public Function ParseCommand(ByVal sCommand As String, ByVal sCommandArg As String, ByVal oConversationWnd As Object, ByRef sResult As String) As Boolean
    If (StrComp(LCase(sCommand), "/xsample", vbTextCompare) = 0) Then
        sResult = ""
        DoEvents
        Sleep 5000
        SendText "/nick " & MyNickName, True, True
        If SendText = True then
                Sleep 5000
                SendText "/brb", True, True
        End If

        ParseCommand = True
        Exit Function
    End If
    ParseCommand = False
End Function

Declarations in the Module
code:
Public Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function GetForegroundWindow Lib "user32" () As Long
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

Public Const VK_RETURN = &HD
Public Const WM_CHAR = &H102
Public Const WM_KEYDOWN = &H100
Public Const WM_KEYUP = &H101

Public hDirectUI As Long
Public hPrevWnd As Long
Public i As Integer
Public imwindowclass As Long

Module Code
code:
Public Sub SendText(sText As String, Optional bSend As Boolean = False, Optional bKeepFocus As Boolean = True) As Boolean

imwindowclass = FindWindow("imwindowclass", vbNullString)
If imwindowclass = 0 Then MsgBox "A conversation window needs to be open before setting text.", vbInformation + vbOKOnly, "Error:": SendText = False: Exit Sub
hDirectUI = FindWindowEx(imwindowclass, 0, "DirectUIHWND", vbNullString)
hPrevWnd = GetForegroundWindow
   
'This bit idles code execution until the window is in focus
'the IM window must be in focus to recieve WM_CHAR notifications
    Do
        Call SetForegroundWindow(hDirectUI)
    Loop Until GetForegroundWindow = hDirectUI
   
'Post each character
    For i = 1 To Len(sText)
        Call PostMessage(hDirectUI, WM_CHAR, Asc(Mid(sText, i, 1)), 0)
    Next i

'Simulate the enter key being pressed if necessary
    If bSend Then
        Call PostMessage(hDirectUI, WM_KEYDOWN, VK_RETURN, 0&)
        Call PostMessage(hDirectUI, WM_KEYUP, VK_RETURN, 0&)
       
    End If

'If the window is not wanted to be kept in focus then bring back the previous
    If Not bKeepFocus Then
        Call SetForegroundWindow(hPrevWnd)
    End If
        SendText = True
End Sub

Yes I know its a lot of code but it would do the job, unless they dont have any conversation windows open
RE: Timer from a form by TheCodeSmith on 07-11-2004 at 03:36 AM

Matty, that's a great help mate, but whilst it's doing the sleep call, it hangs MSN completely for the 5000ms to elapse, then it continues. That's one reason why i've never liked using the Sleep API call.

Isn't there any other way to perform a 5 second delay, after which some code is executed, which is then triggered every 5 seconds? Not just once.


RE: Timer from a form by matty on 07-11-2004 at 04:01 AM

A do while loop with doevents in the middle :S


RE: Timer from a form by Mnjul on 07-11-2004 at 04:27 AM

quote:
Originally posted by Matty.
A do while loop with doevents in the middle :S

That doesn't sound good Matty...:P

Well, TheCodeSmith, search for APIs about SetTimer and callback functions ;)

For example...

code:
Public Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Public Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long
Public TimerEvent As Long


code:
TimerEvent = SetTimer(0, 0, 5000, AddressOf TimerProc_Process)    'Set a timer to fire after 5 seconds - it calls TimerProc_Process on firing


code:
Public Sub TimerProc_Process(ByVal hWnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
    'Do something you want
End Sub


You need to kill the timer using this when you no longer needs it.
code:
KillTimer 0, TimerEvent


TimerProc_Process will be triggered every 5 seconds.
RE: Timer from a form by TheCodeSmith on 07-11-2004 at 05:19 AM

Mnjul, that works great.

Now, inside that timerproc, am i able to send/post a /nick Blah command to an Msn plus chat window?


RE: Timer from a form by Mnjul on 07-11-2004 at 05:32 AM

Then you need to use SendText function Matty posted :) It searches for an existing conversation window and sends texts to the window's input box ;)


RE: Timer from a form by Mike on 07-11-2004 at 08:10 AM

http://www.vbforums.com/showthread.php?s=&threadid=286584 ;)
A nice example...


RE: Timer from a form by Stigmata on 07-11-2004 at 08:28 AM

quote:
Originally posted by Millenium_edition
jwb, your code is *excuse me for the word* crap


well will it work??
RE: Timer from a form by Millenium_edition on 07-11-2004 at 08:53 AM

oh please jwb that's not helping out. everyone knows how to use a timer control. and btw why change the interval twice?

Matty., doing

code:
sResult = ""
is pretty useless as the function MUST be finished for msgplus to continue
Also, it's nice to see some sendtext code, but it's using SendKeys (yes, it IS sendkeys, just win32 api) so... I think holding shift while doing this could alter the text sent. Not sure tough.