Streamline Your API Declares, Part 2

While we are speaking of SendMessage, there is another trick youmay find interesting enough to include in your programming habits.When used with some particular messages, the lParam argument isreally considered as two words combined. The EM_LINESCROLL canscroll a multilined text box, the low word of lParam containsthe number of lines to scroll vertically (positive values scrollup), and the high word contains the number of lines to scrollhorizontally (positive values scroll left); other, similar messagesare EM_SETSEL for text boxes, CB_SETEDITSEL for combo boxes, andLB_SELITEMRANGE for list boxes. In such cases you need to preparethe long value to be passed to the routine, which slows down yourcode and makes it less readable. It seems that a simple multiplicationshould do the work:

 ' scroll a multilined textbox "HO" ' lines horizontally' and "VE" lines vertically' beware: this approach does NOT ' work properlylongValue& = HO * 65536& + VE

The above code does not work correctly when HO is positive andVE is negative, and for a more general scrolling routine you haveto resort to a more convoluted and slower method:

 tmp$ = Right$("000" & Hex$(HO), 4) & _        Right$("000" & Hex$(VE), 4)longValue = Val("&H" & tmp$)

The solution is declaring an aliased function that splits thelParam into two distinct parameters of Integer type, as in:

 Declare Sub SendMessageSub2 Lib _        "User" Alias "SendMessage" _        (ByVal hWnd%, ByVal msg%, ByVal _        wParam, ByVal lParam1%, _        ByVal lParam2%)

Now the call is much simpler:

 SendMessageSub2 Text1.hWnd, _        EM_LINESCROLL, 0, HO, VE

The trick works because a push of a Long value onto the stackhas the same effect as the push of its high word followed by thepush of its low word.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: