Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

win7 support #86

Open
wants to merge 1 commit into
base: debug
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Photino.Native/Photino.Native.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ COPY .\$(OutDir)WebView2Loader.dll ..\Photino.Test\bin\Debug\net5.0\</Command>
<ClCompile Include="Photino.Linux.cpp" />
<ClCompile Include="Photino.Windows.cpp" />
<ClCompile Include="Photino.Windows.DarkMode.cpp" />
<ClCompile Include="Photino.Windows.DpiHelp.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Dependencies\wintoastlib.h" />
Expand All @@ -168,6 +169,7 @@ COPY .\$(OutDir)WebView2Loader.dll ..\Photino.Test\bin\Debug\net5.0\</Command>
<ClInclude Include="Photino.Mac.UrlSchemeHandler.h" />
<ClInclude Include="Photino.Windows.ToastHandler.h" />
<ClInclude Include="Photino.Windows.DarkMode.h" />
<ClInclude Include="Photino.Windows.DpiHelp.h" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
6 changes: 6 additions & 0 deletions Photino.Native/Photino.Native.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
<ClCompile Include="Photino.Windows.DarkMode.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Photino.Windows.DpiHelp.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down Expand Up @@ -69,5 +72,8 @@
<ClInclude Include="Photino.Windows.DarkMode.h">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="Photino.Windows.DpiHelp.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
124 changes: 124 additions & 0 deletions Photino.Native/Photino.Windows.DpiHelp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#include "Photino.Windows.DpiHelp.h"

// =============================================
//
// Types
//
// ===================
typedef UINT(WINAPI *_GetDpiForWindowFunc)(HWND hwnd);
typedef DPI_AWARENESS_CONTEXT(WINAPI *_SetThreadDpiAwarenessContextFunc)(DPI_AWARENESS_CONTEXT dpiContext);
typedef int(WINAPI *_GetSystemMetricsForDpiFunc)(int nIndex, UINT dpi);

// =============================================
//
// Forward declarations
//
// ===================
UINT FallbackGetDpiForWindow(HWND hwnd);
DPI_AWARENESS_CONTEXT FallbackSetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT dpiContext);
int FallbackGetSystemMetricsForDpiFunc(int nIndex, UINT dpi);

// =============================================
//
// Statics
//
// ===================
static HMODULE s_user32 = nullptr;

static _GetDpiForWindowFunc _getDpiForWindow = FallbackGetDpiForWindow;
static _GetSystemMetricsForDpiFunc _getSystemMetricsForDpi = FallbackGetSystemMetricsForDpiFunc;
static _SetThreadDpiAwarenessContextFunc _setThreadDpiAwarenessContext = FallbackSetThreadDpiAwarenessContext;

// =============================================
//
// Helpers
//
// ===================
static UINT FallbackGetDpiForWindow(HWND hwnd)
{
return 96;
}

static DPI_AWARENESS_CONTEXT FallbackSetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT dpiContext)
{
return nullptr;
}

static int FallbackGetSystemMetricsForDpiFunc(int nIndex, UINT dpi)
{
return GetSystemMetrics(nIndex);
}

static void SetFallbacks()
{
_getDpiForWindow = FallbackGetDpiForWindow;
_getSystemMetricsForDpi = FallbackGetSystemMetricsForDpiFunc;
_setThreadDpiAwarenessContext = FallbackSetThreadDpiAwarenessContext;
}

// =============================================
//
// Exports
//
// ===================
void CloseDpiHelper()
{
SetFallbacks();

if (s_user32 != nullptr)
{
FreeLibrary(s_user32);
s_user32 = nullptr;
}
}

int GetScreenHeight(int dpi)
{
return _getSystemMetricsForDpi(SM_CYSCREEN, dpi);
}

int GetScreenWidth(int dpi)
{
return _getSystemMetricsForDpi(SM_CXSCREEN, dpi);
}

unsigned int GetWindowDpi(HWND hwnd)
{
return _getDpiForWindow(hwnd);
}

void InitDpiHelper()
{
SetFallbacks();

s_user32 = LoadLibrary(L"User32.dll");
if (s_user32 == nullptr)
{
return;
}

auto win10GetDpiForWindow = (_GetDpiForWindowFunc)GetProcAddress(s_user32, "GetDpiForWindow");
if (win10GetDpiForWindow != nullptr)
{
_getDpiForWindow = win10GetDpiForWindow;
}

auto win10GetSystemMetricsForDpi = (_GetSystemMetricsForDpiFunc)GetProcAddress(s_user32, "GetSystemMetricsForDpi");
if (win10GetSystemMetricsForDpi != nullptr)
{
_getSystemMetricsForDpi = win10GetSystemMetricsForDpi;
}

auto win10SetThreadDpiAwarenessContext = (_SetThreadDpiAwarenessContextFunc)GetProcAddress(s_user32, "SetThreadDpiAwarenessContext");
if (win10SetThreadDpiAwarenessContext != nullptr)
{
auto oldVal = win10SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE);
if (oldVal == nullptr)
{
CloseDpiHelper(); // failed init of the win10+ apis
return;
}

_setThreadDpiAwarenessContext = win10SetThreadDpiAwarenessContext;
}
}
9 changes: 9 additions & 0 deletions Photino.Native/Photino.Windows.DpiHelp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <Windows.h>

void CloseDpiHelper();
int GetScreenHeight(int dpi);
int GetScreenWidth(int dpi);
unsigned int GetWindowDpi(HWND hwnd);
void InitDpiHelper();
22 changes: 22 additions & 0 deletions Photino.Native/Photino.Windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

#include "Photino.Windows.DarkMode.h"

#ifdef COMPAT_DPI
#include "Photino.Windows.DpiHelp.h"
#endif

#pragma comment(lib, "Urlmon.lib")
#pragma warning(disable: 4996) //disable warning about wcscpy vs. wcscpy_s

Expand Down Expand Up @@ -65,7 +69,11 @@ void Photino::Register(HINSTANCE hInstance)

RegisterClassEx(&wcx);

#ifdef COMPAT_DPI
InitDpiHelper();
#else
SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE);
#endif
}


Expand Down Expand Up @@ -232,6 +240,10 @@ Photino::Photino(PhotinoInitParams* initParams)

Photino::~Photino()
{
#ifdef COMPAT_DPI
CloseDpiHelper();
#endif

if (_startUrl != NULL) delete[]_startUrl;
if (_startString != NULL) delete[]_startString;
if (_temporaryFilesPath != NULL) delete[]_temporaryFilesPath;
Expand Down Expand Up @@ -370,9 +382,15 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)

void Photino::Center()
{
#ifdef COMPAT_DPI
int screenDpi = GetWindowDpi(_hWnd);
int screenHeight = GetScreenHeight(screenDpi);
int screenWidth = GetScreenWidth(screenDpi);
#else
int screenDpi = GetDpiForWindow(_hWnd);
int screenHeight = GetSystemMetricsForDpi(SM_CYSCREEN, screenDpi);
int screenWidth = GetSystemMetricsForDpi(SM_CXSCREEN, screenDpi);
#endif

RECT windowRect = {};
GetWindowRect(_hWnd, &windowRect);
Expand Down Expand Up @@ -454,7 +472,11 @@ void Photino::GetResizable(bool* resizable)

unsigned int Photino::GetScreenDpi()
{
#ifdef COMPAT_DPI
return GetWindowDpi(_hWnd);
#else
return GetDpiForWindow(_hWnd);
#endif
}

void Photino::GetSize(int* width, int* height)
Expand Down
8 changes: 8 additions & 0 deletions Photino.Native/Photino.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
#pragma once

#ifdef _WIN32

#define COMPAT_DPI

#ifdef COMPAT_DPI
#define WINVER _WIN32_WINNT_WIN7
#define _WIN32_WINNT _WIN32_WINNT_WIN7
#endif

#include <Windows.h>
#include <wil/com.h>
#include <WebView2.h>
Expand Down