我提问过这个问题,上次没有测试完全,只拿了一个空白Form测试了一下,但是现在发现BUG了:
就是当鼠标在窗体空白处的时候 没有BUG,但是当把鼠标焦点放在某个控件上,比如button,Edit上面的时候,窗体会自动隐藏,而实际上想要的效果应该是不隐藏,因为这些控件也是窗体上的,比如把鼠标放在QQ上某个按钮上,窗体就不会隐藏。应该怎么解决?谢谢
那个贴子地址是
http://topic.csdn.net/u/20080616/21/e5d5adcf-d2be-43c4-923f-744ea740bd97.html
附上代码
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;type
  TForm1 = class(TForm)
    Timer1: TTimer;
    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, Types;  {  TForm1  }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;
  Animate_Steps = 20; //可调
var
  iLeft, iTop, i: Integer;
  dx, dy, dLeft, dTop: Double;
begin
  iTop := Top;
  iLeft := Left;
  if WindowFromPoint(Mouse.CursorPos) = Handle then
  begin
    if akLeft in FAnchors then
      iLeft := 0;
    if akTop in FAnchors then
      iTop := 0;
    if akRight in FAnchors then
      iLeft := Screen.Width - Width;
    if akBottom in FAnchors then
      iTop := Screen.Height - Height;
  end
  else
  begin
    if akLeft in FAnchors then
      iLeft := -Width + cOffset;
    if akTop in FAnchors then
      iTop := -Height + cOffset;
    if akRight in FAnchors then
      iLeft := Screen.Width - cOffset;
    if akBottom in FAnchors then
      iTop := Screen.Height - cOffset;
  end;  if (iTop <> Top) or (iLeft <> Left) then
  begin
    dx := (iLeft - Left) / Animate_Steps;
    dy := (iTop - Top) / Animate_Steps;
    dTop := Top;
    dLeft := Left;
    for i := 1 to Animate_Steps - 1 do
    begin
      dLeft := dLeft + dx;
      dTop := dTop + dy;
      Top := Trunc(dTop);
      Left := Trunc(dLeft);
      Self.Update;
      Sleep(10);//睡一下zzz
    end;
    Top := iTop;
    Left := iLeft;
  end;
end;end.

解决方案 »

  1.   

    判断控件的Parent属性,如果是主窗体则不隐藏
      

  2.   

        为什么不在WM_MOUSELEAVE消息中处理而要一个Timer呢?    如果你想在现有的基础上修改,那就直接GetWindowThreadProcessId,获取当前鼠标位置的窗体的线程ID是否和主窗体的线程ID相同,相同不隐藏,反之则隐藏。    截获窗体的WM_MOUSELEAVE消息,在其中判断是否到了设定的边界,到了边界就隐藏。这样就不会出现你所说的问题了。
      

  3.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ExtCtrls, StdCtrls;type
      TForm1 = class(TForm)
        Timer1: TTimer;
        Button1: TButton;
        Button2: TButton;
        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;implementationuses Math, Types;{$R *.dfm}{ TForm1 }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;
      Animate_Steps = 20; //可调
    var
      iLeft, iTop, i: Integer;
      dx, dy, dLeft, dTop: Double;
    begin
      iTop := Top;
      iLeft := Left;
      if WindowFromPoint(Mouse.CursorPos) = Handle then
      begin
        if akLeft in FAnchors then
          iLeft := 0;
        if akTop in FAnchors then
          iTop := 0;
        if akRight in FAnchors then
          iLeft := Screen.Width - Width;
        if akBottom in FAnchors then
          iTop := Screen.Height - Height;
      end
      else
      begin
        if akLeft in FAnchors then
          iLeft := -Width + cOffset;
        if akTop in FAnchors then
          iTop := -Height + cOffset;
        if akRight in FAnchors then
          iLeft := Screen.Width - cOffset;
        if akBottom in FAnchors then
          iTop := Screen.Height - cOffset;
      end;  if (iTop <> Top) or (iLeft <> Left) then
      begin
        dx := (iLeft - Left) / Animate_Steps;
        dy := (iTop - Top) / Animate_Steps;
        dTop := Top;
        dLeft := Left;
        for i := 1 to Animate_Steps - 1 do
        begin
          dLeft := dLeft + dx;
          dTop := dTop + dy;
          Top := Trunc(dTop);
          Left := Trunc(dLeft);
          Self.Update;
          Sleep(10);//睡一下zzz
        end;
        Top := iTop;
        Left := iLeft;
      end;
    end;end.
      

  4.   

    本帖最后由 hongqi162 于 2008-07-06 14:24:07 编辑
      

  5.   

    我用的是Rxlib里面的TAppEvents,TRxTrayIcon这两个组件下载地址
    http://www.ccrun.com/view.asp?id=164
    (妖哥的网站)//TAppEvents的OnMinimize事件  最小化隐藏
    procedure TFormMain.AppEvents1Minimize(Sender: TObject);
    begin
      if NewStyleControls then
        ShowWindow(Application.Handle, SW_HIDE);
    end;
    //TRxTrayIcon的DblClick事件   双击显示
    procedure TFormMain.RxTrayIcon1DblClick(Sender: TObject);
    begin
      ShowWindow(Application.Handle, SW_RESTORE);
      SetWindowPos(Application.handle,HWND_TOPMOST,0,0,0,0,swp_hidewindow);
    end;
      

  6.   

    网上现在的DEMO很多呢,不要还是要学会一些原理才行.
      

  7.   

    谢谢月亮,我用的CoolTrayIcon控件,加上代码发现没有效果,不知是何原因?必须用你说的那俩个控件吗?