// TestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Test.h"
#include "AutoHideDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
static int ScreenX = GetSystemMetrics(SM_CXSCREEN);
static int ScreenY = GetSystemMetrics(SM_CYSCREEN);
enum
{
DOCK_NONE, //不停靠
DOCK_TOP, //停靠上边
DOCK_LEFT, //停靠左边
DOCK_RIGHT //停靠右边
};
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAutoHideDlg dialog
CAutoHideDlg::CAutoHideDlg(CWnd* pParent /*=NULL*/)
: CDialog(CAutoHideDlg::IDD, pParent)
,m_DockMode(DOCK_NONE)
,m_bHide(false)
{
//{{AFX_DATA_INIT(CAutoHideDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CAutoHideDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAutoHideDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAutoHideDlg, CDialog)
//{{AFX_MSG_MAP(CAutoHideDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_MOVING()
ON_WM_TIMER()
ON_WM_NCHITTEST()
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAutoHideDlg message handlers
BOOL CAutoHideDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
//SetTimer(1,50,NULL);
::SetWindowPos(GetSafeHwnd(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CAutoHideDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CAutoHideDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CAutoHideDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CAutoHideDlg::AnimateShowHide(BOOL bHide)
{
if(m_DockMode == DOCK_NONE)
return;
m_bHide = bHide;
const int borderWidth = 3;
RECT rc;
GetWindowRect(&rc);
int width = rc.right - rc.left;
int height = rc.bottom - rc.top;
//下边判断窗体该如何移动,由停靠方式决定
int times = 10; //平移对话框的次数,如果你觉得不够平滑,可以增大该值.
int xStep, yStep;
int xEnd, yEnd;
switch (m_DockMode)
{
case DOCK_TOP:
{
//向上移藏
xStep = 0;
xEnd = rc.left;
if (bHide)
{
yStep = -rc.bottom / times;
yEnd = -height + borderWidth;
}
else
{
yStep = -rc.top / times;
yEnd = 0;
}
break;
}
case DOCK_LEFT:
{
//向左移藏
yStep = 0;
yEnd = rc.top;
if (bHide)
{
xStep = -rc.right / times;
xEnd = -width + borderWidth;
}
else
{
xStep = -rc.left / times;
xEnd = 0;
}
break;
}
case DOCK_RIGHT:
{
//向右移藏
yStep = 0;
yEnd = rc.top;
if (bHide)
{
xStep = (ScreenX - rc.left) / times;
xEnd = ScreenX - borderWidth;
}
else
{
xStep = (ScreenX - rc.right) / times;
xEnd = ScreenX - width;
}
break;
}
default:
_ASSERTE(FALSE);
}
//动画滚动窗体.
for (int i = 0; i < times-1; ++i)
{
rc.left += xStep;
rc.top += yStep;
SetWindowPos(NULL, rc.left, rc.top, 0, 0,
SWP_NOSIZE | SWP_NOSENDCHANGING);
RedrawWindow(NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW);
Sleep(5);
}
SetWindowPos(NULL, xEnd, yEnd, 0, 0, SWP_NOSIZE);
}
void CAutoHideDlg::OnMoving(UINT nSide, LPRECT pRect)
{
if (pRect->top <= 0) //在上边有效距离内,自动靠拢。
{
m_DockMode = DOCK_TOP;
pRect->bottom -= pRect->top;
pRect->top = 0;
}
else if (pRect->left <= 0) //在左边有效距离内
{
m_DockMode = DOCK_LEFT;
pRect->right -= pRect->left;
pRect->left = 0;
}
else if (pRect->right + 0 >= ScreenX) //在右边有效距离内,ScreenX为屏幕宽度,可由GetSystemMetrics(SM_CYSCREEN)得到。
{
m_DockMode = DOCK_RIGHT;
pRect->left += (ScreenX - pRect->right);
pRect->right = ScreenX;
}
else
{
m_DockMode = DOCK_NONE;
}
if(m_DockMode != DOCK_NONE)
{
SetTimer(1,50,NULL);
}
else
{
KillTimer(1);
}
}
void CAutoHideDlg::OnTimer(UINT nIDEvent)
{
if(m_DockMode !=DOCK_NONE && nIDEvent == 1)
{
POINT pt;
RECT rc;
GetCursorPos(&pt);
GetWindowRect(&rc);
if (!PtInRect(&rc, pt))
{
AnimateShowHide(TRUE);
KillTimer(1);
}
}
}
LRESULT CAutoHideDlg::OnNcHitTest(CPoint point)
{
if (m_bHide)
{
AnimateShowHide(FALSE);
if(m_DockMode != DOCK_NONE)
SetTimer(1,50,NULL);
}
//必须得调基类的对应函数。
return CDialog::OnNcHitTest(point);
}