Windows API BUTTON篇学习笔记

来源:互联网 发布:十一选五遗漏数据 编辑:程序博客网 时间:2024/04/28 11:04

下面是一些关于windows下的按钮控件的一些东东,希望对大家有用

 

 

常用的按钮有普通按钮、单选按钮、复选框,和组框,普通按钮作用是帮助用户触发指定动作;单选按钮一般各选项之间存在互斥性;复选框用来显示一组选项供用户选择,各选项之间不存在互斥;组框主要用于把控件分成不同的组并加以说明.

hwnd_check = CreateWindow(
            "BUTTON",   // predefined class
            "OK",       // button text
            WS_VISIBLE | WS_CHILD |BS_CHECKBOX  ,  //values for buttons.
            100,         // starting x position
            100,         // starting y position
            100,        // button width
            20,        // button height
            hwnd,       // parent window
            (HMENU)ID_CHECK,       // No menu
            (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE),
            NULL);

 

   下面代码可以实现BS_CHECKBOX    风格的    BUTTON  的选择和取消,在WM_COMMAND消息的LOWORD(wParam)中检查ID_CHECK即可

                 bool flag =SendMessage(hwnd_check,BM_GETCHECK,0,0) == BST_CHECKED ? false:true;
                    SendMessage(hwnd_check,BM_SETCHECK,flag,0);

算了后面发现了这篇文章:

Button Styles
  • BS_3STATE 与复选框一样本样式按钮可被单击变暗。变暗状态通常用于指示本样式的按键正处于禁用状态。

  • BS_AUTO3STATE   与三状态的复选框一样当用户选中它本按钮样式状态外观会改变。

  • BS_AUTOCHECKBOX   与复选框一样,除了在用户点控件后会出现一个选中标志,当用户在下一次点选时,该标志会消失。

  • BS_AUTORADIOBUTTON  与单先框一样,不同的是,用户点选它时会高亮显示,同时,会把同一组的其它同样的按钮的高亮状态转移到自己身上。

  • BS_BITMAP  指定按钮以一张位图显示。

  • BS_BOTTOM 把按钮标题放置到按钮矩形区域的底部。

  • BS_CENTER  按钮标题在按钮的矩形区域中央显示。

  • BS_CHECKBOX    在按钮的右边创建一个小方块(此样式必须与BS_LEFTTEXT结合使用。)

  • BS_DEFPUSHBUTTON   创建一个通用的默认按钮该按钮有一个厚重的黑色边框。用户可以通过按回车键来点选本按钮,该按钮可以实现用户通常要使用的功能(即默认执行动作功能)。

  • BS_FLAT  指定按钮为2D按钮,不采用3D控件所使用的阴影。

  • BS_GROUPBOX   创建一个分组框来给控件分组,如果使用了标题,则标题会出现在分组框的左上角位置。 

  • BS_ICON  指定按钮上显示一个图标。 

  • BS_LEFT  在控件的矩形区域内左对齐标题。如果按钮是一个没有BS_RIGHTBUTTON 样式的复选框或单选框 ,那么文本居将在复选框或单选框的右边居左对齐(这话有些多余,意思文本在复选框或单选框的那个可选被小方框或圆圈的右边。)。

  • BS_LEFTTEXT  当按钮是单选或是复选框时,标题文本将出现在单选或复选框的客户区(即复选框的矩形框,单选框的圆形框)的左边。 

  • BS_MULTILINE   如果标题文本太长,将在绘制区域内对文本进行折行处理。

  • BS_NOTIFY  激活按钮,使之可对父窗口发送BN_DBLCLK, BN_KILLFOCUS,  BN_SETFOCUS 消息,注意:不管有没有使用本样式,按钮都有一个 BN_CLICKED 可发送消息。

  • BS_OWNERDRAW   创建一个自绘风格的按钮。当按钮的外观发生改变时,框架会调用DrawItem成员函数。本样式在使用CBitmapButton类时必须设置。

  • BS_PUSHBUTTON   创建一个按钮(即最常见的按钮),该按钮在点击时,将向父窗口发送一个WM_COMMAND 消息。

  • BS_PUSHLIKE  把(多选框,三态多选框,单选框)以按钮的形式显示,该按钮在未选种状态时是浮起的,但在选中状态时是陷入状态的。 

  • BS_RADIOBUTTON  创建单选框,该按钮有一个圆形的客户区,(在本样式不与BS_LEFTTEXT   样式结合使用的情况下)标题文本在其右方。单选框通常用于有相关联的多个可选项里面,但相互之间只有作一个选择的情况下。 

  • BS_RIGHT  在按钮的绘制区域内右对齐文本。但如果按钮是一个没有BS_RIGHTBUTTON样式的单选或复选框,标题文本将在单选或复选框可点选区的右边居右对齐。 

  • BS_RIGHTBUTTON  设定单选框的圆形可选区或复选框的方开形复选区出现在按钮的矩形区域的右边。BS_LEFTTEXT 的效果一样。 

  • BS_TEXT   指定按钮将显示文本标题。

  • BS_TOP  将标题文本显示在按钮的绘制区域的顶边。

  • BS_USERBUTTON  已废弃不用,只作为兼容16位系统版本的Windows,基于32位windows系统的请用BS_OWNERDRAW样式取代。 

  • BS_VCENTER  设定按钮的标题在绘制区域的垂直方向居中。  

 

原文:

 

MFC Library Reference
Button Styles

 

  • BS_3STATE   Same as a check box, except that the box can be dimmed as well as checked. The dimmed state typically is used to show that a check box has been disabled.

  • BS_AUTO3STATE   Same as a three-state check box, except that the box changes its state when the user selects it.

  • BS_AUTOCHECKBOX   Same as a check box, except that a check mark appears in the check box when the user selects the box; the check mark disappears the next time the user selects the box.

  • BS_AUTORADIOBUTTON   Same as a radio button, except that when the user selects it, the button automatically highlights itself and removes the selection from any other radio buttons with the same style in the same group.

  • BS_BITMAP   Specifies that the button displays a bitmap.

  • BS_BOTTOM   Places text at the bottom of the button rectangle.

  • BS_CENTER   Centers text horizontally in the button rectangle.

  • BS_CHECKBOX   Creates a small square that has text displayed to its right (unless this style is combined with the BS_LEFTTEXT style).

  • BS_DEFPUSHBUTTON   Creates a button that has a heavy black border. The user can select this button by pressing the ENTER key. This style enables the user to quickly select the most likely option (the default option).

  • BS_FLAT   Specifies that the button is two-dimensional; it does not use the default shading to create a 3-D image.

  • BS_GROUPBOX   Creates a rectangle in which other buttons can be grouped. Any text associated with this style is displayed in the rectangle's upper-left corner.

  • BS_ICON   Specifies that the button displays an icon.

  • BS_LEFT   Left aligns the text in the button rectangle. However, if the button is a check box or radio button that does not have the BS_RIGHTBUTTON style, the text is left aligned on the right side of the check box or radio button.

  • BS_LEFTTEXT   When combined with a radio-button or check-box style, the text appears on the left side of the radio button or check box.

  • BS_MULTILINE   Wraps the button text to multiple lines if the text string is too long to fit on a single line in the button rectangle.

  • BS_NOTIFY   Enables a button to send BN_DBLCLK, BN_KILLFOCUS, and BN_SETFOCUS notification messages to its parent window. Note that buttons send the BN_CLICKED notification message regardless of whether it has this style.

  • BS_OWNERDRAW   Creates an owner-drawn button. The framework calls the DrawItem member function when a visual aspect of the button has changed. This style must be set when using the CBitmapButton class.

  • BS_PUSHBUTTON   Creates a pushbutton that posts a WM_COMMAND message to the owner window when the user selects the button.

  • BS_PUSHLIKE   Makes a button (such as a check box, three-state check box, or radio button) look and act like a push button. The button looks raised when it isn't pushed or checked, and sunken when it is pushed or checked.

  • BS_RADIOBUTTON   Creates a small circle that has text displayed to its right (unless this style is combined with the BS_LEFTTEXT style). Radio buttons are usually used in groups of related, but mutually exclusive, choices.

  • BS_RIGHT   Right aligns the text in the button rectangle. However, if the button is a check box or radio button that does not have the BS_RIGHTBUTTON style, the text is right aligned on the right side of the check box or radio button.

  • BS_RIGHTBUTTON   Positions a radio button's circle or a check box's square on the right side of the button rectangle. Same as the BS_LEFTTEXT style.

  • BS_TEXT   Specifies that the button displays text.

  • BS_TOP   Places text at the top of the button rectangle.

  • BS_USERBUTTON   Obsolete, but provided for compatibility with 16-bit versions of Windows. Win32-based applications should use BS_OWNERDRAW instead.

  • BS_VCENTER   Places text in the middle (vertically) of the button rectangle

 

 

用BM_GETCHECK 可以检测其是否被选中,如下

SendMessage(hwnd_check,BM_GETCHECK,0,0) 

BST_CHECKED

Button is checked.

BST_INDETERMINATE

Button is grayed, indicating an indeterminate state (applies only if the button has the BS_3STATE or BS_AUTO3STATE style).

BST_UNCHECKED

Button is cleared

 

但然也可以用BM_GETSTATE 实现,对应的宏为Button_GetState ,而且该消息返回的state更丰富,返回如下:

Return Value

The return value specifies the current state of the button. It is a combination of the following values.

Return codeDescription

BST_CHECKED

The button is checked.

BST_DROPDOWNPUSHED

Windows Vista. The button is in the drop-down state. Applies only if the button has the TBSTYLE_DROPDOWN style.

BST_FOCUS

The button has the keyboard focus.

BST_HOT

The button is hot; that is, the mouse is hovering over it.

BST_INDETERMINATE

The state of the button is indeterminate. Applies only if the button has the BS_3STATE or BS_AUTO3STATE style.

BST_PUSHED

The button is being shown in the pushed state.

BST_UNCHECKED

No special state. Equivalent to zero.

 

当鼠标点击是发生BN_CLICKED消息,LOWORD(wParam)为控件ID  ,HIWORD(wParam)为BN_CLICKED事件

但是我不知道为什么msdn上是这样写的:

This message is sent when the user taps the pen on the touch screen. The parent window of the button receives this message through the WM_COMMAND message. Unlike the other button messages, this message is intended for applications written for any version of Windows.


BN_CLICKED idButton = (int)LOWORD(wParam);
    hwndButton = (HWND) lParam;

我自己经过验证,写了如下代码验证了下,大家看看对不对,希望和大家讨论一下,也请高手指点指点,个人英语能力比较差

                case ID_CHECK:
                {
                    if (HIWORD(wParam)==BN_CLICKED )
                     {MessageBox(hwnd,"","",MB_OK);SendMessage(hwnd,WM_SETTEXT,0,(LPARAM)"****");}
                    bool flag =SendMessage(hwnd_check,BM_GETCHECK,0,0) == BST_CHECKED ? false:true;
                    SendMessage(hwnd_check,BM_SETCHECK,flag,0);
                }

当我点击ID_CHECK时会弹出MessageBox,并且窗口标题变为******,我想应该是msdn描述错误了

 

可能我写的逻辑有点乱,写这些不是为了什么,我只是一边学习一边做笔记,同时吧这些笔记贴出来和大家学习学习,希望

有看到的大牛可以指点一下我这位初学者。下面讨论另一方面吧,

 

 

其实按钮是可以改变样式的,比如可以用一个位图了代替难看的按钮,看看QQ上的一些图标按钮就知道了,当然我不知QQ那些图标是怎么做得那么

漂亮的,但是我想原理是一样的。

当创建的窗口样式有BS_BITMAP or BS_ICON时,可以发生消息 BM_SETIMAGE  给自己创建的按钮来实现位图的添加,我用的编译器是

MinGW + code::block , 我不知道怎么往里添加资源所以没有验证 ,过段时间用VC6试试吧 , 参见如下,

wParam

The type of image to associate with the button. This parameter can be one of the following values:

  • IMAGE_BITMAP
  • IMAGE_ICON
lParam

A handle (HICON or HBITMAP) to the image to associate with the button.

BS_ICON or BS_BITMAP Set?BM_SETIMAGE Called?ResultYesYesShow icon only.NoYesShow icon and text.YesNoShow text only.NoNoShow text only

BM_GETIMAGE 则返回该位图的句柄 msdn这样说的  The BM_GETIMAGE message retrieves a handle to the bitmap or icon associated with a button

 

 A button sends the BN_DISABLE, BN_PUSHED, BN_KILLFOCUS, BN_PAINT, BN_SETFOCUS, and BN_UNPUSHED notification codes

only if it has the BS_NOTIFY style

只有当BUTTON含有BS_NOTIFY时以上消息才有效

原创粉丝点击