Author: Jonas Bilinkevicius
How to disable form movement when the screen resolution exceeds 800x600 pixel
Answer:
Handle the WM_NCHittest and change the HTCaption to HTNowhere. An example, which
doesn't allow a form to be moved if the screen resolution is larger than 800x600
pixel:
1 unit Unit1;
2
3 interface
4
5 uses
6 SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
7 Forms, Dialogs, StdCtrls, Menus;
8
9 type
10 TForm1 = class(TForm)
11 procedure Exit1Click(Sender: TObject);
12 procedure FormShow(Sender: TObject);
13 private
14 { Private declarations }
15 public
16 { Public declarations }
17 procedure DontMove(var Msg: TMessage); message WM_NCHITTEST;
18 procedure DeleteItemsFromSysMenu;
19 end;
20
21 var
22 Form1: TForm1;
23
24 implementation
25
26 {$R *.DFM}
27
28 procedure TForm1.DeleteItemsFromSysMenu;
29 var
30 SysMenuHwnd: THandle;
31 i: integer;
32 begin
33 SysMenuHwnd := GetSystemMenu(Form1.Handle, False);
34 {Have to be done in reverse order because if not the numbering would be
35 different each time the function is called}
36 for i := 6 downto 0 do
37 DeleteMenu(SysMenuHwnd, i, MF_BYPOSITION);
38 end;
39
40 procedure TForm1.DontMove(var Msg: TMessage);
41 var
42 bAllowMove: Boolean;
43 begin
44 bAllowMove := (Screen.Width >= 800);
45 inherited;
46 if (Msg.Result <> htReduce) and (Msg.Result <> htClose) and (Msg.Result <>
47 htSysMenu)
48 then
49 if (Msg.Result = htCaption) and (not bAllowMove) then
50 Msg.Result := htNowhere;
51 end;
52
53 procedure TForm1.Exit1Click(Sender: TObject);
54 begin
55 Close;
56 end;
57
58 procedure TForm1.FormShow(Sender: TObject);
59 begin
60 DeleteItemsFromSysMenu;
61 end;
62
63 end.
|