I tested this code out. It is not very efficient, but it'll work. You can play with it and find better ways to use it.
From the main form when you are showing the other form have code as follows:
Code:
procedure TForm1.Button1Click(Sender: TObject);
begin
SetCaptureControl(Form2);
//this sets the form as the window that
//will receive all the mouse messages
form2.timer1.enabled := true;
//i'll explain in the the code for it
form2.show;
//show the form you want to use
end;
From the code above the next form is shown.
You need to create a timer on the new form. I've just left it called timer1. Set it with the following properties:
Enabled = False
Interval = 100
For the code on the timer enter the following:
Code:
procedure TForm2.Timer1Timer(Sender: TObject);
begin
SetCaptureControl(Form2);
end;
This code sets the form as the window that will receive all the mouse messages. It has to be set again every time the timer runs as other windows and programs call for the control of the mouse. There probably is a better way to do this, but I do not have the time to try figure it out.
Next, click on the form and double click the event that says 'OnMouseDown'. This procedure intercepts settings about the mouse when it is clicked. Enter the code for the procedure as follows:
Code:
procedure TForm2.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
If Not ((x >= 0) and (x <= Form2.Width) And (y >= -30) And (y <= Form2.Height))
Then
begin
timer1.enabled := false;
Form2.Hide;
end;
end;
This code tests to see if the click was in the boundaries of the window. The top is at co-ordinate -30 because Delphi takes the co-ordinate 0 as just below the bar at the top of the window. The top of this is at approximately -30. The x and y values return the co-ordinates of the mouse in relation to the window. If the click is determined to not be on the window, the timer is disabled (this stops it intercepting all the mouse messages) and the form is hidden.
If at any time you want to stop the window intercepting the mouse messages you can enter the code:
This releases it immediately. (You must disable the timer first).
Hope this helps.
British