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

  • The impact of a data loss event can be significant. Real-time data is essential to remaining competitive. Many companies can no longer afford to rely on a truck arriving each day to take backup tapes offsite. For most companies, a cloud backup and recovery solution will eliminate, or significantly reduce, IT resources related to the mundane task of backup and allow your resources to be redeployed to more strategic projects. The cloud - can now be comfortable for you – with 100% recovery from anywhere all …

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds