Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Win32
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.
Posted 10-May-13 3:48am
Edited 10-May-13 7:53am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The following code worked for me, using either an icon or a bitmap :
HINSTANCE		hInstance = GetModuleHandle(NULL);
hButton = CreateWindowEx(0, L"Button", L"АБВГДЕЖЗ", WS_VISIBLE | WS_CHILD | BS_TEXT | BS_BOTTOM, 100, 100, 150, 150, hWnd, (HMENU)4000, hInstance, 0);
 
// use an icon
HICON hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDR_MAINFRAME));
SendMessage(hButton, BM_SETIMAGE, (WPARAM)IMAGE_ICON, (LPARAM)hIcon);
 
// or use a bitmap
HANDLE hBitmap = LoadImage(hInstance, MAKEINTRESOURCE(IDR_BUTTON), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);
SendMessage(hButton, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hBitmap);
 
I have not tried Cyrillic characters, but I would guess you just need to send them in the third parameter of the CreateWindowEx call, after setting a Cyrillic font.
 
[edit]
Cyrillic also works fine.
[/edit]
  Permalink  
v3
Comments
Nenad Smiljkovic at 10-May-13 12:27pm
   
I have made adjustments according to your solution, but it does not work for me.
 
Can you post your entire code, or should I post mine ( it is not so big ), so we can see what is missing?
 
Thank you.
Richard MacCutchan at 10-May-13 12:40pm
   
That is my entire code in terms of displaying the button. It creates a button in the main window of a very basic Windows application. I did discover that the BS_BOTTOM style applies to the image that's painted on the button, rather than the text.
 
Maybe you should show what you have done. But please don't post it as a reply to this message; use the "Improve question" link above and add it in there.
Nenad Smiljkovic at 10-May-13 12:54pm
   
I did as you have told me.
Hope that this will help.
Thank you.
Richard MacCutchan at 10-May-13 13:09pm
   
Thank you, it allowed me to copy your code into VS and build it. And guess what, it works perfectly! However, that was using VS2010 in Windows 7; I shall give VS2008 a try, but probably not before tomorrow.
 
Can you try and explain in more detail exactly what happens when you do it.
Nenad Smiljkovic at 10-May-13 13:13pm
   
Thank you, yes I can explain.
 
It displays text normally, but there is no icon.
 
I suspect that it is Visual Studio's fault, since I am using free edition, and it does have some limitations, like resource editor being unavailable, so I have to use alternative one.
 
Perhaps this is just another part that Microsoft left out from free edition.
Richard MacCutchan at 11-May-13 3:52am
   
I am also using the express edition and it works fine. Are you sure that your icon is correctly included and built in your resources?
Richard MacCutchan at 11-May-13 6:00am
   
I managed to test this using VS 2008 Express edition on Windows XP, and it does not work. It seems you can get an image, or text, but not both. I am not sure whether this is a Windows or Visual Studio problem. You could try upgrading to VS 2010 Express and see if it works then.
Nenad Smiljkovic at 11-May-13 16:08pm
   
It is even worse with Express Edition 2010.
I get the same symptoms when I comment out #pragma comment, yet if I leave it button doesn't show at all!
I will try to get Professional edition, and we shall se what then happens.
Richard MacCutchan at 12-May-13 3:16am
   
Yes, I discovered that; most strange. However, I am sure this has more to do with Windows version than Visual Studio. As I said above, your code works perfectly under Windows 7, I get the image and the text.
Nenad Smiljkovic at 12-May-13 13:01pm
   
It works with Express Edition 2008 on Windows 7.
I guess it is something with XP...
Anyway,thank you for truly trying to help,thus I shall mark this thread as solved by accepting your solution.
Just update your solution to include Cyrillic font please!
It is important ( code or pseudo-code or simple instructions will do )!
Richard MacCutchan at 12-May-13 13:10pm
   
I guess the Microsoft documentation is not up to date.
 
I already put the Cyrillic characters in the solution above; you just need to use Unicode strings.
Nenad Smiljkovic at 12-May-13 13:22pm
   
The UNICODE part is what troubles me.
How do I include UNICODE?
I am embarrassed to ask, but after I type #include I still get errors.
Richard MacCutchan at 13-May-13 2:46am
   
Sorry, I should have explained, you set Unicode in Visual Studio. Right click on your Project in Solution Explorer, then click on Properties, and go to General -> Character Set, and select Use Unicode Character Set.
Nenad Smiljkovic at 13-May-13 14:10pm
   
I have done it, and MS VISUAL C++ reported error by spawning new window with following information:
 
-------------------------------------------------------
Some UNICODE characters in this file could not be saved in the current codepage.
Do you want to resave this file as UNICODE in order to maintain your data?
-------------------------------------------------------
 
I must say that I didn't expect that... Any ideas?
Richard MacCutchan at 14-May-13 2:49am
   
That's because you have Unicode characters in your string, and thus in the source file. Visual Studio just needs to convert the source to Unicode, so you should answer yes to the message.
Nenad Smiljkovic at 14-May-13 9:07am
   
It worked!
THANK YOU SO MUCH!
GOD BLESS YOU!
Richard MacCutchan at 14-May-13 11:04am
   
My pleasure; good luck with the rest of your project.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 130
4 DamithSL 95
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 10 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100