Recent

Author Topic: How to lock or freeze a TForm in a location on the SCREEN?  (Read 13317 times)

Awesome Programmer

  • Sr. Member
  • ****
  • Posts: 451
  • Programming is FUN only when it works :)
    • Cool Technology
How to lock or freeze a TForm in a location on the SCREEN?
« on: April 24, 2014, 05:53:47 pm »
I have a TForm which I would like to lock or freeze it on a screen. So, no one can move it anywhere with a mouse. Can you do it through Callback function or procedure?
« Last Edit: April 24, 2014, 05:56:52 pm by reltek »

kpeters58

  • Sr. Member
  • ****
  • Posts: 267
Re: How to lock or freeze a TForm in a location on the SCREEN?
« Reply #1 on: April 24, 2014, 06:17:49 pm »
Look here:

http://delphidabbler.com/tips/187

And a general note for all of us:

If you cannot find an answer for 'Lazarus + whatever your problem is', also google for 'Delphi + ....' as this will more often than not help you out.
Lazarus 2.0.4/FPC 3.0.4/Win 64

Awesome Programmer

  • Sr. Member
  • ****
  • Posts: 451
  • Programming is FUN only when it works :)
    • Cool Technology
Re: How to lock or freeze a TForm in a location on the SCREEN?
« Reply #2 on: April 25, 2014, 04:47:57 pm »
 :'( I tried all of the solution from that website or webpage and those solution don't work at all.

Maybe I am not doing it right, even though I followed their instruction as written.

When I move the form, the callback procedure is never called....What am I doing wrong?

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: How to lock or freeze a TForm in a location on the SCREEN?
« Reply #3 on: April 25, 2014, 05:08:44 pm »
You could set your BorderStyle to bsNone.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: How to lock or freeze a TForm in a location on the SCREEN?
« Reply #4 on: April 25, 2014, 05:12:49 pm »
What OS are you using?
Post your code or a small sample application that did not work for you. I just tried the first solution posted on the link provided by kpeters58 and it works!

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: How to lock or freeze a TForm in a location on the SCREEN?
« Reply #5 on: April 25, 2014, 05:19:47 pm »
You could set your BorderStyle to bsNone.
On Windows at least, you can still move it using context menu.

Awesome Programmer

  • Sr. Member
  • ****
  • Posts: 451
  • Programming is FUN only when it works :)
    • Cool Technology
Re: How to lock or freeze a TForm in a location on the SCREEN?
« Reply #6 on: April 25, 2014, 05:35:18 pm »


Code: [Select]
private
procedure PosChange(var Msg: TLmWindowPosChanging); message LM_WINDOWPOSCHANGING;


procedure TForm1.PosChange(var Msg: TLmWindowPosChanging) ;
begin
  Msg.WindowPos.x := Left;
  Msg.WindowPos.y := Top;
  Msg.Result := 0;
end;


The above did not work for me. I am using Lazarus 1.0.12 and FPC 2.6.2 on Linux OS. In fact, the CALLBACK is never called when I move the Form. So, I don't understand why and I hoping someone could help me in that regard.

Thanks,

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: How to lock or freeze a TForm in a location on the SCREEN?
« Reply #7 on: April 25, 2014, 06:09:40 pm »
I'm afraid all the methods mentioned in kpeters58's link are for Windows. A quick search for "WindowPosChanging" in GTK2 and QT interfaces comes up with an empty result.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: How to lock or freeze a TForm in a location on the SCREEN?
« Reply #8 on: April 25, 2014, 06:42:05 pm »
If you don't mind a snap back effect, GTK2 interface supports LM_WINDOWPOSCHANGED (supposedly sent after the change):
Code: [Select]
{$mode delphi}...

uses
..., LMessages;

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    PrvLeft, PrvTop: Integer;
    procedure PosChanged(var Msg: TLMWindowPosChanged);
    message LM_WINDOWPOSCHANGED;
  public
    procedure FixIt(Data: PtrInt);
  end;

...

procedure TForm1.FormCreate(Sender: TObject);
begin
  PrvLeft := Left;
  PrvTop := Top;
end;

procedure TForm1.PosChanged(var Msg: TLMWindowPosChanged);
begin
  inherited;
  Application.QueueAsyncCall(FixIt, 0);
end;

procedure TForm1.FixIt(Data: PtrInt);
begin
  Left := PrvLeft;
  Top := PrvTop;
end;

Awesome Programmer

  • Sr. Member
  • ****
  • Posts: 451
  • Programming is FUN only when it works :)
    • Cool Technology
Re: How to lock or freeze a TForm in a location on the SCREEN?
« Reply #9 on: April 25, 2014, 08:51:35 pm »
 :)

I did something similar to that and it did work just like you described it. It allows you to move the TForm, but when you let it go, it snaps back to its original location.

But I don't want to even give the user ability to move the form.

sam707

  • Guest
Re: How to lock or freeze a TForm in a location on the SCREEN?
« Reply #10 on: April 26, 2014, 12:31:24 am »
I did it :) because I also needed that in one of my projects!

my solution is platform independant and accurate responsive.

there's an event handler wich is triggered when a form is moved :

OnChangeBounds

double-click on it at design time, in the events tab of object inspector...

as my form is screen centered and i set Position to poScreenCenter I wrote :

procedure TMainForm.FormChangeBounds(Sender: TObject);
begin
  MoveToDefaultPosition;
end;

and each time you try to move it, the form comes back to the center of the screen.

If you need a special position you just can do something like

procedure TMainForm.FormChangeBounds(Sender: TObject);
begin
  BeginUpdateBounds;
  Left := 120;
  Top := 80;
  EndUpdateBounds;
end;
« Last Edit: April 26, 2014, 12:40:35 am by sam707 »

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: How to lock or freeze a TForm in a location on the SCREEN?
« Reply #11 on: April 26, 2014, 01:05:09 am »
@sam707, that's a cleaner solution.

Welcome back.  :)

sam707

  • Guest
Re: How to lock or freeze a TForm in a location on the SCREEN?
« Reply #12 on: April 26, 2014, 01:06:58 am »
Zorro Coder was here  :D

thanks engkin :) you are welcome

Awesome Programmer

  • Sr. Member
  • ****
  • Posts: 451
  • Programming is FUN only when it works :)
    • Cool Technology
Re: How to lock or freeze a TForm in a location on the SCREEN?
« Reply #13 on: April 28, 2014, 03:36:03 pm »
I did it :) because I also needed that in one of my projects!

my solution is platform independant and accurate responsive.

there's an event handler wich is triggered when a form is moved :

OnChangeBounds

double-click on it at design time, in the events tab of object inspector...

as my form is screen centered and i set Position to poScreenCenter I wrote :

procedure TMainForm.FormChangeBounds(Sender: TObject);
begin
  MoveToDefaultPosition;
end;

and each time you try to move it, the form comes back to the center of the screen.

If you need a special position you just can do something like

procedure TMainForm.FormChangeBounds(Sender: TObject);
begin
  BeginUpdateBounds;
  Left := 120;
  Top := 80;
  EndUpdateBounds;
end;

I did exactly as you described except for BeginUpdateBounds and EndUpdateBounds. Without these two procedures, it works by snapping back to its original location after the user is allowed to drag the TForm. With these two procedures, TForm does nothing right after dragging it from one location to another.

However, when I do set the borderstyle to bsNone, the TForm border completely disappears. Thus, you can't move or resize the TForm. It just that the TForm looks weird without the border.

 :D
I am using Lazarus 1.0.12 and fpc 2.6.2 on Linux operating system.
« Last Edit: April 28, 2014, 04:03:04 pm by reltek »

Awesome Programmer

  • Sr. Member
  • ****
  • Posts: 451
  • Programming is FUN only when it works :)
    • Cool Technology
Re: How to lock or freeze a TForm in a location on the SCREEN?
« Reply #14 on: April 29, 2014, 03:54:27 pm »
This site is a website that deals with the story of many people.
Visitors will get good information.

 %) %) %) %) %) %) %) %) %)

 

TinyPortal © 2005-2018