Event Handling in Native C++: How To Create A Project and Configure A Visual Studio To Run A C++ GUI Application?
Event Handling in Native C++: How To Create A Project and Configure A Visual Studio To Run A C++ GUI Application?
Learning Objectives
Introduction
To develop C++ GUI or C++ graphical user interface application, you need an IDE that
supports the C++ GUI application. To create the GUI app, you must use Visual Studio
2019 because it is better suited for the C++ GUI application.
https://www.simplilearn.com/tutorials/cpp-tutorial/cpp-
gui?source=sl_frs_nav_playlist_video_clicked
https://www.daniweb.com/programming/software-development/threads/77173/c-gui-graphical-
user-interface-for-beginners
In native C++ event handling, you set up an event source and event receiver using
the event_source and event_receiver attributes, respectively, specifying type=native.
These attributes allow the classes they're applied on to fire events and handle events in
a native, non-COM context.
Declaring events
In an event source class, use the __event keyword on a method declaration to declare the
method as an event. Make sure to declare the method, but don't define it. If you do, it
Firing events
To fire an event, call the method declared as an event in the event source class. If
handlers have been hooked to the event, the handlers will be called.
The following example shows how to fire an event in native C++. To compile and run
the example, refer to the comments in the code. To build the code in the Visual Studio
IDE, verify that the /permissive- option is turned off.
Example
// evh_native.cpp
// compile by using: cl /EHsc /W3 evh_native.cpp
#include <stdio.h>
[event_source(native)]
class CSource {
public:
__event void MyEvent(int nValue);
};
int main() {
CSource source;
CReceiver receiver;
receiver.hookEvent(&source);
__raise source.MyEvent(123);
receiver.unhookEvent(&source);
}
https://www.codeproject.com/Articles/1256352/CppEvent-How-to-Implement-Events-using-
Standard-Cp