How can I resize forms independent of screen resolution?

How can I resize forms independent of screen resolution?


The following code comes to us via Peter Jagielski, a formerBorland engineer, and myself. Peteris responsible for the SetSize procedure, which does most of thework. I put the code in a component wrapper so all you have to dois drop the component onto any form, call the component’s execute procedurein FormCreate, and voila! an instantly sized form that fits nicely withinyour screen, above the Windows 95 Taskbar.

For Non-Windows 95 users, yourform will be sized to maximized, but will be still be FormStyle :=wsNormal. Select the text below, put it in afile, save the file as SizeTask.pas, add into your component library, dropit into a form, and watch it go!

unit Sizetask;interfaceuses  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,  Forms, Dialogs;type  TSizer = class(TComponent)  private    { Private declarations }  protected    { Protected declarations }    procedure SetSize(MyForm: TForm);  public    { Public declarations }    procedure   Execute;  end;procedure Register;implementationprocedure TSizer.Execute;begin  SetSize(TForm(Owner));end;procedure TSizer.SetSize(MyForm: TForm);var  TaskBarHandle: HWnd;    { Handle to the Win95 Taskbar }  TaskBarCoord:  TRect;   { Coordinates of the Win95 Taskbar }  CxScreen,               { Width of screen in pixels }  CyScreen,               { Height of screen in pixels }  CxFullScreen,           { Width of client area in pixels }  CyFullScreen,           { Height of client area in pixels }  CyCaption:     Integer; { Height of a window's title bar in pixels }begin  TaskBarHandle := FindWindow('Shell_TrayWnd',Nil); { Get Win95 Taskbar handle }  if TaskBarHandle = 0 then { We're running Win 3.x or WinNT w/o Win95shell, so just maximize }    MyForm.WindowState := wsMaximized  else { We're running Win95 or WinNT w/Win95 shell }    begin      MyForm.WindowState := wsNormal;      GetWindowRect(TaskBarHandle,TaskBarCoord);      { Get coordinates ofWin95 Taskbar }      CxScreen      := GetSystemMetrics(SM_CXSCREEN); { Get various screendimensions and set form's width/height }      CyScreen      := GetSystemMetrics(SM_CYSCREEN);      CxFullScreen  := GetSystemMetrics(SM_CXFULLSCREEN);      CyFullScreen  := GetSystemMetrics(SM_CYFULLSCREEN);      CyCaption     := GetSystemMetrics(SM_CYCAPTION);      MyForm.Width  := CxScreen - (CxScreen - CxFullScreen) + 1;      MyForm.Height := CyScreen - (CyScreen - CyFullScreen) + CyCaption + 1;      MyForm.Top    := 0;      MyForm.Left   := 0;      if (TaskBarCoord.Top = -2) and (TaskBarCoord.Left = -2) then {Taskbar on either top or left }        if TaskBarCoord.Right > TaskBarCoord.Bottom then { Taskbar on top }          MyForm.Top  := TaskBarCoord.Bottom        else { Taskbar on left }          MyForm.Left := TaskBarCoord.Right;    end;end;procedure Register;begin  RegisterComponents('Samples', [TSizer]);end;end.
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


Recent Articles: