A Push Button with auto-repeat

Download Source Code and Example

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


  • Button not getting a LButtonUp message

    Posted by Legacy on 05/08/2003 12:00am

    Originally 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 );

  • dipshit how about a download project

    Posted by Legacy on 03/26/2003 12:00am

    Originally posted by: sss

    dipshit how about a download project

  • Is the IDC_EDIT useful?

    Posted by Legacy on 12/21/2002 12:00am

    Originally posted by: Mike.Sever

    I find that the IDC_EDIT don't be used when creating the CMyEdit object.Why?

  • AVI

    Posted by Legacy on 04/06/2001 12:00am

    Originally posted by: NIVAS


  • Owner-drawn repeat-button

    Posted by Legacy on 12/21/2000 12:00am

    Originally 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.

  • How can I do the same for a toolbar button ?

    Posted by Legacy on 12/16/1999 12:00am

    Originally posted by: Oleg

    How can I make an autorepeat toolbar button ?

  • On Using Timers

    Posted by Legacy on 07/14/1999 12:00am

    Originally 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.
    The 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.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: February 11, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT New computing platforms, expanding information environments, recurrent security breaches and evolving regulatory frameworks are factors that security executives must contend with and address when developing their security strategy. In response to these dynamics, security executives are seeking stronger, more nimble and more pervasive security technologies to help protect business-critical information from unauthorized disclosure, loss or …

  • Live Event Date: February 25, 2015 @ 2:00 p.m. ET / 11:00 a.m. PT Secure Shell (SSH) keys provide unmitigated access for privileged users and applications. However, managing and securing these critical privileged credentials poses a real challenge for organizations, putting sensitive data at risk. In fact, more than 50% of organizations report experiencing an SSH Key related compromise. Check out this upcoming eSeminar and join Adam Bosnian, EVP of Global Business Development at CyberArk, as he discusses the …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date