Adding an Icon to a Button

Pages: 12
Yes, modoran is 100% correct. I confused SendMessage() with SendDlgItemMessage(). Do it just as Modoran stated, or use plain SendMessage() without the GetDlgItem() call.

Sorry 'bout that.

And though modoran participates here somewhat frequently, it's still not enough, 'cause I think he knows a whole lot more about Win32 programming than most of us! -:)
Last edited on
I had similar problem, button dont want to show up if i set image with SendMessage(...) until i added this statement:
 
#pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") 

witch enables visual styles for windows and is somehow related to displaying image with text on button control.
Last edited on
I tried the different SendDlgItemMessage and SendMessage but none of them worked. I even added the code above but that didn't make any difference either.

It looks like the button is REALLY shy. It never shows up whatever I do. So there's only one way:

Could you please write a code, which includes the header file(s), resource file(s) and cpp or c files seperately (like how I did), COMPLETE. (including the WINAPI WinMain, etc...)

If it can work for you that way it should for me too, and if it doesn't, it's definitely a problem with my compiler.

Note: When I compile the current code I have, it gives me a warning:

passing NULL used for non-pointer converting 6 of 'void* LoadImageA (HINSTANCE __*, ...)

(The "..." was added by myself so I don't have to write the syntax of the function.)
Last edited on
Ok. I'll put here small example, give me few minutes.
Ok, here it is:

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
71
72
#include "resources.h"
#include <windows.h>

#pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

HBITMAP hImageBttn = 0;

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

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     static TCHAR szAppName[] = TEXT("BttnImageClass") ;
     HWND         hwnd ;
     MSG          msg ;
     WNDCLASS     wndclass ;
     
     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szAppName ;
     
     if(!RegisterClass(&wndclass)) 
     {
          MessageBox(NULL, TEXT("RegisterClass failed!"), TEXT("Error"), MB_ICONERROR);
          return 0;
     }

     hwnd = CreateWindow(szAppName, TEXT("Button with image example"),
                          WS_OVERLAPPEDWINDOW,
                          20, 20, 300, 120,
                          NULL, NULL, hInstance, NULL) ;

     ShowWindow(hwnd, iCmdShow) ;
     UpdateWindow(hwnd) ;
     
     while(GetMessage (&msg, NULL, 0, 0))
     {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;
     }
     return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_CREATE:
		{
			hImageBttn = LoadBitmap(GetModuleHandle(0), MAKEINTRESOURCE(ID_IMAGE_BTTN));

			DWORD bttnStyle = WS_CHILD | WS_VISIBLE;
			HWND hwndBttn = CreateWindow(TEXT("button"), TEXT("Click me!"), bttnStyle, 10, 10, 200, 50, hwnd, 0, 0, 0);
			SendMessage(hwndBttn, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hImageBttn); 
			return 0;
		}break;
	case WM_DESTROY:
		{
			DeleteObject(hImageBttn);
			PostQuitMessage(0);
			return 0;
		}break;
	};
	return DefWindowProc(hwnd, message, wParam, lParam);
}


resources.h
1
2
3
4
5
6
7
#ifndef reources_HEADER_DEFINED
#define reources_HEADER_DEFINED

#define ID_IMAGE_BTTN     100

#endif


resources.rc
1
2
3
#include "resources.h"

ID_IMAGE_BTTN     BITMAP DISCARDABLE "bttn_image.bmp"


You only need to provide some image as "bttn_image.bmp" and put it into same foder as resources.rc & main.cpp is (project sources folder).

If you comment #pragma comment line image will not show. Why? I don't know.
The #pragma comment is required for Commoncontrols and you should --

#include <commctrl.h> in your header file as well as add this to your linker dependencies --

comctl32.lib

This is if you iinted to use commontrols. IF you don't, the above code will work without it, at least in VS2008.

I have gotten the above code to work with a slight modification, i.e., adding #include <windows> also the resource.h

But the button doesn't show the way I like it. If you're not satsified with the above, I can post a full code using icons a little later.

Anyway, thanks for posting that morando.
Last edited on
I tried the code above; the button appeared but without the bitmap I put in the folder. It only had the text on it. I thank all of you very much for trying to help me so much. Now if you don't mind, please write in your full code. It doesn't matter how long it takes.

Once again, thanks all of you.
I'll try to post the code later on, but in the meantime, are you sure your bitmap was in the SAME folder as your .cpp, .rc, and .h files?
The following code works perfectly. You must follow it EXACTLY, and you must put your icon in the SAME folder as your .cpp, .rc., and .h files.

I am posting this code in two post, this first containing the .cpp file, the next containing both the .rc and .h files.

win_skel.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* Win Dialog Skeleton Application Template
 * Scott Jones, 06/16/2011
 * Unicode Character Set
 * File: win_skel.cpp
 */

#include "win_skel.h"

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine, int iCmdShow)
{
	MSG msg;
	HWND hWinMainWnd;
	TCHAR szNoRegister[]=_T("Unable To Register Window Class! Operation Cancelled.");
	
	if(!DialogRegisterClass(hInstance))
	{
		MessageBox(GetDesktopWindow(),szNoRegister,szAppName,MB_ICONERROR);
		return 0;
	}

	hWinMainWnd=CreateDialog(hInstance, szClassName, 0, NULL);

	SendDlgItemMessage(hWinMainWnd,IDB_BUTTON,BM_SETIMAGE,(WPARAM)IMAGE_ICON,(LPARAM)hMyIcon);

	ShowWindow(hWinMainWnd, iCmdShow);
	UpdateWindow(hWinMainWnd);

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

	return (int)msg.wParam;
}

ATOM DialogRegisterClass(HINSTANCE hInst)
{
	WNDCLASSEX wcex;

	wcex.cbSize				= sizeof(WNDCLASSEX);
	wcex.style				= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc		= DialogWndProc;
	wcex.cbClsExtra			= 0;
	wcex.cbWndExtra			= DLGWINDOWEXTRA; // this required if dialog box is main window
	wcex.hInstance			= hInst;
	wcex.hIcon				= LoadIcon(NULL, MAKEINTRESOURCE(IDI_APPLICATION));
	wcex.hCursor			= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground		= (HBRUSH) GetStockObject(WHITE_BRUSH);
	wcex.lpszMenuName		= MAKEINTRESOURCE(IDM_DIALOG_MENU);
	wcex.lpszClassName		= szClassName;
	wcex.hIconSm			= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
	
	return RegisterClassEx(&wcex);
	
}

LRESULT CALLBACK DialogWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	const int icon_size=25;

	switch(message)
	{
		case WM_CREATE:
			hMyIcon=LoadImage(GetModuleHandle(NULL),MAKEINTRESOURCE(MYICON), IMAGE_ICON, icon_size, icon_size, LR_SHARED);
			return FALSE;

		case WM_COMMAND:
		case WM_SYSCOMMAND:
		switch(LOWORD(wParam))
		{
			case IDOK:
			case IDCANCEL:
				PostQuitMessage(0);
				return 0;
		}
		break;

		case WM_CLOSE:
		case WM_DESTROY:
			PostQuitMessage(0);
			return 0;
	}
	
	return DefWindowProc(hwnd, message, wParam, lParam);
}
win_skel.h - make sure the mainifest statment, i.e., the #pragma statement is all on one line. I broke it off here just to make it fit. Actually, you don't even need the #pragma statement, but it makes your button look better.
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
/* Win Dialog Skeleton Application Template
 * Scott Jones, 06/16/2011
 * Unicode Character Set
 * File: win_skel.h
 */

#include <Windows.h>
#include <tchar.h>

// Enable Commoncontrols ==========================================
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' " "
version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
// ===================================================================

#define szClassName _T("IDD_VANILLA_DIALOG")
#define szAppName _T("Simple Dialog Window")

#define IDM_DIALOG_MENU 101
#define MYICON 102
#define IDB_BUTTON 103

HANDLE hMyIcon;

int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int);
ATOM DialogRegisterClass(HINSTANCE);
LRESULT CALLBACK DialogWndProc(HWND,UINT,WPARAM,LPARAM);


win_skel.rc
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
/* Win Dialog Skeleton Application Template
 * Scott Jones, 06/16/2011
 * Unicode Character Set
 * File: win_skel.rc
 */

#include "win_skel.h"

MYICON ICON "myicon.ico"

IDD_VANILLA_DIALOG DIALOGEX 0, 0, 186, 105
STYLE DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_VISIBLE |
		WS_BORDER | WS_CAPTION | WS_DLGFRAME | WS_GROUP | WS_SYSMENU
EXSTYLE WS_EX_OVERLAPPEDWINDOW | WS_EX_APPWINDOW
CLASS szClassName
CAPTION "Vanilla Dialog"
FONT 8, "Ms Shell Dlg 2", 400, 0, 1
BEGIN
    DEFPUSHBUTTON   "OK", IDOK, 78, 73, 50, 14
    PUSHBUTTON      "Cancel", IDCANCEL, 132, 73, 50, 14
	PUSHBUTTON		"", IDB_BUTTON, 10, 10, 25, 25, BS_ICON
END

IDM_DIALOG_MENU MENU DISCARDABLE
BEGIN
    POPUP "&File"
    BEGIN
		MENUITEM "Exit Application"		IDOK
	END
	POPUP "&Edit"
	BEGIN
		MENUITEM "Click On Cancel"		IDCANCEL
	END
END
Last edited on
Topic archived. No new replies allowed.
Pages: 12