Cant get bitmap to load :/

I mean...maybe this belongs in windows programming but im pretty sure im making a beginners mistake somewhere...My bitmap wont load and yes the relative file path is correct (I put it in run and it did bring up my bmp)
Uh..Heres the code. Much thanks :D
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
#include <windows.h>


LRESULT CALLBACK Mainwndproc(HWND, UINT, WPARAM, LPARAM);

char szClassName[] = "ClassName";

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE xxx, LPSTR lpszArg, int nCmdShow)
{
    HWND hwnd;
    MSG msg;
    WNDCLASSEX wclx;

    wclx.cbClsExtra = 0;
    wclx.cbSize = sizeof(WNDCLASSEX);
    wclx.cbWndExtra = 0;
    wclx.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
    wclx.hCursor = LoadCursor(NULL, IDC_ARROW);
    wclx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wclx.hIconSm= LoadIcon(NULL, IDI_APPLICATION);
    wclx.hInstance = hInstance;
    wclx.lpfnWndProc= Mainwndproc;
    wclx.lpszClassName= szClassName;
    wclx.lpszMenuName = NULL;
    wclx.style = CS_DBLCLKS;
    if(!RegisterClassEx(&wclx))
        return 0;

    hwnd = CreateWindowEx(0,                   /* Extended possibilites for variation */
                          szClassName,         /* Classname */
                          "TetGame",       /* Title Text */
                          WS_OVERLAPPEDWINDOW, /* default window */
                          CW_USEDEFAULT,       /* Windows decides the position */
                          CW_USEDEFAULT,       /* where the window ends up on the screen */
                          544,                 /* The programs width */
                          375,                 /* and height in pixels */
                          HWND_DESKTOP,        /* The window is a child-window to desktop */
                          NULL,                /* No menu */
                          hInstance,       /* Program Instance handler */
                          NULL                 /* No Window Creation data */
                         );

    ShowWindow(hwnd, nCmdShow);

    while(GetMessage(&msg, hwnd,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;

}


LRESULT CALLBACK Mainwndproc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{

    HDC hDC, hMemDC;
    PAINTSTRUCT paint;
    HBITMAP bitmap;
    switch(message)
    {
    case WM_CREATE:
    {
        bitmap=(HBITMAP)LoadImage(NULL,"C:\\Users\\Owner\\Desktop\\TetrisGame\\bitmap.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
    }
    break;

    case WM_DESTROY:
    {
        PostQuitMessage(0);
        return 0;
    }
    break;
    case WM_PAINT:
        hDC= BeginPaint(hwnd, &paint);
        ::SelectObject(hMemDC,bitmap);
        BitBlt(hDC,0,0,200,400,hMemDC,0,0,SRCCOPY);
        ::DeleteDC(hMemDC) ;
        EndPaint(hwnd, &paint);



    default:
        return DefWindowProc(hwnd,message,wParam,lParam);
    }
    return 0;


}
bump
Your HBITMAPis local to Mainwndproc. That means that every time that function exits, it will be destroyed. And when the function is re-entered, a new one is created.

When you get a WM_CREATE message, you load the bitmap and it's put in your 'bitmap' object. Then the function exits and you lose the bitmap because your 'bitmap' object goes out of scope.

Then when you get a WM_CREATE message, another 'bitmap' object is created (but doesn't have any bitmap loaded in it), so there's no bitmap loaded.


All you're accomplishing here is leaking memory.

Plus, you never created hMemDC, so you shouldn't be using it.
Topic archived. No new replies allowed.