BCX_BUTTON function

Purpose:

BCX_BUTTON creates a push button that posts a WM_COMMAND message to the owner window when the user selects the button.

Syntax:

hCtl = BCX_BUTTON(Text AS STRING, _
              hwndParent AS HWND, _
               hCtlID AS INTEGER, _
                 Xpos AS INTEGER, _
                 Ypos AS INTEGER, _
                Width AS INTEGER, _
               Height AS INTEGER  _
          [, WinStyle AS INTEGER] _
        [, ExWinStyle AS INTEGER])

Return Value:

  • Data type: HWND
    hCtl The handle of the push button, if the function succeeds. If the function fails, the return value is NULL.

Parameters:

  • Data type: STRING
    Text A label for the button control being created.
  • Data type: HWND
    hwndParent The handle of the parent window of the button being created.
  • Data type: INTEGER
    hCtlID Specifies the identifier of the button being created. The identifier is an integer value used by the button being created to notify its parent about events. The identifier must be unique for each button created with the same parent window.
  • Data type: INTEGER
    Xpos Specifies the initial horizontal position of the button being created. Xpos The x-coordinate of the upper-left corner of the button being created relative to the upper-left corner of the parent window's client area.
  • Data type: INTEGER
    Ypos Specifies the initial vertical position of the button being created. Ypos The initial y-coordinate of the upper-left corner of the button being created relative to the upper-left corner of the parent window's client area.
  • Data type: INTEGER
    Width Specifies the width, in device units or, if the PIXELS optional parameter was specified in the GUI statement, in pixels, of the button being created.
  • Data type: INTEGER
    Height Specifies the height, in device units or, if the PIXELS optional parameter was specified in the GUI statement, in pixels, of the button being created.
  • Data type: INTEGER
    WinStyle [OPTIONAL] If the WinStyle parameter is used, the default Window Style for a BCX_BUTTON control, WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP, is replaced with the value in WinStyle. For more information, visit the Microsoft Button Control Styles webpage and the Microsoft Window Styles webpage.
  • Data type: INTEGER
    ExWinStyle [OPTIONAL] The default Extended Window Style for a BCX_BUTTON control is WS_EX_CLIENTEDGE. For more information, visit the Microsoft Extended Window Styles webpage.

Remarks:

The default window Style for a BCX_BUTTON control also can be changed by using the MODSTYLE function.

Example:

This example demonstrates colorizing BCX_BUTTON controls and text.

GUI "Ownerdraw Button Demo"

DIM AS HWND Form1, hButton1, hButton2

SUB FORMLOAD
  Form1 = BCX_FORM("Ownerdraw Button Demo", 0, 0, 175, 100)
  hButton1 = BCX_BUTTON ("Ok",  Form1,  IDOK,      10, 30, 50, 20, WS_VISIBLE OR WS_CHILD OR WS_TABSTOP OR BS_OWNERDRAW)
  hButton2 = BCX_BUTTON ("Quit", Form1, IDCANCEL, 110, 30, 50, 20, WS_VISIBLE OR WS_CHILD OR WS_TABSTOP OR BS_OWNERDRAW)
  CENTER Form1
  SHOW Form1
END SUB


BEGIN EVENTS
  SELECT CASE CBMSG

  CASE WM_COMMAND
    SELECT CASE CBCTL
    CASE IDOK
      MSGBOX "Okay!"

    CASE IDCANCEL
      IF HIWORD(CBWPARAM) = BN_CLICKED OR HIWORD(CBWPARAM) = 1 THEN
        END
      END IF
    END SELECT

  CASE WM_DRAWITEM
    IF CBWPARAM = IDOK THEN
      DrawButton(hButton1, CBLPARAM, RGB(0, 192, 0), RGB(0, 0, 0))        ' black text on green background 
    END IF
    IF CBWPARAM = IDCANCEL THEN
      DrawButton(hButton2, CBLPARAM, RGB(255, 255, 124), RGB(255, 0, 0))  ' Red text on yellow background 
    END IF

  END SELECT
END EVENTS


SUB DrawButton(hButton AS HWND, lParam AS LPARAM, bgColor AS COLORREF, txtColor AS COLORREF)
  DIM hBrush AS HBRUSH
  DIM lpDis AS DRAWITEMSTRUCT PTR
  DIM zTxt AS STRING

  lpDis = CAST(DRAWITEMSTRUCT PTR, lParam)

  hBrush = CreateSolidBrush(bgColor)           ' Create background brush 
  SetBkColor(lpDis->hDC, bgColor)              ' Set background color for text 
  SetTextColor(lpDis->hDC, txtColor)           ' Set text color 
  GetWindowText(hButton, zTxt, BCXSTRSIZE)     ' Get button text 

  IF (lpDis->itemState AND ODS_SELECTED) THEN  ' Draw button frame 
    DrawFrameControl(lpDis->hDC, &lpDis->rcItem, DFC_BUTTON, DFCS_BUTTONPUSH OR DFCS_PUSHED)
    OffsetRect(&lpDis->rcItem, 1, 1)           ' Adjust rect for pressed state 
  ELSE
    DrawFrameControl(lpDis->hDC, &lpDis->rcItem, DFC_BUTTON, DFCS_BUTTONPUSH)
  END IF

  InflateRect(&lpDis->rcItem, -2, -2)          ' Prevents text from overlapping border 
  FillRect(lpDis->hDC, &lpDis->rcItem, hBrush) ' Fill button background 
  DrawText(lpDis->hDC, zTxt, -1, &lpDis->rcItem, DT_SINGLELINE OR DT_CENTER OR DT_VCENTER)

  IF (lpDis->itemState AND ODS_FOCUS) THEN                ' Draw focus rectangle if button has focus 
    SetTextColor(lpDis->hDC, GetSysColor(COLOR_BTNTEXT))  ' Reset text color 
    InflateRect(&lpDis->rcItem, -1, -1)                   ' Adjust focus rect slightly 
    DrawFocusRect(lpDis->hDC, &lpDis->rcItem)
  END IF

  DeleteObject(hBrush)  ' Clean up 
END SUB

For an example of the BCX_BUTTON function see Demo.bas.