[SOLVED] multiple ComObjConnect using same "Prefix"

Get help with using AutoHotkey and its commands and hotkeys
tmplinshi
Posts: 1600
Joined: 01 Oct 2013, 14:57

[SOLVED] multiple ComObjConnect using same "Prefix"

05 Jun 2015, 17:05

Example Code
For now, I've to use two different class:

Code: Select all

ComObjConnect(WB1, WB1_Events)
ComObjConnect(WB2, WB2_Events)

class wb1_Events {
    TitleChange(title) {
        TabSetText(HTab, 1, title)
    }
}
class wb2_Events {
    TitleChange(title) {
        TabSetText(HTab, 2, title)
    }
}
I wanted to use one class:

Code: Select all

ComObjConnect(WB1, WB_Events)
ComObjConnect(WB2, WB_Events)

class WB_Events {
    TitleChange(title) {
        ; TabIdx := (ComObject = wb1) ? 1 : 2
        TabSetText(HTab, TabIdx, title)
    }
}
But how can I know which ComObject is calling?
Or is there any other way to use only one class? Something like:

Code: Select all

ComObjConnect(WB1, WB_Events.1)
ComObjConnect(WB2, WB_Events.2)

class WB_Events {
	Class 1 Extends WB_Events {
	}
	Class 2 Extends WB_Events {
	}
    TitleChange(title) {
    	1 or 2?
        TabSetText(HTab, 1, title)
    }
}
Last edited by tmplinshi on 06 Jun 2015, 03:40, edited 1 time in total.
lexikos
Posts: 7185
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: multiple ComObjConnect using same "Prefix"

05 Jun 2015, 20:02

Code: Select all

PrefixEventName([Params..., ComObject])
{
    ... event-handling code ...
    return returnValue
}
Prefix is a prefix of your choosing, while EventName is the name of whatever event the function should handle.

Params corresponds to whatever parameters the event has. If the event has no parameters, Params should be omitted entirely. ComObject is optional, and can only be used if the correct number of Params are defined; it contains a reference to the original wrapper object which was passed to ComObjConnect. "ComObject" should be replaced with a name more meaningful in the context of your script.
You can get the wrapper object like this if you don't want to declare all of the event's parameters:

Code: Select all

EventName(Params*) {
    ComObject := Params.Pop()
    ...
}
lexikos
Posts: 7185
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: multiple ComObjConnect using same "Prefix"

05 Jun 2015, 20:09

Another way would be to use classes the way they're supposed to be used: instantiate the class.

Code: Select all

ComObjConnect(WB1, new WB_Events(1))
ComObjConnect(WB2, new WB_Events(2))
 
class WB_Events {
    __New(TabIdx) {
        this.TabIdx := TabIdx
    }
    TitleChange(title) {
        TabSetText(HTab, this.TabIdx, title)
    }
}
Untested.
tmplinshi
Posts: 1600
Joined: 01 Oct 2013, 14:57

Re: multiple ComObjConnect using same "Prefix"

06 Jun 2015, 03:40

Ah, I wasn't reading the docs carefully enough.

Both solution works, and I prefer the second. The first one is useful that I no more need to specify global WB inside the function.

Thank you very much for the help!
lexikos
Posts: 7185
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: [SOLVED] multiple ComObjConnect using same "Prefix"

06 Jun 2015, 05:29

You can of course do both; just change the parameter list in the second example. You can also store whatever you want (like WB itself) in the object you pass to ComObjConnect.
tmplinshi
Posts: 1600
Joined: 01 Oct 2013, 14:57

Re: [SOLVED] multiple ComObjConnect using same "Prefix"

06 Jun 2015, 08:04

Yeah, that's exactly what I'm doing. :)

Code: Select all

class WB_Events {
    __New(TabIdx) {
        this.TabIdx := TabIdx
    }
    TitleChange(title) {
        TabSetText(HTab, this.TabIdx, title)
    }
    NewWindow3(ppDisp, Cancel, dwFlags, bstrUrlContext, bstrUrl, WB) {
        cancel[] := true ; Prevent openning in iexplore.exe
        WB.Navigate(bstrUrl) ; Open in the WebBrowser instead
    }
}

Return to “Ask For Help”

Who is online

Users browsing this forum: AHKStudent, Bing [Bot], Hackerxd, jly, mikeyww, newbieforever and 55 guests