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 :

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: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 );
    }

    Reply
  • 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

    Reply
  • 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?

    Reply
  • AVI

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

    Originally posted by: NIVAS

    VERY GOOD

    Reply
  • 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.

    Reply
  • 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 ?
    

    Reply
  • 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.

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

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: November 13, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT APIs can be a great source of competitive advantage. The practice of exposing backend services as APIs has become pervasive, however their use varies widely across companies and industries. Some companies leverage APIs to create internal, operational and development efficiencies, while others use them to drive ancillary revenue channels. Many companies successfully support both public and private programs from the same API by varying levels …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds