1、在网看到的一段类似QQ的代码,好象是这里的一个版主(对不住没有记住)写的,因为本人是菜鸟刚开始学习,请高手给加上解释(有*号的行),特别是其中的许多变量等代表的意思,越详细分越多。
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
TForm1 = class(TForm)
Timer1: TTimer;
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
FAnchors: TAnchors;//*************
procedure WMMOVING(var Msg: TMessage); message WM_MOVING;//******
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}uses Math;procedure TForm1.WMMOVING(var Msg: TMessage);
begin
inherited;//**********
with PRect(Msg.LParam)^ do begin//*********
Left := Min(Max(0, Left), Screen.Width - Width);//**********
Top := Min(Max(0, Top), Screen.Height - Height);
Right := Min(Max(Width, Right), Screen.Width);
Bottom := Min(Max(Height, Bottom), Screen.Height);
FAnchors := [];//*******
if Left = 0 then Include(FAnchors, akLeft);//***
if Right = Screen.Width then Include(FAnchors, akRight);
if Top = 0 then Include(FAnchors, akTop);
if Bottom = Screen.Height then Include(FAnchors, akBottom);
Timer1.Enabled := FAnchors <> [];//****
end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
Timer1.Enabled := False;
Timer1.Interval := 200;
FormStyle := fsStayOnTop;
end;procedure TForm1.Timer1Timer(Sender: TObject);
const
cOffset = 2;
var
vHandle: THandle;//****
begin
vHandle := WindowFromPoint(Mouse.CursorPos);//***
while (vHandle <> 0) and (vHandle <> Handle) do //****
vHandle := GetParent(vHandle);//***
if vHandle = Handle then begin
if akLeft in FAnchors then Left := 0;//***
if akTop in FAnchors then Top := 0;
if akRight in FAnchors then Left := Screen.Width - Width;
if akBottom in FAnchors then Top := Screen.Height - Height;
end else begin
if akLeft in FAnchors then Left := -Width + cOffset;
if akTop in FAnchors then Top := -Height + cOffset;
if akRight in FAnchors then Left := Screen.Width - cOffset;
if akBottom in FAnchors then Top := Screen.Height - cOffset;
end;
end;end.2、将这段代码改成:程度开始的时候窗口就自动停留在屏幕上端,和屏幕等宽,有自动隐藏功能。
留言后请给:DJC@GENERSOFT.COM发消息,提醒我给你加分。谢谢
MSN:DONG6785@HOTMAIL.COM
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
TForm1 = class(TForm)
Timer1: TTimer;
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
FAnchors: TAnchors;//*************
procedure WMMOVING(var Msg: TMessage); message WM_MOVING;//******
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}uses Math;procedure TForm1.WMMOVING(var Msg: TMessage);
begin
inherited;//**********
with PRect(Msg.LParam)^ do begin//*********
Left := Min(Max(0, Left), Screen.Width - Width);//**********
Top := Min(Max(0, Top), Screen.Height - Height);
Right := Min(Max(Width, Right), Screen.Width);
Bottom := Min(Max(Height, Bottom), Screen.Height);
FAnchors := [];//*******
if Left = 0 then Include(FAnchors, akLeft);//***
if Right = Screen.Width then Include(FAnchors, akRight);
if Top = 0 then Include(FAnchors, akTop);
if Bottom = Screen.Height then Include(FAnchors, akBottom);
Timer1.Enabled := FAnchors <> [];//****
end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
Timer1.Enabled := False;
Timer1.Interval := 200;
FormStyle := fsStayOnTop;
end;procedure TForm1.Timer1Timer(Sender: TObject);
const
cOffset = 2;
var
vHandle: THandle;//****
begin
vHandle := WindowFromPoint(Mouse.CursorPos);//***
while (vHandle <> 0) and (vHandle <> Handle) do //****
vHandle := GetParent(vHandle);//***
if vHandle = Handle then begin
if akLeft in FAnchors then Left := 0;//***
if akTop in FAnchors then Top := 0;
if akRight in FAnchors then Left := Screen.Width - Width;
if akBottom in FAnchors then Top := Screen.Height - Height;
end else begin
if akLeft in FAnchors then Left := -Width + cOffset;
if akTop in FAnchors then Top := -Height + cOffset;
if akRight in FAnchors then Left := Screen.Width - cOffset;
if akBottom in FAnchors then Top := Screen.Height - cOffset;
end;
end;end.2、将这段代码改成:程度开始的时候窗口就自动停留在屏幕上端,和屏幕等宽,有自动隐藏功能。
留言后请给:DJC@GENERSOFT.COM发消息,提醒我给你加分。谢谢
MSN:DONG6785@HOTMAIL.COM
窗口停留在屏幕上端,可以改变formstyle ->fsStayOnTop自动隐藏你可以把窗口到托盘区就行上面的代码能实现QQ功能吗 ?怎么没有其他控件呢?
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
TForm1 = class(TForm)
Timer1: TTimer;
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
FAnchors: TAnchors;//定义锚点变量
function Min(x,y:Integer):Integer;
function Max(x,y:Integer):Integer;
procedure WMMOVING(var Msg: TMessage); message WM_MOVING;//处理窗口移动时的消息
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}{ TForm1 }procedure TForm1.FormCreate(Sender: TObject);
begin
//增加
FAnchors := [akTop];
// Timer1.Enabled := False;
//改为
Timer1.Enabled := True;
Timer1.Interval := 200;
FormStyle := fsStayOnTop;
end;function TForm1.Max(x, y: Integer): Integer;
begin
if x > y then
result := x
else
result := y;
end;function TForm1.Min(x, y: integer): Integer;
begin
if x > y then
result := y
else
result := x;
end;procedure TForm1.Timer1Timer(Sender: TObject);
const
cOffset = 2;
var
vHandle: THandle;//定义句柄
begin
vHandle := WindowFromPoint(Mouse.CursorPos);//得到当前鼠标位置控件的句柄
while (vHandle <> 0) and (vHandle <> Handle) do //做循环直到判断出当前鼠标是否在窗口上
vHandle := GetParent(vHandle);//得到当前控件上级控件的控件句柄
if vHandle = Handle then begin
if akLeft in FAnchors then Left := 0;//如果鼠标是在窗口上,则改变窗口的位置
if akTop in FAnchors then Top := 0;
if akRight in FAnchors then Left := Screen.Width - Width;
if akBottom in FAnchors then Top := Screen.Height - Height;
end else begin
if akLeft in FAnchors then Left := -Width + cOffset;
if akTop in FAnchors then Top := -Height + cOffset;
if akRight in FAnchors then Left := Screen.Width - cOffset;
if akBottom in FAnchors then Top := Screen.Height - cOffset;
end;
end;procedure TForm1.WMMOVING(var Msg: TMessage);
begin
inherited;//继承父类的窗口移动事件
with PRect(Msg.LParam)^ do begin//因为在WM_MOVING消息中lParam为RECT的地址,如此可取得移动中的窗体的位置
Left := Min(Max(0, Left), Screen.Width - Width);//算出窗口离左屏幕的距离,值在0-(Screen.Width - Width)之间
Top := Min(Max(0, Top), Screen.Height - Height);
Right := Min(Max(Width, Right), Screen.Width);
Bottom := Min(Max(Height, Bottom), Screen.Height);
FAnchors := [];//清空FAnchors变量
if Left = 0 then Include(FAnchors, akLeft);//如果移动到最左边,则增加一个akLeft到FAnchors中
if Right = Screen.Width then Include(FAnchors, akRight);
if Top = 0 then Include(FAnchors, akTop);
if Bottom = Screen.Height then Include(FAnchors, akBottom);
Timer1.Enabled := FAnchors <> [];//如果FAnchors不为空开启计时器
end;
end;end.
我在Win2000+delphi6下调试通过,如果有什么问题再问我。