Click here to Skip to main content
13,189,324 members (68,138 online)
Click here to Skip to main content
Add your own
alternative version


42 bookmarked
Posted 10 May 2001

Simple Auto-Repeat (Fire Control) Button Control

, 10 May 2001
Rate this:
Please Sign up or sign in to vote.
Simple drop-in button control that allows type-o-matic repeating on a button
<!-- Download Links --> <!-- Article image -->

Sample Image - RepeatButton.jpg

<!-- Add the rest of your HTML here -->


I needed an ordinary button that would act like it was being constantly clicked as long as the mouse button was held down. Sounds pretty simple. You see this kind of thing in games and in XWindows projects I have seen widgets that did this everywhere. But I couldn't find one. Noooo!

I did find some neat code that did a "mouse repeat" which sounded like it would do the job, albeit less cleanly. This just repeated the Left Mouse Button Down message as long as the button was down. Cool. But button controls only send the BN_CLICKED message to the owning dialog when the mouse button goes UP (the BN_CLICKED message is the typical one that you handle and that VC++ supports in its resource editor)!

So, it looked like I was going to have to subclass the standard CButton control (subclassing is how one intercepts messages to a control and is a Windows thing, not a C++ thing). Windows subclassing is something I had never done. Fortunately there are some good subclassing articles at CodeProject.

What it does

The new button class, CRepeatButton, captures the LButtonDown, LButtonUp, and Timer messages.

  1. When the left mouse button goes down, a timer is set. The duration is the same as for keyboard auto-repeat. Also, the initial BN_CLICKED message is posted to the parent of the control.
  2. When the timer fires the first time, the timer is reset to the interval between clicks during auto-repeat. And another BN_CLICKED message is sent to the parent.
  3. Every time the timer goes off after this, the BN_CLICKED message is posted.

If the mouse leaves the control's rectangle or if the mouse button is lifted, the timer is cancelled and no further messages are posted to the parent.

So far, not too bad. However, since this button is acting slightly strangely, I felt it was important to give some kind of visual and audible clues. So when in auto-repeat mode (after the initial delay), the background color of the button changes to yellow. This involved the dreaded DrawItem function and turning on Owner Draw. But I grabbed this code from somebody else so it hardly hurt at all.

And each time the BN_CLICKED message is posted, a "click" wav image is played.

Since this may annoy some, I have included lots of comments for removing these features.

How I did it

Basically, I stole reused some code I found here at CodeProject:

How do I get it in my project?

There are probably better, cleaner, more graceful ways to do this, but this worked and was pretty simple.

First, I needed to let VC++ know about the new class, so I used the Class Wizard to create a new class based upon CButton called CRepeatButton. The Class Wizard creates RepeatButton.h and RepeatButton.cpp

Then I copied in my own RepeatButton.h and RepeatButton.cpp files.

I then rebuilt the project on the general principle that making too many changes too fast confuses VC++.

Back to the resource editor to add in the new buttons: Add an ordinary button to the dialog and change the resource id as needed. For the demo project included here, I called it IDC_BUTTON1. Astoundingly, this is the very name the resource editor assigned it!

Now use the Class Wizard to add a member variable to the dialog. The variable Category is Control (not that there is a lot of choice). And the variable type is CRepeatButton (not CButton).

That's it! The ordinary CButton has been hijacked and is now effectively a CRepeatButton.

I built this with VC++ 6.0 (MFC 4.2) but I don't think it is all that version-specific.

One little weirdness is that the WAV sound is not a file nor a resource. It is so small (10 milliseconds) that I couldn't bear the thought of the overhead of searching for it every time through the loop. So I converted the file to an array of characters and send the address to the sndPlaySound() function. But this will annoy some so I included comments to undo this...


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Harold Bamford
Software Developer (Senior) Thales Visionix
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

Generalgood Pin
assaids4-Jan-04 17:36
memberassaids4-Jan-04 17:36 
GeneralNice Pin
Brian Bull23-Oct-03 6:05
memberBrian Bull23-Oct-03 6:05 
GeneralSetFocus when you click Pin
walster21-Jul-03 11:16
susswalster21-Jul-03 11:16 
GeneralRe: SetFocus when you click Pin
Harold Bamford21-Jul-03 11:38
memberHarold Bamford21-Jul-03 11:38 
GeneralRepeat continued even after disabled Pin
Nimai2-Jun-03 15:14
memberNimai2-Jun-03 15:14 
Generalcan't stop the auto repeat Pin
kurin2-Apr-03 21:16
memberkurin2-Apr-03 21:16 
GeneralRe: can't stop the auto repeat Pin
Harold Bamford3-Apr-03 3:48
memberHarold Bamford3-Apr-03 3:48 
GeneralRe: can't stop the auto repeat Pin
Harold Bamford4-Apr-03 4:52
memberHarold Bamford4-Apr-03 4:52 
GeneralSpace bar support Pin
Dirk Luetjens18-Oct-02 15:04
sussDirk Luetjens18-Oct-02 15:04 
QuestionHow does it work for Toolbar buttons Pin
Anonymous20-Sep-02 2:39
sussAnonymous20-Sep-02 2:39 
AnswerRe: How does it work for Toolbar buttons Pin
Harold Bamford20-Sep-02 4:57
memberHarold Bamford20-Sep-02 4:57 
AnswerRe: How does it work for Toolbar buttons Pin
Rod Stone14-Nov-03 0:01
memberRod Stone14-Nov-03 0:01 
GeneralRe: How does it work for Toolbar buttons Pin
jaybonomad11-Dec-03 20:57
memberjaybonomad11-Dec-03 20:57 
Generalbug fix for stuck button Pin
Wally Blume18-Sep-02 11:35
sussWally Blume18-Sep-02 11:35 
GeneralRe: bug fix for stuck button Pin
Harold Bamford19-Sep-02 5:27
memberHarold Bamford19-Sep-02 5:27 
GeneralRe: bug fix for stuck button Pin
bartvandiepen3-May-05 3:24
memberbartvandiepen3-May-05 3:24 
GeneralAlmost perfect Pin
Robert Hoschek9-May-02 11:55
memberRobert Hoschek9-May-02 11:55 
GeneralRe: Almost perfect Pin
Harold Bamford9-May-02 12:39
memberHarold Bamford9-May-02 12:39 
Generaluseful Pin
lucy2-Jan-02 10:10
memberlucy2-Jan-02 10:10 
GeneralGood Pin
Jan Vidar Berger30-May-01 13:04
memberJan Vidar Berger30-May-01 13:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171016.2 | Last Updated 11 May 2001
Article Copyright 2001 by Harold Bamford
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid