There is a modal form in my Delphi 2007 application. I already have applied MinHeight,MaxHeight, MinWidth and MaxWidth constraints on the form.
If the screen resolution is below the Min/Max constraints, I want to re-size and re-position the form according to the screen resolution.
I have written the following code in OnCreate event handler function to handle the screen resolution.
procedure TfrmMyForm.FormCreate(Sender: TObject);
var
WorkArea: TRect;
iTitleHeight: Integer;
begin
inherited;
//-------------------------------------------------------------------
//Adjust Height/Width of the form if min/max values
//don't fall under the current resolution.
iTitleHeight := GetSystemMetrics(SM_CYCAPTION); //Height of titlebar
WorkArea := Screen.WorkAreaRect;
if(Self.Constraints.MinWidth > WorkArea.BottomRight.X) then
begin
if(Self.Constraints.MaxWidth > WorkArea.BottomRight.X) then
begin
Self.Constraints.MinWidth := WorkArea.BottomRight.X;
Self.Constraints.MaxWidth := WorkArea.BottomRight.X;
Self.Position := poDesigned;
SetBounds(0,0,WorkArea.BottomRight.X, WorkArea.BottomRight.Y - 5);
end
else
begin
Self.Constraints.MinWidth := WorkArea.BottomRight.X;
end;
end;
if(Self.Constraints.MinHeight > WorkArea.BottomRight.Y) then
begin
if(Self.Constraints.MaxHeight > WorkArea.BottomRight.Y) then
begin
Self.Constraints.MinHeight := WorkArea.BottomRight.Y - iTitleHeight;
Self.Constraints.MaxHeight := WorkArea.BottomRight.Y;
Self.Position := poDesigned;
SetBounds(0,0,WorkArea.BottomRight.X, WorkArea.BottomRight.Y - 5);
end
else
begin
Self.Constraints.MinHeight := WorkArea.BottomRight.Y;
end;
end;
//-------------------------------------------------------------------
end;
At design time I've set the Position property as follows
Position := poCentreScreen
Now there are three problems I am facing when the screen resolution is low like 1024x768 and Min & Max Height/Width constraint values are above this resolution.
I am changing the Position property to poDesigned because otherwise the form doesn't move to the position where I want it to be.
When I run my application on system have dual monitors it behaves unexpectedly.
I have also added a custom menu item in the system menu of the form and when I change the Position property to poDesigned in OnCreate function it resets the system menu and removes the custom menu item.
Is there any way to re-size and re-position the form without modifying the Position property?
Thanks in anticipation.