📄 transparentwnd.cpp
字号:
END_MESSAGE_MAP()
//********************************************************************************
//* CreateTransparent()
//*
//* Creates the main application window transparent
//********************************************************************************
void CTransparentWnd::CreateTransparent(LPCTSTR pTitle)
{
FindBmpFile(GetCurPath());
CDib dib(m_szBmpFile);
CRect rect(0,0,dib.m_nWidth,dib.m_nHeight);
CreateEx( WS_EX_TOOLWINDOW,
AfxRegisterWndClass(0),
pTitle,
WS_POPUP | WS_SYSMENU|WS_EX_TOOLWINDOW,
rect,
NULL,
NULL,
NULL );
SetupRegion(GetWindowDC());
//m_BitmapID = BitmapID;
SetTimer(1,100,NULL);
// NOTIFYICONDATA tnd;
//
// tnd.cbSize=sizeof(NOTIFYICONDATA);
//
// tnd.hWnd=this->m_hWnd;
//
// tnd.uID=IDR_MAINFRAME;
//
// tnd.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;
//
// tnd.uCallbackMessage=WM_LIBEN;
//
// tnd.hIcon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));
//
// wcscpy(tnd.szTip,TEXT("提示信息"));
//
// Shell_NotifyIcon(NIM_ADD,&tnd);
//
// SetWindowPos(
// (GetExStyle() & WS_EX_TOPMOST) ? &CWnd::wndNoTopMost : &CWnd::wndTopMost,
// 0,0,0,0,
// SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
//
// HCURSOR hCursor;
//
// // Load a cursor resource that was originally created using
// hCursor = AfxGetApp()->LoadCursor(IDC_CURSOR1);
// SetCursor(hCursor);
//
// //加入WS_EX_LAYERED扩展属性
// SetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE,
// GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)^0x80000);
// HINSTANCE hInst = LoadLibrary(TEXT("User32.DLL"));
// if(hInst)
// {
// typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);
// MYFUNC fun = NULL;
// //取得SetLayeredWindowAttributes函数指针
// fun=(MYFUNC)GetProcAddress(hInst, TEXT("SetLayeredWindowAttributes"));
// if(fun)fun(this->GetSafeHwnd(),0,200,2);
// FreeLibrary(hInst);
// }
}
//********************************************************************************
//* SetupRegion()
//*
//* Set the Window Region for transparancy outside the mask region
//********************************************************************************
void CTransparentWnd::SetupRegion(CDC *pDC)
{
CDC memDC;
CBitmap cBitmap;
CBitmap* pOldMemBmp = NULL;
COLORREF col,maskcol;
CRect cRect;
int x, y;
static CRgn wndRgn;
CRgn rgnTemp;
int sum;
GetWindowRect(&cRect);
//cBitmap.LoadBitmap(MaskID);
LoadBMPImage(m_szBmpFile,cBitmap,NULL);
memDC.CreateCompatibleDC(pDC);
pOldMemBmp = memDC.SelectObject(&cBitmap);
wndRgn.CreateRectRgn(0, 0, cRect.Width(), cRect.Height());
maskcol=memDC.GetPixel(0, 0);
for(x=0; x<=cRect.Width(); x++)
{
for(y=0; y<=cRect.Height(); y++)
{
col = memDC.GetPixel(x, y);
if(col ==maskcol)
{
rgnTemp.CreateRectRgn(x, y, x+1, y+1);
wndRgn.CombineRgn(&wndRgn, &rgnTemp, RGN_XOR);
rgnTemp.DeleteObject();
}
else if (col==0)
{
//判断数字的起始黑点
sum=0;
for(int k=0;k<25;k++)
{
sum=sum+memDC.GetPixel(x, y+k)+memDC.GetPixel(x+109, y+k);
if (sum!=0)
break;
}
if (sum==0) //得到起始点x,y
{
m_nNumX=x;
m_nNumY=y;
}
}
}
}
if (pOldMemBmp) memDC.SelectObject(pOldMemBmp);
LoadBMPImage(m_szNumFile,m_mapB,NULL);
assert(wndRgn.RectInRegion(&cRect));
SetWindowRgn((HRGN)wndRgn, TRUE);
}
//********************************************************************************
//* CTransparentWnd message handlers
//********************************************************************************
void CTransparentWnd::OnLButtonDown(UINT nFlags, CPoint point)
{
CWnd::OnLButtonDown(nFlags, point);
// PostMessage(WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM(point.x,point.y));
}
BOOL CTransparentWnd::OnEraseBkgnd(CDC* pDC)
{
CRect rect;
GetWindowRect(&rect);
CDC memDC;
CBitmap cBitmap;
CBitmap* pOldMemBmp = NULL;
LoadBMPImage(m_szBmpFile,cBitmap,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;
}
//********************************************************************************
// draw the current time
//********************************************************************************
void CTransparentWnd::OnPaint()
{
CPaintDC dc(this);
CDC mapDC;
mapDC.CreateCompatibleDC( &dc );
CBitmap* pOB = mapDC.SelectObject( &m_mapB );
dc.BitBlt( m_nNumX, m_nNumY, 2, 25, &mapDC, 160, 0, SRCCOPY );
dc.BitBlt( m_nNumX+70, m_nNumY, 8, 25, &mapDC, 150, 0, SRCCOPY );
//秒
dc.BitBlt( m_nNumX+78, m_nNumY, 15, 25, &mapDC, m_ns1*15, 0, SRCCOPY );
dc.BitBlt( m_nNumX+93, m_nNumY, 15, 25, &mapDC, m_ns2*15, 0, SRCCOPY );
//时
dc.BitBlt( m_nNumX+2, m_nNumY, 15, 25, &mapDC, m_nh1*15, 0, SRCCOPY );
dc.BitBlt( m_nNumX+17, m_nNumY, 15, 25, &mapDC, m_nh2*15, 0, SRCCOPY );
//:
dc.BitBlt( m_nNumX+32, m_nNumY, 8, 25, &mapDC, 150, 0, SRCCOPY );
//分
dc.BitBlt( m_nNumX+40, m_nNumY, 15, 25, &mapDC, m_nm1*15, 0, SRCCOPY );
dc.BitBlt( m_nNumX+55, m_nNumY, 15, 25, &mapDC, m_nm2*15, 0, SRCCOPY );
//:
dc.BitBlt( m_nNumX+108, m_nNumY, 2, 25, &mapDC, 150, 0, SRCCOPY );
mapDC.SelectObject( pOB );
}
//********************************************************************************
// Get the Current time then set value the mambervar
// If the time changed ,repaint it,otherwise return
//********************************************************************************
void CTransparentWnd::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CTime ctCurrentTime;
ctCurrentTime = CTime::GetCurrentTime();
int h=ctCurrentTime.GetHour();
//int y=ctCurrentTime.GetYear();
//int m=ctCurrentTime.GetMonth();
//int d=ctCurrentTime.GetDay();
int f=ctCurrentTime.GetMinute();
int s=ctCurrentTime.GetSecond();
if (m_nSecond==s)
return;
else
m_nSecond=s;
m_ns1=m_nSecond/10;
m_ns2=m_nSecond%10;
m_nh1=h/10;
m_nh2=h%10;
m_nm1=f/10;
m_nm2=f%10;
//只重绘时钟区域
Invalidate(FALSE);
CWnd::OnTimer(nIDEvent);
}
//********************************************************************************
// the tray icon message loop
//********************************************************************************
void CTransparentWnd::OnLiben(WPARAM wParam,LPARAM lParam)
{
if(wParam!=IDR_MAINFRAME)
return;
switch(lParam)
{
case WM_RBUTTONUP://右键起来时弹出快捷菜单,这里只有一个“关闭”
{
LPPOINT lpoint=new tagPOINT;
::GetCursorPos(lpoint);//得到鼠标位置
CMenu *menu;
CMenu PopMenu;
PopMenu.LoadMenu(IDR_MENU_POPUP);
menu = PopMenu.GetSubMenu (0);
//确定弹出式菜单的位置
menu->TrackPopupMenu(TPM_LEFTALIGN,lpoint->x,lpoint->y,this);
//资源回收
HMENU hmenu=menu->Detach();
menu->DestroyMenu();
delete lpoint;
}
break;
case WM_LBUTTONDBLCLK://双击左键的处理
{
this->ShowWindow(SW_SHOW);//简单的显示主窗口完事儿
}
break;
}
return;
}
// LRESULT CTransparentWnd::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
// {
// if (message == WM_LIBEN)
// {
// OnLiben(wParam, lParam);
// }
// return CWnd::WindowProc(message, wParam, lParam);
// }
void CTransparentWnd::OnMinIcon()
{
ShowWindow(SW_HIDE);
}
//********************************************************************************
// Get the mouse pos and draw the pop menu
//********************************************************************************
void CTransparentWnd::OnContextMenu(CWnd* pWnd, CPoint point)
{
CMenu PopMenu;
CMenu *pMenu;
LPPOINT lpoint=new tagPOINT;
::GetCursorPos(lpoint);//得到鼠标位置
CPoint pt;
pt= point ;
PopMenu.LoadMenu(IDR_MENU_POPUP);
ClientToScreen (&pt);
// show the menu (returns, when menu is closed again!)
pMenu = PopMenu.GetSubMenu (0);
pMenu->TrackPopupMenu (TPM_LEFTALIGN,lpoint->x,lpoint->y,this);
}
//********************************************************************************
// When the App exit,delete the tray icon
//********************************************************************************
void CTransparentWnd::OnClose()
{
NOTIFYICONDATA tnid;
tnid.cbSize=sizeof(NOTIFYICONDATA);
tnid.hWnd=this->m_hWnd;
tnid.uID=IDR_MAINFRAME;//保证删除的是我们的图标
Shell_NotifyIcon(NIM_DELETE,&tnid);
SendMessage(WM_CLOSE,0,0);
}
BOOL CTransparentWnd::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class
return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -