(Translation) Programming windowS-2

zhaozj2021-02-16  56

All 32-bit versions of Windows support Win16API to ensure compatibility for the old app. Win32API is also supported to run new programs. It is very interesting that this work is done in different ways in WindowsNT, Windows 95, and Windows 98. In WindowsNT, the Win16 function calls through a translation layer and translated into Win32 function calls performed by the operating system. In Windows 95 and Windows 98, this process is in turn: Win32 functions are then converted to Win16 functions processed by the operating system through a translation layer.

Once, there are two two Windows APIS collections (at least on the name). Win32S ("S" is "Subset") is an API that allows the 32-bit application that allows the program to write a Windows3.1 platform. This API only supports the 32-bit version of the 32-bit version of the Win16 that has been supported. Also, Windows95API has been known as Win32C ("C" is "Compatibility"), but this term is discarded.

Today, WindowsNT and Windows 98 are considered to support Win32API. However, each operating system supports some other party unsupported features. Of course, due to the existence of intersection, the program can be operated on both platforms at the same time. At the same time, two programs will be integrated in the future. This view is widely recognized.

Other options in language

Using C and original APIs are not the only way to write WIN98 programs. However, this pathway gives you the best performance, the most powerful power and the most powerful versatility when using Windows features. The executable is relatively smaller and does not require an external library (of course, the Windows DLLS itself) is not required to run. Most importantly, familiarity to the API makes you deeper understandings in Windows. No matter how you finally write Windows applications.

Although I think that the classic Windows programming is important for any Windows program. But I don't have to recommend that every Windows application is written with C and APIs. Many programmers are particularly programmers who have been programmed at home, or those programmers who are working in housework, enjoy development environments like Microsoft Visual Basic or Borland Delphi (with a PASCAL-oriented version). Convenience. These environments allow programmers to pay attention to the interface between an application and the code connected between objects on the user interface. To learn Visual Basic, you can check some of the books of Microsoft Press, such as "Now learn Visual Basic (1996), by Michael Halvorson.

In programmers, especially in Microsoft Visual C , Microsoft Foundation Class Libray has become other options in recent years. The MFC encapsulates some of the messy aspects of Windows programming in a C class library. Jeff Prosise's "Windows Programming with MFC (Microsoft Press, 1999)" provides a textbook for MFC.

Recently, Internet and World Wide Web have a huge push to Sun Microsystem's Java, a language that is inspired by C , including a graphical application that will be able to run on several platforms. A good book of Microsoft Press About Microsoft J (Microsoft Java Development Environment), "Visual J 6.0 Programming (1998)", by Stephen R. Davis. Obviously, almost no one is the only correct way to write a Windows program. Being out of everything is that the demand of the program itself may have chosen its development tool. But learn the Windows API to have a deep view of Windows work, it is important to manage what you ultimately use. Windows is a complex system. Place a programming level on the API without weakening its complexity, just hide them. In the morning and evening, this complexity will jump out in your thigh. Understand the API to give you better rehabilitation opportunities.

Any software layer above the original Windos API is necessary to limit you in a subset of all features, Visual Basic is ideal for your application, except that it does not allow you to make one or two important things. In this case, you have to use the original API call. The API defines the universe that I am in an inner one in Windows programmers. There is no way to more powerful more power than directly call API.

MFC is a special problem. When it simplifies some huge work (such as OLE), but in order to make him as I wish, I often find that I often wrestle in other characteristics (such as Document / View Architecture). The MFC has not become a universal secret that many people want to be large Windows programming. And few people will describe it as an object-oriented design. MFC programmers benefit many from knowing what happened in the definitions they have, and find them to query the MFC source code. Ability to understand those who learn the benefits of the Windows API.

In the programming environment part of this book, I will assume that you are using the Visual C 6.0 or Enterprise Edition that you want to be standard. The cheaper version is also suitable for the procedures in this book. Visual C is also part of Visual Studio6.0.

The Microsoft Visual C package is such as a C compiler, and other tools for compiling the Linked Windows programs. It also includes Visual C Developer Studio, one in it you can edit your source code, interactive creation, such as icons and dialog resources, editing compiling and debugging your programs.

If you are using Visual C 5.0, you may need to update the header file and introduction library for Windows 98 and Windows NT5.0. These can be found on Microsoft's websites. Visit http://www.mirosoft.com/msdn/ and select Download (Download) This is then Platform SDK (Platform Software Development Kit "Software Development Kit") You can download and install it to your selection update directory. In order to guide Microsft Develop Studio to query these directories, use the Options option under the Tool menu, then select the Directories tab.

The above Microsoft MSDN represents a Microsoft Developer Website (Microsoft Developer Network). It provides developers with frequent updated CD-ROMs, including many knowledge you want about the remote corners of Windows development. You may have to consider subscribing to MSDN to avoid frequently downloading things from Microsoft websites. API documentation

This book is not an alternative to official official Windows API documents. That document is no longer published in the form of printing, it can only be obtained by CD-ROM or Internet.

When you have installed Visual C 6.0, you will get an online help system including an API document. You can update it or use Microsoft-based online help systems by subscribing to MSDN. Visit http://www.microsoft.com/msdn/, then select the MSDN online library.

In Visual C 6.0, select the Contents item from the Help menu to launch the MSDN window. The API documentation is organized in a tree structure. Find some part of the Platform SDK. All the documents that I will refer to in this book come from there. I will use the mesh layer that represents PlatformSDK from the slash display to display their position. (I know that Platform SDK looks like MSDN's entire intellectual wealth. But I promise you that it is an indispensable core of Windows programming). For example, for documents related to how to use mice in your program, you can consult / Platform SDK / User Interface Services / User Input. I mentioned earlier, most of Windows is divided into three subsystems of Kernel, User and GDI. The kernel interface is listed in / Platform SDK / USER Interface Services, and GDI is listed in / Platform SDK / User Interface Services and GDI.

Your first Windows program

It is time to do some actual work. Let me start from a short Windows program, in order to compare, there is a short character mode program. These will help you get the direction and understand the general procedure for the direction and understanding of a program when you use the development environment.

An example of a character pattern:

One of the most popular books in the program is the C Programming Language (Prentice Hall, 1978 and 1988), by Brain W. Kernighan and Dennis M. Ritchie, NAS K & R. The first chapter of this book is started by the program displaying "Hello World".

This is the first version of the C Programming Language, the sixth page:

Main ()

{

Printf ("Hello, WORLD / N");

}

Yes. Once C procedures use C runtime library functions, such as Printf, without prior declaration. But now is the 1990s, and we want to give us a compiler to find out the chance of obvious mistakes in our code. Here is the modified code taken in the second edition of K & R:

#include

Main ()

{

Printf ("Hello, WORLD / N");

}

This program is still not as small as they look. Of course, it can be successfully compiled and run, but many programmers have preferred explicitly pointed out the return value of the main function. In this case, ANSI C specifies the function to actually returns a value:

#include

int main ()

{

Printf ("Hello, World / N"); RETURN 0;

}

We can make this program longer by including parameters to main, but don't take it. As long as there is an include statement, a program entry point, a function call to the runtime library, and a return statement.

Similar version of Windows

Windows and the "Hello World" program are equivalent to the same part of the character mode. It has a statement, a program entry point, a function call, and a return value statement. Here is the program:

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------

Hellomsg.c - displays "Hello, Windows 98!" In A Message Box

(c) Charles Petzold, 1998

-------------------------------------------------- ------------ * /

#include

Int WinApi Winmain (Hinstance Hinstance, Hinstance Hprevinstance,

PSTR SZCMDLINE, INT ICMDSHOW)

{

MessageBox (Null, Text ("Hello, Windows 98!"), Text ("Hellomsg"), 0);

Return 0;

}

Before I start to analyze the program, let's take a look at the steps to create a program in Visual C Developer Studio.

Start, select "New" from the File menu. In the New Dialog box, select the Project tab. Select Win32 program. Select a subdirectory at positions. Enter the project name at Project Name. In this example is Hellomsg. This will be a subdirectory that fills the directory at the location. Select Create a new workspace button. The platform part should be displayed is Win32, choose OK. A dialog that is marked with "WIN32 Application - Step" will appear. Choose you want to create an empty project and press the end button.

Select New from the File menu, and then create a file page in the dialog. Select C source files. Adding to the project should be selected. Enter hellomsg.c at the file name. Option OK.

Now you can enter the above content into the hellomsg.c file, or you can choose the insert menu and the File As Text entry to insert the hellomsg.c file in the CD-ROM with the book.

Structure, Hellomsg.c and K & R "Hello World" programs are the same. The stdio.h file is replaced by Windows.h. The entry point main is replaced with WinMain, and the C runtime library function Printf is replaced by the Windows API function messageBox. However, the program has a lot of new things. Include several versatile uppercase identifiers.

Let us start from the beginning.

head File

Hellomsg.c starts with a preprocessor instruction that you actually discovered at the top of each Windows program written with C.

#include

Windows.h is a total included file that contains other Windows header files, and some of them also include other header files. The most important, most basic, etc. in these headers:

WINDEF.H Basic Type Definition

Winnt.h supports Unicode type definition winbase.h kernel function WinUser.h user interface function WINGDI.H graphics device interface function These header files define all Windows data types, function calls, data structures, and constant identifiers. They are an important part of the Windows documentation. You may find that "Find" items in the file from Visual C Develper Studio is easy to find these headers. Your page can open these headers directly to check them directly in DevelPer Studio.

Program entry point

Just like the entry point of the program is the MIAN function, the entry point for each Windows program is WinMain. It looks like it is always like this:

Int WinApi Winmain (Hinstance Hinstance, Hinstance Hprevinstance,

PSTR SZCMDLINE, INT ICMDSHOW)

The document of this entry point is in the IN / Platform SDK / User Interface Services / Windowing / Windows / Window Reference / Window Functions. It is defined in WinBase.h (all rows are cut):

int

WinAPI

Winmain

Hinstance hinstance,

Hinstance Hprevinstance,

LPSTR LPCMDLINE,

Int nshowcmd

);

You may notice that I have made a little change in Hellomsg.c. In WINBASE.H, the third parameter is defined as LPSTR, and I turn it into PSTR. Both data types are defined in Winnt.h. It is a pointer to the string. The LP prefix is: "long pointer" is a 16-bit Windows legacy product.

I also changed the name of the two parameters in the WinMain statement; many Windows programs use a method called "Hungarian Nomenclature" to name the variable. This method includes giving a variable name a short prefix to indicate the data type of the variable. I will discuss this in Chapter III. For now, only need to remember the prefix i represents int and SZ represents "strings ending at 0".

The WinMain function is declared to return the INT function. WinAPI identifier is defined by the statement in WINDEF.H:

#define winapi __stdcall

This statement defines a call habit. He contains how to generate the machine code used to deploy the function call in the stack. Most Windows function calls are declared as WinAPI.

The first parameter gives WinMain is a playery called "instance handle". In Windows programming, the handle is just a program to identify the number of something. This handle is now unique. It is necessary for other Windows function calls. In earlier versions of Windows, when you run a program at the same time than once. You created multiple instances of that program. All instances of the same application share code and read-only memory (usually resources like menus and dialog templates). A program can determine if there is any other instance of itself in operation by checking the HPREINSTANCE parameter. It can skip some important processes and move some data from previous instances to their own data.

This concept is abandoned in 32-bit version of Windows. The second parameter for WinMain is always empty (defined as 0).

The third parameter for WinMain is the command line used to run the program. Some Windows applications use this to load files in memory when booting. The fourth parameter indicator for WinMain should initialize the display. Either normal, or maximize to fill the window, or minimize the displayed on the taskbar. We will see how these parameters are used in Chapter 3.

Messagebox function

The Messagebox function is set to show your short message. The small window displayed by MessageBox is actually considered to be a dialog, although there is no many functions. The first parameter for MessageBox is usually a handle of a window. We will see what this means in Chapter 3. The second parameter is a string displayed in MessageBox. In Hellomsg.c, all of these strings are hosted by a Text. You usually don't need to use text macros to accommodate all strings, but if you want to convert your program to Unicode character set, this is a good idea. I will conduct more in-depth discussions on this chapter.

The fourth function giving MessageBox is some of the prefix MBs to define a constant combination in Winuser.h. You can select a constant from the first collection to show the button you want to appear in the dialog box.

#define mb_ok 0x00000000L

#define mb_okcancel 0x00000001L

#define MB_AbortRetryIgnore 0x00000002L

#define mb_yesnocancel 0x00000003L

#define mb_yesno 0x00000004L

#define MB_RETRYCANCANCANCEL 0x00000005L

When you are set to 0 in the fourth parameter of Hellomsg, only the OK button appears. You can use the C or the OR operator to take each parameter shown above to indicate which button and the default constant are connected.

#define MB_DEFBUTTON1 0x00000000L

#define MB_DEFBUTTON2 0x00000100L

#define MB_DEFBUTTON3 0x00000200L

#define mb_defbutton4 0x00000300L

You can also use a parameter to indicate the appearance of the Messagebox icon.

#define MB_ICONHAND 0x00000010L

#define MB_ICONQUESTION 0x00000020L

#define MB_ICONEXCLAMATION 0x00000030L

#define MB_ICONASTERISK 0x00000040L

Some of them have an alias:

#define MB_ICONWARNING MB_ICONEXCLAMATION

#define MB_ICONERROR MB_ICONHAND

#define MB_ICONITION MB_ICONASTERISK

#define MB_ICONSTOP MB_ICONHAND

There is also a little other MB_ constant. You can query your header or find the In / Platform SDK / User Interface Services / Windowing / Dialog Boxes / Dialog Box Reference / Dialog Box Functions.

In this program, the Messagebox return value is 1. But if it returns to IDOK, it seems to be more appropriate. IDOK is defined in Winuser.h, the value is 1. The Messagebox function can also return iDyes, IDNO, IDCancel, IDAbout, IDRETRY, or IDignore.

This short Windows program is really the equivalence of K & R's "HelloWorld" program? Ok, you might think that MessageBox does not have the important formatted power of the Printf function in Hello World. But we will see how to write a MersSageBox that you can do the Printf similar in the next chapter. Compile, links, and run

When you are ready to compile hellomsg, you can choose Build Hellomsg.exe from the Build menu. Or press F1, or choose the build icon from the Build toolbar. (This icon appears in the build menu. If the build toolbar is not displayed, you can choose Customize from the Tools menu, and choose the Toolbars latter page. Select Build or Buildminibar.

Alternatively, you can choose Execute Hellomsg.exe from the build menu, or press Ctrl F5, or press the execute program icon from the build toolbar (look like a red exclamation mark). Then you can see if you want to build the dialog box.

Usually, during the compilation process. The compiler generates a .Obj (object) file from the C source file. During the link, the linker merges the .Obj file, and .lib (library) file to generate .exe file. You can see a list of these library files by selecting the setting setting from the Project page. Special, you may notice kernel32.lib, user32.lib and gdi32.lib. These are "important libraries" serving three main Windows subsystem services. They include the name of the dynamic link library and the reference information binding to the .exe file. Windows uses this information to interpret the program to be called in the Kernel32.dll, USER32.DLL, and GDI32.DLL dynamic link library.

In Visual C Developer Studio, you can compile and link with different configurations. By default, these are called Debug (debugging) and release, and the executable is stored in the subdirectory in this name. When configuring the Debug, the information that helps the debug program and the trace source code is added to the .exe file.

If you prefer to work in a command line, there is a .mak file for all examples in the included CD-ROM. (You can select the Build Tag page through the options option under the Tools menu. Here are the same checking mine, select it to tell the exelper studio to generate a Make file) You need to run the vcvars32.bat file in the bin directory in the develper studio to set the environment. variable. To execute the Make file from the command line, switch to the hellomsg directory, execute:

Nmake / f hellomsg.mak cfg = "Hellomsg _ win32 debug"

or

Nmake / f hellomsg.mak cfg = "Hellomsg _ win32 release"

You can run the following content from the command line .EXE file:

Debug / hellomsg

or

Release / HellomeSG

I made a change for the default debug setting of the project file of this book. In the Project Setting dialog, after the C / C page is selected, I define the identifier Unicode at the Pre-Processor Definition, and I have more narratives in the next chapter.

转载请注明原文地址:https://www.9cbs.com/read-26636.html

New Post(0)