如何实现想QQ那样把界面放到桌面的边上,自动就隐藏进去了呢?

解决方案 »

  1.   

    #include <vcl.h>
    #pragma hdrstop
    #include "MainForm.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;//隐藏窗体用到的
    INT alignType; //停靠位置 1上,2左,3右, 0不停靠
    RECT m_rect; //窗体RECT
    POINT m_mouse; //mouse pos
    INT CurrentPos; //当前停靠位置
    enum
    {
    ALIGN_NONE, //不停靠
    ALIGN_TOP, //停靠上边
    ALIGN_LEFT, //停靠左边
    ALIGN_RIGHT //停靠右边
    };#define NEAR_SIZE -2 //定义自动停靠有效距离
    #define NEAR_SIDE 3 //窗体隐藏后在屏幕上保留的像素,以使鼠标可以触及
    #define MSLEEP 100 //定义判断延时//---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
     : TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    //判断当窗体隐藏鼠标移动到窗体时是否还需要再次隐藏窗体
    //---------------------------------------------------------------------------
    BOOL MouseNeedHide()
    {
     if (m_rect.top <0 || m_rect.left <0 || Screen->Width - m_rect.right <2)return TRUE;
     CurrentPos=ALIGN_NONE;
     return FALSE;
    }
    //---------------------------------------------------------------------------
    //判断是否满足隐藏条件
    //---------------------------------------------------------------------------
    BOOL NeedHide()
    {
     return (
     ((m_rect.top < NEAR_SIZE)    //满足向上隐藏
     || (m_rect.left < NEAR_SIZE && m_mouse.x == 0)  //满足左边隐藏
     || (m_rect.right > Screen->Width+NEAR_SIZE && Screen->Width - m_mouse.x < 2))    //满足右边隐藏
     ) ;
    }
    //---------------------------------------------------------------------------
    //判断鼠标是否在窗体rect内
    //---------------------------------------------------------------------------
    BOOL InForm()
    {
     return (m_mouse.x > m_rect.left-2 && m_mouse.x < m_rect.right+2
     && m_mouse.y < m_rect.bottom+2 && m_mouse.y > m_rect.top-2) ;
    }
    //---------------------------------------------------------------------------
    //隐藏窗体的函数,可以在这里添加一些特效,例如滚动隐藏 ^_^
    //---------------------------------------------------------------------------
    HideToAlign(INT HideType)
    {
     alignType=HideType;
     if (alignType==0)return 0;
     if (alignType==1)
     {
      Form1->Top=-Form1->Height+NEAR_SIDE;
      CurrentPos=ALIGN_TOP;
      return 0;
     }
     else if (alignType==2)
     {
      Form1->Left=-Form1->Width+NEAR_SIDE;
      CurrentPos=ALIGN_LEFT;
      return 0;
     }
     else if (alignType==3)
     {
      Form1->Left=Screen->Width+NEAR_SIZE;
      CurrentPos=ALIGN_RIGHT;
      return 0;
     }
            return 0;
    }
    //---------------------------------------------------------------------------
    //主函数
    //---------------------------------------------------------------------------
    DWORD __stdcall AutoHideWinThread(LPVOID)
    {
        while(1)
     {
      //判断是否该显示窗体
      if(InForm()==TRUE) //如果鼠标在窗体RECT内
      {
       if (CurrentPos==ALIGN_TOP) //如果当前隐藏方向是 上
       {
        Form1->Top=-2; //显示出窗体
        while(InForm()==TRUE)
        {
         if (MouseNeedHide()==FALSE) //判断鼠标是否拖动了窗体,是否符合隐藏条件
         {
          alignType=0;
          break;
         }
         Sleep(MSLEEP);
        }
        HideToAlign(alignType); //再次隐藏窗体
       }
       else if (CurrentPos==ALIGN_LEFT)
       {
        Form1->Left=-2;
        while(InForm()==TRUE)
        {
         if (MouseNeedHide()==FALSE)
         {
          alignType=0;
          break;
         }
         Sleep(MSLEEP);
        }
        HideToAlign(alignType);
       }
       else if (CurrentPos==ALIGN_RIGHT)
       {
        Form1->Left=Screen->Width - Form1->Width +2;
        while(InForm()==TRUE)
        {
         if (MouseNeedHide()==FALSE)
         {
          alignType=0;
          break;
         }
         Sleep(MSLEEP);
        }
        HideToAlign(alignType);
       }
      }  //判断停靠上边
      if (m_rect.top < NEAR_SIZE && m_mouse.y ==0) //如果满足向上隐藏
      {
                            alignType=1; //设置隐藏方向
       while(InForm()==TRUE) //检测鼠标是否还在窗体上
       {
        Form1->Top = NEAR_SIZE; //调整窗口位置
        if (MouseNeedHide()==FALSE) //判断鼠标是否拖动了窗体,是否符合隐藏条件
        {
         alignType=0;
         break;
        }
        Sleep(MSLEEP);
       }
       HideToAlign(alignType);
      }
      else if (m_rect.left < NEAR_SIZE && m_mouse.x == 0)
      {
                            alignType=2;
       while(InForm()==TRUE)
       {
        Form1->Left=NEAR_SIZE;
        if (MouseNeedHide()==FALSE)
        {
         alignType=0;
         break;
        }
        Sleep(MSLEEP);
       }
       HideToAlign(alignType);
      }
      else if (m_rect.right > Screen->Width+NEAR_SIZE && Screen->Width - m_mouse.x < 2)
      {
                            alignType=3;
       while(InForm()==TRUE)
       {
        Form1->Left=Screen->Width - Form1->Width - NEAR_SIZE;
        if (MouseNeedHide()==FALSE)
        {
         alignType=0;
         break;
        }
        Sleep(MSLEEP);
       }
       HideToAlign(alignType);
      }
      Sleep(MSLEEP);
     }
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::FormCreate(TObject *Sender)
    {
     CreateThread(NULL,0,AutoHideWinThread,NULL,0,NULL); 
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::FormResize(TObject *Sender)
    {
     if (Form1->Width > 323)Form1->Width =323;
     if (Form1->Width < 323)Form1->Width =323;
     if (Form1->Height > 164)Form1->Height =164;
     if (Form1->Height < 164)Form1->Height =164;}
    //---------------------------------------------------------------------------
    void __fastcall TForm1::Timer1Timer(TObject *Sender)
    {
     GetWindowRect(Form1->Handle,&m_rect);
     GetCursorPos(&m_mouse);        
    }