Results 1 to 2 of 2

Thread: QPixmap -> HICON trouble.

  1. #1
    Join Date
    Jan 2006
    Posts
    36
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QPixmap -> HICON trouble.

    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?

  2. #2

    Default Re: QPixmap -> HICON trouble.

    This worked for me:
    https://mtpforge.melting-pot.org/pro...ayicon_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 );
    }

Similar Threads

  1. QPixmap and HBITMAP
    By ToddAtWSU in forum Qt Programming
    Replies: 1
    Last Post: 21st June 2006, 16:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.