Re: Interpreting a BAS module
- From: "Randy Birch" <rgb_removethis@xxxxxxxx>
- Date: Sun, 23 Sep 2007 00:17:03 -0400
Pop ...
Aliasing is only really required for APIs that pass or return strings (the A
and W versions of similar APIs) due to the differences in passing data using
unicode and ansi strings. APIs taking/returning only numeric data are
typically not aliased at all. A notable exception is the SetFocus API, which
because if conflicts with the SetFocus method of VB, is usually declared as
PutFocus aliased to SetFocus.
As for reading constants... the letters preceding the underscore normally
tell you what the constant is, e.g.
LB_xxx = list box
EM_xxx = edit message (edit is a text box in VB)
WM_xxx = window message
WS_xxx = window style
BM_xxx = button message
BS_xxx = button style
LVM_xxx = list view message
LVS_xxx = list view style
LVN_xxx = list view notification
TVM_xxx = tree view message
.... and so on and so on. So,
Private Const WM_CLOSE As Long = 16
... declares a constant with a private scope using the name WM_CLOSE, which
is a window message, and which the C header files has defined as having a
value of decimal 16. Constants can be declared decimal, hex, octal -
whatever you want. Bottom line is that 16 is 16 is 16 regardless whether
written as 16, &H10 or &O20. So using SendMessage with:
sendmessage somehwnd, 16, 0&, byval 0&
... will cause the window represented by somehwnd to close. Using WM_CLOSE
rather than 16 is a lot more readable and self-documenting.
Re: AddressOf ...
Hmm .... how to start.
Normally Windows applications respond to events generated by the user, or
events raised by the system (e.g. MouseMove etc). Also normally,
applications make calls out to Windows to ask it to do something (e.g.
create a timer, find a file etc.).
But in some instances an application can also request that Windows perform
some task and then call the VB application when information is available. In
order for this to occur, you have to utilize an API that is designed to
provide data back to the calling application, and you have to write a
procedure that conforms to the specific format the API expects to be able to
pass data back in (with respect to number of parameters and data types of
each). These are called "callback" functions, and in order to use them you
have to tell Windows the memory address of the callback function. This is
accomplished with the AddressOf operator, which passes the "address of" the
callback. See http://vbnet.mvps.org/code/enums/enumfontbytype.htm and
http://vbnet.mvps.org/code/locale/localeenumdates.htm for ideas.
AddressOf is also used in subclassing. Normally a transparent (hidden) VB
window for each VB application receives Windows messages and passes just a
few on to the visible form we call the app. But there are hundreds of other
messages VB's hidden window eats and discards because VB has no
corresponding form or control event for the message. This doesn't mean we
can't receive them however, but to do so requires us to create a routine to
receive every message sent so we can process those of interest that VB
naturally discards. This is subclassing, and to subclass you have to, as
above, create a routine to receive all the messages and pass that address to
windows via a special API that changes the message processing structure of
VB.
One example is setting focus to a VB app -- the gotfocus event of the form
only fires when focus is regained from another form in the application.
Switching to another application then back to the VB app does not notify the
form it had gained or lost focus. Subclassing can be used to trap a specific
message that Windows always send when switching windows takes place, but
which is one of the messages eaten by the hidden VB message processing
window. http://vbnet.mvps.org/code/subclass/activation.htm
Another popular request is for a means to restrict form resizing - again we
can get windows to give us the appropriate messages and we can also modify
things so windows behaves differently as a result. This demo shows this
technique: http://vbnet.mvps.org/code/subclass/minmaxinfo.htm
Oh, and passing 0& to an api is the VB way of passing C's NULL - IOW,
telling the API there is nothing in that parameter of any interest.
--
Randy Birch
MS MVP, Visual Basic
http://vbnet.mvps.org/
Please respond to the newsgroups so all can benefit.
.
- References:
- Interpreting a BAS module
- From: Pop`
- Interpreting a BAS module
- Prev by Date: Re: Interpreting a BAS module
- Next by Date: Re: Failed Vista Install
- Previous by thread: Re: Interpreting a BAS module
- Next by thread: Re: Interpreting a BAS module
- Index(es):