PDA

View Full Version : QPixmap -> HICON trouble.



krivenok
28th February 2006, 10:33
Hello All!

This code (from old tray icon example) creates HICON by QPixmap

///////////////////////////////////////////////////////////////////////////////////////////
static HICON createIcon( const QPixmap &pm )
{
QPixmap maskpm( pm.size() );
QBitmap mask( pm.size() );
QPainter p;
if ( !pm.mask().isNull() )
{
maskpm.fill( Qt::black ); // make masked area black
QPainter mp( &mask );
p.begin( &mask );
p.drawPixmap( 0, 0, pm.mask() );
p.end();
}
else maskpm.fill( Qt::color1 );

p.begin( &maskpm );
p.drawPixmap(0, 0, pm);
p.end();

ICONINFO iconInfo;
iconInfo.fIcon = TRUE;
HBITMAP hbm;
iconInfo.hbmMask = hbm = createIconMask(mask);
iconInfo.hbmColor = maskpm.toWinHBITMAP();

HICON icon = CreateIconIndirect( &iconInfo );
DeleteObject(iconInfo.hbmMask);
iconInfo.hbmMask = hbm = 0;
return icon;
}
///////////////////////////////////////////////////////////////////////////////////////////
static HBITMAP createIconMask( const QPixmap &qp )
{
QImage bm = qp.toImage();
int w = bm.width();
int h = bm.height();
int bpl = ((w+15)/16)*2; // bpl, 16 bit alignment
uchar *bits = new uchar[bpl*h];
bm.invertPixels();
for ( int y=0; y<h; y++ ) memcpy( bits+y*bpl, bm.scanLine(y), bpl );
HBITMAP hbm = CreateBitmap( w, h, 1, 1, bits );
delete [] bits;
return hbm;
}
///////////////////////////////////////////////////////////////////////////////////////////

Questions:
1) Why HICON has black background color?
2) How to get HICON with transparent background color?
3) What should I change in this code?

jrbloch
11th August 2006, 16:51
This worked for me:
https://mtpforge.melting-pot.org/projects/ceb/browser/max/src/trayicon_win.cpp

static HICON createIcon( const QPixmap &pm, HBITMAP &hbm )
{
QPixmap maskpm(pm);
QBitmap mask(pm.mask());

ICONINFO iconInfo;
iconInfo.fIcon = TRUE;
iconInfo.hbmMask = createIconMask(mask);
hbm = iconInfo.hbmMask;
iconInfo.hbmColor = maskpm.toWinHBITMAP(QPixmap::PremultipliedAlpha);

return CreateIconIndirect( &iconInfo );
}