Skip to content

Commit

Permalink
Bug 1576: Failure when starting with window position configuration, s…
Browse files Browse the repository at this point in the history
…aved on lower DPI, that does not fit anymore on lower resolution caused by higher DPI

https://winscp.net/tracker/1576

Source commit: 53b884cc82e0d2c515188523a3ebcabe7862719e
  • Loading branch information
martinprikryl committed Jan 28, 2018
1 parent 87ee572 commit b0b9c55
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
24 changes: 18 additions & 6 deletions source/windows/Tools.cpp
Expand Up @@ -220,20 +220,32 @@ void __fastcall RestoreForm(UnicodeString Data, TForm * Form, bool PositionOnly)
(Bounds.Top < Monitor->Top) ||
(Bounds.Top > Monitor->Top + Monitor->WorkareaRect.Height() - 20)))
{
if (Monitor->Primary)
bool ExplicitPlacing = !Monitor->Primary;
if (!ExplicitPlacing)
{
TPosition Position;
if ((Application->MainForm == NULL) || (Application->MainForm == Form))
{
Form->Position = poDefaultPosOnly;
Position = poDefaultPosOnly;
}
else
{
Form->Position = poOwnerFormCenter;
Position = poOwnerFormCenter;
}

// If handle is allocated already, changing Position reallocates it, what brings lot of nasty side effects.
if (Form->HandleAllocated() && (Form->Position != Position))
{
ExplicitPlacing = true;
}
else
{
Form->Width = Bounds.Width();
Form->Height = Bounds.Height();
}
Form->Width = Bounds.Width();
Form->Height = Bounds.Height();
}
else

if (ExplicitPlacing)
{
// when positioning on non-primary monitor, we need
// to handle that ourselves, so place window to center
Expand Down
45 changes: 31 additions & 14 deletions source/windows/VCLCommon.cpp
Expand Up @@ -543,6 +543,26 @@ void __fastcall RecordFormImplicitRescale(TForm * Form)
}
}
//---------------------------------------------------------------------------
static void GetFormRescaleRatio(TForm * Form, int PixelsPerInch, int & M, int & D)
{
TFormRescaleComponent * FormRescaleComponent = GetFormRescaleComponent(Form);
TRatio ReverseRescaleKeyRatio(Form->PixelsPerInch, PixelsPerInch);
if (FormRescaleComponent->RatioMap.count(ReverseRescaleKeyRatio) > 0)
{
M = FormRescaleComponent->RatioMap[ReverseRescaleKeyRatio].second;
D = FormRescaleComponent->RatioMap[ReverseRescaleKeyRatio].first;
}
else
{
M = GetTextHeightAtPixelsPerInch(Form, PixelsPerInch);
D = GetTextHeightAtPixelsPerInch(Form, Form->PixelsPerInch);
}


TRatio RescaleKeyRatio(PixelsPerInch, Form->PixelsPerInch);
FormRescaleComponent->RatioMap[RescaleKeyRatio] = TRatio(M, D);
}
//---------------------------------------------------------------------------
static void __fastcall ChangeFormPixelsPerInch(TForm * Form, int PixelsPerInch)
{
RecordFormImplicitRescale(Form);
Expand All @@ -555,20 +575,7 @@ static void __fastcall ChangeFormPixelsPerInch(TForm * Form, int PixelsPerInch)
TAutoFlag RescalingFlag(FormRescaleComponent->Rescaling);

int M, D;
TRatio ReverseRescaleKeyRatio(Form->PixelsPerInch, PixelsPerInch);
if (FormRescaleComponent->RatioMap.count(ReverseRescaleKeyRatio) > 0)
{
M = FormRescaleComponent->RatioMap[ReverseRescaleKeyRatio].second;
D = FormRescaleComponent->RatioMap[ReverseRescaleKeyRatio].first;
}
else
{
M = GetTextHeightAtPixelsPerInch(Form, PixelsPerInch);
D = GetTextHeightAtPixelsPerInch(Form, Form->PixelsPerInch);
}

TRatio RescaleKeyRatio(PixelsPerInch, Form->PixelsPerInch);
FormRescaleComponent->RatioMap[RescaleKeyRatio] = TRatio(M, D);
GetFormRescaleRatio(Form, PixelsPerInch, M, D);

Form->PixelsPerInch = PixelsPerInch;
TPublicForm * PublicCustomForm = static_cast<TPublicForm *>(Form);
Expand Down Expand Up @@ -788,6 +795,16 @@ inline void __fastcall DoFormWindowProc(TCustomForm * Form, TWndMethod WndProc,
WndProc(Message);
}
}
else if (Message.Msg == WM_GETDPISCALEDSIZE)
{
WndProc(Message);
int M, D;
GetFormRescaleRatio(AForm, LOWORD(Message.WParam), M, D);
SIZE & Size = *(reinterpret_cast<SIZE *>(Message.LParam));
Size.cx = MulDiv(Size.cx, M, D);
Size.cy = MulDiv(Size.cy, M, D);
Message.Result = TRUE;
}
else if (Message.Msg == WM_DPICHANGED)
{
ChangeFormPixelsPerInch(AForm, LOWORD(Message.WParam));
Expand Down
1 change: 1 addition & 0 deletions source/windows/WinApi.h
Expand Up @@ -12,6 +12,7 @@ typedef BOOL WINAPI (* AddClipboardFormatListenerProc)(HWND hwnd);
typedef BOOL WINAPI (* RemoveClipboardFormatListenerProc)(HWND hwnd);
//---------------------------------------------------------------------------
#define WM_DPICHANGED 0x02E0
#define WM_GETDPISCALEDSIZE 0x02E4
//---------------------------------------------------------------------------
typedef enum _Monitor_DPI_Type {
MDT_Effective_DPI = 0,
Expand Down

0 comments on commit b0b9c55

Please sign in to comment.