A Push Button with auto-repeat
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 :
BEGIN_MESSAGE_MAP(CMyEdit, CEdit)
//{{AFX_MSG_MAP(CMyEdit)
... blah blah blah
//}}AFX_MSG_MAP
// add an entry for your message
ON_MESSAGE( WM_SEEKBUTTONVALUECHANGED, OnSeekButtonValueChanged )
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyEdit message handlers
LONG CMyEdit::OnSeekButtonValueChanged( UINT /* id */, LONG value )
{
static CString str;
str.Format("%d", value );
SetWindowText( str );
RedrawWindow();
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

Comments
Button not getting a LButtonUp message
Posted by Legacy on 05/08/2003 12:00amOriginally posted by: Howard Ive
I've been using this code in a more complicated situation with multiple message loops. If another message loop is running on parallel then the button does not always get a WM_LBUTTONUP message. This causes the button to 'self repeat'. To ensure that it it released add an OnLButtonUp direct call to the button itself before the SendMessage.
void CSeekButton::OnLButtonDown( UINT nFlags, CPoint point )
//==========================================================================================
{
CButton::OnLButtonDown( nFlags, point );
=========== // ============
CButton::OnLButtonUp( nFlags, point );
SendMessage( WM_LBUTTONUP );
}
Replydipshit how about a download project
Posted by Legacy on 03/26/2003 12:00amOriginally posted by: sss
dipshit how about a download project
ReplyIs the IDC_EDIT useful?
Posted by Legacy on 12/21/2002 12:00amOriginally posted by: Mike.Sever
I find that the IDC_EDIT don't be used when creating the CMyEdit object.Why?
ReplyAVI
Posted by Legacy on 04/06/2001 12:00amOriginally posted by: NIVAS
VERY GOOD
ReplyOwner-drawn repeat-button
Posted by Legacy on 12/21/2000 12:00amOriginally posted by: Guil
Even though this autorepeat example is very good, I would have prefered it in its original form, as an owner-drawn push button. Thanks anyway.
ReplyHow can I do the same for a toolbar button ?
Posted by Legacy on 12/16/1999 12:00amOriginally posted by: Oleg
ReplyOn Using Timers
Posted by Legacy on 07/14/1999 12:00amOriginally posted by: Viktor
This implementation is simple but not the best. When clicking on a seek button I see unexpected effects repeating the action too soon. Sometimes the control cannot redraw itself before the next increment/decrement occur and it seems the associated value changes by several steps at a time.
ReplyThe better is to use timers: one for initial delay and another for multiple sequential increments. Similar mechanism is used in spin buttons, where a set of timeouts is being gived after construction. But for non-standard controls this behaviour should be implemented on the custom level.