77#include " App.h"
88
99#include < map>
10+ #include < shellapi.h>
1011#include < shellscalingapi.h>
1112#include < shobjidl.h>
1213#include < string.h>
1314#include < vector>
1415#include " AppWindow.h"
16+ #include " DpiUtil.h"
1517
1618HINSTANCE g_hInstance;
1719int g_nCmdShow;
@@ -22,6 +24,9 @@ static int RunMessagePump();
2224static DWORD WINAPI ThreadProc (void * pvParam);
2325static void WaitForOtherThreads ();
2426
27+ #define NEXT_PARAM_CONTAINS (command ) \
28+ _wcsnicmp (nextParam.c_str(), command, ARRAYSIZE(command) - 1) == 0
29+
2530int APIENTRY wWinMain(HINSTANCE hInstance,
2631 HINSTANCE hPrevInstance,
2732 PWSTR lpCmdLine,
@@ -35,130 +40,80 @@ int APIENTRY wWinMain(HINSTANCE hInstance,
3540 // override this.
3641 DPI_AWARENESS_CONTEXT dpiAwarenessContext =
3742 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2;
38- // Same but for older OS versions that don't support per-monitor v2
39- PROCESS_DPI_AWARENESS oldDpiAwareness = PROCESS_PER_MONITOR_DPI_AWARE;
4043 std::wstring appId (L" EBWebView.SampleApp" );
4144 std::wstring initialUri (L" https://www.bing.com" );
45+ DWORD creationModeId = IDM_CREATION_MODE_WINDOWED;
46+
4247 if (lpCmdLine && lpCmdLine[0 ])
4348 {
44- bool commandLineError = false ;
45-
46- PWSTR nextParam = lpCmdLine;
47-
48- if (nextParam[0 ] == L' -' )
49+ int paramCount = 0 ;
50+ LPWSTR* params = CommandLineToArgvW (lpCmdLine, ¶mCount);
51+ for (int i = 0 ; i < paramCount; ++i)
4952 {
50- ++ nextParam;
51- if (nextParam [0 ] == L' -' )
53+ std::wstring nextParam;
54+ if (params[i] [0 ] == L' -' )
5255 {
53- ++nextParam;
56+ if (params[i][1 ] == L' -' )
57+ {
58+ nextParam.assign (params[i] + 2 );
59+ }
60+ else
61+ {
62+ nextParam.assign (params[i] + 1 );
63+ }
5464 }
55- if (_wcsnicmp (nextParam, L" dpiunaware" , ARRAYSIZE (L" dpiunaware" ) - 1 ) ==
56- 0 )
65+ if (NEXT_PARAM_CONTAINS (L" dpiunaware" ))
5766 {
5867 dpiAwarenessContext = DPI_AWARENESS_CONTEXT_UNAWARE;
59- oldDpiAwareness = PROCESS_DPI_UNAWARE;
6068 }
61- else if (_wcsnicmp (nextParam, L" dpisystemaware" ,
62- ARRAYSIZE (L" dpisystemaware" ) - 1 ) == 0 )
69+ else if (NEXT_PARAM_CONTAINS (L" dpisystemaware" ))
6370 {
6471 dpiAwarenessContext = DPI_AWARENESS_CONTEXT_SYSTEM_AWARE;
65- oldDpiAwareness = PROCESS_SYSTEM_DPI_AWARE;
6672 }
67- else if (_wcsnicmp (nextParam, L" dpipermonitorawarev2" ,
68- ARRAYSIZE (L" dpipermonitorawarev2" ) - 1 ) == 0 )
73+ else if (NEXT_PARAM_CONTAINS (L" dpipermonitorawarev2" ))
6974 {
7075 dpiAwarenessContext = DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2;
71- oldDpiAwareness = PROCESS_PER_MONITOR_DPI_AWARE;
7276 }
73- else if (_wcsnicmp (nextParam, L" dpipermonitoraware" ,
74- ARRAYSIZE (L" dpipermonitoraware" ) - 1 ) == 0 )
77+ else if (NEXT_PARAM_CONTAINS (L" dpipermonitoraware" ))
7578 {
7679 dpiAwarenessContext = DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE;
77- oldDpiAwareness = PROCESS_PER_MONITOR_DPI_AWARE;
7880 }
79- else if (_wcsnicmp (nextParam, L" noinitialnavigation" ,
80- ARRAYSIZE (L" noinitialnavigation" ) - 1 ) == 0 )
81+ else if (NEXT_PARAM_CONTAINS (L" noinitialnavigation" ))
8182 {
8283 initialUri = L" " ;
8384 }
84- else if (_wcsnicmp (nextParam, L" appid=" ,
85- ARRAYSIZE (L" appid=" ) - 1 ) == 0 )
85+ else if (NEXT_PARAM_CONTAINS (L" appid=" ))
8686 {
87- PWSTR appidStart = nextParam + ARRAYSIZE (L" appid=" );
88- size_t len = 0 ;
89- while (appidStart[len] && (appidStart[len] != ' ' ))
90- ++len;
91- appId = std::wstring (appidStart, len);
87+ appId = nextParam.substr (nextParam.find (L' =' ) + 1 );
9288 }
93- else if (_wcsnicmp (nextParam, L" initialUri=" ,
94- ARRAYSIZE (L" initialUri=" ) - 1 ) == 0 )
89+ else if (NEXT_PARAM_CONTAINS (L" initialUri=" ))
9590 {
96- PWSTR uriStart = nextParam + ARRAYSIZE (L" initialUri=" ) - 1 ;
97- size_t len = 0 ;
98- while (uriStart[len] && (uriStart[len] != ' ' ))
99- ++len;
100- initialUri = std::wstring (uriStart, len);
91+ initialUri = nextParam.substr (nextParam.find (L' =' ) + 1 );
10192 }
102- else
93+ else if ( NEXT_PARAM_CONTAINS ( L" creationmode= " ))
10394 {
104- // --edge-webview-switches is a supported switch to pass additional
105- // command line switches to WebView's browser process.
106- // For example, adding
107- // --edge-webview-switches="--remote-debugging-port=9222"
108- // enables remote debugging for webview.
109- // And adding
110- // --edge-webview-switches="--auto-open-devtools-for-tabs"
111- // causes dev tools to open automatically for the WebView.
112- commandLineError =
113- ( wcsncmp (nextParam, L" edge-webview-switches " ,
114- ARRAYSIZE ( L" edge-webview-switches " ) - 1 ) != 0 ) &&
115- ( wcsncmp (nextParam, L" restore " ,
116- ARRAYSIZE ( L" restore " ) - 1 ) != 0 );
95+ nextParam = nextParam. substr (nextParam. find (L ' = ' ) + 1 );
96+ if ( NEXT_PARAM_CONTAINS ( L" windowed " ))
97+ {
98+ creationModeId = IDM_CREATION_MODE_WINDOWED;
99+ }
100+ else if ( NEXT_PARAM_CONTAINS ( L" visualdcomp " ))
101+ {
102+ creationModeId = IDM_CREATION_MODE_VISUAL_DCOMP;
103+ }
104+ else if ( NEXT_PARAM_CONTAINS ( L" visualwincomp " ))
105+ {
106+ creationModeId = IDM_CREATION_MODE_VISUAL_WINCOMP;
107+ }
117108 }
118109 }
119- else
120- {
121- commandLineError = true ;
122- }
123-
124- if (commandLineError)
125- {
126- MessageBox (nullptr ,
127- L" Valid command line "
128- L" parameters:\n\t -DPIUnaware\n\t -DPISystemAware\n\t -"
129- L" DPIPerMonitorAware\n\t -DPIPerMonitorAwareV2" ,
130- L" Command Line Parameters" , MB_OK);
131- }
110+ LocalFree (params);
132111 }
133-
134112 SetCurrentProcessExplicitAppUserModelID (appId.c_str ());
135113
136- // Call the latest DPI awareness function possible
137- HMODULE user32 = LoadLibraryA (" User32.dll" );
138- auto func = reinterpret_cast <decltype (&SetProcessDpiAwarenessContext)>(
139- GetProcAddress (user32, " SetProcessDpiAwarenessContext" ));
140- if (func)
141- {
142- // Windows 10 1703+: SetProcessDpiAwarenessContext
143- func (dpiAwarenessContext);
144- }
145- else {
146- HMODULE shcore = LoadLibraryA (" Shcore.dll" );
147- auto func = reinterpret_cast <decltype (&SetProcessDpiAwareness)>(
148- GetProcAddress (shcore, " SetProcessDpiAwareness" ));
149- if (func)
150- {
151- // Windows 8.1+: SetProcessDpiAwareness
152- func (oldDpiAwareness);
153- }
154- else if (dpiAwarenessContext != DPI_AWARENESS_CONTEXT_UNAWARE)
155- {
156- // Windows 7+: SetProcessDPIAware
157- SetProcessDPIAware ();
158- }
159- }
114+ DpiUtil::SetProcessDpiAwarenessContext (dpiAwarenessContext);
160115
161- new AppWindow (IDM_CREATION_MODE_WINDOWED , initialUri);
116+ new AppWindow (creationModeId , initialUri);
162117
163118 int retVal = RunMessagePump ();
164119
@@ -215,6 +170,7 @@ void CreateNewThread(UINT creationModeId)
215170 STACK_SIZE_PARAM_IS_A_RESERVATION, &threadId);
216171 s_threads.insert (std::pair<DWORD, HANDLE>(threadId, thread));
217172}
173+
218174// This function is the starting point for new threads. It will open a new app window.
219175static DWORD WINAPI ThreadProc (void * pvParam)
220176{
0 commit comments