Windows API Tutorial - Part Two, Page 6
In this section, I'll be sending you off on five unique tasks from around the Web.
You'll be asked to disable Ctrl-Alt-Delete key combinations, display that lovely 'Browse for Folders' dialog box, force a Windows restart, get information on a drive, plus display the File Open dialog box without the bulky Common Dialog control.
So, five tasks to complete that will boost your API knowledge no end. Some of these teasers are simple, others more complicated. But each will offer something new and challenging, an awareness of some extra functionality or problem yet to be uncovered.
Please, ensure you complete these tasks before continuing to the end of this tutorial. All ready? Good luck!
- Disabling Ctrl-Alt-Delete - Sometimes you don't want users trying to exit your application using that horrid Ctrl-Alt-Delete combination. So stop them!
- Browse for Folders - You've probably seen the 'Browse for Folders' dialog in applications such as Paint Shop Pro or Frontpage. Well, now you can have it too. Note how you use a special data type here, build it up then pass to the API call. Also, notice that you aren't using one of the standard Win32 DLLs (User32, Kernel32, Gdi32) here.
- Restart Windows - Need to restart Windows after that installation routine? Make users log off, shut down, reboot and more — with this groovy API call.
- Get the Drive Type - Is it a bird, is it a plane? Nope, it's a floppy disk drive. How do you know? Well, that's where this groovy GetDriveType call steps in. Check it out!
- File Open Dialog - You can use the API to display a basic file open dialog box to replace the bulky Common Dialog control that bundles with VB. Note the OPENFILENAME type here, too — as with the Browse Folders section above. It also passes strings around, as we did earlier with the GetWindowsDirectory function.
Have you finished already? Wow. How did you find the tasks?
I can imagine the Browse for Folders and File Open Dialog activities were the most difficult. A new concept was introduced in both those samples types, or User Defined Types (UDTs) to most VB'ers. Some API folk also call them structures.
As an example of this, the File Open Dialog activity used a call titled 'GetOpenFileName'. If you open the API Viewer, you should be able to find this under the Declares section. You'll notice it asks for one parameter 'pOpenfilename As OPENFILENAME'.
If you change the API Type combo to Types, you can look up the 'OPENFILENAME' Visual Basic UDT. And hey look, it's exactly the same as the code in that article!
So you can see the thought process behind this. You now know where the author of that particular article got his information.
Of course, before running that function, the writer had to generally know what it did and how to use it. That may have been discovered via the Microsoft API documentation, another Visual Basic website, one of the variety of sources mentioned earlier — or, heck, it could've just been a darn good guess.
What have we learned from this section? Well, you've reinforced all the API skills you've picked up so far, checked out a few neat calls, plus uncovered the concept of 'types', which you may often find yourself passing to the API.
That's the good news. And now...
Page 6 of 9