15,979
社区成员
发帖
与我相关
我的任务
分享
#define MYWM_NOTIFYICON WM_USER + 1
BOOL CTest001Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
static CDC dcScreen;
static CRect rect;
static CRect headerRect;
static CDC dcMem;
static CBitmap bmpMem;
static CDC dcMemMask;
static CBitmap bmpMemMask;
dcScreen.Attach(::GetDC(0));
rect = CRect(0, 0, 16, 16);
// 绘图
dcMem.CreateCompatibleDC(&dcScreen);
bmpMem.CreateCompatibleBitmap(&dcScreen, rect.Width(), rect.Height());
dcMem.SelectObject(&bmpMem);
dcMem.FillSolidRect(&rect, RGB(255,255,255));
dcMem.SetTextColor(RGB(0,0,0));
dcMem.TextOut(0, 0, "FF");
dcMemMask.CreateCompatibleDC(&dcScreen);
bmpMemMask.CreateCompatibleBitmap(&dcScreen, rect.Width(), rect.Height());
dcMemMask.SelectObject(&bmpMemMask);
dcMemMask.FillSolidRect(&rect, RGB(255,255,255));
// 创建图标
NOTIFYICONDATA nd;
HICON hIcon;
ICONINFO cInfo;
cInfo.fIcon = TRUE;
cInfo.xHotspot = 0;
cInfo.yHotspot = 0;
cInfo.hbmMask = (HBITMAP)bmpMemMask;
cInfo.hbmColor = (HBITMAP)bmpMem;
hIcon = CreateIconIndirect(&cInfo);
// 图标加入到通知区域
nd.cbSize = sizeof(NOTIFYICONDATA);
nd.hWnd = m_hWnd;
nd.uID = IDR_MAINFRAME;
nd.uFlags = NIF_ICON|NIF_MESSAGE|NIF_TIP;
nd.uCallbackMessage = MYWM_NOTIFYICON;
nd.hIcon = hIcon;
strcpy(nd.szTip, "Tooltip");
Shell_NotifyIcon(NIM_ADD, &nd);
return TRUE; // return TRUE unless you set the focus to a control
}
HICON MakeFullTextIcon(LPCTSTR lpText)
{
HICON hIcon;
BITMAP bmp;
DWORD dwSize;
COLORREF *lpData, *lpSourceData;
HBITMAP m_hObject = NULL, hOldSrc = NULL, hOldDest = NULL;
CBitmap bmpObject;
CBitmap *pBmpOld = NULL;
CBitmap *pBmpOldSrc = NULL;
BITMAPINFOHEADER bmi = {0};
CDC dcSrc, dcDest, dcScr;
HDC hdc;
hdc =::GetDC(NULL);
dcScr.Attach(hdc);
dcDest.CreateCompatibleDC(&dcScr);
dcSrc.CreateCompatibleDC(&dcScr);
bmi.biClrImportant = 0;
bmi.biClrUsed = 0;
bmi.biCompression = BI_RGB;
bmi.biWidth = 16;
bmi.biHeight = 16;
bmi.biPlanes = 1;
bmi.biSize = sizeof(bmi);
bmi.biSizeImage = 0;
bmi.biXPelsPerMeter = 0;
bmi.biYPelsPerMeter = 0;
bmi.biBitCount = 32;
m_hObject = CreateDIBSection(dcScr, (LPBITMAPINFO) & bmi, 0, (void**) & lpSourceData, NULL, DIB_RGB_COLORS);
bmpObject.Attach(m_hObject);
pBmpOld = dcDest.SelectObject(&bmpObject);
dcDest.SetBkMode(TRANSPARENT);
dcDest.SetTextColor(RGB(255, 255, 255));
dcDest.TextOut(0, 0, lpText);
// dcSrc.SelectObject(pBmpOldSrc);
dcDest.SelectObject(pBmpOld);
dcSrc.DeleteDC();
dcDest.DeleteDC();
dcScr.Detach();
ReleaseDC(NULL, hdc);
::GetObject(m_hObject, sizeof(BITMAP), &bmp);
if (bmp.bmHeight == 0 || bmp.bmWidth == 0)
return NULL;
HBITMAP hMask, hColor;
LPCOLORREF lpColorData, lpMaskData;
BITMAPINFOHEADER bmi;
bmi.biBitCount = 32;
bmi.biClrImportant = 0;
bmi.biClrUsed = 0;
bmi.biCompression = BI_RGB;
bmi.biHeight = bmp.bmHeight;
bmi.biWidth = bmp.bmWidth;
bmi.biPlanes = 1;
bmi.biSize = sizeof(bmi);
bmi.biSizeImage = 0;
bmi.biXPelsPerMeter = 0;
bmi.biYPelsPerMeter = 0;
hColor = CreateDIBSection(NULL, (LPBITMAPINFO) & bmi, 0, (void**) & lpColorData, NULL, DIB_RGB_COLORS);
if (hColor == NULL)
return NULL;
hMask = CreateDIBSection(NULL, (LPBITMAPINFO) & bmi, 0, (void**) & lpMaskData, NULL, DIB_RGB_COLORS);
if (hMask == NULL)
{
::DeleteObject(hColor);
return NULL;
}
dwSize = bmp.bmHeight * bmp.bmWidth;
lpData = lpSourceData;
for (DWORD d = 0; d < dwSize; d++)
{
if (*lpData == 0)
{
lpMaskData[d] = 0xFFFFFF;
lpColorData[d] = 0;
}
else
{
lpMaskData[d] = 0;
lpColorData[d] = *lpData;
}
lpData++;
}
ICONINFO ii;
ii.fIcon = TRUE;
ii.hbmColor = hColor;
ii.hbmMask = hMask;
ii.xHotspot = 0;
ii.yHotspot = 0;
hIcon = CreateIconIndirect(&ii);
::DeleteObject(hMask);
::DeleteObject(hColor);
return hIcon;
}
NOTIFYICONDATA nd;
HICON hIcon = MakeFullTextIcon( _T("FF") ) ;
// 图标加入到通知区域
nd.cbSize = sizeof(NOTIFYICONDATA);
nd.hWnd = m_hWnd;
nd.uID = IDR_MAINFRAME;
nd.uFlags = NIF_ICON|NIF_MESSAGE|NIF_TIP;
nd.uCallbackMessage = 0x401;
nd.hIcon = hIcon;
lstrcpyn( nd.szTip, _T("Tooltip"), 128 );
Shell_NotifyIcon(NIM_ADD, &nd);
HICON MakeFullTextIcon(LPCTSTR lpText)
{
HICON hIcon;
BITMAP bmp;
DWORD dwSize;
COLORREF *lpData, *lpSourceData;
HBITMAP m_hObject = NULL,hOldSrc = NULL, hOldDest = NULL;
{
BITMAPINFOHEADER bmi = {0};
CDC dcSrc, dcDest, dcScr;
HDC hdc;
hdc=::GetDC(NULL);
dcScr.Attach(hdc);
dcDest.CreateCompatibleDC(dcScr);
dcSrc.CreateCompatibleDC(dcScr);
bmi.biClrImportant=0;
bmi.biClrUsed=0;
bmi.biCompression=BI_RGB;
bmi.biWidth=16;
bmi.biHeight=16;
bmi.biPlanes=1;
bmi.biSize=sizeof(bmi);
bmi.biSizeImage=0;
bmi.biXPelsPerMeter=0;
bmi.biYPelsPerMeter=0;
bmi.biBitCount = 32;
m_hObject=CreateDIBSection(dcScr, (LPBITMAPINFO)&bmi, 0, (void**)&lpSourceData, NULL, DIB_RGB_COLORS);
hOldDest=dcDest.SelectBitmap(m_hObject);
dcDest.SetBkMode(TRANSPARENT);
dcDest.SetTextColor(RGB(255,0,0));
dcDest.TextOut( 0, 0, lpText );
dcSrc.SelectBitmap(hOldSrc);
dcDest.SelectBitmap(hOldDest);
dcSrc.DeleteDC();
dcDest.DeleteDC();
dcScr.Detach();
ReleaseDC(NULL, hdc);
::GetObject(m_hObject, sizeof(BITMAP), &bmp);
}
if(bmp.bmHeight==0 || bmp.bmWidth==0)
return NULL;
HBITMAP hMask, hColor;
LPCOLORREF lpColorData, lpMaskData;
BITMAPINFOHEADER bmi;
bmi.biBitCount=32;
bmi.biClrImportant=0;
bmi.biClrUsed=0;
bmi.biCompression=BI_RGB;
bmi.biHeight=bmp.bmHeight;
bmi.biWidth=bmp.bmWidth;
bmi.biPlanes=1;
bmi.biSize=sizeof(bmi);
bmi.biSizeImage=0;
bmi.biXPelsPerMeter=0;
bmi.biYPelsPerMeter=0;
hColor=CreateDIBSection(NULL, (LPBITMAPINFO)&bmi, 0, (void**)&lpColorData, NULL, DIB_RGB_COLORS);
if(hColor==NULL)
return NULL;
hMask=CreateDIBSection(NULL, (LPBITMAPINFO)&bmi, 0, (void**)&lpMaskData, NULL, DIB_RGB_COLORS);
if(hMask==NULL)
{
::DeleteObject(hColor);
return NULL;
}
dwSize=bmp.bmHeight*bmp.bmWidth;
lpData=lpSourceData;
for(DWORD d=0; d<dwSize; d++)
{
if(*lpData==0)
{
lpMaskData[d]=0xFFFFFF;
lpColorData[d]=0;
}
else
{
lpMaskData[d]=0;
lpColorData[d]=*lpData;
}
lpData++;
}
ICONINFO ii;
ii.fIcon=TRUE;
ii.hbmColor=hColor;
ii.hbmMask=hMask;
ii.xHotspot=0;
ii.yHotspot=0;
hIcon=CreateIconIndirect(&ii);
::DeleteObject(hMask);
::DeleteObject(hColor);
return hIcon;
}