Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Close form by clicking somewhere else.

Status
Not open for further replies.

Nordlund

Programmer
Jul 17, 2001
458
SE
Hi there.
I want to close a topmost form by clicking elsewhere on the screen. How do I accomplish that.

I have seen examples using the procedure:
Code:
procedure WMLButtonDown(var Message: TWMLButtonDown); message WM_LBUTTONDOWN;
begin
  if not PtInRect(ClientRect, Point(message.XPos, message.YPos)) then
    Visible := false;
  inherited;
end;

The procedure is'nt triggered... But why?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
There is always another way to solve it, but I prefer my way.
 
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:

Code:
  ReleaseCapture;

This releases it immediately. (You must disable the timer first).

Hope this helps.

British
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top