HICON CSIM_ext::createIcon(const char *name) { ICON_MAP::iterator it = m_icons.find(name); if (it != m_icons.end()) return (*it).second; string cmd = "ICON "; cmd += name; CComBSTR in(cmd.c_str()); CComBSTR out; if (!ProcessStr || !ProcessStr(in, &out)) return NULL; size_t size = WideCharToMultiByte(CP_ACP, 0, out, wcslen(out), 0, 0, NULL, NULL); char *res = new char[size + 1]; size = WideCharToMultiByte(CP_ACP, 0, out, wcslen(out), res, size, NULL, NULL); res[size] = 0; if (res[0] != '>') return NULL; string r = res + 1; ICONINFO info; info.fIcon = TRUE; info.xHotspot = 8; info.yHotspot = 8; info.hbmMask = createBitmap(r); info.hbmColor = createBitmap(r); HICON hIcon = CreateIconIndirect(&info); m_icons.insert(ICON_MAP::value_type(name, hIcon)); DeleteObject(info.hbmMask); DeleteObject(info.hbmColor); return hIcon; }
bool IconImpl::Create(const Image& icon) { Image windowsIcon(icon); // Vive le COW if (!windowsIcon.Convert(PixelFormatType_BGRA8)) { NazaraError("Failed to convert icon to BGRA8"); return false; } HBITMAP bitmap = CreateBitmap(windowsIcon.GetWidth(), windowsIcon.GetHeight(), 1, 32, windowsIcon.GetConstPixels()); HBITMAP monoBitmap = CreateBitmap(windowsIcon.GetWidth(), windowsIcon.GetHeight(), 1, 1, nullptr); // http://msdn.microsoft.com/en-us/library/windows/desktop/ms648052(v=vs.85).aspx ICONINFO iconInfo; iconInfo.fIcon = TRUE; iconInfo.hbmMask = monoBitmap; iconInfo.hbmColor = bitmap; m_icon = CreateIconIndirect(&iconInfo); DeleteObject(bitmap); DeleteObject(monoBitmap); if (!m_icon) { NazaraError("Failed to create icon: " + Error::GetLastSystemError()); return false; } return true; }
static Status sys_cursor_create_common(int w, int h, void* bgra_img, void* mask_img, int hx, int hy, sys_cursor* cursor) { *cursor = 0; // MSDN says selecting this HBITMAP into a DC is slower since we use // CreateBitmap; bpp/format must be checked against those of the DC. // this is the simplest way and we don't care about slight performance // differences because this is typically only called once. HBITMAP hbmColor = CreateBitmap(w, h, 1, 32, bgra_img); // CreateIconIndirect doesn't access this; we just need to pass // an empty bitmap. HBITMAP hbmMask = CreateBitmap(w, h, 1, 1, mask_img); // create the cursor (really an icon; they differ only in // fIcon and the hotspot definitions). ICONINFO ii; ii.fIcon = FALSE; // cursor ii.xHotspot = (DWORD)hx; ii.yHotspot = (DWORD)hy; ii.hbmMask = hbmMask; ii.hbmColor = hbmColor; HICON hIcon = CreateIconIndirect(&ii); // CreateIconIndirect makes copies, so we no longer need these. DeleteObject(hbmMask); DeleteObject(hbmColor); if(!wutil_IsValidHandle(hIcon)) WARN_RETURN(ERR::FAIL); *cursor = cursor_from_HICON(hIcon); return INFO::OK; }
*/ HCURSOR Image_To_Cursor(REBYTE* image, REBINT width, REBINT height) /* ** Converts REBOL image! to Windows CURSOR ** ***********************************************************************/ { int xHotspot = 0; int yHotspot = 0; HICON result = NULL; HBITMAP hSourceBitmap; BITMAPINFO BitmapInfo; ICONINFO iconinfo; //Get the system display DC HDC hDC = GetDC(NULL); //Create DIB unsigned char* ppvBits; int bmlen = width * height * 4; int i; BitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); BitmapInfo.bmiHeader.biWidth = width; BitmapInfo.bmiHeader.biHeight = -(signed)height; BitmapInfo.bmiHeader.biPlanes = 1; BitmapInfo.bmiHeader.biBitCount = 32; BitmapInfo.bmiHeader.biCompression = BI_RGB; BitmapInfo.bmiHeader.biSizeImage = 0; BitmapInfo.bmiHeader.biXPelsPerMeter = 0; BitmapInfo.bmiHeader.biYPelsPerMeter = 0; BitmapInfo.bmiHeader.biClrUsed = 0; BitmapInfo.bmiHeader.biClrImportant = 0; hSourceBitmap = CreateDIBSection(hDC, &BitmapInfo, DIB_RGB_COLORS, (void**)&ppvBits, NULL, 0); //Release the system display DC ReleaseDC(NULL, hDC); //Copy the image content to DIB COPY_MEM(ppvBits, image, bmlen); //Invert alphachannel from the REBOL format for (i = 3;i < bmlen;i+=4){ ppvBits[i] ^= 0xff; } //Create the cursor using the masks and the hotspot values provided iconinfo.fIcon = FALSE; iconinfo.xHotspot = xHotspot; iconinfo.yHotspot = yHotspot; iconinfo.hbmMask = hSourceBitmap; iconinfo.hbmColor = hSourceBitmap; result = CreateIconIndirect(&iconinfo); DeleteObject(hSourceBitmap); return result; }
static RD_HCURSOR l_ui_create_cursor(struct rdp_inst * inst, uint32 x, uint32 y, int width, int height, uint8 * andmask, uint8 * xormask, int bpp) { wfInfo * wfi; HCURSOR cursor; ICONINFO iconinfo; uint8 * cdata; wfi = GET_WFI(inst); if (bpp == 1) { cursor = CreateCursor(g_hInstance, x, y, width, height, andmask, xormask); } else { iconinfo.fIcon = FALSE; iconinfo.xHotspot = x; iconinfo.yHotspot = y; cdata = wf_cursor_mask_convert(wfi, width, height, andmask); iconinfo.hbmMask = CreateBitmap(width, height, 1, 1, cdata); iconinfo.hbmColor = wf_create_dib(wfi, width, height, bpp, 0, xormask); cursor = CreateIconIndirect(&iconinfo); DeleteObject(iconinfo.hbmMask); DeleteObject(iconinfo.hbmColor); free(cdata); } return (RD_HCURSOR)cursor; }
HCURSOR QWindowsCursor::createPixmapCursor(const QPixmap &pixmap, int hotX, int hotY) { HCURSOR cur = 0; QBitmap mask = pixmap.mask(); if (mask.isNull()) { mask = QBitmap(pixmap.size()); mask.fill(Qt::color1); } HBITMAP ic = qt_pixmapToWinHBITMAP(pixmap, /* HBitmapAlpha */ 2); const HBITMAP im = qt_createIconMask(mask); ICONINFO ii; ii.fIcon = 0; ii.xHotspot = hotX; ii.yHotspot = hotY; ii.hbmMask = im; ii.hbmColor = ic; cur = CreateIconIndirect(&ii); DeleteObject(ic); DeleteObject(im); return cur; }
HICON CTrayNotifyIcon::BitmapToIcon(CBitmap* pBitmap) { //Validate our parameters ASSERT(pBitmap); //Get the width and height of a small icon int w = GetSystemMetrics(SM_CXSMICON); int h = GetSystemMetrics(SM_CYSMICON); //Create a 0 mask int nMaskSize = h*(w/8); unsigned char* pMask = new unsigned char[nMaskSize]; ZeroMemory(pMask, nMaskSize); //Create a mask bitmap CBitmap maskBitmap; BOOL bSuccess = maskBitmap.CreateBitmap(w, h, 1, 1, pMask); //Free up the heap memory now that we have created the mask bitmap delete [] pMask; //Handle the error if (!bSuccess) return NULL; //Create an ICON base on the bitmap just created ICONINFO iconInfo; iconInfo.fIcon = TRUE; iconInfo.xHotspot = 0; iconInfo.yHotspot = 0; iconInfo.hbmMask = maskBitmap; iconInfo.hbmColor = *pBitmap; return CreateIconIndirect(&iconInfo); }
// /// Creates an icon object with the given ICONINFO information. // TIcon::TIcon(const ICONINFO& iconInfo) { WARN(!iconInfo.fIcon, "TIcon constructor called with ICONINFO::fIcon == false"); // Turn this into a precondition? ICONINFO i = iconInfo; // Make a clone, since CreateIconIndirect is not const-correct. Handle = CreateIconIndirect(&i); CheckValid(); }
HICON PhpSearchBitmapToIcon( _In_ HBITMAP BitmapHandle, _In_ INT Width, _In_ INT Height ) { HICON icon; HDC screenDc; HBITMAP screenBitmap; ICONINFO iconInfo = { 0 }; screenDc = GetDC(NULL); screenBitmap = CreateCompatibleBitmap(screenDc, Width, Height); iconInfo.fIcon = TRUE; iconInfo.hbmColor = BitmapHandle; iconInfo.hbmMask = screenBitmap; icon = CreateIconIndirect(&iconInfo); DeleteObject(screenBitmap); ReleaseDC(NULL, screenDc); return icon; }
static void setcursor(uint8_t * rgba, int16_t hotx, int16_t hoty) { HDC dc; HBITMAP bm; ICONINFO ii; BITMAPV5HEADER bh; int i; uint8_t * bits; ZeroMemory(&bh, sizeof(bh)); bh.bV5Size = sizeof(bh); bh.bV5Width = 32; bh.bV5Height = -32; bh.bV5Planes = 1; bh.bV5BitCount = 32; bh.bV5Compression = BI_RGB; dc = GetDC(g_win); bm = CreateDIBSection(dc, (BITMAPINFO*)&bh, DIB_RGB_COLORS, (void **)&bits, 0, 0); ReleaseDC(g_win, dc); for (i = 0; i < 32*32; i++) { bits[4*i+0] = rgba[4*i+2]; bits[4*i+1] = rgba[4*i+1]; bits[4*i+2] = rgba[4*i+0]; bits[4*i+3] = rgba[4*i+3]; } if (bits[3] == 0) bits[3] = 1; // workaround for vbox ii.fIcon = FALSE; ii.xHotspot = hotx; ii.yHotspot = hoty; ii.hbmColor = bm; ii.hbmMask = CreateBitmap(32, 32, 1, 1, 0); g_cursor = CreateIconIndirect(&ii); SetCursor(g_cursor); DeleteObject(bm); DeleteObject(ii.hbmMask); }
Cursor::Cursor(fs::path imgPath, float hotSpotX, float hotSpotY) { sf::Image gif; if(!gif.loadFromFile(imgPath.string())) { std::string error = "Error loading cursor from " + imgPath.string(); throw error; } // Calculate the AND and XOR masks HBITMAP cursorAnd = CreateCompatibleBitmap(GetDC(NULL), gif.getSize().x, gif.getSize().y); HBITMAP cursorXor = CreateCompatibleBitmap(GetDC(NULL), gif.getSize().x, gif.getSize().y); GetMaskBitmaps(gif, cursorAnd, cursorXor); ICONINFO iconinfo = {0}; iconinfo.fIcon = FALSE; iconinfo.xHotspot = hotSpotX; iconinfo.yHotspot = hotSpotY; iconinfo.hbmMask = cursorAnd; iconinfo.hbmColor = cursorXor; HCURSOR hCursor = CreateIconIndirect(&iconinfo); if(hCursor == NULL) { std::string error = "Error creating cursor from " + imgPath.string(); error += " (error code " + std::to_string(GetLastError()) + ")"; throw error; } ptr = hCursor; DeleteObject(cursorAnd); DeleteObject(cursorXor); }
static HICON CreateLayoutIcon(LPTSTR szInd) { HDC hdc, hdcsrc; HBITMAP hBitmap, hBmpNew, hBmpOld; RECT rect; DWORD bkColor, bkText; HFONT hFont = NULL; ICONINFO IconInfo; HICON hIcon = NULL; hdcsrc = GetDC(NULL); hdc = CreateCompatibleDC(hdcsrc); hBitmap = CreateCompatibleBitmap(hdcsrc, 16, 16); ReleaseDC(NULL, hdcsrc); if (hdc && hBitmap) { hBmpNew = CreateBitmap(16, 16, 1, 1, NULL); if (hBmpNew) { hBmpOld = SelectObject(hdc, hBitmap); rect.right = 16; rect.left = 0; rect.bottom = 16; rect.top = 0; bkColor = SetBkColor(hdc, GetSysColor(COLOR_HIGHLIGHT)); bkText = SetTextColor(hdc, GetSysColor(COLOR_HIGHLIGHTTEXT)); ExtTextOut(hdc, rect.left, rect.top, ETO_OPAQUE, &rect, _T(""), 0, NULL); hFont = CreateFont(-11, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_DONTCARE, _T("Tahoma")); SelectObject(hdc, hFont); DrawText(hdc, _tcsupr(szInd), 2, &rect, DT_SINGLELINE|DT_CENTER|DT_VCENTER); SelectObject(hdc, hBmpNew); PatBlt(hdc, 0, 0, 16, 16, BLACKNESS); SelectObject(hdc, hBmpOld); IconInfo.hbmColor = hBitmap; IconInfo.hbmMask = hBmpNew; IconInfo.fIcon = TRUE; hIcon = CreateIconIndirect(&IconInfo); DeleteObject(hBmpNew); DeleteObject(hBmpOld); DeleteObject(hFont); } } DeleteDC(hdc); DeleteObject(hBitmap); return hIcon; }
HICON Win32Window::CreateIconFromTexture(ptr<Graphics::RawTextureData> texture, BOOL isIcon, int hotX, int hotY) { int width = texture->GetImageWidth(); int height = texture->GetImageHeight(); int pitch = (width * 3 + 3) & ~3; BITMAPV5HEADER h; ZeroMemory(&h, sizeof(h)); h.bV5Size = sizeof(h); h.bV5Width = width; h.bV5Height = height; h.bV5Planes = 1; h.bV5BitCount = 32; h.bV5Compression = BI_BITFIELDS; h.bV5RedMask = 0x00FF0000; h.bV5GreenMask = 0x0000FF00; h.bV5BlueMask = 0x000000FF; h.bV5AlphaMask = 0xFF000000; const uint8_t* pixels = (const uint8_t*)texture->GetMipData(); uint8_t* buf = new uint8_t[width * 4 * height]; for(int i = 0; i < height; ++i) { const uint8_t* linePixels = pixels + i * width * 4; uint8_t* lineBuf = buf + (height - 1 - i) * width * 4; for(int j = 0; j < width; ++j) { lineBuf[j * 4 + 0] = linePixels[j * 4 + 2]; lineBuf[j * 4 + 1] = linePixels[j * 4 + 1]; lineBuf[j * 4 + 2] = linePixels[j * 4 + 0]; lineBuf[j * 4 + 3] = linePixels[j * 4 + 3]; } } HDC hdc = GetDC(NULL); HBITMAP hbmpColor = CreateDIBitmap(hdc, (BITMAPINFOHEADER*)&h, CBM_INIT, buf, (BITMAPINFO*)&h, DIB_RGB_COLORS); HBITMAP hbmpMask = CreateBitmap(width, height, 1, 1, NULL); ReleaseDC(NULL, hdc); delete [] buf; if(!hbmpColor || !hbmpMask) { if(hbmpColor) DeleteBitmap(hbmpColor); if(hbmpMask) DeleteBitmap(hbmpMask); THROW("Can't create bitmaps"); } ICONINFO ii; ii.fIcon = isIcon; ii.xHotspot = hotX; ii.yHotspot = hotY; ii.hbmMask = hbmpMask; ii.hbmColor = hbmpColor; HICON icon = CreateIconIndirect(&ii); DeleteBitmap(hbmpColor); DeleteBitmap(hbmpMask); return icon; }
// /// Creates a TCursor object from the specified ICONINFO structure information. // TCursor::TCursor(const ICONINFO& iconInfo) { WARN(iconInfo.fIcon, "TCursor constructor called with ICONINFO::fIcon == true"); // Turn this into a precondition? ICONINFO i = iconInfo; // Make a clone, since CreateIconIndirect is not const-correct. Handle = CreateIconIndirect(&i); CheckValid(); TRACEX(OwlGDI, OWL_CDLEVEL, "TCursor constructed indirectly @" << static_cast<void*>(this)); }
HICON image_make_icon_handle( Handle img, Point size, Point * hotSpot) { PIcon i = ( PIcon) img; HICON r; ICONINFO ii; int bpp = i-> type & imBPP; Bool noSZ = i-> w != size. x || i-> h != size. y; Bool noBPP = bpp != 1 && bpp != 4 && bpp != 8 && bpp != 24; HDC dc; XBITMAPINFO bi; Bool notAnIcon = !kind_of( img, CIcon); ii. fIcon = hotSpot ? false : true; ii. xHotspot = hotSpot ? hotSpot-> x : 0; ii. yHotspot = hotSpot ? hotSpot-> y : 0; if ( noSZ || noBPP) { i = ( PIcon)( i-> self-> dup( img)); if ( noSZ) i-> self-> set_size(( Handle) i, size); if ( noBPP) i-> self-> set_type(( Handle) i, ( bpp < 4) ? 1 : (( bpp < 8) ? 4 : (( bpp < 24) ? 8 : 24)) ); } if (!( dc = dc_alloc())) { if (( Handle) i != img) Object_destroy(( Handle) i); return NULL; } image_get_binfo(( Handle)i, &bi); if ( bi. bmiHeader. biClrUsed > 0) bi. bmiHeader. biClrUsed = bi. bmiHeader. biClrImportant = i-> palSize; if ( !( ii. hbmColor = CreateDIBitmap( dc, &bi. bmiHeader, CBM_INIT, i-> data, ( BITMAPINFO*) &bi, DIB_RGB_COLORS))) apiErr; bi. bmiHeader. biBitCount = bi. bmiHeader. biPlanes = 1; bi. bmiColors[ 0]. rgbRed = bi. bmiColors[ 0]. rgbGreen = bi. bmiColors[ 0]. rgbBlue = 0; bi. bmiColors[ 1]. rgbRed = bi. bmiColors[ 1]. rgbGreen = bi. bmiColors[ 1]. rgbBlue = 255; if ( !( ii. hbmMask = CreateDIBitmap( dc, &bi. bmiHeader, CBM_INIT, notAnIcon ? NULL : i-> mask, ( BITMAPINFO*) &bi, DIB_RGB_COLORS))) apiErr; dc_free(); if ( !( r = CreateIconIndirect( &ii))) apiErr; DeleteObject( ii. hbmColor); DeleteObject( ii. hbmMask); if (( Handle) i != img) Object_destroy(( Handle) i); return r; }
static SDL_Cursor * WIN_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y) { SDL_Cursor *cursor; HICON hicon; HDC hdc; BITMAPV4HEADER bmh; LPVOID pixels; ICONINFO ii; SDL_zero(bmh); bmh.bV4Size = sizeof(bmh); bmh.bV4Width = surface->w; bmh.bV4Height = -surface->h; /* Invert the image */ bmh.bV4Planes = 1; bmh.bV4BitCount = 32; bmh.bV4V4Compression = BI_BITFIELDS; bmh.bV4AlphaMask = 0xFF000000; bmh.bV4RedMask = 0x00FF0000; bmh.bV4GreenMask = 0x0000FF00; bmh.bV4BlueMask = 0x000000FF; hdc = GetDC(NULL); SDL_zero(ii); ii.fIcon = FALSE; ii.xHotspot = (DWORD)hot_x; ii.yHotspot = (DWORD)hot_y; ii.hbmColor = CreateDIBSection(hdc, (BITMAPINFO*)&bmh, DIB_RGB_COLORS, &pixels, NULL, 0); ii.hbmMask = CreateBitmap(surface->w, surface->h, 1, 1, NULL); ReleaseDC(NULL, hdc); SDL_assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888); SDL_assert(surface->pitch == surface->w * 4); SDL_memcpy(pixels, surface->pixels, surface->h * surface->pitch); hicon = CreateIconIndirect(&ii); DeleteObject(ii.hbmColor); DeleteObject(ii.hbmMask); if (!hicon) { WIN_SetError("CreateIconIndirect()"); return NULL; } cursor = SDL_calloc(1, sizeof(*cursor)); if (cursor) { cursor->driverdata = hicon; } else { DestroyIcon(hicon); SDL_OutOfMemory(); } return cursor; }
void Icon::fromBitmap(Bitmap *bitmap, Bitmap *mask) { ICONINFO ii; memset(&ii, 0, sizeof(ICONINFO)); ii.hbmColor = (bitmap != 0) ? bitmap->m_bitmap : 0; ii.hbmMask = (mask != 0) ? mask->m_bitmap : 0; m_icon = CreateIconIndirect(&ii); }
UINT ExtractIcons::_ExtractFromBMP( LPCTSTR pFileName, int iconIndex, int cxIcon, int cyIcon, HICON* phicon, UINT flags ) { if( iconIndex >= 1 ) return 0; flags |= LR_LOADFROMFILE; HBITMAP hbm = (HBITMAP)LoadImage( NULL, pFileName, IMAGE_BITMAP, cxIcon, cyIcon, flags ); if( hbm == NULL ) return 0; if( phicon == NULL ) { DeleteObject(hbm); return 1; } HBITMAP hbmMask = CreateBitmap( cxIcon, cyIcon, 1, 1, NULL ); HDC hdc = CreateCompatibleDC(NULL); SelectObject( hdc, hbm ); HDC hdcMask = CreateCompatibleDC(NULL); SelectObject(hdcMask, hbmMask); SetBkColor( hdc, GetPixel(hdc, 0, 0) ); // this ROP Code will leave bits in the destination bitmap the same color if the // corresponding source bitmap's bit are black. // all other bits in the destination (where source bits are not black) // are turned to black. #define DSTERASE 0x00220326 // dest = dest & (~src) : BitBlt( hdcMask, 0, 0, cxIcon, cyIcon, hdc, 0, 0, SRCCOPY ); BitBlt( hdc, 0, 0, cxIcon, cyIcon, hdcMask, 0, 0, DSTERASE ); ICONINFO ii; ii.fIcon = TRUE; ii.xHotspot = 0; ii.yHotspot = 0; ii.hbmColor = hbm; ii.hbmMask = hbmMask; HICON hicon = CreateIconIndirect( &ii ); DeleteObject(hdc); DeleteObject(hbm); DeleteObject(hdcMask); DeleteObject(hbmMask); *phicon = hicon; return 1; }
static HICON trayicon_draw(Trayicon *t, char *text, int len) { ICONINFO iconInfo; HBITMAP hOldBitmap; HFONT hOldFont; hOldBitmap = (HBITMAP) SelectObject(t->mdc, t->hBitmap); hOldFont = (HFONT) SelectObject(t->mdc, t->hFont); TextOut(t->mdc, t->bitmapWidth / 4, 0, text, len); SelectObject(t->mdc, hOldBitmap); SelectObject(t->mdc, hOldFont); iconInfo = (ICONINFO){TRUE, 0, 0, t->hBitmap, t->hBitmap}; return CreateIconIndirect(&iconInfo); }
HICON BindOverlayIcon(HICON SourceIcon,HICON OverlayIcon) { ICONINFO OverlayIconInfo, TargetIconInfo; BITMAP OverlayBitmapInfo, TargetBitmapInfo; HBITMAP OldOverlayBitmap, OldTargetBitmap; HICON TargetIcon, TempIcon; HDC OverlayDC, TargetDC; BLENDFUNCTION bf = {0,0,255,1}; TempIcon = CopyIcon(SourceIcon); if ( !GetIconInfo( TempIcon, &TargetIconInfo )) return NULL; MakeBitmap32(&TargetIconInfo.hbmColor); CorrectBitmap32Alpha(TargetIconInfo.hbmColor, FALSE); GetObject(TargetIconInfo.hbmColor, sizeof(BITMAP), &TargetBitmapInfo); if ( !GetIconInfo(OverlayIcon, &OverlayIconInfo) || !GetObject(OverlayIconInfo.hbmColor, sizeof(BITMAP), &OverlayBitmapInfo)) return NULL; TargetDC = CreateCompatibleDC(NULL); OldTargetBitmap = (HBITMAP)SelectObject(TargetDC, TargetIconInfo.hbmColor); OverlayDC = CreateCompatibleDC(NULL); OldOverlayBitmap = (HBITMAP)SelectObject(OverlayDC, OverlayIconInfo.hbmColor); AlphaBlend(TargetDC, 0, 0, TargetBitmapInfo.bmWidth, TargetBitmapInfo.bmHeight, OverlayDC, 0, 0, OverlayBitmapInfo.bmWidth, OverlayBitmapInfo.bmHeight, bf); SelectObject(TargetDC, TargetIconInfo.hbmMask); SelectObject(OverlayDC, OverlayIconInfo.hbmMask); BitBlt(TargetDC, 0, 0, TargetBitmapInfo.bmWidth, TargetBitmapInfo.bmHeight, OverlayDC, 0, 0, SRCCOPY); TargetIcon = CreateIconIndirect(&TargetIconInfo); DestroyIcon(TempIcon); SelectObject(TargetDC, OldTargetBitmap); DeleteObject(TargetIconInfo.hbmColor); DeleteObject(TargetIconInfo.hbmMask); DeleteDC(TargetDC); SelectObject(OverlayDC, OldOverlayBitmap); DeleteObject(OverlayIconInfo.hbmColor); DeleteObject(OverlayIconInfo.hbmMask); DeleteDC(OverlayDC); return TargetIcon; }
static HICON trayicon_draw(Trayicon *t, char *text, unsigned len) { ICONINFO iconInfo; HBITMAP hOldBitmap; HFONT hOldFont; hOldBitmap = (HBITMAP) SelectObject(t->mdc, t->hBitmap); hOldFont = (HFONT) SelectObject(t->mdc, t->hFont); TextOut(t->mdc, t->bitmapWidth / 4, 0, text, len); SelectObject(t->mdc, hOldBitmap); SelectObject(t->mdc, hOldFont); iconInfo.fIcon = TRUE; iconInfo.xHotspot = iconInfo.yHotspot = 0; iconInfo.hbmMask = iconInfo.hbmColor = t->hBitmap; return CreateIconIndirect(&iconInfo); }
/*static*/ HICON Win32UIBinding::BitmapToIcon(HBITMAP bitmap, int sizeX, int sizeY) { if (!bitmap) return 0; HBITMAP bitmapMask = CreateCompatibleBitmap(GetDC(NULL), sizeX, sizeY); ICONINFO iconInfo = {0}; iconInfo.fIcon = TRUE; iconInfo.hbmMask = bitmapMask; iconInfo.hbmColor = bitmap; HICON icon = CreateIconIndirect(&iconInfo); DeleteObject(bitmapMask); return icon; }
HICON MakeGrayscaleIcon(HICON SourceIcon) { ICONINFO TargetIconInfo; BITMAP TargetBitmapInfo; HICON TargetIcon, TempIcon; TempIcon = CopyIcon(SourceIcon); if (! GetIconInfo(TempIcon, &TargetIconInfo) || GetObject(TargetIconInfo.hbmColor, sizeof(BITMAP), &TargetBitmapInfo)==0) return NULL; MakeGrayscale(&TargetIconInfo.hbmColor); TargetIcon = CreateIconIndirect(&TargetIconInfo); DestroyIcon(TempIcon); return TargetIcon; }
// return value needs to be released using DestroyIcon() // only operates on color icons, which isn't a problem here static HICON __fastcall ResizeIconCentered(HICON hIcon, int cx, int cy) { HICON hResIcon = nullptr; HDC hdc = CreateCompatibleDC(nullptr); if (hdc != nullptr) { ICONINFO icoi; if (GetIconInfo(hIcon, &icoi)) { BITMAP bm; if (GetObject(icoi.hbmColor, sizeof(bm), &bm) && bm.bmWidth <= cx && bm.bmHeight <= cy) { POINT pt; pt.x = (cx - bm.bmWidth) / 2; pt.y = (cy - bm.bmHeight) / 2; HBITMAP hbmPrev = (HBITMAP)SelectObject(hdc, icoi.hbmColor); if (hbmPrev != nullptr) { /* error on select? */ HBITMAP hbm = icoi.hbmColor; icoi.hbmColor = CreateCompatibleBitmap(hdc, cx, cy); if (icoi.hbmColor != nullptr) if (SelectObject(hdc, icoi.hbmColor) != nullptr) { /* error on select? */ DeleteObject(hbm); /* delete prev color (XOR) */ if (BitBlt(hdc, 0, 0, cx, cy, nullptr, 0, 0, BLACKNESS)) /* transparency: AND=0, XOR=1 */ if (DrawIconEx(hdc, pt.x, pt.y, hIcon, bm.bmWidth, bm.bmHeight, 0, nullptr, DI_IMAGE | DI_NOMIRROR)) { if (SelectObject(hdc, icoi.hbmMask) != nullptr) { /* error on select? */ hbm = icoi.hbmMask; icoi.hbmMask = CreateBitmap(cx, cy, 1, 1, nullptr); /* mono */ if (icoi.hbmMask != nullptr) if (SelectObject(hdc, icoi.hbmMask) != nullptr) { /* error on select? */ DeleteObject(hbm); /* delete prev mask (AND) */ if (BitBlt(hdc, 0, 0, cx, cy, nullptr, 0, 0, WHITENESS)) /* transparency: AND=0, XOR=1 */ if (DrawIconEx(hdc, pt.x, pt.y, hIcon, 0, 0, 0, nullptr, DI_MASK | DI_NOMIRROR)) { SelectObject(hdc, hbmPrev); hResIcon = CreateIconIndirect(&icoi); /* bitmaps must not be selected */ } } } } } SelectObject(hdc, hbmPrev); } } DeleteObject(icoi.hbmColor); DeleteObject(icoi.hbmMask); } DeleteDC(hdc); } return hResIcon; }
HICON CreateTimeIcon(SYSTEMTIME* time) { WORD matrix[][2] = { {10, 10}, {10, 5}, {10, 0}, {5, 10}, {5, 5}, {5, 0}, {0, 10}, {0, 5}, {0, 0} }; HDC hdc0, hdc1; HGDIOBJ undo; HBITMAP bmp, mask; HICON ret; ICONINFO ii = {TRUE}; HBRUSH brush0, brush1; RECT rect; WORD x; brush0 = CreateSolidBrush(RGB(0, 0, 0)); brush1 = CreateSolidBrush(RGB(255, 255, 255)); hdc0 = GetDC(NULL); bmp = CreateCompatibleBitmap(hdc0, 16, 16); mask = CreateCompatibleBitmap(hdc0, 16, 16); hdc1 = CreateCompatibleDC(hdc0); rect.left = 0; rect.top = 0; rect.right = 16; rect.bottom = 16; undo = SelectObject(hdc1, mask); FillRect(hdc1, &rect, brush1); x = time->wMinute + (time->wHour % 6 << 6); for (int i = 0; i < 9; i++, x /= 2) { if (x % 2) { rect.left = matrix[i][0]; rect.top = matrix[i][1]; rect.right = rect.left + 6; rect.bottom = rect.top + 6; SelectObject(hdc1, mask); FillRect(hdc1, &rect, brush0); rect.left++; rect.top++; rect.right--; rect.bottom--; SelectObject(hdc1, bmp); FillRect(hdc1, &rect, brush1); } } ReleaseDC(NULL, hdc0); SelectObject(hdc1, undo); DeleteDC(hdc1); DeleteObject(brush0); DeleteObject(brush1); ii.hbmColor = bmp; ii.hbmMask = mask; ret = CreateIconIndirect(&ii); DeleteObject(bmp); DeleteObject(mask); return ret; }
/************************************************************************* * DuplicateIcon [SHELL32.@] */ HICON WINAPI DuplicateIcon( HINSTANCE hInstance, HICON hIcon) { ICONINFO IconInfo; HICON hDupIcon = 0; TRACE("%p %p\n", hInstance, hIcon); if (GetIconInfo(hIcon, &IconInfo)) { hDupIcon = CreateIconIndirect(&IconInfo); /* clean up hbmMask and hbmColor */ DeleteObject(IconInfo.hbmMask); DeleteObject(IconInfo.hbmColor); } return hDupIcon; }
HICON ImageFType::GetIcon(void) { if (m_bmp == NULL) return NULL; BITMAP bm; GetObject(m_bmp, sizeof(bm), &bm); ICONINFO ii; ii.fIcon = TRUE; ii.xHotspot = 0; ii.yHotspot = 0; ii.hbmMask = CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, NULL); ii.hbmColor = m_bmp; HICON hIcon = CreateIconIndirect(&ii); DeleteObject(ii.hbmMask); return hIcon; }
static HCURSOR CreateBitmapCursor(int xhot, int yhot, HBITMAP and_mask, HBITMAP color_mask) { ICONINFO iconinfo = { FALSE, // fIcon xhot, // xHotspot yhot, // yHotspot and_mask, // hbmMask color_mask // hbmColor }; HCURSOR cursor = CreateIconIndirect(&iconinfo); // Delete the bitmaps. DeleteObject(and_mask); DeleteObject(color_mask); return cursor; }
HICON ImageList_GetIconFixed (HIMAGELIST himl, INT i, UINT fStyle) { ICONINFO ii; HICON hIcon; HBITMAP hOldDstBitmap; HDC hdcDst; int cx, cy; ImageList_GetIconSize(himl, &cx, &cy); hdcDst = CreateCompatibleDC(NULL); ii.fIcon = TRUE; ii.xHotspot = 0; ii.yHotspot = 0; /* draw mask*/ ii.hbmMask = CreateBitmap (cx, cy, 1, 1, NULL); hOldDstBitmap = (HBITMAP)SelectObject (hdcDst, ii.hbmMask); PatBlt(hdcDst, 0, 0, cx, cy, WHITENESS); ImageList_Draw(himl, i, hdcDst, 0, 0, fStyle | ILD_MASK); /* draw image*/ ii.hbmColor = CreateBitmap (cx, cy, 1, 32, NULL); SelectObject (hdcDst, ii.hbmColor); PatBlt (hdcDst, 0, 0, cx, cy, BLACKNESS); ImageList_Draw(himl, i, hdcDst, 0, 0, fStyle | ILD_TRANSPARENT); /* * CreateIconIndirect requires us to deselect the bitmaps from * the DCs before calling */ SelectObject(hdcDst, hOldDstBitmap); hIcon = CreateIconIndirect (&ii); DeleteObject (ii.hbmMask); DeleteObject (ii.hbmColor); DeleteDC (hdcDst); return hIcon; }
static INT_PTR ServiceCreateMergedFlagIcon(WPARAM wParam, LPARAM lParam) { HICON hIcon = nullptr; /* load both icons */ HICON hLowerIcon = (HICON)ServiceLoadFlagIcon((WPARAM)lParam, 0); if (hLowerIcon == nullptr) return 0; HICON hUpperIcon = (HICON)ServiceLoadFlagIcon(wParam, 0); /* merge them */ ICONINFO icoi; if (GetIconInfo(hLowerIcon, &icoi)) { BITMAP bm; if (hUpperIcon != nullptr && GetObject(icoi.hbmColor, sizeof(bm), &bm)) { HDC hdc = CreateCompatibleDC(nullptr); if (hdc != nullptr) { POINT aptTriangle[3] = { 0 }; aptTriangle[1].y = bm.bmHeight - 1; aptTriangle[2].x = bm.bmWidth - 1; HRGN hrgn = CreatePolygonRgn(aptTriangle, _countof(aptTriangle), WINDING); if (hrgn != nullptr) { SelectClipRgn(hdc, hrgn); DeleteObject(hrgn); HBITMAP hbmPrev = (HBITMAP)SelectObject(hdc, icoi.hbmColor); if (hbmPrev != nullptr) { /* error on select? */ if (DrawIconEx(hdc, 0, 0, hUpperIcon, bm.bmWidth, bm.bmHeight, 0, nullptr, DI_NOMIRROR | DI_IMAGE)) if (SelectObject(hdc, icoi.hbmMask) != nullptr) /* error on select? */ DrawIconEx(hdc, 0, 0, hUpperIcon, bm.bmWidth, bm.bmHeight, 0, nullptr, DI_NOMIRROR | DI_MASK); SelectObject(hdc, hbmPrev); } } DeleteDC(hdc); } } /* create icon */ hIcon = CreateIconIndirect(&icoi); DeleteObject(icoi.hbmColor); DeleteObject(icoi.hbmMask); } return (INT_PTR)hIcon; }