加载位图=========================================
HBITMAP hBitmap=(HBITMAP)LoadImage(0,"res\\thinning.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_DEFAULTSIZE||LR_CREATEDIBSECTION);
Gdiplus::Bitmap* pTmpBitmap=Gdiplus::Bitmap::FromHBITMAP(hBitmap,NULL);
HICON hIcon=NULL;
pTmpBitmap->GetHICON(&hIcon);
delete pTmpBitmap;
sendmessage(hwnd, wm_seticon, false,hIcon);
任务栏图片修改
<pre name="code" class="cpp">// 获取到任务栏图标的绝对路径 ==
void SetIcon(HWND hwnd, const std::wstring &strfilename)
{
static HICON icon_handle = NULL; // 防止占有多个内存空间 ==
std::wstring strpath = GetResourcePath();
strpath += strfilename;
if (!nbase::FilePathIsExist(strpath, false))
return;
if (icon_handle)
{
::DestroyIcon(icon_handle);
}
Gdiplus::Bitmap bm(strpath.c_str());
bm.GetHICON(&icon_handle);
if (icon_handle)
{
::SendMessage(hwnd, WM_SETICON, (WPARAM)TRUE, (LPARAM)icon_handle);
::SendMessage(hwnd, WM_SETICON, (WPARAM)FALSE, (LPARAM)icon_handle);
}
}
hIconIn = (HICON)::LoadImage(hInstResource, MAKEINTRESOURCE(nIconIn), IMAGE_ICON, 0, 0, 0);
m_hCursor = (HCURSOR)::LoadImage(hInstResource, MAKEINTRESOURCE(nCursorId), IMAGE_CURSOR, 0, 0, 0);
将BMP转换成ICON图标
方法一、
HICON HICONFromCBitmap( CBitmap& bitmap)
{
BITMAP bmp;
bitmap.GetBitmap(&bmp);
HBITMAP hbmMask = ::CreateCompatibleBitmap(::GetDC(NULL),
bmp.bmWidth, bmp.bmHeight);
ICONINFO ii = {0};
ii.fIcon = TRUE;
ii.hbmColor = bitmap;
ii.hbmMask = hbmMask;
HICON hIcon = ::CreateIconIndirect(&ii);//一旦不再需要,注意用DestroyIcon函数释放占用的内存及资源
::DeleteObject(hbmMask);
return hIcon;
}
方法二、
#include <gdiplus.h>
#pragma comment(lib,"GdiPlus.lib")
HICON CreateIcon(HBITMAP hBitmap)
{
Gdiplus::Bitmap* pTmpBitmap=Gdiplus::Bitmap::FromHBITMAP(hBitmap,NULL);
HICON hIcon=NULL;
pTmpBitmap->GetHICON(&hIcon);
delete pTmpBitmap;
return hIcon;
}
HICON转HBITMAP
利用GetIconInfo函数获取ICONINFO信息, 再将其复制到位图当中去, 代码如下:
HBITMAP ConvertIconToBitmap(HICON hIcon)
{
HBITMAP hBmp;
BITMAP bmp;
CDC bmpDC;
CDC iconDC;
ICONINFO csII;
int bRetValue = ::GetIconInfo(hIcon, &csII);
if (bRetValue == FALSE) return NULL;
bmpDC.Attach(::GetDC(NULL));
iconDC.CreateCompatibleDC(&bmpDC);
if (::GetObject(csII.hbmColor, sizeof(BITMAP), &bmp))
{
DWORD dwWidth = csII.xHotspot*2;
DWORD dwHeight = csII.yHotspot*2;
hBmp= ::CreateBitmap(dwWidth, dwHeight, bmp.bmPlanes,
bmp.bmBitsPixel, NULL);
iconDC.SelectObject(csII.hbmColor);
bmpDC.SelectObject(hBmp);
bmpDC.BitBlt(0,0,dwWidth,dwHeight,&iconDC,0,0,SRCCOPY);
return hBmp;
}
return NULL;
}
还有一种方法就是先创建一个兼容位图, 然后通过API函数::DrawIcon()复制上去, CopyImage函数可以用来替换创建兼容DC以后的那些骤了