18. Windows API graphical user interface (2)

Source: Internet
Author: User

I. Basic Concepts

1. Message-driven Windows systems. When a user operates the input device and causes hardware interruption, the system kernel receives the hardware interruption, and the hardware becomes a message after complicated calculation and processing in the system. The message is sent to the specified window by the system.

All operations on the user interface will produce messages, including mouse clicks, buttons, menu selection, window creation, window movement, and so on.

When creating a window, you must specify the window class to which the window belongs. Each window class corresponds to a message processing function. Therefore, each window has its own message processing function. The message processing function is a call back function called by the system.

ApplicationProgramA window class is registered with the system, and a window is created. After a message is generated, the system calls the message processing function corresponding to the window when the window message processing function is required for processing.

Not only user input can produce messages. Some random events also generate messages, such as system shutdown, peripheral insertion, and timer arrival.

2. Common Control types

◇ Text box (edit, rich edit, etc );

◇ Buttons (buttons, including check boxes, group boxes, push buttons, and radio buttons );

◇ Drop-down menu (ComboBox );

◇ List box );

◇ Group list (List View );

◇ Pager );

◇ Progress bar (process bar );

◇ Property sheet );

◇ Workbar (toolbar );

◇ Tree View)

Controls are actually a special window. Each control has a fixed style and message processing process. The system processes this attribute in a fixed pattern and registers it as a window class in advance.

These controls do not need to be re-registered during programming, nor need to define their message processing functions, interface styles, window sizes, etc. They can be directly used.

Before using controls, it is best to call initcommoncontrols to ensure that the dynamic link library required to call controls is loaded. [1, p258]

3. Resources are some data that may be used by applications during operation. Resources include icons, bitmaps, and menus. During program design, configure Resources in the resource script (. Rc). During program construction, the resource compiler will compile the resources as resource files (.Res), and then the linker links the resource to the executable file.

4A dialog box is a temporary window used to display information and obtain user input. A dialog box is a special window. You can use resources to create a dialog box. The control displayed in the dialog box and dialog box can be configured in the resource script, but the window cannot. The dialog box has a corresponding window. After the dialog box is processed, the window with a dialog box cannot be operated before it is closed.

Ii. Window

1. Common window messages

2. defwindowproc

Defwindowproc is an API function.

The window receives many messages, but these message processing functions do not need to be processed. Some messages may be processed directly by the system. Defwindowproc is used to process functions that are not completed or do not need to be processed.

In fact, the messages that many programmers do not pay attention to are handled by defwindowproc. If a message is not handed over to defwindowproc in the message processing function, there is almost no response to the window action, including moving the window, closing the window, and many other window styles cannot be displayed.

Generally, unless the programmer determines that a message does not need to be processed by defwindowproc, it is best to submit the unneeded message to the defwindowproc function for processing.

3. Message Loop

Getmessage obtains a message from the message queue of the thread. After the translatemessage function converts a message, dispatchmessage sends the message to the message processing function, which is usually used to allocate the message obtained by the getmessage function.

In Windows, you can create a message queue for each thread and use the getmessage function to obtain messages from the message queue. You can also call the API function to add messages to the message queue. The difference between postmessage and sendmessage is that postmessage only returns the message immediately after it is placed in the thread's message queue, while sendmessage directly delivers the message to the window message processing function. The postquitmessage function places the wm_quit message in the message queue. After obtaining the wm_quit message, the getmessage function returns 0 and exits the message loop.

Note that getmessage only obtains the messages in the message pool of the thread in which it is located. When developing a multi-threaded graphical user interface program, you can create all windows in the same thread. It is best to create subwindows in the message processing function of the main window, this ensures that all messages are concentrated in the same thread. If you need to operate the interface in other threads, you can directly use functions such as postthreadmessage and sendmessage to send custom messages to the window, then, perform window operations when processing these custom window messages in the window message processing function.

4. Compile resources and their files [1, p269]

►For more information about windows, see [1 ~ 3] and other seriesArticle.

Windows [4]

Menus [5]

Dialog Boxes [6]

[1] proficient in Windows API functions, interfaces, and programming instances

[2]Http://blog.163.com/zhoumhan_0351/blog/static/3995422720103129131208/

[3]Http://blog.163.com/zhoumhan_0351/blog/static/3995422720103401415721/

[4]Http://msdn.microsoft.com/en-us/library/ms632595%28VS.85%29.aspx

[5]Http://msdn.microsoft.com/en-us/library/ms647553%28VS.85%29.aspx

[6]Http://msdn.microsoft.com/en-us/library/ms632588%28VS.85%29.aspx

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.