Alexandre Julliard : user32: Scale initial window dimensions in CreateWindow() based on DPI awareness.

Alexandre Julliard julliard at winehq.org
Thu Aug 23 16:37:03 CDT 2018


Module: wine
Branch: master
Commit: 110443acef21dcc4639eb34ae5738444fb271562
URL:    https://source.winehq.org/git/wine.git/?a=commit;h=110443acef21dcc4639eb34ae5738444fb271562

Author: Alexandre Julliard <julliard at winehq.org>
Date:   Thu Aug 23 12:13:02 2018 +0200

user32: Scale initial window dimensions in CreateWindow() based on DPI awareness.

Signed-off-by: Alexandre Julliard <julliard at winehq.org>

---

 dlls/user32/sysparams.c | 13 +++++++++++++
 dlls/user32/win.c       | 44 +++++++++++++++++++++++++++++++++++++++-----
 dlls/user32/win.h       |  1 +
 3 files changed, 53 insertions(+), 5 deletions(-)

diff --git a/dlls/user32/sysparams.c b/dlls/user32/sysparams.c
index 149b483..10f90a2 100644
--- a/dlls/user32/sysparams.c
+++ b/dlls/user32/sysparams.c
@@ -3209,6 +3209,19 @@ UINT get_win_monitor_dpi( HWND hwnd )
 }
 
 /**********************************************************************
+ *              get_thread_dpi
+ */
+UINT get_thread_dpi(void)
+{
+    switch (GetAwarenessFromDpiAwarenessContext( GetThreadDpiAwarenessContext() ))
+    {
+    case DPI_AWARENESS_UNAWARE:      return USER_DEFAULT_SCREEN_DPI;
+    case DPI_AWARENESS_SYSTEM_AWARE: return system_dpi;
+    default:                         return 0;  /* no scaling */
+    }
+}
+
+/**********************************************************************
  *              SetProcessDpiAwarenessContext   (USER32.@)
  */
 BOOL WINAPI SetProcessDpiAwarenessContext( DPI_AWARENESS_CONTEXT context )
diff --git a/dlls/user32/win.c b/dlls/user32/win.c
index cbfd8bb..c7a3b06 100644
--- a/dlls/user32/win.c
+++ b/dlls/user32/win.c
@@ -1305,6 +1305,25 @@ static void dump_window_styles( DWORD style, DWORD exstyle )
 #undef DUMPED_EX_STYLES
 }
 
+/***********************************************************************
+ *           map_dpi_create_struct
+ */
+static void map_dpi_create_struct( CREATESTRUCTW *cs, UINT dpi_from, UINT dpi_to )
+{
+    if (!dpi_from && !dpi_to) return;
+    if (!dpi_from || !dpi_to)
+    {
+        POINT pt = { cs->x, cs->y };
+        UINT mon_dpi = get_monitor_dpi( MonitorFromPoint( pt, MONITOR_DEFAULTTONEAREST ));
+        if (!dpi_from) dpi_from = mon_dpi;
+        else dpi_to = mon_dpi;
+    }
+    if (dpi_from == dpi_to) return;
+    cs->x = MulDiv( cs->x, dpi_to, dpi_from );
+    cs->y = MulDiv( cs->y, dpi_to, dpi_from );
+    cs->cx = MulDiv( cs->cx, dpi_to, dpi_from );
+    cs->cy = MulDiv( cs->cy, dpi_to, dpi_from );
+}
 
 /***********************************************************************
  *           WIN_CreateWindowEx
@@ -1319,6 +1338,8 @@ HWND WIN_CreateWindowEx( CREATESTRUCTW *cs, LPCWSTR className, HINSTANCE module,
     WND *wndPtr;
     HWND hwnd, parent, owner, top_child = 0;
     const WCHAR *p = className;
+    UINT win_dpi, thread_dpi = get_thread_dpi();
+    DPI_AWARENESS_CONTEXT context;
     MDICREATESTRUCTW mdi_cs;
     CBT_CREATEWNDW cbtc;
     CREATESTRUCTW cbcs;
@@ -1550,6 +1571,14 @@ HWND WIN_CreateWindowEx( CREATESTRUCTW *cs, LPCWSTR className, HINSTANCE module,
     }
     else SetWindowLongPtrW( hwnd, GWLP_ID, (ULONG_PTR)cs->hMenu );
 
+    style = wndPtr->dwStyle;
+    win_dpi = wndPtr->dpi;
+    WIN_ReleasePtr( wndPtr );
+
+    if (parent) map_dpi_create_struct( cs, thread_dpi, win_dpi );
+
+    context = SetThreadDpiAwarenessContext( GetWindowDpiAwarenessContext( hwnd ));
+
     /* call the WH_CBT hook */
 
     /* the window style passed to the hook must be the real window style,
@@ -1557,10 +1586,9 @@ HWND WIN_CreateWindowEx( CREATESTRUCTW *cs, LPCWSTR className, HINSTANCE module,
      * passed in, so we have to copy the original CREATESTRUCT and get the
      * the real style. */
     cbcs = *cs;
-    cbcs.style = wndPtr->dwStyle;
+    cbcs.style = style;
     cbtc.lpcs = &cbcs;
     cbtc.hwndInsertAfter = HWND_TOP;
-    WIN_ReleasePtr( wndPtr );
     if (HOOK_CallHooks( WH_CBT, HCBT_CREATEWND, (WPARAM)hwnd, (LPARAM)&cbtc, unicode )) goto failed;
 
     /* send the WM_GETMINMAXINFO message and fix the size if needed */
@@ -1584,7 +1612,7 @@ HWND WIN_CreateWindowEx( CREATESTRUCTW *cs, LPCWSTR className, HINSTANCE module,
 
     /* send WM_NCCREATE */
 
-    TRACE( "hwnd %p cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cx, cy );
+    TRACE( "hwnd %p cs %d,%d %dx%d %s\n", hwnd, cs->x, cs->y, cs->cx, cs->cy, wine_dbgstr_rect(&rect) );
     if (unicode)
         result = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
     else
@@ -1618,7 +1646,7 @@ HWND WIN_CreateWindowEx( CREATESTRUCTW *cs, LPCWSTR className, HINSTANCE module,
         MapWindowPoints( 0, parent, (POINT *)&client_rect, 2 );
         set_window_pos( hwnd, insert_after, SWP_NOACTIVATE, &rect, &client_rect, NULL );
     }
-    else return 0;
+    else goto failed;
 
     /* send WM_CREATE */
 
@@ -1662,7 +1690,11 @@ HWND WIN_CreateWindowEx( CREATESTRUCTW *cs, LPCWSTR className, HINSTANCE module,
     /* Notify the parent window only */
 
     send_parent_notify( hwnd, WM_CREATE );
-    if (!IsWindow( hwnd )) return 0;
+    if (!IsWindow( hwnd ))
+    {
+        SetThreadDpiAwarenessContext( context );
+        return 0;
+    }
 
     if (parent == GetDesktopWindow())
         PostMessageW( parent, WM_PARENTNOTIFY, WM_CREATE, (LPARAM)hwnd );
@@ -1689,10 +1721,12 @@ HWND WIN_CreateWindowEx( CREATESTRUCTW *cs, LPCWSTR className, HINSTANCE module,
         HOOK_CallHooks( WH_SHELL, HSHELL_WINDOWCREATED, (WPARAM)hwnd, 0, TRUE );
 
     TRACE("created window %p\n", hwnd);
+    SetThreadDpiAwarenessContext( context );
     return hwnd;
 
 failed:
     WIN_DestroyWindow( hwnd );
+    SetThreadDpiAwarenessContext( context );
     return 0;
 }
 
diff --git a/dlls/user32/win.h b/dlls/user32/win.h
index b6c162a..f9e11ff 100644
--- a/dlls/user32/win.h
+++ b/dlls/user32/win.h
@@ -130,6 +130,7 @@ extern void WINPOS_SysCommandSizeMove( HWND hwnd, WPARAM wParam ) DECLSPEC_HIDDE
 
 extern UINT get_monitor_dpi( HMONITOR monitor ) DECLSPEC_HIDDEN;
 extern UINT get_win_monitor_dpi( HWND hwnd ) DECLSPEC_HIDDEN;
+extern UINT get_thread_dpi(void) DECLSPEC_HIDDEN;
 extern BOOL set_window_pos( HWND hwnd, HWND insert_after, UINT swp_flags,
                             const RECT *window_rect, const RECT *client_rect,
                             const RECT *valid_rects ) DECLSPEC_HIDDEN;




More information about the wine-cvs mailing list