如何让一个窗体不能移动啊 ( 积分: 50 )

  • 主题发起人 主题发起人 jinghj
  • 开始时间 开始时间
J

jinghj

Unregistered / Unconfirmed
GUEST, unregistred user!
想让一个窗体在固定位置不能移动怎么处理啊
 
想让一个窗体在固定位置不能移动怎么处理啊
 
设置Form的BorderStyle=bsNone
不过,这样的话,就没了窗口标题栏。

要不然就拦截移动消息,固定窗口位置
 
拦截WM_MOVE消息即可
TForm1 = class(TForm)
private
procedure WMMove(var Msg: TMessage)
message WM_MOVE;
....
end;

procedure TForm1.WMMove(var Msg: TMessage);
begin
Msg.Result := 0;
end;
 
这个在DELPHI7编译成功。给分吧。分不好赚啊。
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;

type
TForm1 = class(TForm)
private
{ Private declarations }
public
procedure onPosChange(var Msg:TWMWINDOWPOSCHANGING);MESSAGE WM_WINDOWPOSCHANGING;
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
procedure TForm1.onPosChange(var Msg:TWMWINDOWPOSCHANGING);
begin
msg.WindowPos.x:=left;
msg.WindowPos.y :=top;
msg.Result:=0;
end;
end.
 
1.把Form的BorderIcons下的几个子属性值全改为False;
  2.修改Form的BorderStyle的值为bsSingle;
  3.为了让窗口不能移动,可以自已拦下WM—NCHITTEST消息,对该消息的处理为:一概回应鼠标点在窗口的Client区域, 相信这个视窗就不会动了。
  下面是一个例子, 请参考:
  unit Unit1;
  interface
  uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls,Forms, Dialogs, StdCtrls;
  type
  TForm1 = class(TForm)
  Button1: TButton;
  procedure Button1Click(Sender: TObject);
  private
  { Private declarations }
  procedure WMNCHitTest(var Msg: TMessage)
message WM—NCHITTEST;
  public
  { Public declarations }
  end;
  var Form1: TForm1;
  implementation {$R *.DFM}
  procedure TForm1.Button1Click(Sender: TObject);
  begin
  Close

  unit Dragmain;
  interface
  uses
   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
   Forms, Dialogs, StdCtrls;
  type
   TForm1 = class(TForm)
  Button1: TButton;
  procedure Button1Click(Sender: TObject);
   private
    procedure WMNCHitTest(var M: TWMNCHitTest)
message wm—NCHitTest;
   end;
  var Form1: TForm1;
  implementation {$R *.DFM}
  procedure TForm1.WMNCHitTest(var M: TWMNCHitTest);
  begin
   inherited;    
  { call the inherited message handler }
   if M.Result = htClient then
  { is the click in the client area? }
    M.Result := htCaption;  
   { if so, make Windows think it′s   }
      { on the caption bar.  }
  end;
  procedure TForm1.Button1Click(Sender: TObject);
  begin
   Close;
  end

  end.
  { 下面是这个窗体的设置}
  object Form1: TForm1
   Left = 203
   Top = 94
   BorderIcons = []
   BorderStyle = bsNone
   ClientHeight = 273
   ClientWidth = 427
   Font.Color = clWindowText
   Font.Height = -13
   Font.Name = ′System′
   Font.Style = []
   PixelsPerInch = 96
   TextHeight = 16
   object Button1: TButton
    Left = 160
    Top = 104
    Width = 89
    Height = 33
    Caption = ′Close′
    TabOrder = 0
    OnClick = Button1Click
   end
  end
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
TForm1 = class(TForm)
private
{ Private declarations }
PROCEDURE DONMOVEWINDOW(VAR MSG:TMESSAGE)
MESSAGE WM_NCHITTEST;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}


PROCEDURE TFORM1.DONMOVEWINDOW(VAR MSG:TMESSAGE);
BEGIN
INHERITED;
MSG.Result:=HTCLIENT;
END;

end.
 
后退
顶部