7
7
#include " App.h"
8
8
9
9
#include < map>
10
+ #include < shellapi.h>
10
11
#include < shellscalingapi.h>
11
12
#include < shobjidl.h>
12
13
#include < string.h>
13
14
#include < vector>
14
15
#include " AppWindow.h"
16
+ #include " DpiUtil.h"
15
17
16
18
HINSTANCE g_hInstance;
17
19
int g_nCmdShow;
@@ -22,6 +24,9 @@ static int RunMessagePump();
22
24
static DWORD WINAPI ThreadProc (void * pvParam);
23
25
static void WaitForOtherThreads ();
24
26
27
+ #define NEXT_PARAM_CONTAINS (command ) \
28
+ _wcsnicmp (nextParam.c_str(), command, ARRAYSIZE(command) - 1) == 0
29
+
25
30
int APIENTRY wWinMain(HINSTANCE hInstance,
26
31
HINSTANCE hPrevInstance,
27
32
PWSTR lpCmdLine,
@@ -35,130 +40,80 @@ int APIENTRY wWinMain(HINSTANCE hInstance,
35
40
// override this.
36
41
DPI_AWARENESS_CONTEXT dpiAwarenessContext =
37
42
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;
40
43
std::wstring appId (L" EBWebView.SampleApp" );
41
44
std::wstring initialUri (L" https://www.bing.com" );
45
+ DWORD creationModeId = IDM_CREATION_MODE_WINDOWED;
46
+
42
47
if (lpCmdLine && lpCmdLine[0 ])
43
48
{
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)
49
52
{
50
- ++ nextParam;
51
- if (nextParam [0 ] == L' -' )
53
+ std::wstring nextParam;
54
+ if (params[i] [0 ] == L' -' )
52
55
{
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
+ }
54
64
}
55
- if (_wcsnicmp (nextParam, L" dpiunaware" , ARRAYSIZE (L" dpiunaware" ) - 1 ) ==
56
- 0 )
65
+ if (NEXT_PARAM_CONTAINS (L" dpiunaware" ))
57
66
{
58
67
dpiAwarenessContext = DPI_AWARENESS_CONTEXT_UNAWARE;
59
- oldDpiAwareness = PROCESS_DPI_UNAWARE;
60
68
}
61
- else if (_wcsnicmp (nextParam, L" dpisystemaware" ,
62
- ARRAYSIZE (L" dpisystemaware" ) - 1 ) == 0 )
69
+ else if (NEXT_PARAM_CONTAINS (L" dpisystemaware" ))
63
70
{
64
71
dpiAwarenessContext = DPI_AWARENESS_CONTEXT_SYSTEM_AWARE;
65
- oldDpiAwareness = PROCESS_SYSTEM_DPI_AWARE;
66
72
}
67
- else if (_wcsnicmp (nextParam, L" dpipermonitorawarev2" ,
68
- ARRAYSIZE (L" dpipermonitorawarev2" ) - 1 ) == 0 )
73
+ else if (NEXT_PARAM_CONTAINS (L" dpipermonitorawarev2" ))
69
74
{
70
75
dpiAwarenessContext = DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2;
71
- oldDpiAwareness = PROCESS_PER_MONITOR_DPI_AWARE;
72
76
}
73
- else if (_wcsnicmp (nextParam, L" dpipermonitoraware" ,
74
- ARRAYSIZE (L" dpipermonitoraware" ) - 1 ) == 0 )
77
+ else if (NEXT_PARAM_CONTAINS (L" dpipermonitoraware" ))
75
78
{
76
79
dpiAwarenessContext = DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE;
77
- oldDpiAwareness = PROCESS_PER_MONITOR_DPI_AWARE;
78
80
}
79
- else if (_wcsnicmp (nextParam, L" noinitialnavigation" ,
80
- ARRAYSIZE (L" noinitialnavigation" ) - 1 ) == 0 )
81
+ else if (NEXT_PARAM_CONTAINS (L" noinitialnavigation" ))
81
82
{
82
83
initialUri = L" " ;
83
84
}
84
- else if (_wcsnicmp (nextParam, L" appid=" ,
85
- ARRAYSIZE (L" appid=" ) - 1 ) == 0 )
85
+ else if (NEXT_PARAM_CONTAINS (L" appid=" ))
86
86
{
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 );
92
88
}
93
- else if (_wcsnicmp (nextParam, L" initialUri=" ,
94
- ARRAYSIZE (L" initialUri=" ) - 1 ) == 0 )
89
+ else if (NEXT_PARAM_CONTAINS (L" initialUri=" ))
95
90
{
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 );
101
92
}
102
- else
93
+ else if ( NEXT_PARAM_CONTAINS ( L" creationmode= " ))
103
94
{
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
+ }
117
108
}
118
109
}
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);
132
111
}
133
-
134
112
SetCurrentProcessExplicitAppUserModelID (appId.c_str ());
135
113
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);
160
115
161
- new AppWindow (IDM_CREATION_MODE_WINDOWED , initialUri);
116
+ new AppWindow (creationModeId , initialUri);
162
117
163
118
int retVal = RunMessagePump ();
164
119
@@ -215,6 +170,7 @@ void CreateNewThread(UINT creationModeId)
215
170
STACK_SIZE_PARAM_IS_A_RESERVATION, &threadId);
216
171
s_threads.insert (std::pair<DWORD, HANDLE>(threadId, thread));
217
172
}
173
+
218
174
// This function is the starting point for new threads. It will open a new app window.
219
175
static DWORD WINAPI ThreadProc (void * pvParam)
220
176
{
0 commit comments