MCPcopy Create free account
hub / github.com/MicrosoftEdge/WebView2Browser / InitInstance

Method InitInstance

BrowserWindow.cpp:158–213  ·  view source on GitHub ↗

FUNCTION: InitInstance(HINSTANCE, int) PURPOSE: Saves instance handle and creates main window COMMENTS: In this function, we save the instance handle in a global variable and create and display the main program window.

Source from the content-addressed store, hash-verified

156// create and display the main program window.
157//
158BOOL BrowserWindow::InitInstance(HINSTANCE hInstance, int nCmdShow)
159{
160 m_hInst = hInstance; // Store app instance handle
161 LoadStringW(m_hInst, IDS_APP_TITLE, s_title, MAX_LOADSTRING);
162
163 SetUIMessageBroker();
164
165 m_hWnd = CreateWindowW(s_windowClass, s_title, WS_OVERLAPPEDWINDOW,
166 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, m_hInst, nullptr);
167
168 if (!m_hWnd)
169 {
170 return FALSE;
171 }
172
173 // Make the BrowserWindow instance ptr available through the hWnd
174 SetWindowLongPtr(m_hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
175
176 UpdateMinWindowSize();
177 ShowWindow(m_hWnd, nCmdShow);
178 UpdateWindow(m_hWnd);
179
180 // Get directory for user data. This will be kept separated from the
181 // directory for the browser UI data.
182 std::wstring userDataDirectory = GetAppDataDirectory();
183 userDataDirectory.append(L"\\User Data");
184
185 // Create WebView environment for web content requested by the user. All
186 // tabs will be created from this environment and kept isolated from the
187 // browser UI. This enviroment is created first so the UI can request new
188 // tabs when it's ready.
189 HRESULT hr = CreateCoreWebView2EnvironmentWithOptions(nullptr, userDataDirectory.c_str(),
190 nullptr, Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(
191 [this](HRESULT result, ICoreWebView2Environment* env) -> HRESULT
192 {
193 RETURN_IF_FAILED(result);
194
195 m_contentEnv = env;
196 HRESULT hr = InitUIWebViews();
197
198 if (!SUCCEEDED(hr))
199 {
200 OutputDebugString(L"UI WebViews environment creation failed\n");
201 }
202
203 return hr;
204 }).Get());
205
206 if (!SUCCEEDED(hr))
207 {
208 OutputDebugString(L"Content WebViews environment creation failed\n");
209 return FALSE;
210 }
211
212 return TRUE;
213}
214
215HRESULT BrowserWindow::InitUIWebViews()

Callers 1

LaunchWindowMethod · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected