Displaying Bitmap

I cant seem to display a bitmap on my window, i looked all over the web but i get long complicated code, im trying to keep it simple as possible. Im using a resource.rc file so i have a BITMAP control key in there but i cant load the image. Here is my code, what am i doing wrong? Also i tried MSDN but it gave me no useful Information other than the keyword. itself.

Main.cpp:

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
#define WIN32_LEAN_AND_MEAN
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include "resource.h"

HINSTANCE hInst;

BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        case WM_INITDIALOG:
            /*
             * TODO: Add code to initialize the dialog.
             */
            return TRUE;

        case WM_CLOSE:
            EndDialog(hwndDlg, 0);
            return TRUE;

        case WM_COMMAND:

            switch(LOWORD(wParam))
            {
                case Hide:
                    {
                        HWND hide = GetDlgItem(hwndDlg, Button); //Hide the test button
                        ShowWindow(hide, SW_HIDE);

                            {//Disable Text Box
                                HWND disable = GetDlgItem(hwndDlg, Hide);
                                EnableWindow(disable, FALSE);
                            }

                            {//Hide Text
                                HWND HideText = GetDlgItem(hwndDlg, StatText);
                                ShowWindow(HideText, FALSE);
                            }

                        Sleep(600);
                        MessageBox(hwndDlg, "You hid the box and the text, your magical", "Box Hidden", MB_ICONMASK);
                        return TRUE;
                    }


                case Button:
                    {
                        MessageBox(hwndDlg, "Hide Me!!", "Hide Button", MB_ICONMASK);
                        return TRUE;
                    }

                }//End of second switch statement

        }

    return FALSE;
}


int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    hInst = hInstance;

    // The user interface is a modal dialog box
    return DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DialogProc);
}


resource.rc:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "resource.h"

DLG_MAIN DIALOGEX 6, 5, 300, 150

//CAPTION "Hide"

FONT 8, "Tahoma"

STYLE 0x10CE0804
//EXSTYLE WS_EX_TOOLWINDOW // Makes Box Square



BEGIN
    CTEXT "Hide the 'Test' Box", StatText, 110,  80, 96, 15
    CTEXT "You'll notice you can click the 'Test' Button as many times as you want but when you click the \n button labeled 'Hide 'Test' Button' it will make the Test button dissappear and the box will turn grey.", StatText2, 0, 0, 300, 25
    CONTROL "Hide 'Test' Button", Hide, "Button", 0x10010000, 110,  125, 96, 15
    CONTROL "Test", Button, "Button", 0x10010000, 110,  105, 96, 15
    BITMAP "face.bmp", bmap, 110,  105, 96, 15
END



resource.h:

1
2
3
4
5
6
7
8
9
10
11
#include <windows.h>

// ID of Main Dialog
#define DLG_MAIN 101

// ID of Button Controls
#define Hide 1
#define Button 2
#define StatText 3
#define StatText2 4
#define bmap 5 



Syntax error is the only error my compiler gives me

C:\Users\CHAYHA~1\Desktop\KEYLOG~1\resource.rc|19|syntax error|
||=== Build finished: 1 errors, 0 warnings ===|
Last edited on
Bump please help. I tried this but it isnt working it keeps saying syntax error at the Bitmap line in my resource.rc:

http://mirrors.zoreil.com/webclub.kcom.ne.jp/ma/colinp/win32/resources/bitmap.html


ERROR:

BitmapID1 BITMAP "face.bmp"


C:\Users\CHAYHA~1\Desktop\KEYLOG~1\resource.rc|20|syntax error|
||=== Build finished: 1 errors, 0 warnings ===|
Last edited on
You can't have a bitmap inside the dialogbox statement in a resource like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
DLG_MAIN DIALOGEX 6, 5, 300, 150

//CAPTION "Hide"

FONT 8, "Tahoma"

STYLE 0x10CE0804
//EXSTYLE WS_EX_TOOLWINDOW // Makes Box Square



BEGIN
    CTEXT "Hide the 'Test' Box", StatText, 110,  80, 96, 15
    CTEXT "You'll notice you can click the 'Test' Button as many times as you want but when you click the \n button labeled 'Hide 'Test' Button' it will make the Test button dissappear and the box will turn grey.", StatText2, 0, 0, 300, 25
    CONTROL "Hide 'Test' Button", Hide, "Button", 0x10010000, 110,  125, 96, 15
    CONTROL "Test", Button, "Button", 0x10010000, 110,  105, 96, 15
    BITMAP "face.bmp", bmap, 110,  105, 96, 15  //******ERROR- BITMAP statement not allowed in a dialogbox  setup like this
END


You need to make a seperate statement BITMAP resource statement - and in the dialog box part you put a control statement
like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Dialog
//

DLG_MAIN DIALOGEX 6, 5, 300, 150
STYLE DS_SETFONT | DS_3DLOOK | DS_CENTER | WS_MINIMIZEBOX | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME
FONT 8, "Tahoma", 0, 0, 0x1
BEGIN
    CTEXT           "Hide the 'Test' Box",StatText,110,80,96,15
    CTEXT           "You'll notice you can click the 'Test' Button as many times as you want but when you click the \n button labeled 'Hide 'Test' Button' it will make the Test button dissappear and the box will turn grey.",StatText2,0,0,300,25
    PUSHBUTTON      "Hide 'Test' Button",Hide,110,125,96,15
    PUSHBUTTON      "Test",Button,110,105,96,15
    CONTROL         IDB_BITMAP1,IDC_STATIC,"Static",SS_BITMAP,53,47,32,30
END

IDB_BITMAP1             BITMAP                  "bitmap1.bmp"



The last 4 numbers in the CONTROL statement are the x and y position and width and height of the bitmap (actually the static control that displays the bitmap).
Last edited on
Ok, what is IDC_STATIC and SS_BITMAP for? also my bitmap isnt displaying in the window, does it have to be the same size asd the window.

EDIT: also is there anything that needs to be in my main.cpp? i tried what is in the link and got these errors:

C:\Users\Chay Hawk\Desktop\Keylogger\resource.h|14|warning: "SS_BITMAP" redefined|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\winuser.h|311|warning: this is the location of the previous definition|
C:\Users\Chay Hawk\Desktop\Keylogger\main.cpp||In function 'BOOL DialogProc(HWND__*, UINT, WPARAM, LPARAM)':|
C:\Users\Chay Hawk\Desktop\Keylogger\main.cpp|55|error: expected unqualified-id before numeric constant|
C:\Users\Chay Hawk\Desktop\Keylogger\main.cpp|57|error: lvalue required as left operand of assignment|
||=== Build finished: 2 errors, 2 warnings ===|


Main.cpp:

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
#define WIN32_LEAN_AND_MEAN
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include "resource.h"

HINSTANCE hInst;

BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        case WM_INITDIALOG:
            /*
             * TODO: Add code to initialize the dialog.
             */
            return TRUE;

        case WM_CLOSE:
            EndDialog(hwndDlg, 0);
            return TRUE;

        case WM_COMMAND:

            switch(LOWORD(wParam))
            {
                case Hide:
                    {
                        HWND hide = GetDlgItem(hwndDlg, Button); //Hide the test button
                        ShowWindow(hide, SW_HIDE);

                            {//Disable Text Box
                                HWND disable = GetDlgItem(hwndDlg, Hide);
                                EnableWindow(disable, FALSE);
                            }

                            {//Hide Text
                                HWND HideText = GetDlgItem(hwndDlg, StatText);
                                ShowWindow(HideText, FALSE);
                            }

                        Sleep(600);
                        MessageBox(hwndDlg, "You hid the box and the text, your magical", "Box Hidden", MB_ICONMASK);
                        return TRUE;
                    }


                case Button:
                    {
                        MessageBox(hwndDlg, "Hide Me!!", "Hide Button", MB_ICONMASK);
                        return TRUE;
                    }

                }//End of second switch statement

                HBITMAP Bmap;
                Bmap = LoadBitmap (hInst, MAKEINTRESOURCE(Bmap));

        }

    return FALSE;
}


int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    hInst = hInstance;

    // The user interface is a modal dialog box
    return DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DialogProc);
}
Last edited on
- resource.rc -
1
2
3
4
5
6
7
8
9
10
11
#include "resource.h"

DLG_MAIN DIALOGEX 0,0,219,111
CAPTION "Bitmap Test"
STYLE WS_VISIBLE|WS_CAPTION|WS_SYSMENU
BEGIN
  CONTROL "Show Bitmap",BTN_TEST,"Button",WS_VISIBLE,5,5,100,100
  CONTROL "",STC_BMP,"Static",WS_VISIBLE|SS_BITMAP,111,6,100,100
END

BMP_TEST BITMAP "test.bmp"


- resource.h -
1
2
3
4
5
6
#include <windows.h>

#define DLG_MAIN 1000
#define BTN_TEST 1001
#define STC_BMP  1002
#define BMP_TEST 9999 


- main.cpp -
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
#define WIN32_LEAN_AND_MEAN

#include <windows.h>

#include "resource.h"

HINSTANCE hInst;
HBITMAP hBMP = 0;
HWND hBitmap = 0, hButton = 0;
BOOL hidden = TRUE;

BOOL CALLBACK DialogProc(HWND H,UINT M,WPARAM W,LPARAM L)
{
    switch(M)
    {
        case WM_INITDIALOG:
        {
          hButton = GetDlgItem(H,BTN_TEST);
          hBitmap = GetDlgItem(H,STC_BMP);
          hBMP = LoadBitmap(hInst,MAKEINTRESOURCE(BMP_TEST));
          SendMessage(hBitmap,STM_SETIMAGE,(WPARAM)IMAGE_BITMAP,(LPARAM)hBMP);
          ShowWindow(hBitmap,SW_HIDE);
          return TRUE;
        }
        case WM_CLOSE:
        {
          DeleteObject(hBMP);
          EndDialog(H,0);
          return TRUE;
        }
        case WM_COMMAND:
        {
           switch(LOWORD(W))
           {
             case BTN_TEST:
             {
                if(hidden)
                {
                  hidden = FALSE;
                  ShowWindow(hBitmap,SW_SHOW);
                  SendMessage(hButton,WM_SETTEXT,0,(LPARAM)"Hide Image");
                  return TRUE;
                } else {
                  hidden = TRUE;
                  ShowWindow(hBitmap,SW_HIDE);
                  SendMessage(hButton,WM_SETTEXT,0,(LPARAM)"Show Image");
                  return TRUE;
                }
             }
          }
       }
    }
    return FALSE;
}


int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    hInst = hInstance;
    return DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DialogProc);
}

Awesome thanks!!
I think, this is the difference between Microsoft Visual Studio Resource compiler which works the
easy way I described and what ever resource compiler you are using
I just tried my two line solution in codeblocks and got a syntax error.
I use code blocks and it worked for me.
Topic archived. No new replies allowed.