why my image class don't return HBITMAP?

heres my MemoryDC and BitmapDC:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
class MemoryDC
{
private:
    HDC memoryDC;

public:
    MemoryDC ()
    {
        HDC hdc=GetDC(GetDesktopWindow());
        memoryDC=CreateCompatibleDC(hdc);
        ReleaseDC(GetDesktopWindow(),hdc);
    }

    operator HDC() const
    {
        return memoryDC;
    }

    ~MemoryDC ()
    {
       DeleteDC(memoryDC);
    }
};

class BitmapDC
{
private:
    MemoryDC hdcbitmap;
    HGDIOBJ bitmapold;
    HBITMAP bitmapcurrent;
    int intwidth;
    int intheight;

    void init(int width, int height)
    {
        bitmapcurrent = CreateBitmap(width, height, 1, 32, NULL);
        bitmapold = SelectObject(hdcbitmap, bitmapcurrent);
        intwidth=width;
        intheight=height;
    }

    void destroy()
    {
        SelectObject(hdcbitmap, bitmapold);
        DeleteObject(bitmapcurrent);
    }

public:

    BitmapDC(int width, int height)
    {
        init(width, height);
    }

    BitmapDC()
    {
        init(1, 1);
    }

    void size(int width, int height)
    {
        destroy();
        init(width, height);
    }

    int Width()
    {
        return intwidth;
    }

    int Height()
    {
        return intheight;
    }

    BitmapDC& operator= (const BitmapDC &bitmapsource)
    {
        if (this == &bitmapsource)      // Same object?
            return *this;
        destroy();
        init(bitmapsource.intwidth, bitmapsource.intheight);
        BitBlt(bitmapsource, 0, 0, intwidth, intheight, bitmapsource, 0, 0, SRCCOPY);
        return *this;
    }

    BitmapDC& operator= (const HBITMAP &bitmapsource)
    {
        destroy();
        BITMAP bm;
        GetObject(bitmapsource,sizeof(bm),&bm);
        init(bm.bmWidth, bm.bmHeight);
        DrawHBITMAPtoHDC(bitmapsource,hdcbitmap);
        return *this;
    }

    //testing the bitmao value if is nullptr
    bool operator != ( nullptr_t ) const
    {
        return bitmapcurrent != nullptr;
    }

    bool operator==(const BitmapDC &other) const
    {
        return (other.bitmapcurrent == this->bitmapcurrent);
    }

    bool operator!=(const BitmapDC &other) const
    {
        return !(*this == other);
    }

    operator HBITMAP() const
    {
        return bitmapcurrent;
    }

    operator HDC() const
    {
        return hdcbitmap;
    }

    ~BitmapDC()
    {
       destroy();
    }
};

using them i created my image class.
but see these image members:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
HICON HICONFromHBITMAP(HBITMAP bitmap)
    {
        BITMAP bmp;
        GetObject(bitmap, sizeof(BITMAP), &bmp);
        HBITMAP hbmMask = CreateCompatibleBitmap(HBitmap, bmp.bmWidth, bmp.bmHeight);

        ICONINFO ii = {0};
        ii.fIcon    = TRUE;
        ii.hbmColor = bitmap;
        ii.hbmMask  = hbmMask;
        HICON hIcon = CreateIconIndirect(&ii);
        DeleteObject(ii.hbmColor);
        DeleteObject(ii.hbmMask);
        return hIcon;
    }
operator HICON()
    {
        return HICONFromHBITMAP(HBitmap);
    }

    operator HBITMAP() const
    {
        return HBitmap;
    }
operator HDC() const
    {
        return HBitmap;
    }
void draw(HDC control, long posX=0, long posY=0)
    {
        if (blnTransparent==true)
        {
            TransparentBlt(control, posX, posY,width(),height(),HBitmap, 0, 0,width(), height(), clrBackColor.ToCOLORREF());
        }
        else
        {
            BitBlt(control,posX,posY,width(),height(),HBitmap,0,0,SRCCOPY);
        }
    }

the draw(), HICON and HDC works. my problem is that or i lose the HBITMAP or something :(
i can use HBITMAP for return HICON and works fine. but i can't return the HBITMAP... why these error?
but i can't return the HBITMAP... why these error?
What error?
coder777: the problem is that i can't return the HBITMAP from my image class.
can you tell me why?
i'm confused because of Variable Life Time(i don't remember the correct term), but i don't have sure... please correct me
Do you mean a compiler error on line 27? The operator on line 25 returns HDC not HBITMAP. Do you even need that?

Honestly I wouldn't use these implicit cast operators. Especially not several.

i don't recive an error message.. sorry about... some errors are what we see :(
and i don't see an image, if i use it. but know i think i know why: maybe i was creating\copy the image object to another image object. on a function parameter. so the HBITMAP, maybe, was destroyed after finish the instructions that function. i don't have sure. now i can show the image because, on menu, i create a HDC and HBITMAP global class's variables. everytime that i need change the image:
1 - delete all GDI objects;
2 - do like a Double Buffer;
3 - copy the image HDC to Double Buffer;
4 - add the HBITMAP, from Double Buffer, image to menus;
5 - delete GDI objects except the HBITMAP object;
6 - the HBITMAP object is only deleted when the class is destroyed and when i change it.
what i'm learning is that everytime i need a HBITMAP, i must recreate anotherone with origin HDC.
correct me, if i'm wrong on something
I don't understand why you permanently need to recreate/copy images. That's rather odd.

I don't know whether you use it. Take a look at line 82:

BitBlt(bitmapsource, 0, 0, intwidth, intheight, bitmapsource, 0, 0, SRCCOPY);
thanks for that.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const int LastPixel=-1;
const int FirstPixel=-2;
void DrawHBITMAPtoHDC(HBITMAP hBitmap, HDC hdc, int PosX=0, int PosY=0, COLORREF backcolor=-1)
{
    BITMAP bm;
    HDC MemDCExercising = CreateCompatibleDC(hdc);
    HBITMAP oldbitmap =(HBITMAP) SelectObject(MemDCExercising, hBitmap);
    GetObject(hBitmap,sizeof(bm),&bm);
    if(backcolor==LastPixel)
        TransparentBlt(hdc, PosX, PosY, bm.bmWidth , bm.bmHeight, MemDCExercising, 0, 0,bm.bmWidth , bm.bmHeight,GetPixel(MemDCExercising,bm.bmWidth-1,bm.bmHeight-1));
    else if(backcolor==FirstPixel)
        TransparentBlt(hdc, PosX, PosY, bm.bmWidth , bm.bmHeight, MemDCExercising, 0, 0,bm.bmWidth , bm.bmHeight,GetPixel(MemDCExercising,0,0));
    else
        TransparentBlt(hdc, PosX, PosY, bm.bmWidth , bm.bmHeight, MemDCExercising, 0, 0,bm.bmWidth , bm.bmHeight,backcolor);
    SelectObject(MemDCExercising,oldbitmap);
    DeleteDC(MemDCExercising);
    DeleteObject(hBitmap);
}

//...............................

BitmapDC& operator= (const BitmapDC &bitmapsource)
    {
        if (this == &bitmapsource)      // Same object?
            return *this;
        destroy();
        init(bitmapsource.intwidth, bitmapsource.intheight);
        BitBlt(bitmapsource, 0, 0, intwidth, intheight, hdcbitmap, 0, 0, SRCCOPY);
        return *this;
    }

    BitmapDC& operator= (const HBITMAP &bitmapsource)
    {
        destroy();
        BITMAP bm;
        GetObject(bitmapsource,sizeof(bm),&bm);
        init(bm.bmWidth, bm.bmHeight);
        DrawHBITMAPtoHDC(bitmapsource,hdcbitmap);
        return *this;
    }

what do you think about the BitmapDC& operator= (const HBITMAP &bitmapsource)? have errors too?
"I don't understand why you permanently need to recreate/copy images. That's rather odd."
for copy the images from 1 to anotherone
Last edited on
See:

https://msdn.microsoft.com/en-us/library/windows/desktop/dd183370%28v=vs.85%29.aspx

Line 28: swap bitmapsource and hdcbitmap since destination comes first.

for copy the images from 1 to anotherone
So let me rephrase my question: Why do you need to copy the image? Which is very time consuming.

I suggest that you use a [shared_ptr] pointer if you want to draw a certain image more than once.
imagine that you have 1 image object. now we need copy the image object on another function for another image object.
Well, if it works for you it's okay.
using HDC works. but i notice 2 things:
1 - for diferent HDC, i must create a new HBITMAP object;
2 - for use HBITMAP object, on other place, i must recreate another HBITMAP object.
both using a new HDC copyied from original HDC.
maybe i can be wrong. but seems what i notice by experience.
Last edited on
See:

https://msdn.microsoft.com/en-us/library/windows/desktop/dd162957%28v=vs.85%29.aspx

MSDN wrote:
Bitmaps can only be selected into memory DC's. A single bitmap cannot be selected into more than one DC at the same time.
thanks for that information coder777, that's why i can't return the HBITMAP. thanks for all
Topic archived. No new replies allowed.