Windows API BUTTON篇學習筆記

來源:互聯網
上載者:User
文章目錄
  • Return Value

下面是一些關於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 ReferenceButton 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 code Description
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? Result
Yes Yes Show icon only.
No Yes Show icon and text.
Yes No Show text only.
No No Show 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時以上訊息才有效

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.