Thread: [C++ - Win32]- how get HBITMAP form HDC?

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    451

    [C++ - Win32]- how get HBITMAP form HDC?

    the HDC have the image. so i need, with my class, return a HBITMAP value:
    Code:
    operator HBITMAP()
        {
            HBITMAP hBmp = (HBITMAP)GetCurrentObject( hdcimage, OBJ_BITMAP );
            return hBmp;
        }
    i did 2 diferent ways, but the menu don't recive the image
    Code:
    void bitmap(image imgImage)
        {
            //HBITMAP bitimage = (HBITMAP)LoadImage( NULL, filename.c_str(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
            HMENU hMenu = NULL;
            if(primeiromenu)
                hMenu = mnuBar;
            else
                hMenu = MenuHandle;
            SetMenuItemBitmaps(hMenu,menuposition,MF_BYPOSITION ,(HBITMAP)imgImage ,(HBITMAP)imgImage);
        }
    what i'm doing wrong?
    (the problem isn't the HMENU value, because works fine with a string)

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Returning a local variable is NOT allowed in C and I would guess it is also NOT allowed in C++.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    Quote Originally Posted by stahta01 View Post
    Returning a local variable is NOT allowed in C and I would guess it is also NOT allowed in C++.

    Tim S.
    you mean overload the HBITMAP operator? i use C++ 11

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    the HDC have the image. so i need, with my class, return a HBITMAP value:
    O_o

    I haven't used the raw "WINAPI" recently enough to recognize anything wrong.

    You should use the error API to find what might be failing.

    Returning a local variable is NOT allowed in C and I would guess it is also NOT allowed in C++.
    You should elaborate.

    I believe I know where you went wrong in interpreting the code, but I'd like to save myself the trouble of explaining something you know if I'm wrong about your interpretation.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  5. #5
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    Quote Originally Posted by phantomotap View Post
    O_o

    I haven't used the raw "WINAPI" recently enough to recognize anything wrong.

    You should use the error API to find what might be failing.



    You should elaborate.

    I believe I know where you went wrong in interpreting the code, but I'd like to save myself the trouble of explaining something you know if I'm wrong about your interpretation.

    Soma
    i thot build a HBITMAP variable global on my class image:
    Code:
    operator HBITMAP()
        {
            return btBimap;
        }
    now the image is showed hehehe
    but when i click on menu item, i get a memory leak, i can't destroy the btBimap, or i can lose the image
    so what you can advice me?
    heres the class:
    Code:
    class image
    {
    private:
        ULONG_PTR m_gdiplusToken;
        Gdiplus::GdiplusStartupInput gdiplusStartupInput;
        HDC hdcimage=CreateCompatibleDC(NULL);
        HBITMAP btBimap;
        Gdiplus::Graphics *graphic;
        Image *img;
        int imageheight=0;
        int imageweight=0;
        int framecount=0;
        int intSelectFrame=0;
        int framedelay=0;
        string strfilename="";
        Gdiplus::Color clrBackColor=Gdiplus::Color::Transparent;
        HDC hdcwindow;
        bool blnTransparent=true;
    
    
    public:
        image()
        {
            Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
            btBimap=CreateCompatibleBitmap(hdcimage,1,1);
            SelectObject(hdcimage, btBimap);
        }
    
        image(HDC hdcWindow)
        {
            Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
            hdcwindow=hdcWindow;
        }
    
        image(int width, int height)
        {
            Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
            hdcimage = CreateCompatibleDC(NULL);
            btBimap=CreateCompatibleBitmap(hdcimage,width,height);
            SelectObject(hdcimage, btBimap);
            framecount=1;
        }
    
        image(    const string & filename)
        {
            Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
    
            if(toupper(filename[filename.size()-3])=='C' && toupper(filename[filename.size()-2])=='U' && toupper(filename[filename.size()-1])=='R')
            {
                //create the transparent icon handle
                HCURSOR hicon =  (HCURSOR)LoadImage(NULL, filename.c_str(), IMAGE_CURSOR, imageweight, imageheight, LR_LOADFROMFILE|LR_SHARED|LR_DEFAULTSIZE|LR_LOADTRANSPARENT);
    
                ICONINFO ii;
                BOOL fResult = GetIconInfo(hicon, &ii);
                if (fResult)
                {
                    BITMAP bm;
                    fResult = GetObject(ii.hbmMask, sizeof(bm), &bm) == sizeof(bm);
                    if (fResult)
                    {
                        imageweight= bm.bmWidth;
                        imageheight= ii.hbmColor ? bm.bmHeight : bm.bmHeight / 2;
                    }
                    if (ii.hbmMask)  DeleteObject(ii.hbmMask);
                    if (ii.hbmColor) DeleteObject(ii.hbmColor);
                }
    
                btBimap=CreateBitmap(imageweight,imageheight,1,32,NULL);//create the bitmap with icon size
                SelectObject(hdcimage, btBimap);//add the bitmap to memory DC
                DrawIconEx(hdcimage,0,0,hicon,imageweight,imageheight,0,0,DI_NORMAL);//draw the icon to DC with right size
                //seems the DrawIcon(), always, draw it with 32X32 size
                framecount=1;
            }
            else
            {
                Gdiplus::Image img2(towstring(filename).c_str());
    
                btBimap=CreateBitmap(img2.GetWidth(),img2.GetHeight(),1,32,NULL);
                SelectObject(hdcimage, btBimap);
                Gdiplus::Graphics graphics(hdcimage);
                graphics.DrawImage(&img2, 0, 0, img2.GetWidth(), img2.GetHeight());
                imageweight=img2.GetWidth();
                imageheight=img2.GetHeight();
                UINT count = 0;
                count = img2.GetFrameDimensionsCount();
                GUID* pDimensionIDs = (GUID*)malloc(sizeof(GUID)*count);
                img2.GetFrameDimensionsList(pDimensionIDs, count);
    
                framecount=img2.GetFrameCount(&pDimensionIDs[0]);
                framedelay =img2.GetPropertyItemSize(PropertyTagFrameDelay);
                img=new Image(towstring(filename).c_str());
            }
        }
        image (const image &cSource)
        {
            framecount=cSource.framecount;
            framedelay=cSource.framedelay;
            clrBackColor=cSource.clrBackColor;
            img=cSource.img->Clone();
            imageweight=cSource.imageweight;
            imageheight=cSource.imageheight;
            btBimap=CreateBitmap(imageweight,imageweight,1,32,NULL);
            SelectObject(hdcimage, btBimap);
            BitBlt(hdcimage,0,0,imageweight,imageheight,cSource.hdcimage,0,0,SRCCOPY);
        }
        image& operator= (const image &cSource)
        {
            framecount=cSource.framecount;
            framedelay=cSource.framedelay;
            intSelectFrame=cSource.intSelectFrame;
            clrBackColor=cSource.clrBackColor;
            imageweight=cSource.imageweight;
            imageheight=cSource.imageheight;
            btBimap=CreateBitmap(imageweight,imageweight,1,32,NULL);
            SelectObject(hdcimage, btBimap);
            BitBlt(hdcimage,0,0,imageweight,imageheight,cSource.hdcimage,0,0,SRCCOPY);
            return *this;
        }
    
        property <int> SelectFrame
        {
            Get(int)
            {
                return intSelectFrame;
            },
            Set(int selectframe)
            {
                intSelectFrame=selectframe;
                UINT count = 0;
                count = img->GetFrameDimensionsCount();
                GUID* pDimensionIDs = (GUID*)malloc(sizeof(GUID)*count);
                img->GetFrameDimensionsList(pDimensionIDs, count);
                img->SelectActiveFrame(&pDimensionIDs[0],intSelectFrame);
                graphic=new Graphics(hdcimage);
                graphic->Clear(clrBackColor);
                graphic->DrawImage(img, 0, 0, img->GetWidth(), img->GetHeight());
            }
        };
    
        property<int> FramesCount
        {
            Get(int)
            {
                return framecount;
            }
        };
    
        property<Gdiplus::Color> Backcolor
        {
            Get(Gdiplus::Color)
            {
                return clrBackColor;
            },
            Set(Gdiplus::Color bkcolor)
            {
                clrBackColor = bkcolor;
                SetDCBrushColor(hdcimage,clrBackColor.ToCOLORREF());
            }
        };
    
        void draw(HDC control)
        {
    
    
            //if (clrBackColor.GetValue() == Gdiplus::Color::Transparent)
            if (clrBackColor.GetValue()== Gdiplus::Color::Transparent)
            {
                TransparentBlt(control, 0, 0,width(),height(),hdcimage, 0, 0,width(), height(), GetPixel(hdcimage,width()-1,height()-1));
            }
            else
            {
                BitBlt(control,0,0,width(),height(),hdcimage,0,0,SRCCOPY);
            }
    
            //InvalidateRect(WindowFromDC(control),NULL,false);
        }
    
        operator HBITMAP()
        {
            return btBimap;
        }
    
        int height()
        {
            return imageheight;
        }
        int width()
        {
            return imageweight;
        }
    
        operator HDC()
        {
            return hdcimage;
        }
    
        ~image()
        {
            delete img;
            Gdiplus::GdiplusShutdown(m_gdiplusToken);
            //SelectObject(hdcimage, hbmOld);
            DeleteDC(hdcimage);
        }
    };
    on menu class:
    Code:
    void bitmap(image imgImage)
        {
            //HBITMAP bitimage = (HBITMAP)LoadImage( NULL, filename.c_str(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
            HMENU hMenu = NULL;
            if(primeiromenu)
                hMenu = mnuBar;
            else
                hMenu = MenuHandle;
            SetMenuItemBitmaps(hMenu,menuposition,MF_BYPOSITION ,(HBITMAP)imgImage ,(HBITMAP)imgImage);
        }
    the image is showed in right menu item. the problem is only the memory leak

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    I suppose that would depend on what memory is leaking.

    That said, I don't see a corresponding `DeleteObject' call.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  7. #7
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    Quote Originally Posted by phantomotap View Post
    O_o

    I suppose that would depend on what memory is leaking.

    That said, I don't see a corresponding `DeleteObject' call.

    Soma
    the error only happens when i click on menu item.
    how can i use DeleteObject(), if i continue need the btBimap object?

  8. #8
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    strange when i do these:
    Code:
    ~image()
        {
            delete img;
            Gdiplus::GdiplusShutdown(m_gdiplusToken);
            //SelectObject(hdcimage, hbmOld);
            DeleteObject(btBimap);
            DeleteDC(hdcimage);
        }
    its the class destructor so why can affect the constructor?
    (with these way, the image isn't showed)

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    I simply assumed your menu class had a `SetMenuItemBitmaps' member wrapping the API to take ownership of the resource.

    Guess we know what that makes me...

    *shrug*

    You have to delete the resource within the destructor if the class is the owner of the resource. If you don't delete a resource a class owns, the resource leaks when the class is destroyed.

    The "WINAPI" doesn't take control of the resource. Your current problem is that the menu class isn't taking ownership of the resource.

    In short, the underlying `BITMAP' resource must live as long as the menu you using the resource.

    Your job as a programmer is to ensure that the resource lives as long as the menu.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  10. #10
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    Quote Originally Posted by phantomotap View Post
    O_o

    I simply assumed your menu class had a `SetMenuItemBitmaps' member wrapping the API to take ownership of the resource.

    Guess we know what that makes me...

    *shrug*

    You have to delete the resource within the destructor if the class is the owner of the resource. If you don't delete a resource a class owns, the resource leaks when the class is destroyed.

    The "WINAPI" doesn't take control of the resource. Your current problem is that the menu class isn't taking ownership of the resource.

    In short, the underlying `BITMAP' resource must live as long as the menu you using the resource.

    Your job as a programmer is to ensure that the resource lives as long as the menu.

    Soma
    sorry, but i don't use resources on the project... unless you mean anotherthing.
    if i don't show the image, memory leak don't happens. if i use the way for images, the memory leak don't happens:
    Code:
    void bitmap(string filename)
        {
            HBITMAP bitimage = (HBITMAP)LoadImage( NULL, filename.c_str(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
            HMENU hMenu = NULL;
            if(primeiromenu)
                hMenu = mnuBar;
            else
                hMenu = MenuHandle;
            SetMenuItemBitmaps(hMenu,menuposition,MF_BYPOSITION ,(HBITMAP)bitimage ,(HBITMAP)bitimage);
        }
    but i need ask: from what you said, did i miss something?

  11. #11
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    I was talking about "GDI" resources like `BITMAP', `DC', and similar.

    You should read my posts a few more times.

    You have most of the ideas in your head. You just need to put the polished idea into code.

    How could you go about ensuring that the `image', the underlying `BITMAP', lives at least as long as the menu?

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  12. #12
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    how i unselect a HBITMAP from HDC?

  13. #13
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    i did a new test: i drawed the image on form. i can close the form without memory leak.
    so what i forget with menus for change the image?
    i did another test:
    Code:
    void bitmap(image &imgImage)
        {
            //HBITMAP bitimage = (HBITMAP)LoadImage( NULL, filename.c_str(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
            HMENU hMenu = NULL;
            if(primeiromenu)
                hMenu = mnuBar;
            else
                hMenu = MenuHandle;
            SetMenuItemBitmaps(hMenu,ID,MF_BYCOMMAND,(HBITMAP)imgImage ,(HBITMAP)imgImage);
        }
    no memory leak. the menu item size is changed, but don't show the image

  14. #14
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I had a brain fart and I must of thought they was passing back a pointer. No idea why I thought that.

    Quote Originally Posted by phantomotap View Post
    O_o

    I haven't used the raw "WINAPI" recently enough to recognize anything wrong.

    You should use the error API to find what might be failing.



    You should elaborate.

    I believe I know where you went wrong in interpreting the code, but I'd like to save myself the trouble of explaining something you know if I'm wrong about your interpretation.

    Soma
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  15. #15
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    i haded several memory leaks with my image class:
    Code:
    class image
    {
    private:
        ULONG_PTR m_gdiplusToken;
        Gdiplus::GdiplusStartupInput gdiplusStartupInput;
        HDC hdcimage=CreateCompatibleDC(NULL);
        HGDIOBJ obj=NULL;
        HBITMAP btBitmap;
        Image *img;
        int imageheight=0;
        int imageweight=0;
        int framecount=0;
        int intSelectFrame=0;
        int framedelay=0;
        string strfilename="";
        Gdiplus::Color clrBackColor=Gdiplus::Color::Transparent;
        HDC hdcwindow;
        bool blnTransparent=true;
    
        HICON HICONFromHBITMAP(HBITMAP bitmap)
        {
            BITMAP bmp;
            GetObject(bitmap, sizeof(BITMAP), &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);
            DeleteObject(hbmMask);
    
            return hIcon;
        }
    
    public:
    
        image()
        {
            Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
            btBitmap=CreateCompatibleBitmap(hdcimage,1,1);
            obj = SelectObject(hdcimage, btBitmap);
        }
    
        image(HDC hdcWindow)
        {
            Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
            hdcwindow=hdcWindow;
        }
    
        image(int width, int height)
        {
            Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
            hdcimage = CreateCompatibleDC(NULL);
            btBitmap = CreateCompatibleBitmap(hdcimage,width,height);
            obj = SelectObject(hdcimage, btBitmap);
            framecount=1;
        }
    
        image( const string & filename)
        {
            strfilename=filename;
            Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
            if(toupper(filename[filename.size()-3])=='C' && toupper(filename[filename.size()-2])=='U' && toupper(filename[filename.size()-1])=='R')
            {
                //create the transparent icon handle
                HCURSOR hicon = (HCURSOR)LoadImage(NULL, filename.c_str(), IMAGE_CURSOR, imageweight, imageheight, LR_LOADFROMFILE|LR_SHARED|LR_DEFAULTSIZE|LR_LOADTRANSPARENT);
                ICONINFO ii;
                BOOL fResult = GetIconInfo(hicon, &ii);
                if (fResult)
                {
                    BITMAP bm;
                    fResult = GetObject(ii.hbmMask, sizeof(bm), &bm) == sizeof(bm);
                    if (fResult)
                    {
                        imageweight= bm.bmWidth;
                        imageheight= ii.hbmColor ? bm.bmHeight : bm.bmHeight / 2;
                    }
                    if (ii.hbmMask) DeleteObject(ii.hbmMask);
                    if (ii.hbmColor) DeleteObject(ii.hbmColor);
                }
                btBitmap=CreateBitmap(imageweight,imageheight,1,32,NULL);//create the bitmap with icon size
                obj = SelectObject(hdcimage, btBitmap);//add the bitmap to memory DC
                DrawIconEx(hdcimage,0,0,hicon,imageweight,imageheight,0,0,DI_NORMAL);//draw the icon to DC with right size
                //seems the DrawIcon(), always, draw it with 32X32 size
                framecount=1;
                DestroyCursor(hicon);
            }
            else
            {
                Gdiplus::Image img2(towstring(filename).c_str());
                btBitmap=CreateBitmap(img2.GetWidth(),img2.GetHeight(),1,32,NULL);
                obj = SelectObject(hdcimage, btBitmap);
                Gdiplus::Graphics graphics(hdcimage);
                graphics.DrawImage(&img2, 0, 0, img2.GetWidth(), img2.GetHeight());
                imageweight=img2.GetWidth();
                imageheight=img2.GetHeight();
                UINT count = 0;
                count = img2.GetFrameDimensionsCount();
                GUID* pDimensionIDs = (GUID*)malloc(sizeof(GUID)*count);
                img2.GetFrameDimensionsList(pDimensionIDs, count);
                framecount=img2.GetFrameCount(&pDimensionIDs[0]);
                if (framecount>1)
                    framedelay =img2.GetPropertyItemSize(PropertyTagFrameDelay);
                else
                    framedelay =0;
                img=new Image(towstring(filename).c_str());
                free(pDimensionIDs);
            }
        }
    
        image (const image &cSource)
        {
            Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
            framecount=cSource.framecount;
            framedelay=cSource.framedelay;
            clrBackColor=cSource.clrBackColor;
            img=cSource.img->Clone();
            imageweight=cSource.imageweight;
            imageheight=cSource.imageheight;
            strfilename=cSource.strfilename;
            btBitmap=CreateBitmap(imageweight,imageweight,1,32,NULL);
            obj = SelectObject(hdcimage, btBitmap);
            BitBlt(hdcimage,0,0,imageweight,imageheight,cSource.hdcimage,0,0,SRCCOPY);
        }
    
        image& operator= (const image &cSource)
        {
            Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
            framecount=cSource.framecount;
            framedelay=cSource.framedelay;
            clrBackColor=cSource.clrBackColor;
            img=cSource.img->Clone();
            imageweight=cSource.imageweight;
            imageheight=cSource.imageheight;
            strfilename=cSource.strfilename;
            btBitmap=CreateBitmap(imageweight,imageweight,1,32,NULL);
            obj = SelectObject(hdcimage, btBitmap);
            BitBlt(hdcimage,0,0,imageweight,imageheight,cSource.hdcimage,0,0,SRCCOPY);
            return *this;
        }
    
        property <int> SelectFrame
        {
            Get(int)
            {
                return intSelectFrame;
            },
            Set(int selectframe)
            {
                intSelectFrame=selectframe;
                UINT count = 0;
                count = img->GetFrameDimensionsCount();
                GUID* pDimensionIDs = (GUID*)malloc(sizeof(GUID)*count);
                img->GetFrameDimensionsList(pDimensionIDs, count);
                img->SelectActiveFrame(&pDimensionIDs[0],intSelectFrame);
                Gdiplus::Graphics graphics(hdcimage);
                graphics.Clear(clrBackColor);
                graphics.DrawImage(img, 0, 0, img->GetWidth(), img->GetHeight());
                free(pDimensionIDs);
            }
        };
    
        property<int> FramesCount
        {
            Get(int)
            {
                return framecount;
            }
        };
    
        property<string> FileName
        {
            Get(string)
            {
                return strfilename;
            },
            Set(string filename)
            {
                delete img;
                DeleteObject(btBitmap);
                DeleteObject(obj);
                strfilename=filename;
                if(toupper(filename[filename.size()-3])=='C' && toupper(filename[filename.size()-2])=='U' && toupper(filename[filename.size()-1])=='R')
                {
                    //create the transparent icon handle
                    HCURSOR hicon = (HCURSOR)LoadImage(NULL, filename.c_str(), IMAGE_CURSOR, imageweight, imageheight, LR_LOADFROMFILE|LR_SHARED|LR_DEFAULTSIZE|LR_LOADTRANSPARENT);
                    ICONINFO ii;
                    BOOL fResult = GetIconInfo(hicon, &ii);
                    if (fResult)
                    {
                        BITMAP bm;
                        fResult = GetObject(ii.hbmMask, sizeof(bm), &bm) == sizeof(bm);
                        if (fResult)
                        {
                            imageweight= bm.bmWidth;
                            imageheight= ii.hbmColor ? bm.bmHeight : bm.bmHeight / 2;
                        }
                        if (ii.hbmMask) DeleteObject(ii.hbmMask);
                        if (ii.hbmColor) DeleteObject(ii.hbmColor);
                    }
                    btBitmap=CreateBitmap(imageweight,imageheight,1,32,NULL);//create the bitmap with icon size
                    obj = SelectObject(hdcimage, btBitmap);//add the bitmap to memory DC
                    DrawIconEx(hdcimage,0,0,hicon,imageweight,imageheight,0,0,DI_NORMAL);//draw the icon to DC with right size
                    //seems the DrawIcon(), always, draw it with 32X32 size
                    framecount=1;
                    DestroyCursor(hicon);
                }
                else
                {
                    Gdiplus::Image img2(towstring(filename).c_str());
                    btBitmap=CreateBitmap(img2.GetWidth(),img2.GetHeight(),1,32,NULL);
                    obj = SelectObject(hdcimage, btBitmap);
                    Gdiplus::Graphics graphics(hdcimage);
                    graphics.DrawImage(&img2, 0,0,img2.GetWidth(),img2.GetHeight());
                    imageweight=img2.GetWidth();
                    imageheight=img2.GetHeight();
                    UINT count = 0;
                    count = img2.GetFrameDimensionsCount();
                    GUID* pDimensionIDs = (GUID*)malloc(sizeof(GUID)*count);
                    img2.GetFrameDimensionsList(pDimensionIDs, count);
                    framecount=img2.GetFrameCount(&pDimensionIDs[0]);
                    framedelay =img2.GetPropertyItemSize(PropertyTagFrameDelay);
                    img=new Image(towstring(filename).c_str());
                    free(pDimensionIDs);
                }
            }
        };
    
        property<Gdiplus::Color> Backcolor
        {
            Get(Gdiplus::Color)
            {
                return clrBackColor;
            },
            Set(Gdiplus::Color bkcolor)
            {
                clrBackColor = bkcolor;
                SetDCBrushColor(hdcimage,clrBackColor.ToCOLORREF());
            }
        };
    
        void draw(HDC control)
        {
            //if (clrBackColor.GetValue() == Gdiplus::Color::Transparent)
            if (clrBackColor.GetValue()== Gdiplus::Color::Transparent)
            {
                TransparentBlt(control, 0, 0,width(),height(),hdcimage, 0, 0,width(), height(), GetPixel(hdcimage,width()-1,height()-1));
            }
            else
            {
                BitBlt(control,0,0,width(),height(),hdcimage,0,0,SRCCOPY);
            }
            //InvalidateRect(WindowFromDC(control),NULL,false);
        }
    
        operator HICON()
        {
            return HICONFromHBITMAP(btBitmap);
        }
    
        operator HBITMAP()
        {
            /*BITMAP bm;
            HDC MemDCExercising = CreateCompatibleDC(hdcimage);
            GetObject(btBitmap,sizeof(bm),&bm);
            HBITMAP outbitmap=CreateBitmap(bm.bmWidth,bm.bmHeight,1,32,NULL);
            HBITMAP oldbitmap =(HBITMAP) SelectObject(MemDCExercising, outbitmap);
            TransparentBlt(MemDCExercising, 0, 0, bm.bmWidth , bm.bmHeight, hdcimage, 0, 0,bm.bmWidth , bm.bmHeight,GetPixel(hdcimage,0,0) );
            SelectObject(MemDCExercising,oldbitmap);
            DeleteDC(MemDCExercising);*/
            return btBitmap;
        }
    
        int height()
        {
            return imageheight;
        }
    
        int width()
        {
            return imageweight;
        }
    
        operator HDC()
        {
            return hdcimage;
        }
    
        ~image()
        {
            //delete btBitmap;
            //delete graphic; //memory leak, when i excute the aplication
            DeleteObject(obj);
            delete img;
            Gdiplus::GdiplusShutdown(m_gdiplusToken);
            //DeleteObject(btBitmap);//don't show me the image
            DeleteDC(hdcimage);
        }
    };
    
    //on menu class
     void bitmap(image imgImage)
        {
            //HBITMAP bitimage = (HBITMAP)LoadImage( NULL, filename.c_str(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
            HMENU hMenu = NULL;
            if(primeiromenu)
                hMenu = mnuBar;
            else
                hMenu = MenuHandle;
            SetMenuItemBitmaps(hMenu,ID,MF_BYCOMMAND,(HBITMAP)imgImage ,(HBITMAP)imgImage);
        }
    i ahve more 1 problem with gif's files, because i get a black color, on background
    but maybe i can fix that too.
    thanks for all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. win32 - how can i change the windows\form backcolor?
    By joaquim in forum Windows Programming
    Replies: 2
    Last Post: 09-07-2014, 02:29 PM
  2. [win32] - how hide backcolor in a form?
    By joaquim in forum Windows Programming
    Replies: 2
    Last Post: 07-18-2014, 12:33 PM
  3. Create "ghost" form from Win32 handle
    By Opariti in forum C# Programming
    Replies: 2
    Last Post: 02-23-2010, 01:50 PM
  4. Finding a knoppix demo in win32 form
    By Stan100 in forum Tech Board
    Replies: 3
    Last Post: 01-15-2005, 06:08 PM
  5. long form of year to short form?
    By bc120 in forum C Programming
    Replies: 2
    Last Post: 12-31-2001, 05:34 PM