Click here to Skip to main content
15,895,656 members

How can I create button showing icon and text using BS_ICON and sending BM_SETIMAGE?

MyOldAccount asked:

Open original thread
Hello everyone!

I would like to start by saying thanks to everyone who takes some time to view this thread and try to help.

I need to make a button that is a child of a main window with following characteristics:

- It has blue background;
- I displays an icon;
- It displays text under icon;
- Text should be bold;
- Text is in Serbian-Cyrillic;

As far as text is concerned, I know there is BS_BOTTOM style to display it at the bottom of the button, and there is SetFont() function to make it bold, but I don't know how to make it accept cyrillic characters ( code example would be greatly appreciated ).

As for icon + text part, this is what I have tried so far:

***********************************************************************************

UPDATE #1: posted entire code in hope for helping others finding a solution faster.

************************************************************************************

Resource.h
******************************************************
#ifndef IDC_STATIC
#define IDC_STATIC (-1)
#endif

#define IDI_ICON1 100
*******************************************************

main.cpp
*******************************************************

#include "resource.h"
#include <windows.h>

static HINSTANCE hInst;

// icon for desktop, ALT +TAB ... ( this same icon shall be used temporally for testing button )

static HICON hIcon; 

// WinMain's procedure

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

	switch(msg)
	{

	case WM_CREATE:
		
		{
			HWND hbtnUnosPodataka = CreateWindowEx(0, "Button", "Geotermist", WS_VISIBLE | WS_CHILD |  BS_TEXT | BS_BOTTOM, 100, 100,150, 150, hwnd, (HMENU)4000, hInst, 0);

			HICON hIcon1 = LoadIcon( hInst, MAKEINTRESOURCE(IDI_ICON1));
			
			SendMessage( hbtnUnosPodataka , BM_SETIMAGE, (WPARAM)IMAGE_ICON, (LPARAM)hIcon1);
		
		}
		break;

	case WM_CLOSE:

		DestroyWindow(hwnd);

		break;

	case WM_DESTROY:

		PostQuitMessage(0);

		break;

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

// WinMain

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, 
				   int nCmdShow)
{
	// store hInstance in global variable

	hInst = hInstance;
	
	// load main window's icon - this same icon shall be used for testing the button

	hIcon = LoadIcon( hInst, MAKEINTRESOURCE(IDI_ICON1));

	WNDCLASSEX wc;
	HWND hwnd;
	MSG Msg;

	wc.cbSize = sizeof(WNDCLASSEX);
	wc.style = 0;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = hIcon;
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH); 
	wc.lpszMenuName = NULL;
	wc.lpszClassName = "Main_Window";
	wc.hIconSm = hIcon;

	if(!RegisterClassEx(&wc))
	{
		MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | 
			MB_OK);

		return 0;
	}

	// main window

	hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, "Main_Window", "Geotermist",
		WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
		CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

	if(hwnd == NULL)
	{
		MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

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

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

	return Msg.wParam;
}


At MSDN, it is stated that in order for button to display icon and text BS_BITMAP or BS_ICON must not be set, and I should send BM_SETIMAGE.

I have tried that, but it failed.

I work in MS Visual Studio Express 2008, on Windows XP, in C++, using WIN32 API. If any other information is required ( source code or something similar ), please ask for it, I will more than gladly supply it.
Tags: C++, Win32

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900