📄 transparentwnd.cpp
字号:
// TransparentWnd.cpp : implementation file
#include "stdafx.h"
#include <Mmsystem.h>
#include "TransparentWnd.h"
#include "BCMenu.h" //添加位图菜单
#include "resource.h"
#include "statlink.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
BCMenu popmenu; //定义位图菜单变量
/////////////////////////////////////////////////////////////////////////////
// TransparentWnd
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
CStaticLink m_link;
// 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)
virtual BOOL OnInitDialog();
//}}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)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// App command to run the dialog
BOOL CAboutDlg::OnInitDialog()
{
CDialog::OnInitDialog();
m_link .m_link=_T("mailto:fancy_l@sina.com.cn");
m_link.SubclassDlgItem (IDC_NAME ,this);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
//////////////////////////////////////////////////////////////////////////////
TransparentWnd::TransparentWnd()
{
m_iAniSeq=0;
m_szwindows.cx =250;
m_szwindows.cy =400;
}
TransparentWnd::~TransparentWnd()
{
}
BEGIN_MESSAGE_MAP(TransparentWnd, CWnd)
//{{AFX_MSG_MAP(TransparentWnd)
ON_WM_LBUTTONDOWN()
ON_WM_CREATE()
ON_WM_ERASEBKGND()
ON_WM_TIMER()
ON_WM_RBUTTONDOWN()
ON_COMMAND(IDR_HELP, OnHelp)
ON_COMMAND(IDR_EXIT, OnExit)
ON_WM_SETFOCUS()
ON_WM_TIMER()
//}}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(MAKEINTRESOURCE(IDB_SPLA));
//显示第一个图片。
ShowWindow (SW_SHOW);
// Invalidate ();
SetTimer (1,0,NULL);
}
//********************************************************************************
//* 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);
ShowWindow (SW_HIDE);
SetWindowRgn((HRGN)wndRgn, TRUE);
MoveWindow(rcNewWnd);
}
void TransparentWnd::DoChange(char *szBmp)
{
//图像
m_bmpDraw.DeleteObject();
m_bmpDraw.LoadBitmap(szBmp);
CWindowDC dc(this);
SetupRegion(&dc);
}
void TransparentWnd::OnLButtonDown(UINT nFlags, CPoint point)
{
CWnd::OnLButtonDown(nFlags, point);
//实现无标题拖动
PostMessage(WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM(point.x,point.y));
}
int TransparentWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
SetWindowPos(&wndTopMost,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE); //窗体总在总前面
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:{
KillTimer (nIDEvent);
//显示主窗口。
DoChange(MAKEINTRESOURCE(IDB_MAIN2));
if (!m_wndView.Create(NULL, NULL, AFX_WS_DEFAULT_VIEW,
CRect(260, 30,630, 370), this, AFX_IDW_PANE_FIRST, NULL))
{
TRACE0("Failed to create view window\n");
;
} // 创建视图窗口用于显示动画。
CenterWindow ();
ShowWindow (SW_SHOW);
}
break;
case 2:{
}
break;
default:
;
}
CWnd::OnTimer(nIDEvent);
}
void TransparentWnd::OnRButtonDown(UINT nFlags, CPoint point)
{
CWnd::OnRButtonDown(nFlags, point);
//添加右键快捷图标菜单(用于下拉式菜单)
popmenu.LoadMenu(IDR_MENU);
popmenu.ModifyODMenu(NULL, IDR_HELP,IDB_HELP);
popmenu.ModifyODMenu(NULL,IDR_EXIT,IDB_EXIT);
ClientToScreen(&point);
BCMenu *psub = (BCMenu *)popmenu.GetSubMenu(0);
psub->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,point.x,point.y,this);
popmenu.DestroyMenu();
}
void TransparentWnd::OnHelp()
{
CAboutDlg dlg;
if( !FindWindow(NULL,"关于生日快乐"))
dlg.DoModal();
}
void TransparentWnd::OnExit()
{
DestroyWindow();
}
BOOL TransparentWnd::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
return CWnd::PreTranslateMessage(pMsg);
}
BOOL TransparentWnd::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{
if (m_wndView.OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
return TRUE;
//先处理视的command 消息,如果不是则由主窗口处理.
// TODO: Add your specialized code here and/or call the base class
return CWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}
void TransparentWnd::OnSetFocus(CWnd* pOldWnd)
{
// forward focus to the view window
if (m_wndView.m_hWnd !=NULL)
m_wndView.SetFocus();
//当主窗口获得焦点时,激活视窗口。
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -