Skip to content

Commit

Permalink
Fix maximized appearance on Windows 10/11
Browse files Browse the repository at this point in the history
  • Loading branch information
ppescher committed Sep 2, 2024
1 parent cf3e201 commit c7e8399
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 11 deletions.
78 changes: 67 additions & 11 deletions ResizableDialog/DemoDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,7 @@ BOOL CDemoDlg::OnInitDialog()
_T("Isn't it cool?"));

// min/max size settings

// get desktop size
CRect rc;
GetDesktopWindow()->GetClientRect(&rc);

// set max tracking size to half a screen
SetMaxTrackSize(CSize(rc.Width(), rc.Height()/2));

// maximized position and size on top of the screen
rc.bottom = 100;
SetMaximizedRect(rc);
UpdateMaxSize();

// save/restore
// (for dialog based app, default is a .INI file with
Expand All @@ -119,18 +109,70 @@ BOOL CDemoDlg::OnInitDialog()
return FALSE; // return TRUE unless you set the focus to a control
}

void CDemoDlg::UpdateMaxSize()
{
ResetMaxTrackSize();
ResetMaximizedRect();

// get desktop size
CRect rcMax;
GetDesktopWindow()->GetClientRect(&rcMax);

if (GetThemeAppProperties() & STAP_ALLOW_NONCLIENT)
{
// modern style windows use the borders for drop-shadow effects but window size appears smaller when maximized
// try to determine the correct size for maximized state, based on the current style
CRect rcWnd, rcClient, rcMargins;
GetWindowRect(&rcWnd);
GetClientRect(&rcClient);
::MapWindowPoints(GetSafeHwnd(), NULL, (LPPOINT)&rcClient, 2);

rcMargins.left = rcClient.left - rcWnd.left;
rcMargins.top = rcClient.top - rcWnd.top - GetSystemMetrics(GetExStyle() & WS_EX_TOOLWINDOW ? SM_CYSMCAPTION : SM_CYCAPTION);
rcMargins.right = rcWnd.right - rcClient.right;
rcMargins.bottom = rcWnd.bottom - rcClient.bottom;

rcMax.InflateRect(&rcMargins);
}
// use half height for tracking size limits
int half = rcMax.Height() / 2;
// clip maximized size to the top 100 pixels
rcMax.bottom = 100;

// maximized position and size on top of the screen
SetMaximizedRect(rcMax);

// limit max tracking size to half a screen vertically
SetMaxTrackSize(CSize(rcMax.Width(), half));

if (IsZoomed())
{
// window already maximized needs to be refreshed, but we try to avoid flickering
// when you disable redraw the window loses WS_VISIBLE but stay displayed on the screen
// so the only chance is to use SW_HIDE to change the maximized state without affecting visibility
// (any other show command would make the window appear again and flash, or it won't refresh the maximized state)
// then we maximize again, which implies the window will also be visible and then we restore redraw
SetRedraw(FALSE);
ShowWindow(SW_HIDE);
ShowWindow(SW_SHOWMAXIMIZED);
SetRedraw(TRUE);
}
}

void CDemoDlg::OnRadio1()
{
ModifyStyle(WS_THICKFRAME, 0, SWP_FRAMECHANGED | SWP_DRAWFRAME);
HideSizeGrip(&m_dwGripTempState);
UpdateSizeGrip();
UpdateMaxSize();
}

void CDemoDlg::OnRadio2()
{
ModifyStyle(0, WS_THICKFRAME, SWP_FRAMECHANGED | SWP_DRAWFRAME);
ShowSizeGrip(&m_dwGripTempState);
UpdateSizeGrip();
UpdateMaxSize();
}

BOOL CALLBACK CDemoDlg::SendThemeChangedProc(HWND hwnd, LPARAM /*lParam*/)
Expand All @@ -139,6 +181,20 @@ BOOL CALLBACK CDemoDlg::SendThemeChangedProc(HWND hwnd, LPARAM /*lParam*/)
return TRUE;
}

DWORD CDemoDlg::GetThemeProperties()
{
static HMODULE hThemeLib = GetModuleHandle(_T("UxTheme.dll"));
typedef DWORD(STDAPICALLTYPE* LPFNGETTHEMEAPPPROPERTIES)();
static LPFNGETTHEMEAPPPROPERTIES lpfnGetThemeAppProperties = (hThemeLib == NULL) ? NULL :
(LPFNGETTHEMEAPPPROPERTIES)GetProcAddress(hThemeLib, "GetThemeAppProperties");

if (lpfnGetThemeAppProperties == NULL)
return 0; // do nothing if no theme support

// return theme settings
return lpfnGetThemeAppProperties();
}

void CDemoDlg::SetThemeProperties(DWORD dwFlags)
{
static HMODULE hThemeLib = GetModuleHandle(_T("UxTheme.dll"));
Expand Down
2 changes: 2 additions & 0 deletions ResizableDialog/DemoDlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ class CDemoDlg : public CResizableDialog
HICON m_hIcon;

static BOOL CALLBACK SendThemeChangedProc(HWND hwnd, LPARAM lParam);
static DWORD GetThemeProperties();
void SetThemeProperties(DWORD dwFlags);
void UpdateMaxSize();

// Generated message map functions
//{{AFX_MSG(CDemoDlg)
Expand Down

0 comments on commit c7e8399

Please sign in to comment.