[Win32SDK基本]Button Control(1)Push Button 和 BM_SETIMAGE

本文由CSDN用户zuishikonghuan所作,转载请注明出处:http://blog.csdn.net/zuishikonghuan/article/details/46649211

前两篇中介绍了Static Control,这几篇来介绍一下Button Control

Button Control有很多类型,下图中展示了这一点(图片来自MSDN:https://msdn.microsoft.com/en-us/library/windows/desktop/bb775947(v=vs.85).aspx)

[Win32SDK基本]Button Control(1)Push Button 和 BM_SETIMAGE_第1张图片

这一篇中就介绍一下Push Button

MSDN:

A push button is a rectangle containing an application-defined text label, an icon, or a bitmap that indicates what the buttohe user selects it.

A push button can be one of two styles, standard or default, as defined by the constants BS_PUSHBUTTON and BS_DEFPUSHBUTTON. A standard push button is typically used to start an operation. It receives the keyboard focus when the user clicks it. A default push button is typically used to indicate the most common or default choice, such as closing the dialog box. It is a button that the user can select by simply pressing ENTER when no other push button in the dialog box has the input focus.

When the user clicks a push button, it receives the keyboard focus. The system sends the button's parent window a WM_COMMAND message that contains the BN_CLICKED notification code.

The split button is a special kind of push button introduced in Windows Vista and Version 6.00. A split button is divided into two parts. The main part functions like a regular or default push button. The second part has an arrow pointing downward. Typically a menu is displayed when the arrow is clicked.

A split button has the BS_SPLITBUTTON style, or the BS_DEFSPLITBUTTON style if it is the default button in a dialog box. You can modify the appearance of the button by using the BCM_SETSPLITINFO message or the corresponding Button_SetSplitInfo macro.

When the user clicks on the main part of the split button, it sends a BN_CLICKED notification just like a normal push button. But when the user clicks on the down arrow, it sends a BCN_DROPDOWN notification. It is the application's responsibility to display a menu in response to BCN_DROPDOWN.

Windows Vista and Version 6.00 also introduced another kind of push button, the command link. Visually, a command link is very different from a normal push button, but it has the same functionality. A command link typically displays an arrow icon, a line of text, and additional text in a smaller font.


这里取其中的关键一部分bing翻译:

按钮 可以 两种 风格 标准 默认情况下 BS_PUSHBUTTON BS_DEFPUSHBUTTON 常量 定义 一个 标准 按钮 通常 用于 启动 操作 用户 单击 接收 键盘 焦点 默认 按钮 通常 用于 指示 常见 默认 选项 关闭 对话框 一个 按钮 用户 可以 选择 只需 enter 键 对话框 没有 其他 按钮 具有 输入 焦点
用户 单击 按钮 接收 键盘 焦点 系统 按钮的 窗口 发送 WM_COMMAND 消息 包含 发送的 BN_CLICKED 通知 代码


好了,无非是创建几个子窗口而已,相信各位在之前的两篇文中已经大致明白了,接下来,就是创建按钮了,这次创建两个Push Button,一个是默认按钮,另一个是普通按钮。

还是以我的博客“[Win32SDK基本] 窗口详解(超详细)”(地址:http://blog.csdn.net/zuishikonghuan/article/details/46378475)为模板,进一步编写。

先创建全局变量

HWND button1 = 0;
HWND button2 = 0;

在WM_CREATE消息里添加一下代码,即可创建Push Button

		button1 = CreateWindow(TEXT("Button"), TEXT("默认按钮"), WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON, 10, 60, 100, 30, hwnd, (HMENU)4, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
		SendMessage(button1, WM_SETFONT, (WPARAM)GetStockObject(17), 0);

接下来,我们创建一个标准按钮

		button2 = CreateWindow(TEXT("Button"), TEXT("标准按钮"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 10, 100, 100, 30, hwnd, (HMENU)5, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
		SendMessage(button2, WM_SETFONT, (WPARAM)GetStockObject(17), 0);

效果图:

[Win32SDK基本]Button Control(1)Push Button 和 BM_SETIMAGE_第2张图片
如何得到按钮的单机事件呢,当然是利用消息,和菜单非常类似

case WM_COMMAND:
		int id;
		int ent;
		ent = HIWORD(wParam);//通知吗
		id = LOWORD(wParam);//子窗口ID
		//判断菜单ID,注意不需要再判断HIWORD(wParam)了,因为菜单的通知码是一个定值(和控件不一样)
		switch (id)
		{
		case 3://这里使用在RC资源里面注册的菜单号
			MessageBox(hwnd, TEXT("菜单3被点击"), TEXT(""), 0);
			break;
		}
		//判断按钮的单机事件
		if (ent == BN_CLICKED){
			switch (id)
			{
			case 4://按钮的子窗口ID
				MessageBox(hwnd, TEXT("按钮被点击"), TEXT(""), 0);
				break;
			}
		}
		break;
[Win32SDK基本]Button Control(1)Push Button 和 BM_SETIMAGE_第3张图片

只是注意判断一下 HIWORD(wParam) 通知码就行了, BN_CLICKED 是按钮单机

创建带图片(位图)或图标的按钮

核心是使用 BM_SETIMAGE 消息

MSDN:https://msdn.microsoft.com/en-us/library/windows/desktop/bb761822(v=vs.85).aspx

wParam
图像与按钮相关联的类型。此参数可以是下列值之一:
IMAGE_BITMAP 位图
IMAGE_ICON 图标

lParam
位图或图标句柄

文本图标两者都有的按钮控件外观取决BS_ICONBS_BITMAP风格是否调用BM_SETIMAGE消息可能结果如下所示:
[Win32SDK基本]Button Control(1)Push Button 和 BM_SETIMAGE_第4张图片
下面,创建一个只有一个图标的按钮:
		HBITMAP oldimage;
		HICON hicon;

<pre class="cpp" name="code">		button3 = CreateWindow(TEXT("Button"), TEXT(""), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_ICON, 10, 140, 100, 50, hwnd, (HMENU)6, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
		hicon = LoadIcon((HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), TEXT("ICON_1"));
		oldimage = (HBITMAP)SendMessage(button3, BM_SETIMAGE, IMAGE_ICON, (LPARAM)hicon);
		if (oldimage != NULL)
		{
			DeleteObject(oldimage);
		}
		DeleteObject(hicon);

 
 
[Win32SDK基本]Button Control(1)Push Button 和 BM_SETIMAGE_第5张图片
再创建一个既有图标又有文字的按钮
<pre class="cpp" name="code">		hicon = LoadIcon((HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), TEXT("ICON_1"));
		button4 = CreateWindow(TEXT("Button"), TEXT("标准按钮"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON , 10, 220, 100, 30, hwnd, (HMENU)7, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
		SendMessage(button4, WM_SETFONT, (WPARAM)GetStockObject(17), 0);
		oldimage = (HBITMAP)SendMessage(button4, BM_SETIMAGE, IMAGE_ICON, (LPARAM)hicon);
		if (oldimage != NULL)
		{
			DeleteObject(oldimage);
		}
		DeleteObject(hicon);
 
 
注意,只有开启了系统风格才能看见文字,否则是看不见文字的,关于系统风格我会在后面详细说
[Win32SDK基本]Button Control(1)Push Button 和 BM_SETIMAGE_第6张图片


你可能感兴趣的:(Win32,windows,Desktop,sdk)