|
|
|
|
|
BB4W Windows Programming PrimerGetting started in Windows programming within the BB4W environment.
In this article we see how using BBC BASIC for Windows lets the programmer concentrate on the actual application development and not the Windows OS.
IntroductionWindows programming can be tricky. It requires a certain amount of paradigm specific knowledge and a little patience. The average Hello World Windows Application can run to around 100 lines of code, if accepted programming practices are strictly adhered to. At the very least, the programmer has to cater for:
If we cast our collective mind back to other articles on Windows programming (see beow for links), we will remember that Windows is a message driven architecture, and as such nothing happens without the exchange of messages between the system and out application. The application becomes a recipent for messages, which, in processing them, also provides some other functionality. If there is any 'idle time' processing that needs to be done, then there are also ways to make sure that it is catered for. The BBC BASIC for Windows (BB4W) programming environment allows us to forget about many of these little peculiarities, and concentrate on the application itself. Of course, we still have access to the Windows API, can trap messages, and do idle tiem processing. BB4W just makes it easier to follow. Setting up the WindowBB4W provides us with a basic window which we can further customize. In other words, without actually writing any code, we have a window into which we can place controls, and which has an associated message loop, preamble and shutdown code. At first, all we will probably want to do is change the title bar to reflect the application title. This is done by usign the SYS command: Of note in the above is the system variable @hwnd% which is the handle (reference) given by Windows to our own window, and hence our application. Trapping MessagesIn classic Windows programming, we would construct a message translation and forwarding loop, picking messages off the queue associated with @hwnd%, and either allowing Windows to process them, or handle them ourselves. In BB4W however, all we need to do is create a handler proceure for the particular messages we want to trap. The reader will note that the name of the API function is supplied, and in this case, we know that it returns a handle. If we then want to measure the client area of the desktop window, we can make a call to GetClientRect(HWND, RECT) as follows: SYS "GetClientRect", hwndDesktop%, rcDesktop{} In this snippet, we have introduced rcDesktop{}, which is a BBC BASIC structure that needs to be mapped to a sensible equivalent. Windows Specific Data TypesSince it is often helpful to be able to map API data types (RECT, POINT etc.) to BBC BASIC equivalents, we should define some global Windows styled data structures. For example, we could define a RECT as: DIM _RECT{ Left%, Top%, Right%, Bottom% } Subsequently, before using a variable of the _RECT type, we need to define it via a DIM statement: DIM rcDesktop{} = _RECT {} Having done so, we are free to use the variable in calls to those API functions requiring a type RECT, as in the section above. BB4W GUI Helper FunctionsFinally, in the BB4W libraries, there are some helper functions which provide a basic interface to the API which removes another layer of the inherent complexity of Windows programming. Some programmers will prefer to use the direct access, but for others, being able to create a button using the following code: hwndButton% = FN_button("Click Me", ButtonX%, ButtonY%, \ ButtonWidth%, ButtonHeight%, \ 100, 0) (Notice the use of the \ .. \ as line continuation operator) As long as we have issued the INSTALL @lib$+"WINLIB5" command prior to using it, the FN_button function allows a less complex alternative to CreateWindow and CreateWindowEx. It returns the handle to the button, and allows for text, location, size, child ID, and extended options. The resulting child window can be trapped with the ON SYS handler, where @wparam% will be equal to the ID in the second to last parameter. Further Reading
The copyright of the article BB4W Windows Programming Primer in Computer Programming is owned by Guy Lecky-Thompson. Permission to republish BB4W Windows Programming Primer in print or online must be granted by the author in writing.
|
|
|
|