Mega Code Archive

 
Categories / Delphi / Forms
 

Having focus on a window not executing

Title: Having focus on a window not executing Question: Have you ever needed a small window with a progressbar with focus, but having the code being executed in another window? I use this trick for our client/server application when retrieving data from the server. The connection is non-interruptable, and the user is not allowed to click anywhere while the connection is open. This small trick prohibits that, and tells the user about the process of retrieving his data. Answer: The receipe is like this: Create a small sized window with a TProgressBar inside. Name the TProgressBar "ProgressBar". Make an instance of the window. For example: var f : TfrmProgress; initialization f := nil; end. To open the window, use the following lines of code: procedure OpenWindow; begin if f = nil then begin f := TfrmProgress.Create( Application ); f.WindowList := DisableTaskWindows(f.Handle); f.Show; end; end; The window is now open and modal, but your thread of code continues where you left off. You can set the progressbar by: procedure SetProgressBar( APosition : integer ); begin if f nil then f.ProgressBar.Position := APosition; end; To close the window, do the following: procedure CloseWindow; begin if f nil then begin EnableTaskWindows(f.WindowList); f.Close; f.Free; f := nil; end; end; Usage: Use the code like this: begin OpenWindow; SetProgressBar( 33 ); do_something; SetProgressBar( 66 ); // Increase the progressbar to indicate // progress do_something_else; SetProgressBar( 100 ); CloseWindow; end;