Mega Code Archive

 
Categories / Delphi / Ide Indy
 

How to create a shaking window using Delphi code

Title: How to create a shaking window using Delphi code 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 ;))