A Menu Button | CodeGuru

A Menu Button

. Download Source In my latest project, File Axe, I decided I wanted something very like the little arrow buttons that one can find in DevStudio’s Tools|Customize… property sheet, Tools tab. The little button on the right of the edit control for “Arguments:” to be precise. So, after a quick browse on codeguru reveled nothing, […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 25, 1998
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

.

CMenuButton in action

Download Source


In my latest project, File Axe, I decided I wanted something very like the
little arrow buttons that one can find in DevStudio’s Tools|Customize…
property sheet, Tools tab. The little button on the right of the edit control
for “Arguments:” to be precise. So, after a quick browse on codeguru
reveled nothing, I decide it would not be hard to make, and I could wrap
Randy More’s cool way of using the TrackPopupMenu in immediate mode. I have a
feeling I will be using it more you see. (:


Uses code from…


The niffy way of using the menu in immediate mode is (directly) from

Using CMenu::TrackPopupMenu as an Immediate Function – Randy More

,


General owner draw format from

Flat owner drawn buttons – Girish Bharadwaj

,


and the idea for drawing the arrow from

PushButton with bitmap & text – Michael Santoro


UNICODE?


I have no way to test if this will work with UNICODE, however it

SHOULD

. (:



This was compiled with VC5 and MFC4.2. Tested on Win95 OSR2 (no IE4


integration). This code should compile cleanly under warning level 4.



To Use:


Simply include

MenuButton.h

and

MenuButton.cpp

in your


project, and include

MenuButton.h

in whatever file you wish to use it in


(

stdafx.h

mayhaps).





Then in the dialog editor, make a normal button, a suggested size is 12

×

14. The


little arrow gfx will be centered in the button in any case. Make a handler for


a button click, and copy the sample usage code to there, then of course modify to suit your needs.


Advertisement

A way to use CMenuButton… (aka the sample usage code)

// -[Feral]---------------------------------------------------------------
// pop up a menu, and add pre defined text to the edit ctrl.
void CPP_Split::OnBUTTONNameFormatOptions()
{
    // -[Feral]-----------------------------------------------------------
    // Vars and Assignment
    CEdit *pEdit = (CEdit*)GetDlgItem(IDC_EDIT_NameFormat);


    // -[Feral]-----------------------------------------------------------
    // popup a menu and let the user make a selection...
    DWORD dwSelectionMade = m_menubuttonNameFormatOptions.DoSingleClick(IDM_NameFormatOptions);

    // -[Feral]-----------------------------------------------------------
    // Do something with the selection
    // -------------------------------------------------------------------
    // The value of dwSelectionMade is the id of the command selected or 0 if no selection was made
    switch(dwSelectionMade)
    {
        default:                        // Do nothing (No selection)
            break;
        case `Menu Resource ID`:
            pEdit->ReplaceSel(_T("`Text to add at cursor, or replace selection with`") );
            break;
// Example case...
//Tip: Open the resource file (<apname>.rc) and copy your menu from there... much easier.
        case ID_PREDEFINEDPOPUP_SAMPLE1FILENAME1XEXTSPLITPIECE:       // "Sample #&1: Filename.1_X.Ext.SplitPiece",
            pEdit->ReplaceSel(_T("<File>.<1_X>.<Ext>.SplitPiece") );
            break;
    }

    // -[Feral]-----------------------------------------------------------
    // Clean Up and Return
}


Questions, comments, bug fixes (gasp! (: ) and whatever else comes to mind are always welcome!


Happy Coding!



CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.