Step-by-Step windows programming (1)

Source: Internet
Author: User

Chapter 1 course on the first day-Learn From Practice

This chapter briefly introduces the windows program design and provides two short programs as an example, so that the reader can start running the program as soon as possible.
This chapter is just an introduction to Windows 9x programming technology, but it is the foundation of the core content of this book.

1.1 A simplified C ++ windows program

Whenever I start writing a new computer program or learning a new language, I am always eager to get some experience from others as soon as possible. I think you may also be like me, so I wrote a C ++ windows applet. In program list 1.1, the source code is its source code. You only need to spend a few minutes to read and run the program.

    • MessageBox(0,"He who is ruled by mem lives in sorrow.",  "He who rules mem lives in confusion.",  MB_OK|MB_ICONINFORMATION);return 0;
    Program list 1.1 your first Windows program
    // Program LaoTzu.cpp#include&ltwindows.h>// Some programs to surpress unneeded warnings#pragma warning (disable:4068)#pragma argsusedint WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInstance,  LPSTR lpszCmdParam,int nCmdShow){
    }
    Program list 1.1 is a fully compliant C/C ++ windows application that complies with all the conventions and rules made to Windows programmers. The only function of these simple program lines is to display a message box, which contains a famous saying of the taoist philosopher (Lao Tzu) before 2500. To run laotzu. CPP, the only thing you need to do is to open your development environment, enter the code in the program list 1.1 and compile it. At this point, you don't have to really understand the meaning of the code, and why it can work. For now, you just need to move forward based on my plan. Laotzu. cpp is not used to teach you how Windows programs work, but to show you how simple a Windows program can be. If you are using Microsoft Visual C ++ software package, you can run your program using the following methods: first enter the program and save it in laotzu. CPP, then create a new project from the File menu, select the project type as Win32 application, enter your project file name, and then set laotzu. CPP is inserted into the project and compiled and run it. 1.2 core of Lao Tuz Program
    The core of the Lao Tuz program is a MessageBox function, which seems very simple, but in fact it is a bit tortuous, and you must master it.
      Syntax

      MessageBox Functions

      Int MessageBox (hwnd, lpcstr, lpcstr, uint );

      The MessageBox function is used to create a window with four parameters:

    The first parameter hwnd is the main window handle of the program. In program list 1.1, this parameter is set to 0 because the laotzu program does not have the main window.
    The second parameter, lpcstr, is a long pointer to a String constant. This string is the body you want to display in the message box and is the main part of the message box.
    The third parameter, lpcstr, is also a long pointer to a String constant. This string serves as the title of the message box.
    The fourth uint includes one or more of the following marks:

      // Buttons // ----------------------- # definemb_ OK 0x0000 includes the okay button # definemb_okcancel 0x0001 includes the okay cancel button # definemb_abortretryignore 0x0002 abort, retry, ignore # definemb_yesnocancel 0x0003 Yes No cancel button # definemb_yesno 0x0004 Yes No button # definemb_retrycancel 0x0005 retry, cancel button
      // Icons # definemb_iconhand 0x0010 stop icon # define 0x0020 problem icon # define 0x0030 exclamation mark icon # define 0x0040 asterisk icon # definemb_iconinformation icon # definemb_iconstop mb_iconhand
      // Scope and focus issues # definemb_applmodal 0x0000 # definemb_systemodal 0x0001 # definemb_taskmodal 0x0002
      // Default button specificationmb_defbutton1mb_defbutton2mb_defbutton3
      // Win32mb_setforegroundmb_default_reply top_only

      If you want to use multiple tags at the same time, use the "or" operator to connect them together, as in the example in program listing 1.1. If you want to see how they work, try to change mb_iconinformation to mb_iconinformation.
      The MessageBox function returns an integer to indicate which button is selected when MessageBox appears on the screen. For example, if you press the OK button, the function returns idok. If you press the cancel button, the function returns idcancel. The possible return values of the function are listed below:

        Idabort: select the abort button; idcancel: select the cancel button; idignore: select the ignore button; IDNO: select the no button; idretry: select the Retry button; idyes: select the Yes button;

        The following is an example of using this function:
        MessageBox (0, "the Astrolabe of the mysteries of God is love ",
        "Jalal-Uddin Rumi said:", mb_ OK | mb_iconexclamation );
        1.3 voices

        The windows program presents users with a well-crafted user interface, instead of simply outputting a row of data. In terms of concept, the objective of a Windows program is to present information to users by drawing, drawing, and sound.
        There are two programs that illustrate how to make your computer sound. If you have speaker. DRV files or multimedia functions, you can use the first version of the program (program list 1.2 ). The second version (program list 1.3) can run in any system as long as there is a built-in speaker. You 'd better run both programs, because you should try to understand the messagebeep process as much as possible.

          Program list 1.2 chimes. cpp Program
        // Program CHIMES.CPP#defineSTRICT#include&ltwindows.h>#include&ltmmsystem.h>int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInstance,  LPSTR lpszCmdParam,int nCmdShow){  sndPlaySound("CHIMES.WAV",SND_SYNC);  return 0;}
        The chimes program calls a function sndplaysound, which is part of the Windows multimedia category. This function is used to play wav files. Note the # define strict directive in the second line to define it to help build stronger programs. The strict type check gives a new meaning to the special integer values (such as hinstance) based on windows, which can help programmers avoid careless errors. The fourth line contains the mmsystem. h file. The first two letters mm at the beginning of the file represent multimedia (Multimedia). mmsystem. H is a standard part of the Windows programming environment. The sndplaysound function is used to play wav files. These files use WAV as the extension, which contains the digital information of analog sound. Wav files can save voice, music, and other sounds.
          SyntaxSndplaysound FunctionSndplaysound (filename, snd_format );
          The sndplaysound function is an advanced multimedia command that can be used to play wav files in a simple way. Its first parameter filename is the wav file name to be played. The second snd_format parameter represents a simple command sent to windows, which can be one of the following constants:
            Snd_sync 0x0000 synchronous playback (default) snd_async 0x0001 asynchronous playback snd_nodefault 0x0002 no default sound snd_memory 0x0004 the first parameter is the memory file snd_loop 0x0008 loop playback until the next call sndplaysoundsnd_nostop 0x0010 does not stop the current the following is another example of playing any sound: sndplaysound ("chord.wav", snd_loop); sndplaysound ("guitar.wav", snd_sync );
            In program list 1.3, the beeper program does not require multimedia functions, but can also play multimedia files, as long as your system supports this function.
              Program list 1.3 beeper. cpp programs can generate sound in any PC System// Program beeper. CPP # define strict # include & ltwindows. h> int winapi winmain (hinstance hinst, hinstance hprevinstance, lpstr lpsz1_param, int ncmdshow) {messagebeep (-1); Return 0 ;}
              The beeper. cpp program uses a built-in speaker in your system to generate a transient sound. This program calls the messagebeep function to generate sound, which depends on your system. If you do not have multimedia sound for the system, you should send-1 to the messagebeep function. If yes, you can use iconexclamation as the parameter. SyntaxMessagebeep FunctionMessagebeep (0); messagebeep is one of the simplest function calls in all windows APIs. It has only one parameter and is usually set to-1. By default, the messagebeep function generates a simple "Split" sound, which sounds like what you often hear in DOS programs. You can also use this function to generate other sounds if you have multimedia functions or systems equipped with speaker. DRV. To achieve this, you can send one of the following constants to the messagebeep function:
                MB_ICONSTERISKMB_ICONEXCLAMATIONMB_ICONHANDMB_ICONQUESTIONMB_OK
                You can select any constant above to generate the corresponding sound-you only need to open the control panel and select the sound icon. If you choose to run the beeper program again, you will hear a completely different sound (but remember that this function is available only after the system is equipped with the multimedia sound function ). The following is another example of using this function. MessageBox (mb_ OK); 1.4 What is the winmain function?


                In DoS or UNIX environments, the traditional C/C ++ program uses the main () function as the program entry point, while the windows program uses the winmain function as the program entry point.

                Most of these two routines are the same in terms of functionality. That is to say, both of them are the first called program in a specific program.

                  SyntaxWinmain FunctionThe following describes winmain in Microsoft: int winapi winmain (hinstance hinst, hinstance hprevinstance, lpstr lpsz1_param, int ncmdshow)

                  The winmain function has four parameters:
                  The first parameter of 'distinct' is a unique value or handle. It is related to the current program. Now you can think of hinstance as an integer. But you will see it later

                  The hinstance type is more complex than the previous one.
                  The second parameter is only important for 16-bit windows. It is the unique handle that connects to another instance of the Program (if it is stored in another instance ).

                  For example, if two copies of clock. EXE are started, the second copy of the program uses the hinstance of the first copy as the second parameter. If the program does not exist before

                  Instance, this parameter is set to null. In Windows 95 and Windows NT, these two parameters are meaningless, because each program is in its own 4 GB space.

                  . In other words, every process runs in a virtual world, believing that it is the only resident of the world. In Windows 95, applications do not feel

                  Other programs run in the system, even if the program is the second instance of the same executable file. In this way

                  The two parameters are always set to null. Microsoft still sets this parameter on these platforms only to be compatible with Windows 3.1.
                  The first parameter of arguments is a string that contains any parameters passed to the program. The type of this parameter lpstr is a reference in windows, indicating a string pointing

                  .
                  The first parameter of the program indicates whether the program is running normally or maximized or minimized.
                  The winmain function returns an integer, But windows never checks the return value. That is to say, when the number of winmain functions ends, your application will also

                  It's over. The specified return value is mainly used to help debugging, or to make the program easier to read. Therefore, whether the return value of winmain is true or false is irrelevant.

                  Actual Meaning.

                  1.5 Windows API

                  To understand what Windows APIs are, first understand what Windows is. So what is windows?
                  Many people think that DOS and Windows 3.x are not real operating systems, because they do not provide real preemptible multi-task or any robust protection methods.

                  Prevents errors caused by diseased programs. Windows NT provides all of these features. To a large extent, this is also true for Windows 9X.
                  Simply put, the word Windows is quite general and has no special meaning. In fact, Windows NT is an operating system, and Windows 9x looks like

                  Operating systems, while Windows 3.x is the operating environment, which are linked by a common API (or Application Programming Interface.
                  An API is a set of routines that can be used to control the entire computer, or to control a specific function of the computer, such as a modem, display card, or mouse. For example, you have

                  A group of three routines are used to provide interfaces between the program and the mouse. I call these three routines inintializemouse, setnouseposition, and getmouseposition.
                  These three routines can represent a Simple API between the program and the mouse, allowing you to start the mouse, place the mouse at a specified position, and get to the current position of the mouse cursor. These cons

                  A single function is sufficient to form an interface between your application and a hardware component (such as a mouse.
                  Windows APIs have nothing but a similar set of routines, which form interfaces for applications and the entire computer. Windows is actually just

                  API, but it is also a very functional API.

                  1.6 difficult to answer

                   

                  • What is windows. h?

                  Windows. H is a file or a series of files. It contains many important constants, functions, structures, and macros. Windows programs written in C or C ++

                  This information is required. To some extent, it is the most accurate definition available in the Windows programming environment.

                  • What is the entry point of a program?

                  The entry point of the program refers to the place where the program starts. When you click an icon and start a program, Windows loads the program to the memory and calls its

                  Winmain function. This is the same as starting a program in DOS at any time. The biggest difference is that winmain requires more parameters than the main () function.

                  • Is it necessary to read the windows. h complex from start to end?

                  Reading all windows. h and related files may be challenging. However, if you are interested, you may spend some time reading the windows. H files.

                  Different modules provide a good solution.

                  • What does the winexec function do?

                  The winexec function is used to start a Windows or DoS application.

                  • What is Gui?

                  GUI is a graphical user interface. Generally, these interfaces support and are familiar with graphical functions, such as Windows, scroll bars, buttons, and dialog boxes. Most guis

                  It is designed to display a relatively unified interface in front of the user no matter which application is running.

                  • What is PIF?

                  PIF is used to store information, which is used when Windows runs a DOS session. For example, you can use PIF to specify a DoS

                  Whether a session uses full screen or window, or specify the size of the storage space that Windows should allocate to a DoS session.

                  Today in history:

                  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.