描述:实现类似qq窗口悬挂功能,当窗口靠近屏幕边缘(一般为上,右),窗口自动悬挂隐藏,鼠标再放到边缘,窗口又显示出来
步骤1:开启监控鼠标窗口出来悬挂的线程
pCursorThread = AfxBeginThread(HandleCursorThread, this);
步骤2:线程具体内容
#define HIDEWINDOW_LENTH 5 // 窗口悬挂后的留下的宽度
UINT CMainWnd::HandleCursorThread(LPVOID lParam) // min
{
ASSERT(lParam);
CMainWnd* pDlg = reinterpret_cast<CMainWnd*>(lParam);
while(1)
{
//static bool bwindowshow = true;
POINT m_pMouse;
CRect rcWindow;
int nSrcWidth = ::GetSystemMetrics(SM_CXSCREEN);
int nSrcHeight = ::GetSystemMetrics(SM_CYSCREEN);
GetCursorPos(&m_pMouse);
pDlg->GetWindowRect(&rcWindow);
bool bCurInWindow; // 判断鼠标是否在窗口中
if ((m_pMouse.x>=rcWindow.left&&m_pMouse.x<=rcWindow.right)&&(m_pMouse.y>=rcWindow.top&&m_pMouse.y<=rcWindow.bottom))
{
bCurInWindow = true;
}
else
{
bCurInWindow = false;
}
bool bwindowshow;
if (rcWindow.Width()<=HIDEWINDOW_LENTH || rcWindow.Height()<=HIDEWINDOW_LENTH)
{
bwindowshow = false;
}
else
{
bwindowshow = true;
}
if (rcWindow.left<1 && bwindowshow && !bCurInWindow)
{
pDlg->SliderWindow(LEFT, false); // 向左滑进去
}
else if(rcWindow.left<1 && rcWindow.Width()<=HIDEWINDOW_LENTH
&& m_pMouse.x<HIDEWINDOW_LENTH && m_pMouse.y>rcWindow.top && m_pMouse.y<rcWindow.bottom
&& !bwindowshow)
{
pDlg->SliderWindow(LEFT, true); // 从左滑出来
}
else if (rcWindow.top<1 && bwindowshow && !bCurInWindow)
{
pDlg->SliderWindow(TOP, false); // 向上滑进去
}
else if (rcWindow.top<1 && rcWindow.Height()<=HIDEWINDOW_LENTH
&& m_pMouse.y<HIDEWINDOW_LENTH && m_pMouse.x>rcWindow.left && m_pMouse.x<rcWindow.right
&& !bwindowshow)
{
pDlg->SliderWindow(TOP, true); // 从上滑出来
}
else if (rcWindow.right>=nSrcWidth && bwindowshow && !bCurInWindow)
{
pDlg->SliderWindow(RIGHT, false); // 向右滑进去
}
else if (rcWindow.right>=nSrcWidth && rcWindow.Width()<=HIDEWINDOW_LENTH
&& m_pMouse.x>nSrcWidth-HIDEWINDOW_LENTH && m_pMouse.y>rcWindow.top && m_pMouse.y<rcWindow.bottom
&& !bwindowshow)
{
pDlg->SliderWindow(RIGHT, true); // 从右滑出来
}
//TRACE(_T("x=%d y=%d rcWindow.left=%d rcWindow.right=%d \r\n"),m_pMouse.x, m_pMouse.y,rcWindow.left,rcWindow.right);
Sleep(500);
}
return 0;
}
void CMainWnd::SliderWindow(int nPos, bool bShow)
{
CRect rc;
GetWindowRect(rc);
int nSrcWidth = ::GetSystemMetrics(SM_CXSCREEN);
int ifreq = 7; // 隐藏显示速率
static int iWindowWidth = rc.Width();
static int iWindowHeight = rc.Height();
if (!bShow)
{
iWindowWidth = rc.Width(); // 存储窗口(即将隐藏)的宽度
iWindowHeight = rc.Height(); // 存储窗口(即将隐藏)的高度
}
switch(nPos)
{
case LEFT:
if(bShow)
{
for(int i=1; i<=ifreq; i++)
{
SetWindowPos(&CWnd::wndTopMost, 0, rc.top, iWindowWidth*i/ifreq, rc.Height(), SWP_SHOWWINDOW);
Sleep(20);
}
}
else
{
for(int i=1; i<=ifreq; i++)
{
SetWindowPos(&CWnd::wndTopMost, 0, rc.top, (rc.Width()-rc.Width()*i/ifreq)+HIDEWINDOW_LENTH, rc.Height(), SWP_SHOWWINDOW);
Sleep(20);
}
}
break;
case RIGHT:
if(bShow)
{
for(int i=1; i<=ifreq; i++)
{
int iwidth = iWindowWidth*i/ifreq;
int ileft = nSrcWidth - iwidth;
SetWindowPos(&CWnd::wndTopMost, ileft, rc.top, iwidth, rc.Height(), SWP_SHOWWINDOW);
Sleep(20);
}
}
else
{
for(int i=1; i<=ifreq; i++)
{
int iwidth = (rc.Width()-rc.Width()*i/ifreq)+HIDEWINDOW_LENTH;
int ileft = nSrcWidth - iwidth;
SetWindowPos(&CWnd::wndTopMost, ileft, rc.top, iwidth, rc.Height(), SWP_SHOWWINDOW);
Sleep(20);
}
}
break;
case TOP:
if(bShow)
{
for(int i=1; i<=ifreq; i++)
{
SetWindowPos(&CWnd::wndTopMost, rc.left, 0, rc.Width(), iWindowHeight*i/ifreq, SWP_SHOWWINDOW);
Sleep(20);
}
}
else
{
for(int i=1; i<=ifreq; i++)
{
SetWindowPos(&CWnd::wndTopMost, rc.left, 0, rc.Width(), (rc.Height()-rc.Height()*i/ifreq)+HIDEWINDOW_LENTH, SWP_SHOWWINDOW);
Sleep(20);
}
}
break;
case BOTTOM:
break;
default:
break;
}
}
以上代码均为本人调试过ok的