技术含量:没有
实现方法:UpdateLayeredWindow
完整源码下载:XunleiX.zip
// XunleiXDlg.cpp : 实现文件
//#include "stdafx.h"
#include "XunleiX.h"
#include "XunleiXDlg.h"#ifdef _DEBUG
#define new DEBUG_NEW
#endif// CXunleiXDlg 对话框CXunleiXDlg::CXunleiXDlg(CWnd* pParent /*=NULL*/): CDialog(CXunleiXDlg::IDD, pParent)
{m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);m_bTracking=m_bHover=false;
}void CXunleiXDlg::DoDataExchange(CDataExchange* pDX)
{CDialog::DoDataExchange(pDX);
}BEGIN_MESSAGE_MAP(CXunleiXDlg, CDialog)ON_WM_PAINT()ON_WM_QUERYDRAGICON()//}}AFX_MSG_MAPON_WM_MOUSEMOVE()ON_WM_LBUTTONDOWN()ON_MESSAGE(WM_MOUSEHOVER,OnMouseHover)ON_MESSAGE(WM_MOUSELEAVE,OnMouseLeave)ON_WM_RBUTTONDOWN()ON_COMMAND(IDM_TENCENT,OnMenu_Tencent)ON_COMMAND(IDM_SINA,OnMenu_Sina)ON_COMMAND(IDM_EXIT,OnMenu_Exit)
END_MESSAGE_MAP()// CXunleiXDlg 消息处理程序BOOL CXunleiXDlg::OnInitDialog()
{CDialog::OnInitDialog();// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动// 执行此操作SetIcon(m_hIcon, TRUE); // 设置大图标SetIcon(m_hIcon, FALSE); // 设置小图标//===============================================SetWindowText(_T("XunLei X"));::SetWindowPos(m_hWnd,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);InitUI();DrawUI();//===============================================m_ToolTip.Create(this);m_ToolTip.SetMaxTipWidth(1024);EnableToolTips(TRUE);CString strTooltip=_T("XunleiX 左键移动、右键弹出菜单");CRect rc(5,5,95,25);m_ToolTip.AddTool(this,strTooltip,&rc,100);//===============================================return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。void CXunleiXDlg::OnPaint()
{if (IsIconic()){CPaintDC dc(this); // 用于绘制的设备上下文SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);// 使图标在工作矩形中居中int cxIcon = GetSystemMetrics(SM_CXICON);int cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int x = (rect.Width() - cxIcon + 1) / 2;int y = (rect.Height() - cyIcon + 1) / 2;// 绘制图标dc.DrawIcon(x, y, m_hIcon);}else{CDialog::OnPaint();}
}//当用户拖动最小化窗口时系统调用此函数取得光标显示。
//
HCURSOR CXunleiXDlg::OnQueryDragIcon()
{return static_cast<HCURSOR>(m_hIcon);
}void CXunleiXDlg::InitUI(void)
{m_nWidth=100;m_nHeight=30;LoadResImage(IDR_PNG1,m_pSrcImage);//载入资源中的皮肤PNG图片ModifyStyleEx(0,WS_EX_LAYERED);
}void CXunleiXDlg::DrawUI(void)
{HDC hDC=::GetDC(m_hWnd);HDC hMemDC=::CreateCompatibleDC(hDC);BITMAPINFO bitmapinfo;bitmapinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);bitmapinfo.bmiHeader.biBitCount = 32;bitmapinfo.bmiHeader.biHeight = m_nHeight;bitmapinfo.bmiHeader.biWidth = m_nWidth;bitmapinfo.bmiHeader.biPlanes = 1;bitmapinfo.bmiHeader.biCompression=BI_RGB;bitmapinfo.bmiHeader.biXPelsPerMeter=0;bitmapinfo.bmiHeader.biYPelsPerMeter=0;bitmapinfo.bmiHeader.biClrUsed=0;bitmapinfo.bmiHeader.biClrImportant=0;bitmapinfo.bmiHeader.biSizeImage = bitmapinfo.bmiHeader.biWidth * bitmapinfo.bmiHeader.biHeight * bitmapinfo.bmiHeader.biBitCount / 8;HBITMAP hBitmap=::CreateDIBSection (hMemDC,&bitmapinfo, 0,NULL, 0, 0);HBITMAP hOldBitmap = (HBITMAP)::SelectObject (hMemDC,hBitmap); //画出各个界面元素---------------------------------------------DrawUI(hMemDC); //设置透明窗口-------------------------------------------------CPoint DestPt(0,0);CSize psize(m_nWidth,m_nHeight);BLENDFUNCTION blendFunc32bpp;blendFunc32bpp.AlphaFormat = AC_SRC_ALPHA;blendFunc32bpp.BlendFlags = 0;blendFunc32bpp.BlendOp = AC_SRC_OVER;blendFunc32bpp.SourceConstantAlpha = 255;::UpdateLayeredWindow(m_hWnd,hDC,NULL,&psize,hMemDC,&DestPt,0,&blendFunc32bpp,ULW_ALPHA);//释放资源-------------------------------------------------::SelectObject (hMemDC,hOldBitmap);::DeleteObject(hBitmap);::DeleteDC(hMemDC);::ReleaseDC(m_hWnd,hDC);
}
void CXunleiXDlg::DrawUI(HDC hDestDC)
{Graphics g(hDestDC);if(m_bHover){g.DrawImage(m_pSrcImage,RectF(0,0,100,30),0,30,100,30,UnitPixel,NULL,NULL,NULL);}else{g.DrawImage(m_pSrcImage,RectF(0,0,100,30),0,0,100,30,UnitPixel,NULL,NULL,NULL);}
}
void CXunleiXDlg::LoadResImage(int nResID,Image * &lpImage)
{HINSTANCE hIns=AfxGetInstanceHandle();HRSRC hRsrc = ::FindResource (hIns,MAKEINTRESOURCE(nResID),_T("PNG")); // type if (!hRsrc) return; // load resource into memory DWORD len = SizeofResource(hIns, hRsrc); BYTE* lpRsrc = (BYTE*)LoadResource(hIns, hRsrc); if (!lpRsrc) return; // Allocate global memory on which to create stream HGLOBAL m_hMem = GlobalAlloc(GMEM_FIXED, len); BYTE* pmem = (BYTE*)GlobalLock(m_hMem); memcpy(pmem,lpRsrc,len); IStream* pstm; CreateStreamOnHGlobal(m_hMem,FALSE,&pstm); // load from stream lpImage=Gdiplus::Image::FromStream(pstm); // free/release stuff GlobalUnlock(m_hMem); pstm->Release(); FreeResource(lpRsrc);
}
void CXunleiXDlg::OnMouseMove(UINT nFlags, CPoint point)
{CDialog::OnMouseMove(nFlags, point);if (!m_bTracking){TRACKMOUSEEVENT tme;tme.cbSize = sizeof(tme);tme.hwndTrack = m_hWnd;tme.dwFlags = TME_LEAVE | TME_HOVER;tme.dwHoverTime = 50;m_bTracking = (bool)_TrackMouseEvent(&tme);}
}
LRESULT CXunleiXDlg::OnMouseHover(WPARAM wParam, LPARAM lParam)
{m_bHover=true;DrawUI();return 0;
}
LRESULT CXunleiXDlg::OnMouseLeave(WPARAM wParam, LPARAM lParam)
{m_bTracking=false;m_bHover=false;DrawUI();return 0;
}
void CXunleiXDlg::OnLButtonDown(UINT nFlags, CPoint point)
{CDialog::OnLButtonDown(nFlags, point);ReleaseCapture ();SendMessage (WM_NCLBUTTONDOWN, HTCAPTION, 0);
}void CXunleiXDlg::OnRButtonDown(UINT nFlags, CPoint point)
{CDialog::OnRButtonDown(nFlags, point);ReleaseCapture ();TrackPopupMenu();
}
void CXunleiXDlg::TrackPopupMenu()
{CMenu menu;menu.CreatePopupMenu();menu.AppendMenu(MF_ENABLED|MF_STRING,0,_T("XunleiX"));menu.AppendMenu(MF_SEPARATOR);menu.AppendMenu(MF_ENABLED|MF_STRING,IDM_TENCENT,_T("作者腾讯微博(&T)"));menu.AppendMenu(MF_ENABLED|MF_STRING,IDM_SINA,_T("作者新浪微博(&W)"));menu.AppendMenu(MF_SEPARATOR);menu.AppendMenu(MF_ENABLED|MF_STRING,IDM_EXIT,_T("退出(&X)"));//==================================================CRect rc;GetWindowRect(&rc);menu.TrackPopupMenu(TPM_LEFTBUTTON,rc.left,rc.bottom,this);
}
void CXunleiXDlg::OnMenu_Tencent()
{ShellExecute(NULL,_T("open"),_T("http://t.qq.com/dengxuebin"),NULL,NULL,SW_SHOW);
}
void CXunleiXDlg::OnMenu_Sina()
{ShellExecute(NULL,_T("open"),_T("http://www.weibo.com/dengxuebin"),NULL,NULL,SW_SHOW);
}
void CXunleiXDlg::OnMenu_Exit()
{PostMessage(WM_CLOSE);
}
BOOL CXunleiXDlg::PreTranslateMessage(MSG* pMsg)
{m_ToolTip.RelayEvent(pMsg);return CDialog::PreTranslateMessage(pMsg);
}
完整源码下载: XunleiX.zip