📄 transparentwnd.cpp
字号:
CPoint ptOffset((m_ptDest.x > rcW.left)?10:-10,(m_ptDest.y > rcW.top)?5:-5);
rcW+=ptOffset;
MoveWindow(rcW);
if(m_ptDest.x<rcW.left) //向左运动
{
sprintf(szBmp,"RUN%d",m_iAniSeq%5+1);
m_iLastDirection=1;
}
else //向右运动
{
sprintf(szBmp,"RUN%d",m_iAniSeq%5+6);
m_iLastDirection=2;
}
}
else
{
int xcounter=10,ycounter=10;
m_Rect=GetTopWndTitleRect();
if(rcW.left < m_Rect.left +30 || m_iLastDirection==2)
{
xcounter=10;
m_iLastDirection=2;
}
if(rcW.left > m_Rect.right -30 || m_iLastDirection==1)
{
xcounter=-10;
m_iLastDirection=1;
}
ycounter=0; //上、下位置不变
rcW.top =m_Rect.top-rcW.Height();
rcW.bottom =m_Rect.top;
CPoint ptOffset(xcounter,ycounter);
rcW+=ptOffset;
MoveWindow(rcW);
if(m_iLastDirection==1) //局部运动时,往左为散步
{
KillTimer(GetCurAction());
SetTimer(TIMER_WALK,500,NULL);
}
else if((m_iAniSeq%30)==0)
{
KillTimer(GetCurAction());
SetTimer(TIMER_IDLE,600,NULL);
}
if(m_iLastDirection==1) //向左运动
{
sprintf(szBmp,"RUN%d",m_iAniSeq%5+1);
}
else //向右运动
{
sprintf(szBmp,"RUN%d",m_iAniSeq%5+6);
}
}
m_bmpDraw.DeleteObject();
m_bmpDraw.LoadBitmap(szBmp);
CWindowDC dc(this);
SetupRegion(&dc);
Invalidate();
}
//********************************************************************************
//* 名称:DoIdle()
//* 作者:徐景周(jingzhou_xu@163.net)
//* 功能:休息
//********************************************************************************
void CTransparentWnd::DoIdle(void)
{
if(m_iAniSeq%12==0)
{
SoundPlay(); //播放声音
//显示提示窗信息
CRect rc;
GetWindowRect(&rc);
CPoint pt;
pt.x=rc.left +rc.Width ()/2;
pt.y=rc.top;
if(m_bDownloadFailed)
{
CString str;
if(m_sdFile.ReadString(str) == NULL)
{
m_sdFile.SeekToBegin();
m_sdFile.ReadString(str);
}
m_Tip.Show(str,&pt); //注:同一行中显示的字数不能过大
}
else
m_Tip.Show(_T("欢迎您,来到未来工作室!"),&pt);
m_Tip.Hide();
KillTimer(TIMER_IDLE);
SetTimer(TIMER_SET,10,NULL);
return;
}
char szBmp[20];
if(m_iLastDirection==1) //向左运动
sprintf(szBmp,"IDLE%d",m_iAniSeq%4+1);
else //向右运动
sprintf(szBmp,"IDLE%d",m_iAniSeq%4+5);
m_bmpDraw.DeleteObject();
m_bmpDraw.LoadBitmap(szBmp);
CWindowDC dc(this);
SetupRegion(&dc);
Invalidate();
}
//********************************************************************************
//* 名称:DoDrag()
//* 作者:徐景周(jingzhou_xu@163.net)
//* 功能:拖动窗体时行为表现
//********************************************************************************
void CTransparentWnd::DoDrag(void)
{
CRect rcW;
POINT point;
GetWindowRect(rcW);
//实现拖动时窗体跟着移动
::GetCursorPos(&point);
MoveWindow(point.x-m_ptCurPoint.x ,point.y-m_ptCurPoint.y ,rcW.Width(),rcW.Height() ,true);
char szBmp[20];
sprintf(szBmp,"FUNK%d",m_iAniSeq%4+1);
m_bmpDraw.DeleteObject();
m_bmpDraw.LoadBitmap(szBmp);
CWindowDC dc(this);
SetupRegion(&dc);
Invalidate();
}
//********************************************************************************
//* 名称:OnLButtonDown()
//* 作者:徐景周(jingzhou_xu@163.net)
//* 功能:实现窗体无标题拖动
//********************************************************************************
void CTransparentWnd::OnLButtonDown(UINT nFlags, CPoint point)
{
CWnd::OnLButtonDown(nFlags, point);
KillTimer(GetCurAction()); //关闭当前行为表现
SetTimer(TEMER_DragDrop,60,NULL); //打开拖动时计数器
//鼠标移动到窗体时改变鼠标指针
::SetCursor(AfxGetApp()->LoadCursor(IDC_DROP));
m_bDragDrop=true; //精灵助手拖动标志为真
SetCapture(); //设置鼠标全屏捕捉
m_ptCurPoint=point; //记下按下时的鼠标坐标
// PostMessage(WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM(point.x,point.y));
}
//********************************************************************************
//* 名称:OnLButtonUp()
//* 作者:徐景周(jingzhou_xu@163.net)
//* 功能:实现窗体实时拖动效果
//********************************************************************************
void CTransparentWnd::OnLButtonUp(UINT nFlags, CPoint point)
{
m_bDragDrop=false; //精灵助手拖动标志为假
ReleaseCapture();
KillTimer(TEMER_DragDrop); //关闭拖动时记数器
SetTimer(TIMER_SET,10,NULL);
CWnd::OnLButtonUp(nFlags, point);
}
//********************************************************************************
//* 名称:OnCreate()
//* 作者:徐景周(jingzhou_xu@163.net)
//* 功能:创建窗体
//********************************************************************************
int CTransparentWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
//系统区显示的图标文件
m_hIcon[0]=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
//创建系统区图标
if (!m_TrayIcon.Create(this,
IDR_POPUP,
_T("精灵助手 V1.0"),
m_hIcon,
1, //一幅图像
1000, //延时1秒变化
WM_TRAYNOTIFY))
{
AfxMessageBox(_T("错误:创建系统图标失败!"), MB_OK | MB_ICONSTOP);
return -1;
}
// 建立信息提示窗
m_Tip.Create(GetDesktopWindow());
//全屏运动标志初始置真
m_bFullScreenWalk = TRUE;
m_iAction = 1;
SetTimer(1,300,NULL);
//窗体拖运时变量初始化
m_bDragDrop=false;
m_ptCurPoint=CPoint(0,0);
//置窗体初始显示位置
CRect rcW;
GetWindowRect(&rcW);
m_Rect=GetTopWndTitleRect();
rcW.top =m_Rect.top-rcW.Height();
rcW.bottom =m_Rect.top;
rcW.left =10;
//置窗体为最顶层显示及其位置
SetWindowPos(&wndTopMost,rcW.left,rcW.top,rcW.Width(),rcW.Height(),SWP_NOSIZE|SWP_NOMOVE);
//获取主程序所在路径,存在全局变量strPath中
GetModuleFileName(NULL,strPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
strPath.ReleaseBuffer ();
int nPos;
nPos=strPath.ReverseFind ('\\');
strPath=strPath.Left (nPos);
CFileFind fFind;
BOOL bFailed;
bFailed=fFind.FindFile(strPath+"\\Tip.txt",0);
//是否已下载提示文件,否则下载它并打开文件
if(!bFailed)
m_bDownloadFailed=GetSourceHtml("http://www.microsoft.com/"); //下载提示文件的默认网址
else
m_bDownloadFailed=TRUE;
fFind.Close ();
if(m_bDownloadFailed)
m_bSuccess=m_bDownloadFailed=m_sdFile.Open(strPath+"\\Tip.txt",
CFile::modeRead | CFile::typeText);
return 0;
}
//********************************************************************************
//* 名称:OnTrayNotification()
//* 作者:徐景周(jingzhou_xu@163.net)
//* 功能:系统区创建的图标映射涵数
//********************************************************************************
LRESULT CTransparentWnd::OnTrayNotification(WPARAM wParam, LPARAM lParam)
{
return m_TrayIcon.OnTrayNotification(wParam, lParam);
}
//********************************************************************************
//* 名称:OnEraseBkgnd()
//* 作者:徐景周(jingzhou_xu@163.net)
//* 功能:擦除背景
//********************************************************************************
BOOL CTransparentWnd::OnEraseBkgnd(CDC* pDC)
{
CRect rect;
GetWindowRect(&rect);
CDC memDC;
CBitmap &cBitmap=m_bmpDraw;;
CBitmap* pOldMemBmp = NULL;
CFont* pOldMemFont=NULL;
memDC.CreateCompatibleDC(pDC);
pOldMemBmp = memDC.SelectObject(&cBitmap);
pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &memDC, 0, 0, SRCCOPY);
if (pOldMemBmp) memDC.SelectObject( pOldMemBmp );
return TRUE;
}
//********************************************************************************
//* 名称:OnPaint()
//* 作者:徐景周(jingzhou_xu@163.net)
//* 功能:刷新背景
//********************************************************************************
void CTransparentWnd::OnPaint()
{
CPaintDC dc(this);
}
//********************************************************************************
//* 名称:OnTimer()
//* 作者:徐景周(jingzhou_xu@163.net)
//* 功能:触发定时器
//********************************************************************************
void CTransparentWnd::OnTimer(UINT nIDEvent)
{
switch(nIDEvent)
{
case(1): //判定何种运行方式
DoSet();
break;
case(2): //散步
DoWalk();
break;
case(3): //休息
DoIdle();
break;
case(4): //奔跑
DoRun();
break;
case(5): //播放声音
KillTimer(TIMER_PLAYSOUND);
break;
case(6):
DoDrag(); //实时拖动
break;
default:
break;
}
m_iAction= nIDEvent; //当前行为表现方式标志
m_iAniSeq++; //下一幅图像
if(m_iAniSeq>80) m_iAniSeq=0;
CWnd::OnTimer(nIDEvent);
}
//********************************************************************************
//* 名称:OnDestroy()
//* 作者:徐景周(jingzhou_xu@163.net)
//* 功能:销毁窗体
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -