|Posted: Mar 18th, 2007, 10:29pm Post subject: Introduction to WMLiberty
|I wrote this paper a few months ago for school. It was intended to be part of a book about Windows API programming from LB, but I've neither the time nor talent to do so. That should explain the lack of end and beginning. In any event, hopefully this will help new WMLiberty users find their way around. I release it as public domain.
The Windows API may be fine and dandy for creating and managing controls, but unlike with native LB controls, we are not notified when events occur. Fortunately, we have WMLiberty.dll. WMLiberty.dll (commonly referred to as WMLiberty) enables you to trap Window Messages from the Windows API. In plain English, that means you can trap events from API-created controls without too much hacking.
Understanding WMLiberty requires at least a basic understanding of Window Messages. Window Messages, as you may have guessed, are messages sent to your window when an event occurs. They contain small bits of information that allow you to identify what sort of event has occurred.
But how are these messages received? Well, the Windows API uses a neat trick called a callback for exactly that purpose. When using callbacks, you send the Windows API the address of a function, which is stored in memory. Then, whenever it decides to do so (in this case, when a control event happens), the Windows API calls your function back. On top of that, you can return data to the Windows API. Hereís a chart showing the callback process:
[image goes here]
Thatís not so scary, is it? When used correctly, callbacks can greatly aide your programming experience.
With an understanding of callbacks under our belt, we can now delve in to WMLiberty. The problem with using Window Messages in plain LB is that Windows sends quite a few. In fact, so many messages are sent that the program crashes within a second after running. Try the sample program apiwindow.bas to see for yourself.
Since LB canít handle so many messages at such a speed, we need a way to filter out the messages we donít need, so we only get the messages we want to receive. Thatís where WMLiberty comes in. With a simple call to WMLiberty, we can trap the Window Messages we want, and use built-in windowing functions, too.
One way I like to use WMLiberty is to trap the WM_SIZE message. While LB does have a built-in resizehandler command, there is a short but visible delay between when the window itself is sized and when the resize handler is called. With WMLiberty and callbacks, there is no such delay. See the sample program trapwmsize.bas for an example.
There are a few disadvantages to using WMLiberty and callbacks. For instance, you cannot use the wait command. Instead, use the scan command, calling the Sleep function occasionally to make sure every available CPU cycle isnít consumed. An example of this is seen in every demo using callbacks. You can use the last example, trapwmsize.bas, to see it right now.
[note: the demos are somewhat incomplete at this time. If I ever get some free time, I'll wrap 'em up.]