?? transparentwnd.cpp
字號:
// TransparentWnd.cpp : implementation file
//
// Modified by 徐景周, 2001.3.3
//功能:動態透明位圖、屏蔽鼠標,系統熱鍵,自定義熱鍵退出等。
//
#include "stdafx.h"
#include "FixYou.h"
#include "TransparentWnd.h"
#include "FixYouDlg.h"
#include "Registry.h" //注冊表存取類
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CRect rt; //鼠標移動范圍
bool bMove=false;
/////////////////////////////////////////////////////////////////////////////
// TransparentWnd
TransparentWnd::TransparentWnd()
{
m_iAniSeq=0; //圖像變化初始值
}
TransparentWnd::~TransparentWnd()
{
}
BEGIN_MESSAGE_MAP(TransparentWnd, CWnd)
//{{AFX_MSG_MAP(TransparentWnd)
ON_WM_CREATE()
ON_WM_ERASEBKGND()
ON_WM_TIMER()
ON_WM_RBUTTONDOWN()
ON_WM_CLOSE()
// ON_MESSAGE(WM_HOTKEY,OnHotKey)
ON_WM_SYSKEYDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//********************************************************************************
//* CreateTransparent()
//*
//* Creates the main application window transparent
//********************************************************************************
void TransparentWnd::CreateTransparent(LPCTSTR pTitle, RECT &rect)
{
// 創建一個隱藏窗口
CreateEx( 0,
AfxRegisterWndClass(0,AfxGetApp()->LoadStandardCursor(IDC_ARROW)),
pTitle,
WS_POPUP ,
rect,
NULL,
NULL,
NULL);
DoChange();
}
//********************************************************************************
//* SetupRegion()
//*
//* Set the Window Region for transparancy outside the mask region
//********************************************************************************
void TransparentWnd::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);
}
void TransparentWnd::DoChange(void)
{
int iSW=GetSystemMetrics(SM_CXFULLSCREEN);
int iSH=GetSystemMetrics(SM_CYFULLSCREEN);
CRect rcW;
GetWindowRect(rcW);
if(rcW.left<50) xcounter=5;
if(rcW.top<50) ycounter=5;
if( rcW.left <50) m_iLastDirection=1; //向右移動
if(rcW.left>iSW-50) xcounter=-5;
if(rcW.top>iSH-50) ycounter=-5;
if(rcW.left >iSW-50) m_iLastDirection=0; //向左移動
CPoint ptOffset(xcounter,ycounter);
rcW+=ptOffset;
MoveWindow(rcW);
if(!bMove) //
{
rt.TopLeft() =rcW.CenterPoint () ; //限定鼠標只在窗體中移動
rt.BottomRight() =rcW.CenterPoint ();
SetCursorPos(rt.CenterPoint ().x,rt.CenterPoint ().y);
ClipCursor(rt);
}
char szBmp[20];
if(m_iLastDirection==1) //向右移動時顯示時圖像
sprintf(szBmp,"BEER%d",m_iAniSeq%4+5);
else//向左移動時顯示時圖像
sprintf(szBmp,"BEER%d",m_iAniSeq%4+1);
m_bmpDraw.DeleteObject();
m_bmpDraw.LoadBitmap(szBmp);
CWindowDC dc(this);
SetupRegion(&dc);
Invalidate();
}
int TransparentWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
xcounter=5,ycounter=5;
m_iLastDirection=0; //跑動的方向標志位,開始為向左
SetTimer(1,500,NULL); //圖像顯示變隔時間
//設置開機自啟動鍵值
TCHAR strCurDirectory[255];
// 得到程序全路徑名
GetModuleFileName( NULL, strCurDirectory, 255 );
CRegistry Reg;
Reg.SetRootKey(HKEY_LOCAL_MACHINE);
Reg.SetKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true);
Reg.WriteString ("FixYou",strCurDirectory);
ShowCursor(false);
//注冊系統熱鍵
// RegisterHotKey(GetSafeHwnd(),2000,MOD_CONTROL ,'z');
//屏蔽掉系統鍵
SystemParametersInfo(SPI_SETSCREENSAVERRUNNING,true,&bOld,SPIF_UPDATEINIFILE);
//隱藏WINDOWS系統任務欄
::ShowWindow (::FindWindow("Shell_TrayWnd",NULL),SW_HIDE);
SetWindowPos(&wndTopMost,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE); //窗體總在總前面
return 0;
}
/*
//系統熱鍵響應涵數
LRESULT TransparentWnd::OnHotKey(WPARAM wParam,LPARAM lParam)
{
if(wParam==2000)
{
bMove=true;
ClipCursor(NULL); //恢復鼠標正常移動
CFixYouDlg dlg;
ShowCursor(true);//恢復鼠標和系統鍵
if(dlg.DoModal ()==IDCANCEL) // 關閉程序
{
SystemParametersInfo(SPI_SETSCREENSAVERRUNNING,false,&bOld,SPIF_UPDATEINIFILE);
DestroyWindow();
}
else //繼續程序
{
ShowCursor(false);
bMove=false;
}
}
return 0;
}
*/
BOOL TransparentWnd::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;
// return CWnd::OnEraseBkgnd(pDC);
}
void TransparentWnd::OnTimer(UINT nIDEvent)
{
switch(nIDEvent)
{
case(1)://變換圖像
DoChange();
break;
default:
break;
}
m_iAniSeq++;
if(m_iAniSeq==4)
m_iAniSeq=0;
CWnd::OnTimer(nIDEvent);
}
//自定義鼠標退出熱鍵"Ctrl+鼠標右鍵"
void TransparentWnd::OnRButtonDown(UINT nFlags, CPoint point)
{
CWnd::OnRButtonDown(nFlags, point);
//如果是同時按下鍵盤鍵Ctrl+鼠標右鍵則彈出關閉對話框
if(nFlags==(MK_CONTROL|MK_RBUTTON ))
{
bMove=true;
ClipCursor(NULL); //恢復鼠標正常移動范圍
CFixYouDlg dlg;
ShowCursor(true);//恢復鼠標
if(dlg.DoModal ()==IDCANCEL) // 關閉程序
{
//恢復系統熱鍵
SystemParametersInfo(SPI_SETSCREENSAVERRUNNING,false,&bOld,SPIF_UPDATEINIFILE);
//恢復WINDOWS系統任務欄正常顯示
::ShowWindow (::FindWindow("Shell_TrayWnd",NULL),SW_SHOW);
//刪除開機自啟動鍵值
CRegistry Reg;
Reg.SetRootKey(HKEY_LOCAL_MACHINE);
Reg.SetKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true);
Reg.DeleteValue( "FixYou");
DestroyWindow();
}
else //繼續程序
{
ShowCursor(false);
bMove=false;
}
}
}
//自定義鍵盤退出熱鍵"Alt+F8"
void TransparentWnd::OnSysKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// CWnd::OnSysKeyDown(nChar, nRepCnt, nFlags);
if( nChar==VK_F8)
{
bMove=true;
ClipCursor(NULL); //恢復鼠標正常移動范圍
CFixYouDlg dlg;
ShowCursor(true);//恢復鼠標
if(dlg.DoModal ()==IDCANCEL) // 關閉程序
{
//恢復系統熱鍵
SystemParametersInfo(SPI_SETSCREENSAVERRUNNING,false,&bOld,SPIF_UPDATEINIFILE);
//恢復WINDOWS系統任務欄正常顯示
::ShowWindow (::FindWindow("Shell_TrayWnd",NULL),SW_SHOW);
//刪除開機自啟動鍵值
CRegistry Reg;
Reg.SetRootKey(HKEY_LOCAL_MACHINE);
Reg.SetKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true);
Reg.DeleteValue( "FixYou");
DestroyWindow();
}
else //繼續程序
{
ShowCursor(false);
bMove=false;
}
}
}
void TransparentWnd::OnClose()
{
//恢復正常顯示
ClipCursor(NULL);
ShowCursor(true);
SystemParametersInfo(SPI_SETSCREENSAVERRUNNING,false,&bOld,SPIF_UPDATEINIFILE);
//恢復WINDOWS系統任務欄正常顯示
::ShowWindow (::FindWindow("Shell_TrayWnd",NULL),SW_SHOW);
//注銷已注冊的系統熱鍵
// UnregisterHotKey(GetSafeHwnd(),2000);
CWnd::OnClose();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -