?? start.html
字號:
<HTML><LINK HREF="style.css" REL="STYLESHEET" TYPE="text/css"><HEAD><TITLE>Tutorial: Getting Started</TITLE></HEAD><BODY><FONT SIZE="-1">[ <A HREF="./index.html">contents</A>| <A HREF="http://www.winprog.org/">#winprog</A>]</FONT><HR><H1>Getting Started</H1><H2>What this tutorial is all about</H2>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++ compilerswill compile it as well. As a matter of fact, most of the information is applicableto 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 yourown in that regard, but several people have previously used this document in saidlanguages with quite a bit of success.<P>This tutorial <I>will not</I> teach you the C language, nor will it tell you how torun your perticular compiler (Borland C++, Visual C++, LCC-Win32, etc...) I willhowever take a few moments in the appendix to provide some notes on using the compilersI have knowledge of.<P>If you don't know what a <I>macro</I> or a <I>typedef</I> are, or how a <CODE>switch()</CODE>statement works, then turn back now and read a good book or tutorial on the C language first.<H2>Important notes</H2>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 caughttoo. The first one is this: <P><B>The source provided in the example ZIP file is not optional!</B> I don't include all the code in the text itself, only that which is relevant to whatever I'mcurrently discussing. In order to see how this code fits in with the rest of theprogram, you <I>must</I> take a look at the source provided in the ZIP file.<P>And here's the second one:<P><B>Read the whole thing!</B> If you have a question during one section of the tutorialjust have a little patience and it might just be answered later on. If you just can'tstand 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.<P>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.<P>Ok I think that's all the ranting I have to do for the moment, lets try some actual code.<H2>The simplest Win32 program</H2>If you are a complete beginner lets make sure you are capable of compiling abasic windows application. Slap the following code into your compiler andif all goes well you should get one of the lamest programs ever written.<P>Remember to compile this as C, not C++. It probably doesn't matter, but sinceall 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 <CODE>.c</CODE> file instead ofa <CODE>.cpp</CODE> file. If all of this hurts your head, just call the file <CODE>test.c</CODE> and be done with it.<PRE CLASS="LIST">#include <windows.h>int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){ MessageBox(NULL, "Goodbye, cruel world!", "Note", MB_OK); return 0;}</PRE><P>If that doesn't work, your first step is to read whatever errors you get andif you don't understand them, look them up in the help or whatever documentsaccompany your compiler. <B>Make sure you havespecified a Win32 GUI (NOT "Console") project/makefile/target, whatever appliesto your compiler.</B> Unfortunately I can't help much with this part either, aserrors and how to fix them vary from compiler to compiler (and person to person).<P>You may get some warnings about you not using the parameters supplied to<CODE>WinMain()</CODE>. This is OK.Now that we've established you can in fact compile a program, lets go throughthat little bit of code....<PRE CLASS="SNIP">int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)</PRE><CODE>WinMain()</CODE> is windows equivalent of <CODE>main()</CODE> from DOS or UNIX.This is where your program starts execution. The parameters are as follows:<DL><DT><CODE>HINSTANCE hInstance</CODE><DD>Handle to the programs executable module (the .exe file in memory)<DT><CODE>HINSTANCE hPrevInstance</CODE><DD>Always <CODE>NULL</CODE> for Win32 programs.<DT><CODE>LPSTR lpCmdLine</CODE><DD>The command line arguments as a single string. NOT including the program name.<DT><CODE>int nCmdShow</CODE><DD>An integer value which may be passed to <CODE>ShowWindow()</CODE>. We'll get to this later.</DL><P><CODE>hInstance</CODE> is used for things like loading resources and any other task which isperformed 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.<P><CODE>hPrevInstance</CODE> used to be the handle to the previously run instanceof your program (if any) in Win16. This no longer applies. In Win32 you ignorethis parameter.<H3>Calling Conventions</H3><P><CODE>WINAPI</CODE> specifies the calling convention and is definedas <CODE>_stdcall</CODE>. If you don't know what this means, don't worry about itas it will not really affect us for the scope of this tutorial. Just remember thatit's needed here.<H3>Win32 Data Types</H3>You will find that many of the normal keywords ortypes have windows specific definitions, <CODE>UINT</CODE> for <CODE>unsignedint</CODE>, <CODE>LPSTR</CODE> for <CODE>char*</CODE> etc... Which youchoose is really up to you. If you are more comfortable using <CODE>char*</CODE>instead of <CODE>LPSTR</CODE>, feel free to do so. Just make sure that you know what a type is before you substitute something else.<P>Just remember a few things and they will be easy to interpret. An <CODE>LP</CODE>prefix stands for <I>Long Pointer</I>. In Win32 the <I>Long</I> part is obsolete sodon't worry about it. And if you don't know what a pointer is, you can either 1) Gofind a book or tutorial on C, or 2) just go ahead anyway and screw up a lot. I'dreally recommend #1, but most people go with #2 (I would :). But don't say I didn'twarn you.<P>Next thing is a <CODE>C</CODE> following a <CODE>LP</CODE> indicates a <CODE>const</CODE>pointer. <CODE>LPCSTR</CODE> indicates a pointer to a const string, one that can not or will not be modified. <CODE>LPSTR</CODE> on the other hand is not <CODE>const</CODE> andmay be changed.<P>You might also see a <CODE>T</CODE> mixed in there. Don't worry about this for now, unless you are intentionally working with <I>Unicode</I>, it means nothing.<HR><FONT SIZE="-1">Copyright © 1998-2003, Brook Miles (<A HREF="mailto:forger(nospam)winprog.org">theForger</A>). All rights reserved.</FONT><SCRIPT language="JavaScript"><!-- var re = /\(nospam\)/ig; var str; for(i = 0;i < document.links.length;i++) { str = "" + document.links(i).href; if(str.search(re) != -1) document.links(i).href = str.replace(re, "@"); str = "" + document.links(i).innerHTML; if(str.search(re) != -1) document.links(i).innerHTML = str.replace(re, "@"); }--></SCRIPT></BODY></HTML>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -