Icon in Button (Without making the button in resources)

Is there a way to add an image to a button WITHOUT CREATING THE BUTTON IN A RESOURCE FILE? Like use LoadImage() to load the icon, and then SendMessage() the icon to the button? Is this even possible when not making the button in the resource file? Lamblion should know how to do this, even though when he last specified how to do it he made the button in a resource file, then sent it to the button in the .cpp file.

BOTTOM LINE: I want to know how to get an image AND text into a button in a WinAPI window without making the button in the .rc file.

Suggestions?
Last edited on
You can do it with using BS_BITMAP button style and then send message to button

SendMessage( Button, BM_SETIMAGE, (WPARAM) IMAGE_BITMAP, (LPARAM)(HBITMAP) hbmp );

And as far I know you should do this without using BS_DEFPUSHBUTTON style
Last edited on
Well, I'm not using BS_DEFPUSHBUTTON, and it compiles successfully, but the button doesn't even show up. Here's the current code I'm using:

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
#include <windows.h>
#include <winver.h>
#include "resource.h"
#include "FreeImage.h"

using namespace std;

LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);

HWND saveFile    = NULL;
HWND hMenu       = NULL;
HINSTANCE hInst;

int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, int nCmdShow) {

	MSG			msg;
	WNDCLASS	wc;
	HWND		hwnd = NULL;
	hInst = hThisInstance;

	wc.cbClsExtra		= 0;
	wc.cbWndExtra		= 0;
	wc.hbrBackground	= (HBRUSH)(COLOR_WINDOW + 1);
	wc.hCursor			= LoadCursor(NULL, IDC_ARROW);
    wc.hIcon            = LoadIcon(hInst, MAKEINTRESOURCE(SMALL_ICON));
	wc.hInstance      	= hThisInstance;
	wc.lpfnWndProc		= WinProc;
	wc.lpszClassName	= "Window";
	wc.lpszMenuName		= NULL;
	wc.style			= CS_HREDRAW | CS_VREDRAW;

 	if (!RegisterClass(&wc)) {
		MessageBox(NULL, "Error registering class", "ERROR", MB_OK);
		return 0;
	}

	hwnd = CreateWindow("Window",
						"Window",
						WS_OVERLAPPEDWINDOW,
						CW_USEDEFAULT,
						CW_USEDEFAULT,
						620,
						500,
						NULL,
						NULL,
						hThisInstance,
						NULL);

	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);

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

	return msg.wParam;
}

LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {

    static HMENU hMenu, hSubMenu;

	switch (msg) {

        case WM_CREATE: {

            hMenu = CreateMenu();

            hSubMenu = CreatePopupMenu();
            AppendMenu(hSubMenu, MF_STRING, 100, "&Open");
            AppendMenu(hSubMenu, MF_SEPARATOR, 0, 0);
            AppendMenu(hSubMenu, MF_STRING, 101, "E&xit");
            AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&File");

            hSubMenu = CreatePopupMenu();
            AppendMenu(hSubMenu, MF_STRING, 103, "&Read-only");
            AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Options");

            hSubMenu = CreatePopupMenu();
            AppendMenu(hSubMenu, MF_STRING, 102, "&About");
            AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Help");

            saveFile = CreateWindowEx(0, "button", 0, WS_VISIBLE | WS_CHILD | BS_BITMAP, 10, 10, 113, 178, hwnd, (HMENU)BTN, hInst, NULL);

            LoadBitmap(hInst, "JAILBREAK_ICON");

        } break;

        case WM_DESTROY: {
		    PostQuitMessage(0);
            return 0;
        } break;

        case WM_CLOSE: {
		    DestroyWindow(hwnd);
        } break;
    }

    return DefWindowProc(hwnd, msg, wParam, lParam);
}

void LoadResources(HWND hwnd){
     LoadBitmap(hInst, "JAILBREAK_ICON");
}

/*

m_hWndButton = CreateWindowEx( 0, L"BUTTON", L"Button text", WS_VISIBLE | WS_CHILD | BS_BITMAP, 10, 145, 50, 50, m_hWnd, NULL, (HINSTANCE)GetWindowLong(m_hwnd, GWL_HINSTANCE), NULL);

SendMessage( (HWND) m_hWndButton, (UINT) BM_SETIMAGE, (WPARAM) IMAGE_BITMAP, (LPARAM) IMAGE_BUTTON );

*/


I'm also using modern XML styles (XP/Vista) for manifest. The image is included in the RC file.
Last edited on
Topic archived. No new replies allowed.