Archive

Posts Tagged ‘set’

How to Snap a Window to the Screen Edge

December 15th, 1999 m3Rlin No comments

WinAMP has this very useful feature. If you drag it to the edge of the screen it will automatically position itself. Sometimes you want to make room on the screen but you don’t want to minimize your program and again you want to see it in full. The only way is the put it in a corner and set it exactly to the screen border. Well, this can be quite a challenge :-)
But there’s no need to panic. All we have to do is intercept when the window is being moved and adjust it as needed.
This code adjusts the window within 30 pixels.

type
  Form1 = class(TForm)
    ...
    procedure WMExitSizeMove(var Message: TMessage); message WM_EXITSIZEMOVE;
...
procedure TForm1.WMExitSizeMove(var Message: TMessage);
var
  Pabd         : APPBARDATA;
  iScreenWidth,
  iScreenHeight: Integer;
  ScreenRect,
  TaskBarRect  : TRect;
begin
  if Message.Msg = WM_EXITSIZEMOVE then begin
    Pabd.cbSize := SizeOf(APPBARDATA);
    SHAppBarMessage(ABM_GETTASKBARPOS, Pabd);

    iScreenWidth := GetSystemMetrics(SM_CXSCREEN);
    iScreenHeight := GetSystemMetrics(SM_CYSCREEN);
    ScreenRect := Rect(0, 0, iScreenWidth, iScreenHeight);
    TaskBarRect := Pabd.rc;

    if (TaskBarRect.Left = -2) and (TaskBarRect.Bottom = iScreenHeight + 2) and (TaskBarRect.Right = iScreenWidth + 2) then
      ScreenRect.Bottom := TaskBarRect.Top
    else if (TaskBarRect.Top = -2) and (TaskBarRect.Left = -2) and (TaskBarRect.Right = iScreenWidth + 2) then
      ScreenRect.Top := TaskBarRect.Bottom
    else if (TaskBarRect.Left = -2) and (TaskBarRect.Top = -2) and (TaskBarRect.Bottom = iScreenHeight + 2) then
      ScreenRect.Left := TaskBarRect.Right
    else if (TaskBarRect.Right = iScreenWidth + 2) and (TaskBarRect.Top = -2) and (TaskBarRect.Bottom = iScreenHeight + 2) then
      ScreenRect.Right := TaskBarRect.Left;

    if Left > ScreenRect.Left + 30 then
      Left := ScreenRect.Left;
    if Top > ScreenRect.Top + 30 then
      Top := ScreenRect.Top;
    if Left + Width < ScreenRect.Right - 30 then
      Left := ScreenRect.Right - Width;
    if Top + Height < ScreenRect.Bottom - 30 then
      Top := ScreenRect.Bottom - Height;
  end;
  Message.Result := 1;
end;

How to Create a Splash Screen

December 15th, 1999 m3Rlin No comments

When you are creating a splash screen you probably want it to show up as soon as possible. I have seen source codes where the splash screen has been shown on the main form’s OnCreate event and closed on its OnShow event. Well, this is not acceptable. Why? Because the splash screen is only shown for a very short period of time. Well, some come up with the idea to put a TTimer on the splash screen… I don’t suggest making the user wait until the splash screen goes away while being able to see the program already “waiting” behind. Aslo, you want to free the splashform after closing it because there is no use of keeping it in the memory since anyway its not going to be used. The solution to this is quite simple. Create the splashform first and release it last. But how? If we set the splashform to be the first created form it automitaclly becomes the main form. Then we have problems. We can’t even close it, because this will close the application itself. Well, there is a way around to this. All we have to do is create it manually. Here is a sample code:

{ Project source file }
...
uses
  Splash; { The unit with the splash screen }
...
var
  SplashScreen: TSplashScreen; { In the Splash unit }
...
begin
  try
    SplashScreen := TSplashScreen.Create(Application);
    with SplashScreen do begin
      Show;
      Update; { To paint the splash screen }
    end;
    Application.CreateForm(TForm1, Form1);
    (* Create other forms or any other processing before the
    ** application is to be opened
    *)
   SplashScreen.Close;
  finally  { Make sure the splash screen gets released }
    SplashScreen.Free;
  end;
  Application.Run;
end.