1. Home
  2. Computing & Technology
  3. Delphi Programming

Shake a window (form) from Delphi code

By Zarko Gajic, About.com

Whether you want to shake your Delphi application forms just for fun or to attract a user attention it's up to you :)
Here's a simple code to shake a window provided a window's handle:

~~~~~~~~~~~~~~~~~~~~~~~~~
procedure WindowShake(wHandle: THandle) ;
const
   MAXDELTA = 4;
   SHAKETIMES = 500;
var
   oRect, wRect :TRect;
   deltax : integer;
   deltay : integer;
   cnt : integer;
   dx, dy : integer;
begin
   //remember original position
   GetWindowRect(wHandle,wRect) ;
   oRect := wRect;

   Randomize;
   for cnt := 0 to SHAKETIMES do
   begin
     deltax := Round(Random(MAXDELTA)) ;
     deltay := Round(Random(MAXDELTA)) ;
     dx := Round(1 + Random(2)) ;
     if dx = 2 then dx := -1;
     dy := Round(1 + Random(2)) ;
     if dy = 2 then dy := -1;
     OffsetRect(wRect,dx * deltax, dy * deltay) ;
     MoveWindow(wHandle, wRect.Left,wRect.Top,wRect.Right - wRect.Left,wRect.Bottom - wRect.Top,true) ;
   end;
   //return to start position
   MoveWindow(wHandle, oRect.Left,oRect.Top,oRect.Right - oRect.Left,oRect.Bottom - oRect.Top,true) ;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~

Usage: WindowShake(Application.MainForm.Handle) ; will shake the main form of your application.
Just for fun: go over all opened applications, and shake ("nudge 'em") a random window ;))

Delphi tips navigator:
» UDP Makes a Difference (UDP vs. TCP)
« Place a TEdit inside a TListBox to enable inplace editing of ListBox items

More Delphi Programming Quick Tips
Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. 2005 Delphi Tips
  7. How to create a shaking window using Delphi code

©2009 About.com, a part of The New York Times Company.

All rights reserved.