by Stephan Greiger - Independent Developer
Sometimes being a programmer does actually have its benefits. When you think about it, we write programs all day that sit on other people's computers. We ourselves hardly use the programs we write. When you look at the program included with this article, you'll come to appreciate why!
Miles Forrest and I decided that on Miles' last day, we should play a prank on one of the trainee developers here. Not really the level of professionalism we generally exhibit, but it was his last day after all. And, hey, it seemed like a Good Idea at the time. OK, I'll come clean - over a lager-enhanced farewell luncheon, we decided to develop a small program that would reside on this poor trainee's PC and basically cause a little mischief but nothing too serious.
We decided that, first of all, he really needed to learn to use his mouse backwards… at least sometimes. And, we felt, it would be a good idea if he understood that the desktop isn't always what it appears.
Swapping the Mouse Buttons
When we first came up with this idea, we thought it might just be a tough one to implement. But, of course, if Windows Control Panel can do it, then there's probably an API floating around somewhere. There is, of course :
Declare Function SwapMouseButton& Lib "user32" (ByVal bSwap As Long)
This made the process trivial :
Sub SwapMouse() Static lsState As Long Dim lpReturn As Long lsState = Not (lsState) lpReturn = SwapMouseButton(lsState) End Sub
Covering the Desktop
The idea here was to periodically grab a copy of the desktop, then paste onto a form exactly the same height and width of the desktop, effectively confusing for at least a little while!
For this to work, we needed to get hold of a quick, easy, reliable capture routine. Just the sort of thing that Dan Appleman at Desaware is noted for we thought. And were correct! His MyCapture routine below filled the bill perfectly.
Public Sub MyCapture(ByVal mode%, ByRef frmrForm As frmDummyScreen) Dim altscan% Dim dl& Dim snapparam% altscan% = MapVirtualKey(VK_MENU, 0) If mode Then keybd_event VK_MENU, altscan, 0, 0 ' It seems necessary to let this key get processed before ' taking the snapshot. End If ' Why does this work? Who knows! snapparam = 1 DoEvents ' These seem necessary to make it reliable ' Take the snapshot keybd_event VK_SNAPSHOT, snapparam, 0, 0 On Error Resume Next frmrForm.Picture = Clipboard.GetData(vbCFBitmap) If mode Then keybd_event VK_MENU, altscan, KEYEVENTF_KEYUP, 0 End Sub
With that in place, the rest was obviously easy :
Function ShowDummyWindow() Dim frmpDummyScreen As frmDummyScreen Set frmpDummyScreen = New frmDummyScreen Load frmpDummyScreen MyCapture 0, frmpDummyScreen frmpDummyScreen.Show vbModal End Function
We decided that, just to round off the confusion, we'd add the ability to run a 'random' application as well.
With that, and three timers running the whole shebang, we had a fun little afternoon in store...
A couple of final points.
The reason we are publicising this application is not because we want to promote this sort of activity, but because it does actually have a couple of interesting features in it which may help a programmer or two - and not the sort of thing you'd code every day.
Second, be gentle with the code. Miles and I had to knock it up in an hour and as a result, there is a severe lack of structure and comments. No lack of lager or humour, though!
Enjoy - we did. He didn't. Well, not initially, anyway.
The code, such as it is, is available for download.