Global Information Lookup Global Information

Message loop in Microsoft Windows information


The message loop is an obligatory section of code in every program that uses a graphical user interface under Microsoft Windows.[1] Windows programs that have a GUI are event-driven. Windows maintains an individual message queue for each thread that has created a window. Usually only the first thread creates windows. Windows places messages into that queue whenever mouse activity occurs on that thread's window, whenever keyboard activity occurs while that window has focus, and at other times. A process can also add messages to its own queue. To accept user input, and for other reasons, each thread with a window must continuously retrieve messages from its queue, and act on them. A programmer makes the process do that by writing a loop that calls GetMessage (which blocks for a message and retrieves it), and then calls DispatchMessage (which dispatches the message), and repeats indefinitely. This is the message loop. There usually is a message loop in the main program, which runs on the main thread, and additional message loop in each created modal dialog. Messages for every window of the process pass through its message queue, and are handled by its message loop. A message loop is one kind of event loop.

A basic message loop appears as follows:

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    MSG msg;
    BOOL bRet;

    while (1)
    {
        bRet = GetMessage(&msg, NULL, 0, 0);

        if (bRet == -1)  // If bRet is -1, GetMessage has failed.
        {
            // Handle or log the error; possibly exit.
            // ...
        }
        else if (bRet != 0)  // If bRet is not 0 or -1, the message must be processed.
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else  // If bRet is 0, the message loop should exit.
        {
            break;
        }
    }
    return msg.wParam;
}

It is conventional for the event loop to call TranslateMessage on each message which can translate virtual keystrokes into strings. Calling TranslateMessage is not technically required, but problems can result if it is not called. The message loop must call DispatchMessage.

The message loop does not directly act on the messages that it handles. It dispatches them by calling DispatchMessage, which transfers the message to the "window procedure" for the window that the message was addressed to. (The "window procedure" is a callback procedure, which got associated with the window class when it was registered.) (More than one window can use the same window procedure.)

Code can also send messages directly to a window procedure. These are called nonqueued messages.

A strict message loop is not the only option. Code elsewhere in the program can also accept and dispatch messages. PeekMessage is a non-blocking call that returns immediately, with a message if any are waiting, or no message if none is waiting. WaitMessage allows a thread to sleep until a message is in the queue.

Modern graphical interface frameworks, such as Windows Forms, Windows Presentation Foundation, MFC, Delphi, Qt, and others do not require applications to code a Windows message loop, because they automatically route events such as key presses and mouse clicks to their appropriate handlers as defined within the framework. However, each framework implements a message loop somewhere, and the message loop can usually be accessed or replaced when more direct control is required.

  1. ^ "Entering the Message Loop". 25 March 2010. Retrieved 2023-11-23.

and 22 Related for: Message loop in Microsoft Windows information

Request time (Page generated in 0.9061 seconds.)

Message loop in Microsoft Windows

Last Update:

The message loop is an obligatory section of code in every program that uses a graphical user interface under Microsoft Windows. Windows programs that...

Word Count : 593

Event loop

Last Update:

In computer science, the event loop (also known as message dispatcher, message loop, message pump, or run loop) is a programming construct or design pattern...

Word Count : 1677

Windows USER

Last Update:

existed in all versions of Windows, and includes functionality for window management, message passing, input processing and standard controls. Windows USER...

Word Count : 777

Microsoft Office

Last Update:

a personal information manager that replaces Windows Messaging, Microsoft Mail, and Schedule+ starting in Office 97; it includes an e-mail client, calendar...

Word Count : 15741

Microsoft Kin

Last Update:

wanted Kin to run an in-house Microsoft operating system. Microsoft planned to base Kin on Windows Phone. Due to delays with Windows Phone, however, the...

Word Count : 3569

Microsoft Teams

Last Update:

Teams replaced other Microsoft-operated business messaging and collaboration platforms, including Skype for Business and Microsoft Classroom. Throughout...

Word Count : 2944

Microsoft SmartScreen

Last Update:

included in several Microsoft products: All versions of the Microsoft Windows operating system since Windows 8 Web browsers Internet Explorer and Microsoft Edge...

Word Count : 2606

Microsoft Excel

Last Update:

Microsoft Excel is a spreadsheet editor developed by Microsoft for Windows, macOS, Android, iOS and iPadOS. It features calculation or computation capabilities...

Word Count : 9192

Microsoft mobile services

Last Update:

mobile browser for Windows Phone platforms, BREW, and Java. Microsoft's mobile services are typically connected with a Microsoft account and often come...

Word Count : 9028

Message queue

Last Update:

queues Win32 system message queues. "About Messages and Message Queues". Windows User Interface. Microsoft Developer Network. Archived from the original...

Word Count : 1927

Microsoft SQL Server

Last Update:

Microsoft SQL Server (Structured Query Language) is a proprietary relational database management system developed by Microsoft. As a database server, it...

Word Count : 7116

Visual Studio

Last Update:

development platforms including Windows API, Windows Forms, Windows Presentation Foundation (WPF), Windows Store and Microsoft Silverlight. It can produce...

Word Count : 15451

Batch file

Last Update:

script file in DOS, OS/2 and Microsoft Windows. It consists of a series of commands to be executed by the command-line interpreter, stored in a plain text...

Word Count : 4179

History of Microsoft Word

Last Update:

Word 95 for Windows, synchronizing its name with Windows 95, and Word 98 for Macintosh), once again breaking the synchronization. When Microsoft became aware...

Word Count : 2999

Visual FoxPro

Last Update:

scenarios with various Microsoft technologies including SQL Server 2005, .NET Framework, Windows Vista, Office 2007, Windows Search and Team Foundation...

Word Count : 1844

Microsoft HoloLens

Last Update:

Microsoft HoloLens is an augmented reality (AR)/mixed reality (MR) headset developed and manufactured by Microsoft. HoloLens runs the Windows Mixed Reality...

Word Count : 4350

Microsoft BASIC

Last Update:

in wide use on Microsoft Windows platforms in its most recent incarnation, Visual Basic .NET. The Altair BASIC interpreter was developed by Microsoft...

Word Count : 3057

Infinite loop

Last Update:

In computer programming, an infinite loop (or endless loop) is a sequence of instructions that, as written, will continue endlessly, unless an external...

Word Count : 2613

Microsoft Small Basic

Last Update:

as follows: TextWindow.WriteLine("Hello, World!") Microsoft Small Basic is Turing complete. It supports conditional branching, loop structures, and subroutines...

Word Count : 1787

Zune

Last Update:

Microsoft Band & Band 2, KIN, WP7, WP8, Windows 8, W10M, Windows 10, Xbox 360, Xbox One and S/X, and Xbox Series S/X.) On October 11, 2010, Microsoft...

Word Count : 6587

DirectInput

Last Update:

Microsoft recommends that new applications make use of the Windows message loop for keyboard and mouse input instead of DirectInput (as indicated in the...

Word Count : 960

WindowProc

Last Update:

messages are processed, Windows provides a default window procedure called DefWindowProc that provides default processing for messages that the application...

Word Count : 227

PDF Search Engine © AllGlobal.net