Windows Programming 1 starts

Source: Internet
Author: User

Self-taught "Windows Programming" (Fifth Edition edition), the content of the study notes.

1 Basics

Windows is a preemptive, multi-tasking, multi-threaded graphics operating system. Windows has a graphical user interface (graphical user Interface, GUI), sometimes referred to as a "virtual interface" or "graphical interface." All GUIs display graphics using a bitmap (bitmap) video display. Graphics improve the utilization of screen resources, provide a convenient environment for visually transmitting information, and use WYSIWYG (what is WYSIWYG) to display graphics and formatted text prepared for printing files.

Programs that run in a Windows environment can share routines stored in a so-called dynamic-link library (dynamic-link Libraries, DLL) file. Windows provides a mechanism for subroutines the programs and examples in the dynamic-link library at run time. In fact, Windows itself is basically a set of dynamic link libraries.

Windows is a graphical interface that Windows programs can use to make the most of graphics and formatted text on a video display or printer. The graphical interface not only looks cooler and more compelling, but also communicates information to users at a higher level.

Programs written for Windows environments do not need direct access to graphics display hardware such as screens or printers. Windows itself comes with a graphical programming language, called the Graphics Device interface (graphical device Interface, GDI), for easy display of graphics and formatted text. Windows displays hardware virtualization. This allows programs written for the Windows environment to run on any video card or printer, as long as the appropriate Windows device drivers are available. The application itself does not need to know what type of device the system is equipped with.

The central idea of how Windows works is the concept of "dynamic linking." Windows itself has a large set of functions that the application invokes to implement its user interface and display text and graphics on the screen. These functions are implemented in a dynamic-link library. The names of these files are prefixed with a suffix. DLL, or sometimes with a suffix. Exe. WIN10 systems, these files are usually placed under the C:\Windows\System32 subdirectory.

In Windows programs, calling Windows functions is no different than calling library functions (such as strlen) in C. The main difference is that the machine code of the C library function is directly linked to your program code, while the Windows function is placed in a DLL outside of your program.

When the Windows program runs, it passes through a process called "dynamic linking" with the Windows interface. The exe file for each Windows contains the various dynamic-link libraries it uses and the reference address (reference) of the functions in the library. When a Windows program is loaded into memory, the function calls in the program are parsed into a pointer to the DLL function entry, and the called functions are loaded into memory.

When linking Windows programs to generate execution files, be sure to link to the special "import library" provided by your programming environment. These import libraries contain the names and reference information of the dynamic-link libraries that are encountered by all Windows function calls. The linker uses this information to build tables in an EXE file that Windows relies on to parse Windows function calls when the program is loaded.

2 Configuring Visual Studio

Get the Windows API documentation for visual Studio 2017, which is MSDN, for specific reference http://blog.csdn.net/qq_33369475/article/details/74931006

3 First Windows program
/*----------------------------------------------------------HELLOMSG.C--Displays "Hello, win10!" in a message box  ----------------------------------------------------------*/#include <windows.h>int WINAPI WinMain ( HInstance hinstance,   hinstance hprevinstance,   PSTR szcmdline,   int icmdshow) {MessageBox (NULL, TEXT (" Hello, win10! "), TEXT (" Hellomsg "), 0); return 0;}

3.1 Header Files

Windows. H is one of the most important include files, which includes several other Windows files, some of which contain other header files. The following are some of the most essential and basic header files:

    • Windef. H Basic Data type definition
    • WINNT. H Unicode-supported type definitions
    • Winbase. H kernel functions
    • Winuser. H User Interface functions
    • WinGDI. H Graphics Device Interface function

3.2 Program Entry

Just as main is the entrance to the C program, the entrance to the Windows program is WinMain. Move the cursor to WinMain and press F1 to open Help.

  WinMain entry point--the user-provided entry point for a graphical windows-based application.

Look at the syntax:

int CALLBACK WinMain (  _in_ hinstance hinstance,  _in_ hinstance hprevinstance,  _in_ LPSTR lpcmdline     ,  _in_ int       ncmdshow);

  Callback macros and WINAPI are essentially the same, defined in WINDEF.H as follows:

#define CALLBACK    __stdcall#define WINAPI      __stdcall

This is a function calling convention that shows how to generate machine code that places function call parameters on the stack. Most Windows function calls are defined as WINAPI.

_IN_ macro tells us that this is an input parameter, and also _out_, which represents the output parameter. The macros are not involved in compiling and computing (empty macros), but they are a hint to the user of the API.

The definition details of the parameter type do not scrutiny first, interested can alt+f12 browse.

The first argument is called an instance handle. In a Windows program, a handle is a numeric value that the program uses to identify something. For example, in this program, the handle uniquely identifies the program.

Earlier versions of Windows required hPrevInstance (for reasons P15) that were no longer needed in 32-bit Windows. So the second argument is always null.

The third parameter type changes to PSTR,LPSTR and PSTR are declared as pointers to strings in WINNT.H. LP is a product of 16 bits. This parameter is the command line used to run the program. Some Windows programs use it to load files into memory at startup.

The fourth parameter is used to indicate how the program initially displays: or normal display, or maximized to full screen, or minimized on the taskbar.

In the name of unification, most Windows programs use the so-called "Hungarian Notation" (Hungarian Notation) in variable naming. In this system, there is a short prefix in front of the variable name to indicate the data type of the change amount. Here, I means that Int,sz represents "string ending in 0" (String Terminated with a Zero), considering the nature, so changed the name of the last two parameters.

3.3 MessageBox function

The old way, move into the cursor, press F1.

  MessageBox function--displays A modal dialog box that contains a system icon, a set of buttons, and a brief application-sp Ecific message, such as status or error information. The message box returns an integer value that indicates which button the user clicked.

int WINAPI MessageBox (  _in_opt_ hwnd    hwnd,  _in_opt_ lpctstr lptext,  _in_opt_ lpctstr lpcaption,  _ In_     UINT    utype);

_in_opt_ indicates that the parameter is optional, that is, it can be null.

The first argument is typically a window handle.

The second parameter is the text string that will appear in the message box.

The third parameter is the text string that will be displayed on the title bar. These text strings are packaged in the text macro code. In general, it is not necessary to package all the strings into text, because it is so much easier to convert the program to Unicode.

The fourth parameter is a combination of some constants that preceded the prefix mb_. Winuser. These related constants are defined in H. Mainly used to control icons, buttons and other styles. You can also look at it in MSDN.

The return value is related to the button, which means that different values are returned when the different buttons are pressed.

Windows Programming 1 starts

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.