LAPTOP-SNT8I5JK\Boounion
2025-09-11 eaf39ceeef11c5685ddbb840541460e5dc5fccaa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// TerminalDisplayDlg.cpp : ÊµÏÖÎļþ
//
 
#include "stdafx.h"
#include "Servo.h"
#include "TerminalDisplayDlg.h"
#include "afxdialogex.h"
 
 
#define TIMER_ID_HIDE        1
 
 
// CTerminalDisplayDlg ¶Ô»°¿ò
 
IMPLEMENT_DYNAMIC(CTerminalDisplayDlg, CDialogEx)
 
CTerminalDisplayDlg::CTerminalDisplayDlg(CWnd* pParent /*=NULL*/)
    : CDialogEx(IDD_DIALOG_TERMINAL_DISPLAY, pParent)
{
    m_crBkgnd = RGB(0x3d, 0x0, 0xd0);
    m_hbrBkgnd = nullptr;
}
 
CTerminalDisplayDlg::~CTerminalDisplayDlg()
{
}
 
void CTerminalDisplayDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_BUTTON_CLOSE, m_btnClose);    
}
 
 
BEGIN_MESSAGE_MAP(CTerminalDisplayDlg, CDialogEx)
    ON_WM_CTLCOLOR()
    ON_WM_DESTROY()
    ON_WM_SIZE()
    ON_WM_CREATE()
    ON_WM_TIMER()
    ON_BN_CLICKED(IDC_BUTTON_CLOSE, &CTerminalDisplayDlg::OnBnClickedButtonClose)
    ON_WM_NCHITTEST()
END_MESSAGE_MAP()
 
 
// CTerminalDisplayDlg ÏûÏ¢´¦Àí³ÌÐò
 
void CTerminalDisplayDlg::SetTemplateHtml(const char* pszFilepath)
{
    m_strFilepath = pszFilepath;
}
 
BOOL CTerminalDisplayDlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();
 
    CString strIcon1;
    strIcon1.Format(_T("%s\\Res\\close_white_24.ico"), theApp.m_strAppDir);
    HICON hIcon = (HICON)::LoadImage(AfxGetInstanceHandle(),
        strIcon1, IMAGE_ICON, 24, 24,
        LR_LOADFROMFILE | LR_DEFAULTCOLOR | LR_CREATEDIBSECTION | LR_DEFAULTSIZE);
    m_btnClose.SetIcon(hIcon, hIcon, 24);
    m_btnClose.SetBkgndColor(BS_HOVER, RGB(232, 17, 35));
    m_btnClose.SetBkgndColor(BS_PRESS, RGB(162, 34, 44));
    UpdateCloseBtn();
 
    return TRUE;  // return TRUE unless you set the focus to a control
                  // Òì³£: OCX ÊôÐÔÒ³Ó¦·µ»Ø FALSE
}
 
 
HBRUSH CTerminalDisplayDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
 
    if (nCtlColor == CTLCOLOR_STATIC) {
        pDC->SetBkColor(m_crBkgnd);
    }
 
    if (m_hbrBkgnd == nullptr) {
        m_hbrBkgnd = CreateSolidBrush(m_crBkgnd);
    }
 
    return m_hbrBkgnd;
}
 
 
void CTerminalDisplayDlg::OnDestroy()
{
    CDialogEx::OnDestroy();
 
    if (m_hbrBkgnd != nullptr) {
        ::DeleteObject(m_hbrBkgnd);
    }
}
 
 
void CTerminalDisplayDlg::OnSize(UINT nType, int cx, int cy)
{
    CDialogEx::OnSize(nType, cx, cy);
    if (m_webviewController == nullptr) return;
    Resize();
}
 
void CTerminalDisplayDlg::Resize()
{
    CWnd* pItem;
    CRect rcClient, rcItem;
    GetClientRect(&rcClient);
    rcClient.top += 38;
    m_webviewController->put_Bounds(rcClient);
    m_btnClose.MoveWindow(rcClient.right - 38, 0, 38, 38);
}
 
void CTerminalDisplayDlg::ShowText(const char* pszText, unsigned int duration/* = -1*/)
{
    if (m_webWiew == nullptr) return;
    std::string strMsg = pszText;
    std::wstring wstrMsg(strMsg.begin(), strMsg.end());
    size_t size = (wstrMsg.size() + 1) * sizeof(wchar_t);
    wchar_t* memory = (wchar_t*)CoTaskMemAlloc(size);
    if (memory == nullptr) return;
    wcsncpy_s(memory, wstrMsg.size() + 1, wstrMsg.c_str(), wstrMsg.size());
    wil::unique_cotaskmem_string message(memory);
    m_webWiew->PostWebMessageAsString(message.get());
    ShowWindow(SW_SHOW);
    m_webviewController->put_IsVisible(TRUE);
    SetTimer(TIMER_ID_HIDE, duration, nullptr);
}
 
int CTerminalDisplayDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CDialogEx::OnCreate(lpCreateStruct) == -1)
        return -1;
 
    HWND hWnd = this->m_hWnd;
 
    // Step 3 - Create a single WebView within the parent window
    // Locate the browser and set up the environment for WebView
    CreateCoreWebView2EnvironmentWithOptions(nullptr, nullptr, nullptr,
        Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(
            [hWnd, this](HRESULT result, ICoreWebView2Environment* env) -> HRESULT {
 
                // Create a CoreWebView2Controller and get the associated CoreWebView2 whose parent is the main window hWnd
                env->CreateCoreWebView2Controller(hWnd, Callback<ICoreWebView2CreateCoreWebView2ControllerCompletedHandler>(
                    [hWnd, this](HRESULT result, ICoreWebView2Controller* controller) -> HRESULT {
                        if (controller != nullptr) {
                            this->m_webviewController = controller;
                            this->m_webviewController->get_CoreWebView2(&this->m_webWiew);
                        }
                        else {
                            OutputDebugString(_T("WebView2 Controller ´´½¨Ê§°Ü£¡\n"));
                            return E_FAIL;
                        }
                        // Add a few settings for the webview
                        // The demo step is redundant since the values are the default settings
                        wil::com_ptr<ICoreWebView2Settings> settings;
                        this->m_webWiew->get_Settings(&settings);
                        settings->put_IsScriptEnabled(TRUE);
                        settings->put_AreDefaultScriptDialogsEnabled(TRUE);
                        settings->put_IsWebMessageEnabled(TRUE);
 
                        // Resize WebView to fit the bounds of the parent window
                        RECT bounds;
                        ::GetClientRect(hWnd, &bounds);
                        this->m_webviewController->put_Bounds(bounds);
                        Resize();
 
                        EventRegistrationToken token;
 
                        // Step 5 - Scripting
                        // Schedule an async task to add initialization script that freezes the Object object
                        this->m_webWiew->AddScriptToExecuteOnDocumentCreated(L"Object.freeze(Object);", nullptr);
                        // Schedule an async task to get the document URL
                        this->m_webWiew->ExecuteScript(L"window.document.URL;", Callback<ICoreWebView2ExecuteScriptCompletedHandler>(
                            [](HRESULT errorCode, LPCWSTR resultObjectAsJson) -> HRESULT {
                                LPCWSTR URL = resultObjectAsJson;
                                return S_OK;
                            }).Get());
 
 
                        // Step 6 - Communication between host and web content
                        // Set an event handler for the host to return received message back to the web content
                        this->m_webWiew->add_WebMessageReceived(Callback<ICoreWebView2WebMessageReceivedEventHandler>(
                            [this](ICoreWebView2* webview, ICoreWebView2WebMessageReceivedEventArgs* args) -> HRESULT {
                                wil::unique_cotaskmem_string message;
                                args->TryGetWebMessageAsString(&message);
                                this->ProcessWebViewMessage(message);
                                return S_OK;
                            }).Get(), &token);
 
                        if (!m_strFilepath.empty()) {
                            wchar_t url[512];
                            swprintf(url, 512, L"file:///%hs", m_strFilepath.c_str());
                            m_webWiew->Navigate(url);
                        }
 
                        return S_OK;
                    }).Get());
                return S_OK;
            }).Get());
 
    return 0;
}
 
 
void CTerminalDisplayDlg::OnTimer(UINT_PTR nIDEvent)
{
    // TODO: ÔÚ´ËÌí¼ÓÏûÏ¢´¦Àí³ÌÐò´úÂëºÍ/»òµ÷ÓÃĬÈÏÖµ
    if (TIMER_ID_HIDE == nIDEvent) {
        KillTimer(TIMER_ID_HIDE);
        ShowWindow(SW_HIDE);
    }
 
    CDialogEx::OnTimer(nIDEvent);
}
 
void CTerminalDisplayDlg::OnBnClickedButtonClose()
{
    ShowWindow(SW_HIDE);
}
 
LRESULT CTerminalDisplayDlg::OnNcHitTest(CPoint point)
{
    return HTCAPTION;
    return CDialogEx::OnNcHitTest(point);
}
 
void CTerminalDisplayDlg::ProcessWebViewMessage(wil::unique_cotaskmem_string& message)
{
    // ×ª»»ÎªCString
    LPCWSTR wideStr = message.get();
    int bufferSize = WideCharToMultiByte(CP_UTF8, 0, wideStr, -1, nullptr, 0, nullptr, nullptr);
    if (bufferSize == 0)return;
    char* charStr = new char[bufferSize];
    WideCharToMultiByte(CP_UTF8, 0, wideStr, -1, charStr, bufferSize, nullptr, nullptr);
    CString strMsg = charStr;
    delete[] charStr;
 
 
    // È¡header, body
    CString strHeader, strBody;
    int n = strMsg.Find(":");
    if (n < 0) return;
    strHeader = strMsg.Left(n);
    strBody = strMsg.Right(strMsg.GetLength() - n - 1);
 
 
    // ±³¾°É«
    if (strHeader.CompareNoCase("backgroundColor") == 0) {
        int n1 = strBody.Find("rgb(");
        int n2 = strBody.Find(",");
        int n3 = strBody.Find(",", n2+1);
        int n4 = strBody.Find(")");
        if (n1 >= 0 && n2 > 0 && n3 > 0 && n4 > 0) {
            CString strRed, strGreen, strBlue;
            strRed = strBody.Mid(n1 + 4, n2 - n1 - 4);
            strGreen = strBody.Mid(n2 + 1, n3 - n2 - 1);
            strBlue = strBody.Mid(n3 + 1, n4 - n3 - 1);
            m_crBkgnd = RGB(atoi(strRed), atoi(strGreen), atoi(strBlue));
            if (m_hbrBkgnd != nullptr) {
                ::DeleteObject(m_hbrBkgnd);
                m_hbrBkgnd = nullptr;
                Invalidate();
            }
            UpdateCloseBtn();
        }
    }
}
 
void CTerminalDisplayDlg::UpdateCloseBtn()
{
    m_btnClose.SetFrameColor(BS_NORMAL, m_crBkgnd);
    m_btnClose.SetFrameColor(BS_HOVER, m_crBkgnd);
    m_btnClose.SetFrameColor(BS_PRESS, m_crBkgnd);
    m_btnClose.SetBkgndColor(BS_NORMAL, m_crBkgnd);
    m_btnClose.Invalidate();
}