WxWidgets custom message and processing __wxwidgets

Source: Internet
Author: User
Tags wxwidgets
Custom Messages

User-defined messages typically derive from Wxevent, and the following is an example of a user-defined projectevent that describes the steps for customizing a message:
First, the message is declared in the header file.

UserEvent.h

class projectevent:wxevent
{public
:
    projectevent (int itemId, int eventtag);

Public:
    int  mitemid;
    int  Meventtag;
    ...
}

Wxdeclare_event (Project_event, projectevent);

Where the meaning ofwxdeclare_event (project_event, projectevent) is to declare a new message type project_event, the message type corresponding to the message class is projectevent. That is, when a user processes a project_event message in a message loop, the message loop provides a projectevent class for the parameter class provided by the user message handler function.

The message is then defined in the source file.

UserEvent.cpp

#include "UserEvent.h"

wxdefine_event (Project_event, projectevent);

Projectevent::P rojectevent (int itemId, int eventtag)
: Wxevent (), Mitemid (ItemId), Meventtag (Eventtag)
{
}

Note that wxdefine_event (project_event, projectevent)must be declared in the source file. Otherwise, an error occurs during execution. The main reason for the error seems to be the allocation of message code. Processing of custom messages

Custom messages are processed in the same way as predefined messages, including two processing methods: static processing and dynamic processing. static processing example.

Declare the message handler function in the header file.

MainFrame.h

class Mainframe:public wxframe
{
    ...
Public:
    void Onproject (Wxevent & evt);
    ..... Declare_event_table ();
}

Define the message handler function in the implementation file.

MainFrame.cpp

#include "MainFrame.h"
#include "UserEvent.h"

begin_event_table (MainFrame, Wxframe) ...
    Evt_custom (Project_event, Wxid_any, onproject) ...
End_event_table ()


void Mainframe::onproject (Wxevent & evt)
{
    ///as the EVENT type of the argument, Should be convertible to the projectevent type.
    Projectevent * projevt = dynamic_cast<projectevent *> (& evt);
    ...
}
Dynamic Processing example.

The declaration in the header file is the same as static processing.
In the implementation file, the message processing function is dynamically bound when initialized without defining a message processing table. The code is as follows:

MainFrame.cpp

#include "MainFrame.h"
#include "UserEvent.h"


void Mainframe::mainframe (...)
{
    ...
    .. Bind (Project_event, &mainframe::onproject, this, wxid_any);
    ...
}
Customizing the sending of messages

The user invokes the processWindowEvent function on the specified window, the parameter is a custom projectevent message type, and immediately sends the PROJECTEVENT message to the specified window, which is processed by the message handler function of the window.
The code is as follows:

MainFrame.cpp


void Mainframe::sendevent (...)
{
    ...
    .. Projectevent event (ID, tag);
    Targetwnd->processwindowevent (event); Targetwnd window processing messages ...
}

Note that this function uses a synchronous processing mechanism, similar to the SendMessagein MFC, that is, the function returns after the message processing is complete. Similar to MFC, Wxwidgets also has an asynchronous message-sending mechanism, by invoking an asynchronous message that queueevent can complete the message, sending the message back immediately, and the target window will process the user message in a subsequent idle message loop, similar to the one in MFC PostMessage.

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.