Getting Started Guide
What's this tutorial about?
Win32 Api The basics of writing programs (and general notation). The language used is c But most c++ api java , assembly and visual Basic api based on the use of other languages for programming has been quite successful.
This tutorial will not teach you C language, and will not tell you how to run your particular compiler (Borland C + +,Visual C ++,lcc-win32 etc.) However, I will spend some time in the appendix to provide some knowledge of the use of compiler notes.
If you don't know what a macro or type definition is or how a switch statement works, look back now for a a good book or a tutorial in C language to study and study well.
Precautions
Throughout the tutorial, I will sometimes point out exactly what is important, and the content of these places needs to be studied. Because these places have caused a lot of people to screw up, if you haven't studied it well, you're probably screwed.
The first place is:
in the the code provided in the ZIP archive is a must-see! I did not provide all the code in the tutorial, only contains the code related to the part I am discussing, in order to understand how this code is embedded in the program, you must look at the source in the ZIP file.
A second aspect is:
Read all the content completely! If you have questions in one of the sections of this tutorial, please stay patient and your questions may be resolved in the next section. If you can't stand reading with questions, at least browse and search for the rest of the document before sending me a message.
One more thing to keep in mind is that you about the subject A some of the questions may be answered in the discussion of B or C or even L , so read the document and look for answers when you encounter problems.
OK, here are some things you need to know about white before the tutorial starts, and now let's try some actual code.
The simplest Win32 Program
If you are a thoroughly novice, then make sure at least you can compile this most basic window application. Copy the code into your compiler, and if nothing happens, you should see one of the simplest windows.
remember to treat the code as C compilation, not C + +, this may not matter, but because all the code here is C Code, we should have a reasonably correct start. In most cases, you should add your code to the . C file instead of the . cpp file, and if you feel very sore, put the code in the test.c The file is then compiled and then it's done.
1#include <windows.h>2 intWINAPI WinMain (hinstance hinstance, hinstance hprevinstance,3LPSTR lpCmdLine,intncmdshow)4 {5MessageBox (NULL,"Goodbye, cruel world!","Note", MB_OK);6 return 0;7}
If the code does not work, first look at the error and then resolve it, if you do not understand the contents of the error, please check the applicable to your compiler's help or any other documents. Make sure you create a new Win32 GUI Project (not a console application), and no matter what compiler you're using, it's a pity that I can't provide more help, because the standards for different compilers are not the same.
you may get some warning to tell you that no use applies to WinMain () parameter, which doesn't matter, can be compiled through just fine. Now that we have built a simple program that can be compiled, let's take a closer tour of the code.
int WINAPI WinMain (hinstance hinstance, hinstance hprevinstance, int ncmdshow)
WinMain () is the equivalent of the DOS or UNIX program's Main (), which is where your program begins to execute, with the following list of parameters:
HINSTANCE hinstance
Handle to the program executable module (executable file in memory)
official The API (the executable module of the handler ) is typically used to load resources and perform other tasks in the underlying module ( exe that is loaded into your program or DLL module ) dll:dynamic link library
( PS. about handle: handle (handle) is the identity of a resource, and the operating system manages these resources by using a handle to find the corresponding resource. The handle is a bit like a pointer, the resource consumes a certain amount of memory, when we need to use these resources, we need to obtain these memory resources, through the handle can access the memory.
by type of resource, you can divide a handle into icon Handle ( Hicon), cursorhandle (hcursor), Window handle (HWND), application instance handle (hinstance))
HINSTANCE hPrevInstance
It is usually NULL for Win32 programs, useful for Win16 programs, and for Win32 Useless
LPSTR lpCmdLine
A string of command-line arguments, excluding the program name, related to the path of the open file, default to NULL
int nCmdShow
one that might be passed to An integer value of the ShowWindow () method, which we'll cover in the next section
HINSTANCE is used to load resources and other tasks based on a Single Window module, where all the modules are EXE or DLLs loaded into your program , and for the most part of This tutorial, We just need to focus on a module, that is EXE.
hPrevInstance is used only in the Win16 program, as a pre-processing handle before instantiation of the program, This parameter is no longer used in Win32, where you can ignore it.
Conventions of invocation
WINAPI Specifies the convention for parameter invocation, which is defined as the standard of _stdcall. If you don't know what it means, don't worry, it's not going to affect our next tutorial, you just need to know where to specify it.
data types for Win32
You'll find a lot of generic keywords or types in win32 There is a specified definition in Span style= "FONT-FAMILY:CALIBRI;" >win32 uint to represent unsigned int , with lpstr to represent char* ... etc... Which expression to choose depends on you, if you feel the use of char* lpstr more comfortable, then use the char* now! You just need to make sure that you understand what type of
you have before replacing
Remember some specific definitions and it's easy to explain! A lp long Pointer Span style= "font-family: the song Body;" > in win32 long is discarded so we don't have to worry about pointers plus long But if you don't even know the pointers, you can choose: 1 ) find this c book or tutorial see 2 ) continue to this tutorial, then a face confused. I suggest that people who do not understand choose 1 But most people who don't understand choose Span style= "FONT-FAMILY:CALIBRI;" >2 The next thing you don't understand, don't blame me for not reminding you.
then in The C behind LP indicates that this is a constant pointer, so LPCSTR represents a pointer to a constant string, which cannot be changed, and so on LPSTR represents a very volume pointer, which is immutable.
you may see a T is mixed up inside, we don't need to care about this now, unless you are using Unicode encoding, otherwise T doesn't mean anything.
The reason why I want to translate it is because this tutorial is very good, and the internet did not find the corresponding translation, due to my english level limit, can only translate to this extent, there are flaws also hope to point out, attached to the translation of the original English course address:/http Www.winprog.org/tutorial/start.html
WIN32 Programming API Basics--1. Getting Started Guide translation according to English course