The first article of "Vernacular windows programming" reads the world of messages in chapter 3 of Windows (I) What is a message

Source: Internet
Author: User
Tags define null old windows
Chapter 1 view the world of messages in chapter 3 of Windows (I) What is a message?

3.1 send messages to the "Drawing" program

3.2 process custom messages in VCL

3.2.1 custom message

3.2.2 prepare two windows

3.2.3 message sending

3.2.4 message recipient Processing

3.2.5 display form2 and form1 together

3.2.6 run

Conclusion 3.3

 

What is a message? It cannot be explained. If you want to ask what is a "Short Message", it is better to answer: After you press the "send" button on your phone, it can cause China Mobile or China Unicom to deduct your 1 hair or 1.5 hair. Or, when you take a train from Beijing to Fujian, you will receive a message from the people of all provinces on your cell phone about your passing. Greetings:

"HEBEI mobile welcomes you !" "Welcome to Jiangxi, and Jiangxi mobile will always serve you !"...... At last, I got my cell phone and got no power. As a result, I didn't receive any welcome from Fujian Mobile. What am I thinking? It is estimated that "hey, you are here !".

Of course, these are all junk information. Delete is. But what if I receive other information? I think of a joke. It is said that a guy is idle during the National Day holiday and wants to greet his boss in an abrupt whim, so he sent a text message:

"Happy holidays, boss !"

Soon, the boss sent a text message:

"Thank you, and I wish you happiness. By the way, the company is not on duty today. Can you stare at it ?"

 

Now you can refer to the term "message-driven. It's actually very easy, isn't it? The reason why the guy rushed to the company on holiday was the news that the boss came back to "Drive. Any standard windows program is "driven" by a variety of messages, and also sends a variety of messages to drive other programs, or drive themselves.

This example shows the upper-level logic of a standard windows program, that is, a cycle of message receipt and processing.

(The top layer of any windows program is a message loop)

Q:Some programmers who have already written many programs may question the saying that "any windows program is a message loop": "I have written many programs, but no messages are found? ". In fact, this is because when we use VB, Pb, Java, Delphi, or CB to write Windows programs, there are usually many message processing processes, are encapsulated by the Code provided behind these tools. Message Processing is mostly presented in a form called "Event. However, this does not mean that we do not need to understand the more essential content of windows. Otherwise, our topic will not be called "seeing through.

 

In theory, Dongdong is always not intuitive. Here is an example.

 

3.1 send messages to the "Drawing" program

Perform the following two steps:

 

Step 1: click the "Start" button on the taskbar and select "All Programs" or "programs"> "accessories"> "Drawing ". The old windows friend is shown below. This program is available since Windows is installed.

(Drawing program)

Make sure that:The title Text of the "Drawing" program you see must be shown in "untitled-drawing ". If not, click "file" in the main menu and select "new ".

 

Step 2: Open c ++ builder. Create a Windows application project.

 

2.1 put a tbutton in the default tform1. (If not, see the previous chapter ).

2.2 double-click button1 and write the following code (in bold) in the editing window ):


Void _ fastcall tform1: button1click (tobject * sender)

{

/* The free version is not available */

}

(Button1 onclick Event code)

Explanation:

 

A) null is a macro, that is, # define null (void * 0 ). It can be understood as: 0, or an empty address. (For more information about the address, see the pointer chapter in vernacular C ++ ).

B) hwnd is also a type definition alias, that is, typedef void * hwnd. That is, it is a non-type pointer. You can understand that it is just a number: Used to number each window in each program. We are also used to refer to this number as "handle ".

 

Auxiliary:This chapter guides you through how to view original macro or alias definitions.

 

B) findwindow is an API function provided by Windows (see the "Vernacular C ++" function section ). This function requires two parameters: "Class Name" and "title" of the window to be searched ". "Class Name" is not explained for the moment. Here we do not search by "Class Name", so I gave a null. The title is "untitled-drawing ". Note the two spaces. If it is a non-Chinese version of Windows. So I had to see what the title was. Findwindows means "finding windows ". As the name suggests, it will go to the specified window. Here we want it to find the number (handle) of the main window of the drawing program ).

 

Auxiliary:This chapter guides you through how to find API function descriptions, and allows you to configure a more handy Integrated C ++ Builder environment.

 

C) Does findwindows find the main window of the "Drawing" program we want? We use this sentence to judge:

If (hwnd! = NULL)

That is, if the number is not 0, it is found.

D) What to do later? This is the key to this program. We will send a message to the window we found. Sendmessage is also an API function. Similarly, as the name implies, it provides the function of "sending messages ". It requires four parameters:

The first parameter specifies "who sends a message", that is, the target window. Here we find the main window of the drawing program.

The second and fourth parameters are used to indicate the message to be sent ". Wm_close is also a macro. In fact, it must be an integer, which may be 101 or 777? The specific amount. If you use VB or Pb numbers, it may take some effort to check them. For example, if we use C ++, it is no longer necessary. Because Windows itself is written in C, we can simply use the macros provided by windows. But what does this macro mean? In Windows, "message" consists of three integers: The first integer is the message number, and the other two are the parameters attached to the message. "Wm_close" indicates the message that closes/closes a window. No parameter is required for this message, so we fill in 0 for the remaining two parameters, that is:

Sendmessage (hwnd, wm_close, 0, 0 )//To the hwnd window, send a wm_close message with the parameter 0.

E) Think about it. Is our program a bit like "China Mobile" now? People's drawing programs stay well on the screen, and they don't move, just take a rest, so we don't care to send it a message. What's more, China Mobile sends a "welcome" message, and we send it. You will understand later that this is a "rolling" message.

 

One program receives a forced "tumble" Message from another program. What will happen? There are two types: the first type is "inverse", that is, it disappears silently. Another type is "resolutely ignore.

The drawing program, an old employee of windows, is a typical inverse model. Speak nonsense. Save the project and code. Press F9! Make sure that your drawing is still running on the desktop. Then press the button1 of our program. You will find that the drawing program automatically exits.

 

Try:Double-click my computer on the desktop to open the my computer window. Modify the title of the window to be found in the previous Code as "My Computer ". Recompile, run, and click button1 ...... What is the result? Once, do I remember Windows 95? This is fine. At that time, the Windows "my computer" window was available. You can also disable it in this way. Now, I only hear "when ...... (What is this "when? "My Computer" is not satisfied with our attempt to send a "Short Message ?)

 

Auxiliary: The auxiliary textbook provides a "password viewer" based on the same principle ". Like many netizens, I used to apply for many free emails online. Then, I installed a Foxmail on the machine. All these mailboxes require passwords, but after I set them one by one during Foxmail, I forgot what these passwords are for one month. "*******" Is displayed in the Foxmail password column. What should I do? Or force a message "I want to see your text" to Foxmail, and then check whether Foxmail is "easy to accept" or "resolutely ignore.

 

3.2 process custom messages in VCL

 

Without consultation, it is impolite to send messages to another program (such as "Drawing.

Next we will send messages to the self-written window.

Borland C ++ Builder provides a set of program frameworks mainly used to seal windows Windows, called VCL (visual component library ). Of course, it also includes methods to process various common Windows messages. Therefore, in most cases, we do not have to worry about how to handle messages that all applications have to process. In fact, this process is quite boring, because it is the same. However, windows also allows us to customize messages and then send and process these messages in the program.

 

This section is available even more interesting, and to better understand the complete process of "message" from birth to death.

3.2.1 custom message

As mentioned above, the message requires three unsigned integers: the message number and two parameters of the message.

In order not to repeat the predefined messages in windows, Windows requires that custom messages start with a base number. This base number is also defined as a macro:Wm_user.

The two parameters of the message are defined by us. In fact, this is just a "Convention". That is to say, you must set the meaning of these two parameters between the "sender" and "receiver" of the message. Imagine how sweet it would be if you sent a "7521" number to your wife, which is "My wife and I love you ...... But without prior agreement, who can ensure that your wife will not be "deceiving my children? Therefore, it is important to ensure that this agreement is correct. Even if we integrate the message's "definer", "sender", and "receiver" as we do now, we have to be careful. How can we ensure no errors? Of course, macro definition is also used. Defining integers as literal macros is a good method.

 

Many Message Macros in Windows Start with "WM _". To indicate the difference, you can customize the message macros by starting with "cM.

 

Let's talk about what we want to do first? Suppose we want to send a message to the window. After receiving the message, the window can move the specified distance to the "Up, down, left, right" direction.

 

// Customize the message number. Based on wm_user, let's start by adding one day.

# Define cm_movewindow (wm_user + 1)

 

Next, let's make an agreement: the first parameter is used to specify the direction to move. To avoid errors, we define an enumeration type representation:

Enum tmovedir {mdleft, mdbottom, mdright, mdup}; // left, bottom, right, top

 

The second parameter indicates the distance to be moved.

 

That is to say, if a window receives such a message:

Cm_movewindow, mdleft, 2

Then, the window should move two pixels to the left.

 

After the conventions are met, you can start to write the program. Let's create a Windows application project in CB (remember to save the original project ).

 

3.2.2 prepare two windows

 

1. A main form is generated by default in the new project, and a label control: tlabel is placed on the form. Label1.

2. Select "file"-> "new"-> "form" in the main menu. Let's create another form. The new form is named form2 by default, and we use it to send messages. The original form (Main Window) is used to receive messages.

3. To ensure that form2 can be automatically created, click "project"> "options" in the main menu and find "forms ", make sure that both form1 and form2 appear in the list on the left. If not, use the <button on the interface to move it to the left.

(Make sure that both forms are in the list on the left)

Confirm to exit step 3 dialog box.

4. Change the positions and sizes of form1 and form2, and place four buttons on form2. Pay attention to their positions and order on the graph. The final result is.

(Form1 in the upper left and form2 in the lower right)

 

3.2.3 message sending

 

5. Click the form2 title, press F12, open unit2.cpp, press Ctrl + F6, or click the header file "unit2.h" in the edit box ". Enter the following bold code, which is our previous convention.

 

...

# Include <stdctrls. HPP>

# Include <forms. HPP>

//---------------------------------------------------------------------------

 

/* The free version is not available */

 

Class tform2: Public tform

{

...

(Add code in the unit2.h file)

6. switch back to unit2.cpp. Add the following bold line to the top of the Code to reference unit1.h. Because we need to use form1 in unit2.cpp, and form1 is defined in unit1.h.

//---------------------------------------------------------------------------

# Include <VCL. h>

# Pragma hdrstop

 

# Include "unit2.h"

# Include "unit1.h" // Add this line

//---------------------------------------------------------------------------

(In unit2.cpp, include unit1.h)

 

7. On form2, double-click button1 and enter the following bold code in the code editing window.

Void _ fastcall tform2: button1click (tobject * sender)

{

// Send a "Move Up" message, which must be moved up to 10 pixels:

/* The free version is not available */

}

(Onclick code of button1: Send messages that move up to 10 pixels)

 

8. Similar to Step 7, click button2, button3, and button4 to complete the following bold code input:

Void _ fastcall tform2: button2click (tobject * sender)

{

// Send the message "Move to the left", which requires 10 pixels to move to the left:

/* The free version is not available */

}

//---------------------------------------------------------------------------

Void _ fastcall tform2: button3click (tobject * sender)

{

// Send the message "Move to the right", which requires 10 pixels to the right:

/* The free version is not available */

}

//---------------------------------------------------------------------------

Void _ fastcall tform2: button4click (tobject * sender)

{

// Send a "Move Down" message, which requires 10 pixels to move down:

/* The free version is not available */

}

//----------------------------------------------------------------------------

(Complete the remaining three directions)

 

Now we have completed the code for sending the message. The following process receives the message.

From the code above, the message is sent to "form1-> handle". form1 is the form created by default when a new project is created, and handle is the window handle of the form. Therefore, the message receiver is form1.

Switch to unit1.cpp, press Ctrl + F6, and switch to unit1.h.

Auxiliary:The basic usage of the C ++ Builder integrated environment.

 

3.2.4 message recipient Processing

9. In unit1.h. Add reference to unit2.h:

# Include <stdctrls. HPP>

# Include <forms. HPP>

//---------------------------------------------------------------------------

# Include <unit2.h>

//---------------------------------------------------------------------------

 

10. There are dedicated methods in VCL to complete message response. In unit1.h, right-click and choose View explorer from the shortcut menu, or press Ctrl + Shift + E. The "class experts" window appears:

(Expert-like window. When this window appears, it may be "nested" in the code window)

 

11. For example, select tform1. Right-click. In the pop-up menu, select "new method ...". We will create a new member function for tform1.

(New method: Creates a member Method for a specified class)

12. In the displayed dialog box, make the following red circles:

/* The free version is not available */

(Add method: add method)

13. After you click OK to exit, CB will add a function in unit1.cpp. Add the following code to the bold Section.

Void tform1: cmmovewindow (tmessage & MSG)

{

// Todo: add your source code here

If (msg. MSG! = Cm_movewindow)

Return;

// Lparam is the message's 2nd parameters, which specify the distance to be moved:

Int Len = MSG. lparam;
 

// Wparam is the 1st parameter of the message, which is used to indicate the direction of movement:

Switch (msg. wparam)

{

Case mdleft:

Left = left-len;

Label1-> caption = "move to left ";

Break;

Case mdright:

Left = left + Len;

Label1-> caption = "shift to the right ";

Break;

Case mdup:

Top = Top-len;

Label1-> caption = "Move Up ";

Break;

Case mdbottom:

Top = Top + Len;

Label1-> caption = "Move Down ";

Break;

}

}

(Code for receiving and processing messages)

3.2.5 display form2 and form1 together

 

14. Compile and run the program now. You will find that only form1 is visible, but form2is not found. This is because only the main window generated by default, that is, form1, is automatically displayed. Therefore, we hope that form2is automatically displayed when form1 is displayed. Click form1. then press F11 to find the Control Properties window and switch to the event page:

(Locate the onshow event of form1)

For example, find onshow, and double-click its right blank.

15. In the displayed code editing window, add the following rough line of code:

Void _ fastcall tform1: formshow (tobject * sender)

{

/* The free version is not available */

}

//---------------------------------------------------------------------------

 

3.2.6 run

 

16. Haven't the Code been saved yet? This can be dangerous. Save (CTRL + shift S ). Then compile and run according to F9. Press the four buttons of form2 to check whether the form1 is correct. The following is a program running on my machine:

(Final Run)

 

We have completed three steps: defining, sending, and processing messages. In fact, to complete the same function, you can simply process it without using custom messages (Auxiliary:In this example, the "non-custom message" version has the same function ). However, through message processing, we can make the program more flexible in design. The focus of this chapter is to establish the concept of "message processing.

In this example, the cm_movewindow message is sent to form1 by pressing the four buttons in form2. Remember: cm_movewindow is only an integer. The program will not automatically move the form1 up, down, and left and right because the macro contains the text of "Move window! To complete the relevant processing, we need to rely entirely on our code. A message has two parameters, which are also integers. the specific meaning of these parameters depends on the actual code to process them.

 

Conclusion 3.3

This chapter has only two sections.

In section 1, we try to send a Windows scheduled message wm_close to an external program: "Drawing. After the message is received in the main window of the "Drawing" program, a predefined action is taken: exit. However, other programs may make judgments and find that the message comes from external programs without processing it.

In section 2, we send custom messages between two different windows in the same program. As a result, we know that the original WINDOWS Message consists of three parts: the message number and two message parameters.

This is the main point of this chapter. If you do not know that there is a "message" in the Windows world, you should understand it now. However, if you just copied the Code while reading the course, it's time to go back and review it again.

See the next chapter.

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.