Covellite++  Version: 2.3.0 Revision: ??? Platform: x64 Build: 23:13 04.01.2025
Кроссплатформенный фреймворк для разработки приложений на С++
Загрузка...
Поиск...
Не найдено
Application.windows.cpp
1
2#include "stdafx.h"
3#include <Covellite/App/Application.hpp>
4#include <alicorn/platform/windows.hpp>
5#include <windef.h>
6#include <winuser.h>
7#include <processenv.h>
8#include <synchapi.h>
9#include <boost/core/ignore_unused.hpp>
10#include <alicorn/platform/winapi-check.hpp>
11#include <Covellite/Events.hpp>
12#include <Covellite/App/Events.hpp>
13#include "ClassName.windows.hpp"
14
15#undef GetCommandLine
16
17using namespace covellite::app;
18
19static WNDCLASSEX WindowClass = { 0 };
20
25Application::Application(const Run_t & _Run) :
26 Run(_Run)
27{
28 const auto hInstance = GetModuleHandle(nullptr);
29
30 WindowClass.cbSize = sizeof(WindowClass);
31 WindowClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
32 WindowClass.hInstance = hInstance;
33 WindowClass.lpszClassName = ClassName;
34 WindowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
35 WindowClass.hIcon = USING_MOCK ::LoadIcon(hInstance, L"ApplicationIcon");
36 WindowClass.hIconSm = USING_MOCK ::LoadIcon(hInstance, L"ApplicationIcon");
37
38 WindowClass.lpfnWndProc =
39 [](HWND _hWnd, UINT _Message, WPARAM _wParam, LPARAM _lParam) -> LRESULT
40 {
41 auto * pEvents = reinterpret_cast<::covellite::events::Events *>(
42 USING_MOCK ::GetWindowLongPtr(_hWnd, GWLP_USERDATA));
43 if (pEvents != nullptr)
44 {
45 const ::std::pair<WPARAM, LPARAM> Params{ _wParam, _lParam };
46 const auto IsSuccess = (*pEvents)[_Message](Params);
47 if (IsSuccess) return 0;
48 }
49
50 return USING_MOCK ::DefWindowProc(_hWnd, _Message, _wParam, _lParam);
51 };
52
53 WINAPI_CHECK RegisterClassEx(&WindowClass);
54
55 m_Events[events::Application.Exit].Connect([](void) noexcept
56 {
57 PostQuitMessage(0);
58 });
59}
60
66Application::Application(Continuous) :
67 Application([&](void) { PostCommand(false); })
68{
69}
70
76Application::Application(EventBased) :
77 Application([&](void) { PostCommand(true); })
78{
79}
80
81/*virtual*/ Application::~Application(void) noexcept
82{
83 USING_MOCK ::UnregisterClass(ClassName, GetModuleHandle(nullptr));
84}
85
97/*static*/ void Application::Main(CreateApp_t _fnCreateApp, void * _pParams) noexcept
98{
99 ::boost::ignore_unused(_pParams);
100
101 try
102 {
103 _fnCreateApp()->Run();
104 }
105 catch (const ::std::exception & _Ex)
106 {
107 const auto ErrorDescription =
108 ::std::string{ "Exception: " } + _Ex.what() + ".";
109 MessageBoxA(NULL, ErrorDescription.c_str(), "Covellite++", MB_OK);
110 }
111 catch (...)
112 {
113 const auto ErrorDescription =
114 ::std::string{ "Exception: " } + "unknown" + ".";
115 MessageBoxA(NULL, ErrorDescription.c_str(), "Covellite++", MB_OK);
116 }
117}
118
124::std::string Application::GetCommandLine(void) const
125{
126 const auto * const pCommandLine = GetCommandLineA();
127 return (pCommandLine == nullptr) ? "" : pCommandLine;
128}
129
130bool Application::PostCommand(bool _IsWaitMessage)
131{
132 using ProcessEvents_t = ::std::function<bool(MSG &)>;
133
134 const ProcessEvents_t ProcessEventsEventBased = [](MSG & _Message)
135 {
136 const auto Result = USING_MOCK::GetMessage(&_Message, NULL, 0, 0);
137 if (Result == -1) WINAPI_CHECK FALSE;
138 if (_Message.message == WM_QUIT) return true;
139
140 USING_MOCK::TranslateMessage(&_Message);
141 USING_MOCK::DispatchMessage(&_Message);
142 return true;
143 };
144
145 bool IsLostFocus = false;
146
147 const ProcessEvents_t ProcessEventsContinuous =
148 [&IsLostFocus](MSG & _Message) noexcept
149 {
150 const auto Result = (IsLostFocus) ?
151 USING_MOCK ::GetMessage(&_Message, NULL, 0, 0) :
152 USING_MOCK ::PeekMessage(&_Message, 0, 0, 0, PM_REMOVE);
153 if (Result == FALSE) return true;
154
155 if (_Message.message == WM_SETFOCUS) IsLostFocus = false;
156 if (_Message.message == WM_KILLFOCUS) IsLostFocus = true;
157 if (_Message.message == WM_QUIT) return true;
158
159 USING_MOCK::TranslateMessage(&_Message);
160 USING_MOCK::DispatchMessage(&_Message);
161 return false;
162 };
163
164 const auto ProcessEvents = (_IsWaitMessage) ?
165 ProcessEventsEventBased : ProcessEventsContinuous;
166
167 m_Events[events::Application.Start]();
168
169 MSG Message = { 0 };
170
171 while (true)
172 {
173 while (!ProcessEvents(Message)) {};
174 if (Message.message == WM_QUIT) break;
175
176 m_Events[events::Application.Update]();
177
178 Sleep(0);
179 }
180
181 return true;
182}
Класс входит в проект Covellite.App Базовый класс приложения для Android.
Definition Application.hpp:51
VIRTUAL_MOCK bool PostCommand(bool)
Функция вызова сигнала произошедшего события..
Definition Application.android.cpp:233
Класс входит в проект Covellite.Events Класс для работы с событиями фреймворка.
Definition Events.hpp:36