Borland - C++ Win32 API.pdf

(938 KB) Pobierz
c++4beginingers.PDF
Contents
Getting Started................................................................................................................................ 4
What this tutorial is all about....................................................................................................... 4
Important notes ........................................................................................................................... 4
The simplest Win32 program ...................................................................................................... 4
Calling Conventions................................................................................................................ 5
Win32 Data Types .................................................................................................................. 5
A Simple Window .......................................................................................................................... 6
Step 1: Registering the Window Class......................................................................................... 7
Step 2: Creating the Window ...................................................................................................... 9
Step 3: The Message Loop ........................................................................................................ 10
Step 4: the Window Procedure .................................................................................................. 10
Step 5: There is no Step 5.......................................................................................................... 11
Handling Messages ....................................................................................................................... 11
Understanding the Message Loop ................................................................................................. 15
What is a Message? ............................................................................................................... 15
Dialogs ................................................................................................................................. 16
What is the Message Queue................................................................................................... 16
What is a Message Loop ....................................................................................................... 16
Using Resources ........................................................................................................................... 17
Menus and Icons ........................................................................................................................... 19
The program file icon ............................................................................................................ 24
Dialogs, GUI coders best friend .................................................................................................... 24
Modeless Dialogs ......................................................................................................................... 27
Standard Controls: Button, Edit, List Box ..................................................................................... 29
Controls ................................................................................................................................ 30
Messages............................................................................................................................... 30
Edits ......................................................................................................................................... 30
Edits with Numbers............................................................................................................... 31
List Boxes................................................................................................................................. 32
Adding Items ........................................................................................................................ 32
Notifications ......................................................................................................................... 32
Getting Data from the ListBox .............................................................................................. 33
Statics ....................................................................................................................................... 33
Dialog FAQ .................................................................................................................................. 34
Changing Colours ..................................................................................................................... 34
Giving the Dialog an Icon ......................................................................................................... 34
Why Doesn't my Combo Box Work? ........................................................................................ 35
What about all the other controls!.............................................................................................. 35
App Part 1: Creating controls at runtime ....................................................................................... 36
Creating controls ................................................................................................................... 36
Sizing of dynamically created controls .................................................................................. 37
Creating other controls at runtime ......................................................................................... 37
App Part 2: Using files and the common dialogs ........................................................................... 38
The Common File Dialogs ........................................................................................................ 38
Reading and Writing Files......................................................................................................... 39
Reading................................................................................................................................. 39
Writing.................................................................................................................................. 40
App Part 3: Tool and Status bars ................................................................................................... 41
An IMPORTANT Word on Common Controls ..................................................................... 41
Toolbars.................................................................................................................................... 42
Toolbar buttons ..................................................................................................................... 42
Adding Standard Buttons ...................................................................................................... 42
Status bars................................................................................................................................. 43
Proper Sizing ............................................................................................................................ 44
App Part 4: Multiple Document Interface...................................................................................... 44
MDI Overview.......................................................................................................................... 45
Getting Started with MDI.......................................................................................................... 46
MDI Client Window ............................................................................................................. 46
Child Window Class ............................................................................................................. 47
MDI Child Procedure............................................................................................................ 48
Creating and Destroying Windows ........................................................................................ 50
Bitmaps, Device Contexts and BitBlt ............................................................................................ 51
GDI .......................................................................................................................................... 52
Device Contexts .................................................................................................................... 52
Bitmaps................................................................................................................................. 52
GDI Leaks ............................................................................................................................ 52
Displaying Bitmaps................................................................................................................... 53
Getting the Window DC........................................................................................................ 54
Setting up a Memory DC for the Bitmap ............................................................................... 54
Drawing ................................................................................................................................ 54
Cleanup................................................................................................................................. 54
Transparent Bitmaps ..................................................................................................................... 55
Transparency ............................................................................................................................ 55
BitBlt operations ................................................................................................................... 55
Mask Creation....................................................................................................................... 56
How does all this work? ............................................................................................................ 57
SRCAND .............................................................................................................................. 57
SRCPAINT ........................................................................................................................... 58
SRCINVERT ........................................................................................................................ 58
Example.................................................................................................................................... 58
Timers and Animation .................................................................................................................. 58
Setting up.................................................................................................................................. 58
Setting the Timer ...................................................................................................................... 59
Animating in WM_TIMER ....................................................................................................... 60
Double Buffering ...................................................................................................................... 61
Faster Double Buffering ........................................................................................................ 61
Killing the Timer ...................................................................................................................... 62
Text and Fonts .............................................................................................................................. 62
Loading Fonts ........................................................................................................................... 62
Default Fonts ........................................................................................................................ 63
Drawing Text............................................................................................................................ 63
Choosing Fonts ......................................................................................................................... 64
Choosing Colours ..................................................................................................................... 65
Control Fonts ............................................................................................................................ 66
Appendix A: References ............................................................................................................... 66
Books ....................................................................................................................................... 66
Links......................................................................................................................................... 67
Appendix B: Free Borland C++ Command Line Tools.................................................................. 67
Getting Them ............................................................................................................................ 67
Using Them .............................................................................................................................. 68
Basic commands ................................................................................................................... 68
Linking in Resources............................................................................................................. 68
Appendix C: Why you should learn the API before MFC.............................................................. 69
The Controversy ....................................................................................................................... 69
My Answer ............................................................................................................................... 69
So basically............................................................................................................................... 70
Appendix D: Resource file notes................................................................................................... 70
Argh! ........................................................................................................................................ 70
Compatibility ............................................................................................................................ 71
Compiling resources under BC++ ............................................................................................. 71
Getting Started
What this tutorial is all about
This tutorial is intended to present to you the basics (and common extras) of writing programs using the Win32 API.
The language used is C, most C++ compilers will compile it as well. As a matter of fact, most of the information is
applicable to any language that can access the API, inlcuding Java, Assembly and Visual Basic. I will not however
present any code relating to these languages and you're on your own in that regard, but several people have previously
used this document in said languages with quite a bit of success.
This tutorial will not teach you the C language, nor will it tell you how to run your perticular compiler (Borland C++,
Visual C++, LCC-Win32, etc...) I will however take a few moments in the appendix to provide some notes on using the
compilers I have knowledge of.
If you don't know what a macro or a typedef are, or how a switch() statement works, then turn back now and read a
good book or tutorial on the C language first.
Important notes
Sometimes throughout the text I will indicate certain things are IMPORANT to read. Because they screw up so many
people, if you don't read it, you'll likely get caught too. The first one is this:
The source provided in the example ZIP file is not optional! I don't include all the code in the text itself, only that
which is relevant to whatever I'm currently discussing. In order to see how this code fits in with the rest of the program,
you must take a look at the source provided in the ZIP file.
And here's the second one:
Read the whole thing! If you have a question during one section of the tutorial just have a little patience and it might
just be answered later on. If you just can't stand the thought of not knowing, at least skim or search (yes computers can
do that) the rest of the document before asking the nice folks on IRC or by email.
Another thing to remember is that a question you might have about subject A might end up being answered in a
discussion of B or C, or maybe L. So just look around a little.
Ok I think that's all the ranting I have to do for the moment, lets try some actual code.
The simplest Win32 program
If you are a complete beginner lets make sure you are capable of compiling a basic windows application. Slap the
following code into your compiler and if all goes well you should get one of the lamest programs ever written.
Remember to compile this as C, not C++. It probably doesn't matter, but since all the code here is C only, it makes
sense to start off on the right track. In most cases, all this requires if you add your code to a .c file instead of a .cpp
file. If all of this hurts your head, just call the file test.c and be done with it.
#include <windows.h>
22472324.001.png
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MessageBox(NULL, "Goodbye, cruel world!", "Note", MB_OK);
return 0;
}
If that doesn't work, your first step is to read whatever errors you get and if you don't understand them, look them up in
the help or whatever documents accompany your compiler. Make sure you have specified a Win32 GUI (NOT
"Console") project/makefile/target, whatever applies to your compiler. Unfortunately I can't help much with this
part either, as errors and how to fix them vary from compiler to compiler (and person to person).
You may get some warnings about you not using the parameters supplied to WinMain() . This is OK. Now that we've
established you can in fact compile a program, lets go through that little bit of code....
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
WinMain() is windows equivalent of main() from DOS or UNIX. This is where your program starts execution. The
parameters are as follows:
HINSTANCE hInstance
Handle to the programs executable module (the .exe file in memory)
HINSTANCE hPrevInstance
Always NULL for Win32 programs.
LPSTR lpCmdLine
The command line arguments as a single string. NOT including the program name.
int nCmdShow
An integer value which may be passed to ShowWindow() . We'll get to this later.
hInstance is used for things like loading resources and any other task which is performed on a per-module basis. A
module is either the EXE or a DLL loaded into your program. For most (if not all) of this tutorial, there will only be one
module to worry about, the EXE.
hPrevInstance used to be the handle to the previously run instance of your program (if any) in Win16. This no
longer applies. In Win32 you ignore this parameter.
Calling Conventions
WINAPI specifies the calling convention and is defined as _stdcall . If you don't know what this means, don't worry
about it as it will not really affect us for the scope of this tutorial. Just remember that it's needed here.
Win32 Data Types
You will find that many of the normal keywords or types have windows specific definitions, UINT for unsigned
int , LPSTR for char* etc... Which you choose is really up to you. If you are more comfortable using char* instead
of LPSTR , feel free to do so. Just make sure that you know what a type is before you substitute something else.
Just remember a few things and they will be easy to interpret. An LP prefix stands for Long Pointer . In Win32 the Long
part is obsolete so don't worry about it. And if you don't know what a pointer is, you can either 1) Go find a book or
tutorial on C, or 2) just go ahead anyway and screw up a lot. I'd really recommend #1, but most people go with #2 (I
would :). But don't say I didn't warn you.
22472324.002.png
Zgłoś jeśli naruszono regulamin