Step-by-Step windows programming (II)

Source: Internet
Author: User
Course of the next day-create a project and create a window

In this chapter, you have the opportunity to run another short program, a complete windows program. You can learn how to create a Windows project. If it is difficult for you to correctly compile and run this simple windows program, this chapter solves these problems. In this chapter, you will learn how to do the following:

  • Create a project in Visual C ++
  • Use makefile
  • Use the module definition file (DEF file)
  • A traditional window is displayed, which can be scaled into icons and maximized.

The goal of this chapter is to avoid a lot of configuration problems and focus on writing windows code.

2.1 Module definition files (DEF) and makefile files

Standard Windows programs generally consist of at least three files:

    1. The main module or multiple modules of the program. The extension is. cpp.
    2. A module-defination file with the extension of. Def.
    3. A project or makefile with the extension. Mak.

In addition, C/C ++ programmers also use resource files in large quantities. Its source file extension is. RC. I am not going to discuss this question yet. Of course, many programs also include header files ).
Almost all windows programmers, especially the designers of Windows 3.x, use the module definition file (DEF file). What it does is what its name means, it defines the features of a main program module, including:

  • File Name
  • Its purpose or simple description of its main features
  • Its stack and heap size (WIN 32 option)
  • Several statements used to define the program's memory processing method (this is not required in Win 32)

However, you can create a Windows application before defining the module definition file. In fact, in Windows 32 applications, this practice has become a habit. Of course, the lack of module definition file programs will give a warning, but this is generally not serious. After a warning is issued, the compiler uses the default value to replace the value that should appear in the def file. In most cases, it is feasible for the compiler to use these default values.
Makefile helps you compile independent source files in a project into an executable file. In the Microsoft IDE environment, all these files are created automatically. I will not discuss the makefile syntax here.

2.2 Use Microsoft tools to create project and makefile files

Users who use Microsoft tools can easily create makefile files in IDE, but the file generated by using this method is long and complex.
To create a MAKEFILE file in Microsoft IDE, first enter Microsoft IDE and select file | new. In the new dialog box, select project. In the new project dialog box, follow these steps:

    1. Enter your project file name in the Project Name box.
    2. Set project type to Win32 application.
    3. Click OK.

Then you can select project | add to project | files to insert the. cpp (or. RC file, if any) file you have edited in advance.
Next, you can choose build | rebuild all to compile your program, and then press Ctrl + F5 to run it.

2.3 create dialog box

So far, you have seen several different Windows programs, but you have not yet seen a real window. There are also three steps for setting up a window. As long as you write such a program once, it can be used repeatedly in many different programs in the future, and almost no modification is required.
Next you can see a 50-Line Short Program (program list 2.1). I use it to create a real window that can complete the actions you want to accomplish in a window. At this stage, you do not need to understand it. As long as you know how it works, you do not have to worry about any details. First, create and run the program according to program list 2.1, and you will see all the things that the Code has done, that is, what a real windows program has done. That is to say, you can maximize, minimize, or change the size of a window. These are exactly the features of any professional windows program.
Program list 2.1 is a makewin program, which creates a traditional window with a title, boundary, and system menu.

    Program list 2.1 makewin Program// Program makewin. CPP # define strict # include & ltwindows. h> # include & ltwindowsx. h> # include & ltstring. h> char name [] = "makewin"; lresult callback wndproc (hwnd, uint, wparam, lparam); # pragma warning (Disable: 4068) # pragma argsused int winapi winmain (hinstance hinst, hinstance hprevinstance, lpstr lpsz1_param, int ncmdshow) {hwnd; MSG; wndclass; memset (& wndclass, 0, sizeof (wndclass); wndclass. style = cs_hredraw | cs_vredraw; wndclass. lpfnwndproc = wndproc; wndclass. hinstance = hinst; wndclass. hbrbackground = (hbrush) (color_window + 1); wndclass. lpszclassname = Name; registerclass (& wndclass); hwnd = createwindow (name, name, ws_overlappedwindow, 600,400, null, null, hinst, null); showwindow (hwnd, ncmdshow ); updatewindow (hwnd); While (getmessage (& MSG, null, 0, 0) {translatemessage (& MSG); dispatchmessage (& MSG);} return MSG. wparam;} lresult callback wndproc (hwnd, uint message, wparam, lparam) {If (Message = wm_destroy) {postquitmessage (0); Return 0 ;} return defwindowproc (hwnd, message, wparam, lparam);} The makewin program contains two main parts: 1. winmain function 2. the winmain function of wndproc can be divided into three parts:
  • The first part (Lines 18 to 24) is used to indicate where the window is registered:
    18: memset(&WndClass,0,sizeof(WNDCLASS));19: WndClass.style = CS_HREDRAW|CS_VREDRAW;20: WndClass.lpfnWndProc = WndProc;21: WndClass.hInstance = hInst;22: WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);23: WndClass.lpszClassName = Name;24: RegisterClass(&WndClass);
  • The Register process tells windows about the features of a window class. However, at present, you still do not need to worry about the reason for doing so and the purpose of each step. You only need to know that this is the first thing to do in a typical winmain process. In the next chapter, registering a complex action is separated into independent processes.

    • The second part (lines 26 to 30) is the creation window:

      26: hwnd = CreateWindow(Name,Name,WS_OVERLAPPEDWINDOW,27:10,10,600,400,NULL,NULL,hInst,NULL);28:29: ShowWindow(hwnd,nCmdShow);30: UpdateWindow(hwnd);

    Create a window in two steps. The first step is to call the createwindow function (rows 26th and 27), and the second step is the showwindow function (row 29th) and updatewindow function (row 30th ). The process of creating a window is similar to the process of registering a window. It is usually handled as a separate process to break down a complicated problem and build a well-structured and robust program. But now I put these things together to make you feel what politicians often say "big scenes ".

    • The third part of the winmain process is the message loop (32 to 36 rows ):

      32: while(GetMessage(&Msg,NULL,0,0))33: {34:TranslateMessage(&Msg);35:DispatchMessage(&Msg);              36: }

    When a user moves the mouse or key, the message is sent to the message loop, which is equivalent to the "driver's seat" of a Windows program, which is the command center, A loop maintains the repetitive experience of a program.
    So far, if you haven't understood anything, you don't have to worry about it. The last section of this chapter aims to give you an overview of the classic windows program. In the next chapter, you will understand the details you want to know.

      Now you just need to remember this clear conclusion: a Windows program process has three basic parts: 1. Registration window 2. Creation window 3. Entering the message loop

      This is as simple as numbers 1, 2, and 3!
      Another key part of the makewin program is the wndproc process, which responds to messages received by the program.

        41: LRESULT CALLBACK WndProc(HWND hwnd,UINT Message,42:WPARAM wParam,LPARAM lParam)  43: { 44:    if(Message==WM_DESTROY)45:    {46:   PostQuitMessage(0);47:   return 0;48:    }49:    return DefWindowProc(hwnd,Message,wParam,lParam);50: } 

        The makewin program only responds directly to the wm_destroy message (line 1). All other messages are forwarded to defwindowproc for processing. In the next chapter, you will see that the defwindowproc process only processes the default behaviors related to the window. That is to say, when you want to maximize a window or minimize a window, the defwindowproc process will process the messages you send and know how to handle them.
        However, defwindowproc does not process the wm_destroy message, which is a programmer's responsibility. When a message arrives, only postquitmessage (46 rows) is called ). In this way, you can exit winproc and the return value is 0 (47 rows ).
        I know that when reading this chapter, some readers are not clear about the importance of messages in Windows programming. If you are a newbie, don't worry if you are not familiar with this subject. Now, simply accept the fact that a Windows program responds to messages rather than being interrupted. This is different from the habit of many programmers. You will find that message-oriented operating systems are very simple and well-designed.
        In the next chapter, you will see how to use macros to process standard Windows messages, so you can place the message response function outside wndproc. This is like moving the create and register functions Out Of The winmain process. In this way, Windows programmers can establish well-structured programs for easy maintenance and debugging. Here I put these processes back into winmain and wndproc functions so that you can see a full picture of a typical Windows program.

        So far, you only need to know about the makewin program. Let's summarize:

        · The program has two main processes: winmain and wndproc.
        · The winmain process consists of three parts: first registering the window, then creating a window, and finally sending messages to the window through a loop.
        · Any message sent to the window is sent through wndproc. The window process can directly process messages or pass messages to the defwindowproc function, which is the default message processor. In fact, some programs first process the message themselves, and then pass it to defwindowproc for further processing.

        This is all you need to know about a Windows program. The key is to master the entire process of a typical Windows application program and have a clear concept in your mind so that you can understand the details in the next chapter.

        Related Article

        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.