有什么简单的方法禁止Form移动?(100分)

  • 主题发起人 主题发起人 DingDang
  • 开始时间 开始时间
D

DingDang

Unregistered / Unconfirmed
GUEST, unregistred user!
如题:
比较简单的方法设置使form不能移动!
 
hehe, 最简单, 没有title就可以了:-)
 
uses

SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls;


type

TForm1 = class(TForm)


private
procedure WMNCHitTest(var M: TWMNCHitTest);
message wm_NCHitTest;?[delphi]
end;


var
Form1: TForm1;


implementation

{$R *.DFM}

procedure TForm1.WMNCHitTest(var M: TWMNCHitTest);

begin

inherited;
{ call the inherited message handler }
if M.Result = htCaption then
{ is the click in the client area? }
M.Result := htClient;
{ if so, make Windows think it's }
{ on the caption bar. }
end;

 
borderstyle:=bsnone就可以.
 
to hubdog: 我帮你改了一下注释
procedure TForm1.WMNCHitTest(var M: TWMNCHitTest);

begin

inherited;
{ call the inherited message handler }
if M.Result = htCaption then
{ is the click in the CAPTION BAR? }
M.Result := htClient;
{ if so, make Windows think it's }
{ on the CLIENT AREA. }
end;
 
如果不想form 有输入焦点 ,那就设 form 的 enalble 为 false
 
想不到这种办法也有倒过来用的时候。 :)
 
补充CAKK:
自己再用SHAPER画个兰的标题栏,加个LABEL
——0 代码实现窗口无法移动:-)
cakk:你的resource编辑器到底是什么?
 
CJ:还惦记着我的宝贝哪?
礼拜一寄给你吧!放在公司了.
 
先谢了,有宝贝大家用嘛,呵呵,正好要用这个。
 
hubdog 的方法应该说比较可行的
但是,整个项目里所有的form都要这么做还是比较麻烦的
看来是没有什么更加好的办法了!
 
补充一点:截获WM_NCHITTEST消息后,
如果M.Result =HTSYSMENU 也需要屏蔽掉
 
很简单吗!只要把form的BorderStyle属性改为bsNone就可。
 
我写了个控件,你只要把他放在form上,form就不会到处乱动了,可以不用写代码改
了。
unit StaticForm;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TStaticForm = class(TComponent)
private
{ Private declarations }
OldWndProc,NewWndProc:Pointer;
protected
{ Protected declarations }
function FormHandle:THandle;
procedure NewWndMethod(var Msg:TMessage);
public
{ Public declarations }
constructor Create(AOwner:TComponent);override;
destructor Destroy;override;
published
{ Published declarations }
end;

procedure Register;
implementation
procedure Register;
begin
RegisterComponents('hubdog lib', [TStaticForm]);
end;

{ TStaticForm }
constructor TStaticForm.Create(AOwner: TComponent);
var
I:integer;
begin
for I:=0 to AOwner.ComponentCount-1do
if AOwner.Components is TStaticForm then
raise Exception.Create('only one instatnce can be put on Form');
inherited;
if (Owner=nil) or not (Aowner is TForm) then
raise Exception.Create('Owner must be a Form');
if not (csDesigning in ComponentState) then
begin
NewWndProc:=MakeObjectInstance(NewWndMethod);
OldWndProc:=Pointer(SetWindowLong(FormHandle,gwl_wndPRoc,Longint(NewWndPRoc)));
end
else
begin
NewWndPRoc:=nil;
OldWndProc:=nil;
end;
end;

destructor TStaticForm.Destroy;
begin
if Assigned(NewWndPRoc) then
begin
//SetWindowLong(FormHandle,gwl_WndPRoc,
//Longint(OldWndProc));
FreeObjectInstance(NewWndProc);
end;
inherited;
end;

function TStaticForm.FormHandle: THandle;
begin
Result:=(Owner as TForm).Handle;
end;

procedure TStaticForm.NewWndMethod(var Msg: TMessage);
begin
if Msg.Msg=wm_NCHitTest then
begin
Msg.Result:=CallWindowProc(oldWndProc,
FormHandle,Msg.Msg,Msg.wParam,Msg.lParam);
if Msg.Result=htCaption then
Msg.Result:=htClient;
end
else
Msg.Result:=CallWindowProc(oldWndProc,
FormHandle,Msg.Msg,Msg.wParam,Msg.lParam);
end;

end.
 
该BorderStyle比较好,
处理消息会对程序运行速度有所降低。
 
procedure TForm1.FormCreate(Sender: TObject);
var
menuh:hmenu;
i:integer;
begin
menuh:=GetSystemMenu(handle,false);
for i:=0 to 6 do
DeleteMenu(menuh,0,MF_BYPOSITION);
end;
 
UHBDOG说的对啊,截住wm_NCHitTest消息就可以了
 
是呀,是呀
把Form的BorderStyle属性改为bsNone
然后放一个TImage在那里
用printscreen => painter 伪装一个Border就可以了,不用太麻烦
要是想有max ,min ,close功能,在放上SpeedButton就可以了
^_^
 
form1:=Tform1.create(self);
deletemenu(getsystemmenu(form1.handle, false), SC_MOVE, MF_BYCOMMAND);
form1.show;
 
多人接受答案了。
 
后退
顶部