Movable toggle button in View?

Posted on 1998-10-21
Last Modified: 2013-11-20
I have two view pane separate by splitter, In one of the view window which is CHostView : public CScrollView, I want to place Toggle buttons and possibly to drag each individual button vertically to different position. I also like to have a individual popup dialog popup when the button is right mouse clicked. And also the total number of buttons is variable. I appreciate your expertise advice. Thank You!
Question by:nicolet
  • 4
  • 3
LVL 10

Accepted Solution

RONSLOW earned 200 total points
ID: 1323637
Do you mean that you want the user to be able to drag the button?

You'd need to set the NM_NOTIFY flag for the button (so you get mouse up/down etc) then, on a mouse down event, call ::DragDetect to determine whether the user is clicking of dragging the button (DrawDetect returns TRUE if the button is being dragged).  If it is being dragged, then set a flag (or write your own message pump loop) and respond to mouse moves by changing the window position for your button.

Similarly, you should be able to respond to a OnContextMenu in your button class that would then call TrackPopupMenu to display the context menu.


Author Comment

ID: 1323638
Hi Ronslow,
Thank you for your answer, however I am not clear about how to create these toggle buttons in one of my splitter view area which is a CHostView : public CScrollView. By the way the number of buttons is various, it depends on application to decide how many buttons should it provide in the view area). I am using MFC developer studio, Since the view class is not a dialog, How do I create buttons in View? and how to attach the callback message such as NM_NOTIFY or OnContextMenu for each button in the application's View class?
Thank You!

LVL 10

Expert Comment

ID: 1323639
I'll see if I can whip up a short example for you ... get back to you soon.

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.


Author Comment

ID: 1323640
I have not received workable answer for this question yet. Can I post this again to get some more help?
LVL 10

Expert Comment

ID: 1323641
to create a button, add a member to your view

CButton m_myButton;

Setup a CRect with the position and size of the button

CRect myButtonRect;
myButtonRect.left = ... etc

In your OnInitialUpdate (or maybe OnCreate)

m_myButton.Create (

If you want several, then mane an array of CButton's and set the ID's to be sequential (or have an array of ID's)


Author Comment

ID: 1323642
In your first answer you mention that
>Similarly, you should be able to respond to a OnContextMenu in >your button class that would then call TrackPopupMenu to >display the context menu.

If I create a CMyButton() class, to create and postion a button. How do I setup the messages such as "OnContextMenu" or "NM_NOTIFY" for this button in MFC. In App Wizard, I supposed I need to get CMyButton to be added as a new class, but I am confused with what should I enter for "Based class" and "Dialog ID" field in Add new class dialog? Since CMyButton is not a wizard created dialog. I am lost in this MFC way.
Thank you very much for your help!

LVL 10

Expert Comment

ID: 1323643
Generate the entries for a button in a dialog.  Put the same code into the message map, class header etc for your view.  The Class Wizard doesn't do anything that you as a programmer cannot (or should not) do ... it just helps by writing some simple code for you.

The only catch is if you don't know how many buttons you will have.  If it is truely dynamic, then you will want to handle all the buttons you created with a single member function (say, OnAnyButtonClicked) and use the ID of the button to take the appropriate action.

MFC supports this with

ON_NOTIFY_RANGE( wNotifyCode, id, idLast, memberFxn )

if your case, wNotifyCode is BN_CLICKED, id and idLast are the range of id numbers for the buttons you create and memberFxn is the name of the handler, which looks like..

afx_msg void memberFxn( UINT id, NMHDR * pNotifyStruct, LRESULT * result );

where id is the id of the button the was clicked, pNotifyStruct you can probably ignore, and *result should probably be set to zero

see MFC technical article TN061: ON_NOTIFY and WM_NOTIFY Messages for more info

Regarding tooltips ... look at the MS KB article:

DOC: How to Display Tool Tips After Calling EnableToolTips
Last reviewed: July 31, 1997
Article ID: Q140595


Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
not able to insert into temp table 68 150
post4 challenge 28 83
unix example issues 18 49
Not needed 13 57
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now