如何使用BS_ICON创建显示图标和文本的按钮并发送BM_SETIMAGE? [英] How can I create button showing icon and text using BS_ICON and sending BM_SETIMAGE?

查看:128
本文介绍了如何使用BS_ICON创建显示图标和文本的按钮并发送BM_SETIMAGE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!



我想首先感谢所有花时间查看此主题并尝试提供帮助的人。



我需要制作一个主窗口的子按钮,具有以下特征:



- 它有蓝色背景;

- 我显示一个图标;

- 它在图标下显示文字;

- 文字应该是粗体;

- 文字是塞尔维亚语 - 西里尔语;



就文字而言,我知道有BS_BOTTOM风格可以在按钮底部显示它,有SetFont()函数使其变粗,但我不知道如何让它接受西里尔字符(代码示例将不胜感激)。



As对于图标+文字部分,这是我到目前为止所尝试的:



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



UPDATE#1:发布完整代码,希望帮助他人更快地找到解决方案。



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



Resource.h

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

#ifndef IDC_STATIC

#define IDC_STATIC(-1)

#endif



#define IDI_ICON1 100

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



main.cpp

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


 #includeresource.h
#include< windows.h>

静态HINSTANCE hInst;

//桌面图标,ALT + TAB ...(此图标应暂时用于测试按钮)

静态HICON hIcon;

// WinMain的程序

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);

}
休息;

case WM_CLOSE:

DestroyWindow(hwnd);

休息;

案例WM_DESTROY:

PostQuitMessage(0);

休息;

默认值:
返回DefWindowProc(hwnd,msg,wParam,lParam);
}
返回0;
}

// WinMain

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,
int nCmdShow)
{
//在全局变量中存储hInstance

hInst = hInstance;

//加载主窗口的图标 - 同样的图标将用于测试按钮

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,窗口注册失败!,错误!,MB_ICONEXCLAMATION |
MB_OK );

返回0;
}

//主窗口

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);
返回0;
}

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

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

返回Msg.wParam;
}





在MSDN上,声明按钮显示图标和文字BS_BITMAP或BS_ICON不得设置,我应该发送BM_SETIMAGE。



我试过了,但它失败了。



我使用WIN32 API在MS Visual Studio Express 2008,Windows XP,C ++中工作。如果需要任何其他信息(源代码或类似的东西),请求它,我将非常乐意提供它。

解决方案

以下代码对我有用,使用图标或位图:

 HINSTANCE hInstance = GetModuleHandle(NULL); 
hButton = CreateWindowEx( 0 ,L 按钮,L АБВГДЕЖЗ,WS_VISIBLE | WS_CHILD | BS_TEXT | BS_BOTTOM, 100 100 150 150 ,hWnd,(HMENU) 4000 ,hInstance, 0 );

// 使用图标
HICON hIcon = LoadIcon( hInstance,MAKEINTRESOURCE(IDR_MAINFRAME));
SendMessage(hButton,BM_SETIMAGE,(WPARAM)IMAGE_ICON,(LPARAM)hIcon);

// 或使用位图
HANDLE hBitmap = LoadImage (hInstance,MAKEINTRESOURCE(IDR_BUTTON),IMAGE_BITMAP, 0 0 ,LR_DEFAULTCOLOR);
SendMessage(hButton,BM_SETIMAGE,(WPARAM)IMAGE_BITMAP,(LPARAM)hBitmap);



我有尝试过西里尔字符, 但我想你只需要在设置西里尔字体 CreateWindowEx 调用的第三个参数中发送它们>。





西里尔语也行得正常。

[/ edit]


如果您在2018年遇到问题 - >你只需要先启用视觉风格!

然后发布的代码应该正常工作。



链接:

启用视觉样式Microsoft Docs [ ^

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.

解决方案

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]


If you run into the problem in 2018 --> you simply need to enable visual style first!
Then the posted codes should be working.

Link:
Enabling Visual Styles | Microsoft Docs[^]


这篇关于如何使用BS_ICON创建显示图标和文本的按钮并发送BM_SETIMAGE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆