📄 transparentwnd.cpp
字号:
//////////////////////////////////////////////////
//类名:CTransparentWnd
//功能:透明窗体实现
//////////////////////////////////////////////////
#include "stdafx.h"
#include "TransparentWnd.h"
#include <assert.h>
#include "HelpTip.h"
#include "HelpTipDlg.h"
#include "DownloadAddress.h"
#include "About.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define TIMER_SET 1
#define TIMER_WALK 2
#define TIMER_IDLE 3
#define TIMER_RUN 4
#define TIMER_PLAYSOUND 5
#define TEMER_DragDrop 6
#define WM_TRAYNOTIFY WM_USER + 100 //自定义系统托盘消息函数
//********************************************************************************
//* 名称:CTransparentWnd()
//* 作者:徐景周(jingzhou_xu@163.net)
//* 功能:构造涵数,在此初始化变量
//********************************************************************************
CTransparentWnd::CTransparentWnd()
{
m_iAniSeq=0;
strPath="";
m_bSuccess=FALSE;
}
//********************************************************************************
//* 名称:~CTransparentWnd()
//* 作者:徐景周(jingzhou_xu@163.net)
//* 功能:析构涵数,可在此删除创建的变量
//********************************************************************************
CTransparentWnd::~CTransparentWnd()
{
}
BEGIN_MESSAGE_MAP(CTransparentWnd, CWnd)
//{{AFX_MSG_MAP(CTransparentWnd)
ON_MESSAGE(WM_TRAYNOTIFY, OnTrayNotification) //自定义系统托盘消息涵数
ON_WM_PAINT()
ON_WM_ERASEBKGND()
ON_WM_LBUTTONDOWN()
ON_WM_CREATE()
ON_WM_TIMER()
ON_WM_DESTROY()
ON_WM_RBUTTONDOWN()
ON_COMMAND(IDR_EXIT, OnExit)
ON_COMMAND(IDR_FULLSCREEN_WALK, OnFullscreenWalk)
ON_UPDATE_COMMAND_UI(IDR_FULLSCREEN_WALK, OnUpdateFullscreenWalk)
ON_COMMAND(IDR_SHOWHIDE, OnShowhide)
ON_WM_INITMENUPOPUP()
ON_COMMAND(IDR_DOWNLOAD, OnDownload)
ON_COMMAND(IDR_ACTION_RUN, OnActionRun)
ON_COMMAND(IDR_ACTION_WALK, OnActionWalk)
ON_COMMAND(IDR_ACTION_IDLE, OnActionIdle)
ON_WM_MEASUREITEM()
ON_WM_MENUCHAR()
ON_COMMAND(IDR_ABOUT, OnAbout)
ON_WM_LBUTTONUP()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//********************************************************************************
//* 名称:CreateTransparent()
//* 作者:徐景周(jingzhou_xu@163.net)
//* 功能:建立透明窗体
//********************************************************************************
void CTransparentWnd::CreateTransparent(LPCTSTR pTitle, RECT &rect)
{
CreateEx( 0,
AfxRegisterWndClass(0,AfxGetApp()->LoadStandardCursor(IDC_ARROW)),
pTitle,
WS_POPUP ,
rect,
NULL,
NULL,
NULL );
}
//********************************************************************************
//* 名称:GetSourceHtml()
//* 作者:徐景周(jingzhou_xu@163.net)
//* 功能:下载网页涵数
//********************************************************************************
BOOL CTransparentWnd::GetSourceHtml(CString theUrl)
{
CInternetSession session;
CInternetFile* file = NULL;
try
{
// 试着连接到指定URL
file = (CInternetFile*) session.OpenURL(theUrl);
}
catch (CInternetException* m_pException)
{
// 如果有错误的话,置文件为空
file = NULL;
m_pException->Delete();
return FALSE;
}
// 用dataStore来保存读取的网页文件
CStdioFile dataStore;
if (file)
{
CString somecode; //也可采用LPTSTR类型,将不会删除文本中的\n回车符
BOOL bIsOk = dataStore.Open(strPath+"\\Tip.txt",
CFile::modeCreate
| CFile::modeWrite
| CFile::shareDenyWrite
| CFile::typeText);
if (!bIsOk)
return FALSE;
// 读写网页文件,直到为空
while (file->ReadString(somecode) != NULL) //如果采用LPTSTR类型,读取最大个数nMax置0,使它遇空字符时结束
{
dataStore.WriteString(somecode);
dataStore.WriteString("\n"); //如果somecode采用LPTSTR类型,可不用此句
}
file->Close();
delete file;
}
else
{
dataStore.WriteString(_T("到指定服务器的连接建立失败..."));
return FALSE;
}
return TRUE;
}
//********************************************************************************
//* 名称:SoundPlay()
//* 作者:徐景周(jingzhou_xu@163.net)
//* 功能:播放内镶WAV声音文件
//********************************************************************************
void CTransparentWnd::SoundPlay(void)
{
//先关闭原声音播放
PlaySound("IDR_WAVE",AfxGetResourceHandle(),SND_RESOURCE|SND_PURGE|SND_NODEFAULT );
//设置播放声音时间0.84秒
SetTimer(TIMER_PLAYSOUND,840,NULL);
//资源WAV文件的ID须加双引号,用下API函数播放
PlaySound("IDR_WAVE",AfxGetResourceHandle(),SND_RESOURCE|SND_ASYNC|SND_NODEFAULT );
}
//********************************************************************************
//* 名称:SetupRegion()
//* 作者:徐景周(jingzhou_xu@163.net)
//* 功能:将窗体背景透明化
//********************************************************************************
void CTransparentWnd::SetupRegion(CDC *pDC)
{
CDC memDC;
CBitmap &cBitmap=m_bmpDraw;
CBitmap* pOldMemBmp = NULL;
COLORREF col,colMask;
CRect cRect;
int x, y;
CRgn wndRgn, rgnTemp;
GetWindowRect(&cRect);
CPoint ptOrg=cRect.TopLeft();
BITMAP bmInfo;
cBitmap.GetObject(sizeof(bmInfo),&bmInfo);
CRect rcNewWnd=CRect(ptOrg,CSize(bmInfo.bmWidth,bmInfo.bmHeight));
memDC.CreateCompatibleDC(pDC);
pOldMemBmp = memDC.SelectObject(&cBitmap);
colMask=memDC.GetPixel(0,0);
wndRgn.CreateRectRgn(0, 0, rcNewWnd.Width(), rcNewWnd.Height());
for(x=0; x<=rcNewWnd.Width(); x++)
{
for(y=0; y<=rcNewWnd.Height(); y++)
{
col = memDC.GetPixel(x, y);
if(col == colMask)
{
rgnTemp.CreateRectRgn(x, y, x+1, y+1);
wndRgn.CombineRgn(&wndRgn, &rgnTemp, RGN_XOR);
rgnTemp.DeleteObject();
}
}
}
if (pOldMemBmp) memDC.SelectObject(pOldMemBmp);
SetWindowRgn((HRGN)wndRgn, TRUE);
MoveWindow(rcNewWnd);
}
//********************************************************************************
//* 名称:DoSet()
//* 作者:徐景周(jingzhou_xu@163.net)
//* 功能:判断该进行何种运行方式
//********************************************************************************
void CTransparentWnd::DoSet(void)
{
int iSW=GetSystemMetrics(SM_CXFULLSCREEN);
int iSH=GetSystemMetrics(SM_CYFULLSCREEN);
KillTimer(TIMER_SET);
switch(rand()%5)
{
case(0): //散步
case(1):
m_ptDest.x=rand()%(iSW - 10);
m_ptDest.y=rand()%(iSH - 10);
SetTimer(TIMER_WALK,500,NULL);
break;
case(2): //奔跑
case(3):
m_ptDest.x=rand()%(iSW - 10);
m_ptDest.y=rand()%(iSH - 10);
SetTimer(TIMER_RUN,500,NULL);
break;
case(4): //休息
SetTimer(TIMER_IDLE,600,NULL);
break;
}
}
//********************************************************************************
//* 名称:DoWalk()
//* 作者:徐景周(jingzhou_xu@163.net)
//* 功能:散步
//********************************************************************************
void CTransparentWnd::DoWalk(void)
{
char szBmp[20];
CRect rcW;
GetWindowRect(rcW);
//决定全屏或局部运动的范围
if(m_bFullScreenWalk)
{
if((m_ptDest.x> rcW.left && m_ptDest.x < rcW.right)||(m_ptDest.y> rcW.top && m_ptDest.y < rcW.bottom ))
{
KillTimer(TIMER_WALK);
SetTimer(TIMER_SET,10,NULL);
return;
}
CPoint ptOffset((m_ptDest.x > rcW.left)?5:-5,(m_ptDest.y > rcW.top)?5:-5);
rcW+=ptOffset;
MoveWindow(rcW);
if(m_ptDest.x<rcW.left) //向左运动
{
sprintf(szBmp,"WALK%d",m_iAniSeq%5+1);
m_iLastDirection=1;
}
else //向右运动
{
sprintf(szBmp,"WALK%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==2) //局部运动时,往右为奔跑
{
KillTimer(GetCurAction());
SetTimer(TIMER_RUN,500,NULL);
}
else if((m_iAniSeq%30)==0)
{
KillTimer(GetCurAction());
SetTimer(TIMER_IDLE,600,NULL);
}
if(m_iLastDirection==1) //向左运动
{
sprintf(szBmp,"WALK%d",m_iAniSeq%5+1);
}
else //向右运动
{
sprintf(szBmp,"WALK%d",m_iAniSeq%5+6);
}
}
m_bmpDraw.DeleteObject();
m_bmpDraw.LoadBitmap(szBmp);
CWindowDC dc(this);
SetupRegion(&dc);
Invalidate();
}
//********************************************************************************
//* 名称:DoRun()
//* 作者:徐景周(jingzhou_xu@163.net)
//* 功能:奔跑
//********************************************************************************
void CTransparentWnd::DoRun(void)
{
char szBmp[20];
CRect rcW;
GetWindowRect(rcW);
//决定全屏或局部运动的范围
if(m_bFullScreenWalk)
{
if((m_ptDest.x> rcW.left && m_ptDest.x < rcW.right)||(m_ptDest.y> rcW.top && m_ptDest.y < rcW.bottom ))
{
KillTimer(TIMER_RUN);
SetTimer(TIMER_SET,10,NULL);
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -