Two-State Graphic Button

The following figure is a sample definition for creating a two-state graphic button in a dialog resource.

CONTROL "", IDD_MP_PAUSE, 65, 10, 40, 30,
            WC_GRAPHICBUTTON,
            GBS_TWOSTATE | GBS_3D_TEXTRECESSED |
            WS_VISIBLE | WS_TABSTOP
            CTLDATA GB_RESOURCE, "PAUSE", 3, ID_MP_PAUS0, ID_MP_PAUS1,
                                             ID_MP_PAUS2, 0

The control data for the graphic button provides the text "PAUSE" (with no mnemonic) for the button face and associates the names of three bitmaps with the button. The bitmaps are assigned indexes 0, 1, and 2, according to the order they appear in the control data. In this case, the bitmaps are used to indicate the various states of the button: up, down, and highlighted.

Because the button does not have a GBS_AUTOTWOSTATE style, the owner window must send a GBM_SETSTATE message to the graphic button, requesting the button change its state.

A two-state graphic button can also be created by specifying the WC_GRAPHICBUTTON window class name as a parameter of the WinCreateWindow call. The following figure shows an example of setting up the GBTNCDATA structure with the graphic button data and using the WinCreateWindow call.

HWND        hwndTSB;    /* Two-state window handle             */
PGBTNCDATA  pgbtn;      /* Graphic button control data         */
LONG        lSize;      /* Size of graphic button control data */
# define NUMBITMAPS  2  /* Number of bitmaps for button */

lSize = sizeof(GBTNCDATA) + sizeof(USHORT) * (NUMBITMAPS - 1);
pgbtn = (PGBTNCDATA)malloc(lSize);

if (pgbtn)
{
  memset(pgbtn, 0, lSize);

  pgbtnpszText    = "Text";
  pgbtncBitmaps   = NUMBITMAPS;
  pgbtnaidBitmap[0] = BMP0;
  pgbtnaidBitmap[1] = BMP1;

  WinRegisterGraphicButton();

  /* Create the two-state graphic button.  hwnd is the window handle
   * of the owning window (for example, client window)
   */
  hwndTSB = WinCreateWindow (hwnd,
    WC_GRAPHICBUTTON,   /* No text here; see pgbtnpszText */
    "",
    WS_VISIBLE |, WS_TABSTOP | WS_POINTSELECT
    | GBS_AUTOTWOSTATE | GBS_3D_TEXTRECESSED,
    0,0,80,40,
    hwnd,
    HWND_TOP,
    ID_TSB,            /* Graphic button identifier */
    MPFROMP(pgbtn),
    NULL);

  WinSendMsg(hwndTSB, GBM_SETGRAPHICDATA, MPFROMP(pgbtn), 0);

  WinSendMsg(hwndTSB, GBM_SETBITMAPINDEX,
             MPFROMSHORT(GB_DOWN),           /* Which state  */
             MPFROMSHORT(GB_INDEX_LAST));    /* Which bitmap */
}


[Back: Animated Graphic Button]
[Next: Processing Messages for a CD Player Graphic Button]