Traver REALbasic for Windows: The Blog

REALbasic discussion and programs specifically for Windows (and some discussion of differences between REALbasic and Visual Basic).

Friday, July 13, 2007

Announcing Windows API Audio/Video Player

AVPlayer.rbp is the source code for a Windows API AVPlayer. It will Play various types of Audio and Video files (essentially the same that the Windows Media Player will play).

Note: although rather barebones, this program does allow you to set the position in the file or set the volume of the sound.

Caution: The slider is not handled in the traditional way (I forget why at the moment). If you click anyplace on the slider bar, the slider will go immediately to that position.

For video, you can make the playback full-screen only by pulling (dragging) the borders of the window.

You can download the file here.

Enjoy!

Barry Traver

Sunday, May 07, 2006

Paster: Another Windows Clipboard Utility

Years (a decade?) ago I wrote in Visual Basic a Windows clipboard utility called "Paster." At some point in time, Paster stopped working, and I don't know what caused the change, although it may have been caused by a change in a later version of Windows.

Paster was, however, a useful utility. (Shouldn't all utilities be that by definition?). Basically it was an always-on-top window that had a small footprint and here's what the program did: You could be working with a different program (e.g., WordPad), click on "Paster" to paste some boilerplate text into whatever you were working on with a mouse-clip or two. "Paster" not only pasted in the selected boilerplate text, but also restore the focus back to the program with which you were working.

I was hoping to get something like that going in REALbasic, and Brian Rathbone did all the hard work to make that possible. You will find the resulting source code (paster5.zip) at http://traver.org/traverrbw/.

Here's how to use "Paster." Put your "boilerplate" text in a plain text file, with one item per paragraph (but no blank lines between paragraphs). When you're using "Paster," load that file into Paster. (For some reason, I wasn't able to get LoadFile working in the MenuBar, which is where such is usually found, so I had to use a PushButton.) Then work with your other program. When you want to paste in some boilerplate text, double-click on the appropirate line in the Paster ListBox. That's all there is to it!

Due to the eccentricities of various programs, "Paster" works with some and not with others. The following list will give you an idea of some of the ones that work with "Paster" and some that don't, based on my experimentation thus far:

With Edit Pad Pro, I get an error message, "Cannot open clipboard."
With Firefox, it seems to work fine.
With Internet Explorer, it seems to work fine.
With Notepad, nothing seems to happen. Nothing is pasted in.
With NoteTab Pro, it's rather flaky (some clicks will work, but most will not).
With Thunderbird, it seems to work fine.
With Microsoft Word, it seems to work fine.
With WordPad, it seems to work fine.

Thus "Paster" works more often than not, but it's difficult to predict in advance whether it will work with the particular program with which you may want to use it.

One more comment about "Paster." You can move the window using "Drag" and "Drop," BUT you see no indication of its being moved until the "Drop." The fancy tricks that allow "Paster" to work somehow also produce this side effect, for which no alternative has yet been found. As long, however, as you know about the quirk, you should be able to live with it.

As usual, the source code is supplied with the understanding that if you improve the program you are to send me (if possible) a copy of your modified version. Thank you (and thanks again to Brian Rathbone for sharing his code).

Enjoy!

Barry Traver

Sunday, April 23, 2006

More on CollectClips: A Windows Clipboard Utility

Somehow - while "moderating comments" on this blog - I managed to mess up and delete a recent comment from Steve Garman, so I'm making it the main contents of this entry, where it should be read by more people anyway.
Here's what Steve had to say:
 __________________________________________
/
Nice utility, but there are a couple of small points I'd make about the Timer1.Action event handler.

I'd hesitate to keep the clipboard open while you mess about with strings. That's very time consuming. Remember the clipboard's locked while it's open and your OS has preemptive threads.

I also can't see any need for all the duplicated code based on whether mainbuffer starts empty or not.

I'd have thought all the code you need is:

Sub Action()
   Dim c As Clipboard
   Dim newText As String
   Dim Count1 As Integer
   Dim EOL As String
   EOL = Left(EndOfLine,1)
   c = New Clipboard
   If c.TextAvailable Then
      newText = c.Text
      c.Close
      If newText <> FormerText Then
         MainBuffer = MainBuffer + EOL + newText + EOL _
            + "________________" + EOL
         EditField1.Text = MainBuffer
         Count1 = CountFields(EditField1.Text, EOL) - 4
         EditField1.Scrollposition = Count1
         FormerText = newText
         Self.Title = FormerText + " " + newText + " " _
            + "B " + Str(Count1)
      End If
   End If
End Sub
\__________________________________________

I appreciate the comments from Steve, because I'm primarily an "idea" person and often need all the help I can get from friends who have more professional expertise than I have. That is, I have lots of ideas for interesting programs and can sometimes (not always) come up with enough code to prove that the idea can become a practical reality, but I often rely on others to make the code more professional and polished.

Barry Traver

Tuesday, April 18, 2006

CollectClips: A Windows Clipboard Utility

Whenever one copies an item of text to the Windows clipboard, ordinarily the new item replaces what was there earlier. But suppose you want to "collect the clips," so to speak?

My CollectClips utility is a simple way to do that. I put it together when I was using a scanner to scan text and the associated OCR program copied the related text to the Windows clipboard. Since I didn't want to bother with pasting the copied text in the Windows clipboard to some safe place each time before I did a new scan, I decided to write this utility.

There are no especially impressive "tricks" here. I do use a Timer to accomplish my purpose, so the program does provide an example of one way in which the Timer may be useful, but that's about it.

The program works automatically. The only things you need to do are to start up CollectClips when you want to start saving things from the Windows clipboard and to use the File menu to Save As Text the collected clips before you Exit from the CollectClips program.

Here's where you can find the source code for the program (Collect2Clips.zip):

http://traver.org/traverrbw

Feel free to improve upon the program (but please send me a copy of the improved result). Enjoy!

Barry Traver

Tuesday, January 31, 2006

Autoload Menu Programs Launching Programs

Some of what I have to say in this entry may also be useful to Mac owners, but (1) they will have to make the necessary changes in the source code (essentially replacing the Windows-style filenames - such as menu.exe - with Mac-style filenames - such as menu), and (2) they will have to ignore what is said here about autoload (or else find some Mac equivalent).

What do I mean by "autoload"? Well, in Windows if you put a CD-ROM in a CD-ROM drive, the computer (unless you have told it to do otherwise) automatically looks for certain autorun information and, if it finds it, will "autoload" (i.e., automatically load) and run some program on the CD-ROM. The Mac may or may not have such a feature.

First, a historical digression. About two decades ago in the world of the Texas Instruments TI-99/4A computer (a contemporary of the Apple II and Commodore 64), I published for several years an influential "diskazine" (or magazine on disk) called the Genial TRAVelER. (Although I've been elected into the "TI Hall of Fame," they have not yet gotten around to putting up my bio, but when they do, you may be able to read more about it there.)

Anyway, this "magazine-on-a-disk" had an automatic load program which presented a menu of the contents. The disk was a collection of articles and programs, including games. Much of the time you could start at the automatically loaded menu, choose to read an article, return to the menu, play a game, return to the menu, read another article, and so on. Everything could be done easily right from the floppy disk.

The same sort of thing could easily be done in REALbasic (but not in Visual Basic). You would need, of course, an automatically loading menu on the CD-ROM that could launch separate programs on the CD-ROM, and those programs would need to be able to re-launch the menu program, but that's easily done. (And - except perhaps for the automatically loading menu - the same could be done on a Mac.)

I'm not sure what this new form of media would be called. The Genial TRAVelER was called a "diskazine" because it was published on a floppy disk. What do we call it if we publish something similar on a CD-ROM? To continue to call it a "diskazine" is not quite right, because a floppy "disk" is spelled with a "k" and a CD-ROM (for "Compact Disc - Read Only Memory") spells its "disc" with a "c. Shall we call it a "discazine"? Somehow that doesn't look right, and C-Discazine looks even worse. How about maybe "CD eZine"?

Well, whatever you call it, I don't see anybody really doing anything like that today (maybe because it's difficult to do outside of REALbasic). The closest thing I've seen is certain computer magazines distributed with CD-ROMs, but in the ones I've seen there is no attempt to make the CD into a self-contained entity complete with menu (or "table of contents"). The CD's I've seen are simply collections of supplementary resources discussed in the magazine, not an attempt to publish a magazine on a CD-ROM.

If you like the idea, my CD.zip contains the necessary files to show you how to go about doing it. The cd.zip file contains the important menu.rbp program plus two "stupid programs" (hello.rbp and goodbye.rbp) just to show how it works. Compile all three, and put the .exe files on a CD-ROM, along with the file autorun.inf and the file CDROM01.ICO.

Put the CD-ROM in a CD-ROM drive, and the menu should be auto(mattically) load(ed). (More about that in a moment.) You have three choice on the menu: (1) "Hello" (which launches the Hello program), (2) "Goodbye" (which launches the Goodbye program), and "Exit."

Run the Hello program (press the "Hello Button" and a MsgBox will say "Hello, world!"), and then exit (using either File -> Exit or the "x" in the top right corner of the window). You'll be brought back to the menu program. Do the same with "Goodbye" and finally Exit the menu.

The code to return from another program to the menu program can be found in the CancelClose event handler. The code to launch another program from the menu program is fairly straightforward. That leaves one remaining question: How do you get the menu program to autoload?

It all involve creating an appropriate autorun.inf file. You can find an autorun.inf editor for free at Karen's Power Tools. Here's the URL:

http://www.karenware.com/powertools/ptautorun.asp

In addition, there are many such utilities available for download on the Internet, some free, some not. Just do a Google search on

"autorun.inf" freeware

or

"autorun.inf" shareware

If you look with a text editor at the autorun.inf file I've included, you'll see that you probably don't even need a special program for the purpose.

So why not create a "CD eZine" of your own to pass along to your friends or the wider community? As you can see, it's not that difficult to do. Enjoy!

Barry



Home Page for This Blog: http://traverrbw.blogspot.com/

Programs and Files Discussed in the Blog: http://traver.org/traverrbw/

Sunday, January 29, 2006

Comments on Ends of Lines and Comment Blocks

First, some comments about end-of-line markers.

You don't need to know this as a Windows user, but Windows, the Mac, and Linux use different end-of-line markers: the Mac uses Chr(13) all of the time and Linux uses Chr(10) all of the time, while Windows uses Chr(13) + Chr(10) ... most of the time (more about that in a minute). REALbasic, however, has a useful constant which varies according to the operating system on which RB is being run: EndOfLine.

If you're running RB on a Mac, EndOfLine is always Chr(13). If you're running RB on Linux, EndOfLine is always Chr(10). And if you're running RB in Windows, EndOfLine is always Chr(13) + Chr(10). Can you see the problem? EndOfLine works most of the time in Windows, but NOT all of the time.

Here's what you need to know: in Windows, the end-of-line marker is NOT Chr(13) + Chr(10) in text in an EditField, but (don't ask me why - ask Microsoft!) simply Chr(13). That's important, because if you want to divide text in an EditField into separate lines (say, so that you can put each line into an element in a string array), EndOfLine won't work for you.

Don't take my word for it. Prove it for yourself. Put an EditField and a PushButton in a window. Put this code in the Action event handler for the PushButton:

Dim Lines(-1) As String
Lines = Split (EditField1.Text, EndOfLine)
MsgBox Lines(0)

Run the program, type "one" in the EditField, press enter, type "two," press enter, type "three," and press enter. The MsgBox (contrary to what you might expect) will display this:

one
two
three

Split did NOT split the EditField text into separate lines!

Now change the the PushButton Action event handler to read like this:

Dim Lines(-1) As String
Lines = Split (EditField1.Text, Chr(13))
MsgBox Lines(0)

This time the MsgBox will display this:

one

The preceding has nothing to do with my next topic, which is the matter of commenting blocks, which I find to be a very helpful feature of REALbasic. The only thing is that I prefer // to ' as a comment marker. Thus I wrote a "commenter" program, which has no other purpose than to allow you to do just that.

Brief digression: years ago I wrote a "paster" program in Visual Basic which also made use of an always-on-top utility program. It worked beautifully: click on a button, and not only would associated "boilerplate" text be inserted at the appropriate point in the text of whatever program you were using (e.g., Notepad, WordPad, Microsoft Word, or whatever), but the focus was also immediately restored to that window, so that you could immediately continue work from where you left off.

Well, with one of Microsoft's updates (either to the Windows operating system or to Visual Basic, but my guess is the former), that feature stopped working. It became impossible for me to restore automatically the focus back to the original program (or at least so difficult that I could never figure out how to do it) or, for that matter, to paste the text into that original program automatically. And I have the same problem in REALbasic that I had in Visual Basic, so two additional steps are now needed . (If anyone knows how I can still do what I used to be able to do, please let me know!)

If you're happy with the apostrophe as a comment marker, use the built-in routines in the RB IDE to "comment block" and to "uncomment block." If you - like me - prefer two slashes as a comment marker, then here are the directions on how to use my commenter.rbp:

To comment a block within the RB IDE, with commenter.rbp compiled and running:

(1) Select the block of text you want to comment out.
(2) Use Ctrl-Insert to copy the text to the Windows clipboard.
(3) Click on "Comment Lines" in the "commenter" program
(4) Click on someplace safe in the RB IDE that will leave the selected text still highlighted (the title bar for the RB IDE is fine).
(5) Use Shift-Insert to paste the (now commented) block into the RB IDE (replacing the selected text).

To uncomment a block within the RB IDE, with commenter.rb compiled and running:

(1) Select the block text you want to uncomment.
(2) Use Ctrl-Insert to copy the text to the Windows clipboard.
(3) Click on "Uncomment Lines" in the "commenter" program
(4) Click on someplace safe in the RB IDE that will leave the selected text still highlighted (the title bar for the RB IDE is fine).
(5) Use Shift-Insert to paste the (now uncommented) block into the RB IDE (replacing the selected text).

By the way, when you select the text, I find that it is best to begin with the first actual character of the first line and end with the entire last line to be commented (or uncommented).

One final comment: In RB 2005rx and in RB 2006r1, Ctrl-C, Ctrl-X, and Ctrl-V do not always work (it's an apparent bug) to Copy, Cut, and Paste text, but Ctrl-Insert, Shift-Delete, and Shift-Insert do seem to work consistently. (At least that has been my experience.)

Barry Traver



Home Page for This Blog: http://traverrbw.blogspot.com/

Programs and Files Discussed in the Blog: http://traver.org/traverrbw/

Thursday, January 26, 2006

Resources for Users of RB, VB, and Windows

The purpose of today's entry is to suggest some resources that may be useful to Windows users of REALbasic, particularly those who have some experience with Visual Basic.

One excellent resource is "Porting VB Applications to Linux and Mac OS X: A how-to guide for Visual Basic developers" by Hank Marquis. You'll find it here:

http://snurl.com/hmxq

Ignore the mentions of Linux and Mac OS X in the title. As the URL suggests, the real focus of the paper is on "porting Visual Basic." It's not so much a matter of going from Windows to Linux or Mac, but a matter of going from VB to RB. I myself run REALbasic in Windows, and the information in this excellent paper is just as important for Windows users to know, even if you have no interest in Linux or Mac.

Also helpful are the REALbasic Forums at this address:

http://forums.realsoftware.com/index.php

In particular, there is a section (moderated by Aaron Ballman) devoted specifically to "Windows" questions and another section devoted specifically to "Visual Basic to REALbasic Conversion" questions.

An excellent starting point for all things related to developing programs in REALbasic is REALDev (REALbasic Developers Network) to be found here:

http://www.classicteck.com/rbdn.php

You'll have to do some searching to find which of the resources listed there deal specifically with Windows. For example, try these pages:

REALDev: Windows

REALDev: Windows: API

On the REALDev home page, there is this notice:

"Aaron Ballman's Windows Functionality Suite is now at version 2.2. This collection of classes and modules is quickly becoming a must have for every REALbasic programmer looking to develop professional applications for Windows. This suite is free." You can find more information here:

http://snurl.com/lq52

For information on Aaron's other contributions (including his "VB Declare Converter"), check out his Web site:

http://www.aaronballman.com/programming/

Well, that should be enough to get you started. Enjoy!

Barry Traver



Home Page for This Blog: http://traverrbw.blogspot.com/

Programs and Files Discussed in the Blog: http://traver.org/traverrbw/

Thursday, January 19, 2006

hideshow: Demo of Some (Useless?) API Calls

This progrm is simply a demo (for Windows version of REALbasic only) of how to use API calls to do the following:

     * Hide Desktop Icons
     * Show Desktop Icons
     * Hide TaskBar
     * Show TaskBar
     * Hide Start Button
     * Show Start Button
     * Hide System Tray (plus Clock)
     * Show System Tray (plus Clock)
     * Hide Clock
     * Show Clock

I hear some of you saying, "But why would you want to do that?" And I don't have any answer to that. Everything doesn't have to have a practical purpose, does it?

The program is at least an example of how Declares can be made (and used) in a simple REALbasic program.

The RB expert on Windows Declares is Aaron Ballman. If you are interested in more information on Windows Declares, check out the following:

Windows Functionality Suite 2.2
"The Windows Functionality Suite is a set of REALbasic classes and modules that provide convenient access to features that are not already included in the REALbasic framework. It is an on-going open source project that includes many different pieces of Windows-specific functionality for your application. It is provided free of charge and includes a rich feature set, including:

     * System Parameters, Colors and Metrics
     * OS Version and Process Information
     * Service Support
     * System Colors
     * Registry Helpers
     * High Performance Counters
     * Locale Information
     * Cryptography
     * Plug and Play Notifications
     * StatusBar Control
     * TreeView Control
     * Date/Time and Calendar Controls
     * Extended ToolTip Support
     * Window and FolderItem Extensions
     * And Much More!

What version of REALbasic does the Windows Functionality Suite 2.0 (and up) support?

Version 2.0 and up are only supported on REALbasic 2005r1 and higher. This is due to the Suite's heavy reliance on the new Soft Declare functionality that was introduced with REALbasic 2005r1."

You will also find the following (also by Aaron Ballman) to be of benefit:

VB Declare Converter 1.0
"One question I have seen come across the various lists about REALbasic comes from people who are trying to port declares from Visual Basic into REALbasic. The declare syntax is strikingly close between the two languages, but there are some very subtle differences to watch out for. So the VB Declare Converter is a whitepaper that explains how to convert VB declares that you find online or have written yourself into RB declares. Also included with the whitepaper is an open source application that will convert declares automatically. The program isn't 100% fool-proof, but it will convert the majority of VB declares."

For my avplayer program as well as my hideshow program, I made use of Aaron's VB Declare Converter, but the particular Windows API Declares used in avplayer and hideshow were not in Aaron's Windows Functionality Suite (at least at the time when I wrote my programs).

Barry Traver



Home Page for This Blog: http://traverrbw.blogspot.com/

Programs and Files Discussed in the Blog: http://traver.org/traverrbw/

Tuesday, January 17, 2006

CardTrick: Comments by Steve Garman

The starting entry for this blog included reference to a simple RB CardTrick program utilizing the Windows Cards.dll file. (See http://snurl.com/inwc for the entry. The CardTrick discussion is the second part of the entry.))

In response, Steve Garman sent some useful comments. With his permission, I'm placing them here for your benefit:

Steve Garman wrote:

> Barry,
>
> I just downloaded your cardtrick program from your new windows blog.
>
> It's great to see it, the trick takes me back nearly 50 years.
>
> I hope you won't be offended but I've got a couple of suggestions.
>
> Firstly, are you aware that to dimension an array of 52 elements (0 to 51)
> Dim cards(51)
> is sufficient ?
>
> Also, did you know that RB arrays have a very efficient Shuffle method so
> your whole ShuffleCards routine could be:
> Dim i As Integer
> For i = 0 To 51
> n(i) = i
> Next i
> n.Shuffle
>
> I also have some suggetions for shortening PrepareToRedeal
> I would write it something like this.
>
> Dim i As Integer
> Dim NewArrangement(26) As Integer
>
> If WhichTime = 1 Then
> CardLocation = ColumnChosen - 1
> lblInstructions.Text = "Tell me what column your card is in now."
> ElseIf WhichTime = 2 Then
> CardLocation = CardLocation + 3 * (ColumnChosen - 1)
> lblInstructions.Text = _
> "Last time: Tell me what column your card is in."
> ElseIf WhichTime = 3 Then
> CardLocation = CardLocation + 9 * (ColumnChosen - 1)
> lblInstructions.Text = ""
> End If
>
> For i = 0 To 8
> NewArrangement(i) = n(i*3)
> NewArrangement(i+9) = n(i*3+1)
> NewArrangement(i+18) = n(i*3+2)
> Next i
>
> For i = 0 To 26
> n(i) = NewArrangement(I)
> Next i
>
> Regards,
> Steve
>

Thanks, Steve, for the comments!

Barry Traver



Home Page for This Blog: http://traverrbw.blogspot.com/

Programs and Files Discussed in the Blog: http://traver.org/traverrbw/

Monday, January 16, 2006

Mini AVPlayer for Windows Using API Calls

Traver Mini AV player (only for the Windows version of REALbasic) is a very simple media player that can play many standard audio and video files, including .mid or .midi, .mp3, .wav, .avi, and .mpg files as well as audio CD's.

The Windows Media Player must be installed on your computer for the RB program to work, since the RB program uses a mciSendString API call to get an invisible Windows Media Player to perform the task.

The program is "bare bones," so it's an easy-to-understand foundation on which to build or from which to extract the relevant code for your use in other REALbasic programs for Windows.

Using mciSendString, you can make use of many different CommandStrings to expand and improve this program greatly (e.g., you may want to do things like "pause" or "rewind"), but I'll leave you to do your own research on that.

IMPORTANT: This new version removes a bug that prevented the earlier version from working properly. Enjoy!

Barry Traver

P.S. You can get the source code here:

http://traver.org/traverrbw/



Home Page for This Blog: http://traverrbw.blogspot.com/

Programs and Files Discussed in the Blog: http://traver.org/traverrbw/

Sunday, January 15, 2006

Introducing REALbasic for Windows: The Blog

REALbasic (RB) is a cross-platform computer language used to develop programs for Windows, Mac, and Linux. In some ways, it is very similar to Visual Basic 6.0, except that an RB program will run without the hassle of a complicated installation procedure or the presence of a separate runtime file: you can put your program into a single file on a CD-ROM, and run it from there!

The purpose of this blog is to two-fold: (1) to share comments about things I've learned about RB and (2) to announce the availability of RB source code for (I hope) hundreds of short RB programs."

The emphasis will be upon short programs that are "simple samples," learning examples that may serve as building blocks for larger programs. In many cases the programs will do things that you won't find elsewhere, in spite of the small size of the programs. Only source code will be provided, so this blog (or RBlog) is intended specifically for REALbasic programmers. Most of the programs have only been tested for Windows (using RB 2006 on Windows XP, although earlier versions of RB or Windows may often work as well).

Disclaimer: No promises, warranties, guarantees, etc. of any kind are made here. The code may or may not work for the purpose you intend. If it doesn't, then I warned you of that. If it does, and if you would like to say "Thank you" in some concrete way (e.g., by sending a donation), then that would be great and much appreciated (although I will have to earn it first by providing enough useful programs that you will want to do so). I'll explain in a future entry how that may be done.

The plans are also to include comments on and make reference to longer programs from time to time. Links will be provided to those larger programs as well. Most of the programs, however, will be "barebones," not much more than enough code snippets to accomplish the purpose.

Even though REALbasic is a cross-platform language, I sometimes write programs that work only in Microsoft Windows. That's because that's what my computer runs, and I used to program primarily in Visual Basic, a language very similar to REALbasic. I was accustomed to being able to use API calls to access the Windows operating system, so as to put to use the DLLs (Dynamic Link Libraries) that already existed as part of the Windows OS.

Well, one of those DLLs, specifically Cards.dll, makes it easy to do the graphics for playing cards in any program in which playing cards play a part: solitaire (e.g., Klondike), card games (e.g., Poker), and even magic (card tricks)! Aaron Ballman shows how to make a card game for Windows on his Web site. Here's what he says:

Making a card game with Microsoft's Cards.dll
If you're like me and enjoy making fun card games,
but hate the tedium of the graphics logic, then you'll
be very interested in this simple set of classes which
make use of the Microsoft Cards.dll library. The sample
project includes a simple demonstrating of the classes
by implementing the game "War."

You can find Aaron Ballman's article and code here

Well, I thought I'd write a card trick based on Aaron's code. The program is called CardTrick.rbp and it works only for Microsoft Windows. Most of the programs I reference here in this blog will work on all three platforms, - Mac, Windows, and Linux - but occasionally (as in this case) I'll write a program that will only work in Windows.

The trick isn't especially impressive. If you're familiar with how doing a "binary search" is an efficient way to locate an item, similarly a "ternary search" is efficient as well. Mathematically, it is only necessary for you to tell the computer which of the three columns three times in order for the computer to narrow the choice down to one card out of twenty-seven (since 3 x 3 x 3 = 27). So it's not much of a magic trick, but the main purpose of the program is just to show another simple use of the Windows Cards.dll.

Doing graphics can be a bit tricky. Working with graphics on the screen causes the Paint event to take place, and if the Paint even does some painting itself, you can easily get lost in a vicious circle. My solution to that was to insert this at the top of the Paint event handler:

If DontDoPaintStuff = True Then Exit

DontDoPaintStuff is a global Boolean, and before I do some graphics work (like dealing a card) I set DontDoPaintStuff to True and then set it back to False as soon as I've finished. This keeps me out of vicious loops and keeps the graphics from getting messed up, as you'll see if you try out the program.

If you've got a Mac and have access to playing card graphics, you may want to see if you can modify CardTrick.rbp to work on the Mac. The fifty-two cards are numbered from 0 to 51 (four complete suits, no joker), and here's how I shuffled the deck:

Dim I, RandomPosition, Holder As Integer
Dim r as New Random
For I = 0 To 51
N(I) = I
Next I
For I = 51 DownTo 0
RandomPosition = r.InRange(0,I)
Holder = N(I)
N(I) = N(RandomPosition)
N(RandomPosition) = Holder
Next I

What's happening is that you're taking the last card and swapping it with a random card chosen from the rest of the deck, taking the next-to-last card and swapping it with a random card chosen from the (now one card smaller) remainder of the deck, and so on. The end result is that the entire deck has been randomized (shuffled), and you haven't had to worry about accidentally choosing the same card twice. This is simpler than some methods I've seen of shuffling a deck and is no less random than any other method.

Barry Traver



Home Page for This Blog: http://traverrbw.blogspot.com/

Programs and Files Discussed in the Blog: http://traver.org/traverrbw/