TOPIC: A Trick from SuneidoControl
A Trick from SuneidoControl 1 Year, 7 Months ago
Communicating with Windows controls is normally done via SendMessage. For example:

SendMessage(.Hwnd, SCI.SETSEL, start, end)

Using helper methods in Hwnd, this can be shortened to:

.SendMessage(SCI.SETSEL, start, end)

But what I really want to write is:

.SetSel(start, end)

The normal way to do this is to define helper methods:


SetSet(start, end)
    .SendMessage(SCI.SETSEL, start, end)

We had done this for a number of methods, but Scintilla has a lot of them and we still ended up calling SendMessage for some.

I realized that we could do this automatically using a Default method:


    f = args[0].Upper()
    if not SCI.Member?(f)
        throw "method not found: " $ args[0]
    args[0] = SCI[f]
    return .SendMessage(@args)

This translates .FooBar(...) to .SendMessage(SCI.FOOBAR, ...)

So now we can call .SetSel(start, end) without defining a SetSel method and it will automatically get converted to .SendMessage(SCI.SETSEL, start, end)

We could use this in other wrapper classes for Windows controls, but most of them do not have enough methods/messages to need it.

Note: This only handles methods/messages with integer arguments. For example, we still have to use .SendMessageText to pass a string argument. I guess we could handle this automatically too, but it is probably not worth it.
Last Edit: 2008/12/27 21:24 By andrew.