A Push Button with auto-repeat

Download Source Code and

After answering a question on how to update an edit control inside a dialog from
an action performed in another dialog, I remembered that I had implemented an
owner-drawn seek button for a CD player application. As you held the button pressed,
it incremented a value until you released the button.

Here is a simplified version of this control: CSeekButton. It’s a simple button that,
when pressed, increments or decrements a value. The direction (positive or negative),
initial, step, min and max values are all configurable.

As the value is changed, CSeekButton sends a user-defined message to the window you
want. The ID of the button that sent the message, and the value are passed along
with the message.

To use it, download the Source Code, add both SeekButton.h and
SeekButton.cpp to your project, and include SeekButton.h where appropiate. Typically,
you should declare instances of CSeekButtons in your window class, then create the
button controls in your window’s OnInitDialog (or OnCreate):

backwardBut.Create( “<<", WS_CHILD | WS_VISIBLE, CRect( 10, 10, 50, 30 ), this, IDC_BACKWARD_BUTTON ); backwardBut.SetParameters( SomeCWnd.GetSafeHwnd(), 0, CSeekButton::BACKWARD, 1, 0, 10); forwardBut.Create( ">>”, WS_CHILD | WS_VISIBLE, CRect( 50, 10, 90, 30 ), this, IDC_FORWARD_BUTTON );
forwardBut.SetParameters( SomeCWnd.GetSafeHwnd(), 0, CSeekButton::FORWARD, 1, 0, 10);

As a first argument to SetParameters(), you can pass any valid window’s handle. If
this argument is NULL, it defaults to the parent of the button.

What you have to do next is to make the window you are sending the message to react
to that message. For example, say you derive class CMyEdit from CEdit. In that class
declaration, add the following function declaration:

afx_msg LONG OnSeekButtonValueChanged( UINT id, LONG value );

Then add a message map entry to the message map, and write the handler :

… blah blah blah
// add an entry for your message

// CMyEdit message handlers

LONG CMyEdit::OnSeekButtonValueChanged( UINT /* id */, LONG value )
static CString str;
str.Format(“%d”, value );
SetWindowText( str );

return 0L;

In the above example, I just derived CMyEdit from CEdit and made it listen to the
WM_SEEKBUTTONVALUECHANGED message defined in SeekButton.h. You can do the same
with any other type of window, but of course what the handler does will change.

As you can see, this button class is very simple indeed. It doesn’t check for valid
values and range, nor does it support floats, but I leave that to you!

Last updated: 12 July 1998

More by Author

Must Read