bool ImGui::BeginMenuBar()
Append to menu-bar of current window (requires ImGuiWindowFlags_MenuBar flag set on parent window).
True if menu bar is open. Only call EndMenuBar() if this returns true.
// Example
ImGui::Begin("Window", NULL, ImGuiWindowFlags_MenuBar);
if (ImGui::BeginMenuBar()) {
if (ImGui::BeginMenu("File")) {
if (ImGui::MenuItem("Open", "Ctrl+O")) { /* Do stuff */ }
if (ImGui::MenuItem("Save", "Ctrl+S")) { /* Do stuff */ }
ImGui::EndMenu();
}
ImGui::EndMenuBar();
}
ImGui::End();
Close menu bar. Only call EndMenuBar() if BeginMenuBar() returns true.
BeginMainMenuBar
bool ImGui::BeginMainMenuBar()
Create and append to a full screen menu-bar.
True if menu bar is open. Only call EndMainMenuBar() if this returns true.
// Example
if (ImGui::BeginMainMenuBar()) {
if (ImGui::BeginMenu("File")) {
if (ImGui::MenuItem("New", "Ctrl+N")) { /* Do stuff */ }
if (ImGui::MenuItem("Open", "Ctrl+O")) { /* Do stuff */ }
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Edit")) {
if (ImGui::MenuItem("Undo", "Ctrl+Z")) { /* Do stuff */ }
if (ImGui::MenuItem("Redo", "Ctrl+Y")) { /* Do stuff */ }
ImGui::EndMenu();
}
ImGui::EndMainMenuBar();
}
EndMainMenuBar
void ImGui::EndMainMenuBar()
Close main menu bar. Only call EndMainMenuBar() if BeginMainMenuBar() returns true.
bool ImGui::BeginMenu(const char* label, bool enabled = true)
Create a sub-menu entry. Only call EndMenu() if this returns true.
Whether the menu is enabled (grayed out if false)
// Example
if (ImGui::BeginMenu("File")) {
if (ImGui::MenuItem("New")) { /* Do stuff */ }
if (ImGui::MenuItem("Open")) { /* Do stuff */ }
if (ImGui::BeginMenu("Recent Files")) {
ImGui::MenuItem("file1.txt");
ImGui::MenuItem("file2.txt");
ImGui::EndMenu();
}
ImGui::EndMenu();
}
// Disabled menu
if (ImGui::BeginMenu("Disabled", false)) {
// This won't be reached
ImGui::EndMenu();
}
Close menu. Only call EndMenu() if BeginMenu() returns true.
bool ImGui::MenuItem(const char* label, const char* shortcut = NULL, bool selected = false, bool enabled = true)
Menu item. Returns true when activated.
shortcut
const char*
default:"NULL"
Keyboard shortcut text (display only, not processed by ImGui)
Whether the item is selected (displays checkmark)
Whether the item is enabled
// Example
if (ImGui::MenuItem("New", "Ctrl+N")) {
// New file
}
if (ImGui::MenuItem("Open", "Ctrl+O")) {
// Open file
}
ImGui::Separator();
if (ImGui::MenuItem("Quit", "Alt+F4")) {
// Quit application
}
// With checkmark
static bool show_grid = true;
if (ImGui::MenuItem("Show Grid", NULL, show_grid)) {
show_grid = !show_grid;
}
// Disabled
if (ImGui::MenuItem("Disabled Item", NULL, false, false)) {
// Won't be reached
}
bool ImGui::MenuItem(const char* label, const char* shortcut, bool* p_selected, bool enabled = true)
Menu item with pointer to selection state. Returns true when activated, and toggles *p_selected if p_selected != NULL.
Pointer to selection state (automatically toggled)
Whether the item is enabled
// Example
static bool show_wireframe = false;
static bool show_normals = false;
static bool show_grid = true;
if (ImGui::BeginMenu("View")) {
ImGui::MenuItem("Wireframe", NULL, &show_wireframe);
ImGui::MenuItem("Normals", NULL, &show_normals);
ImGui::MenuItem("Grid", NULL, &show_grid);
ImGui::EndMenu();
}
Complete Example
// Full menu system example
void ShowMenuExample() {
if (ImGui::BeginMainMenuBar()) {
if (ImGui::BeginMenu("File")) {
if (ImGui::MenuItem("New", "Ctrl+N")) {
// Create new file
}
if (ImGui::MenuItem("Open", "Ctrl+O")) {
// Open file
}
if (ImGui::BeginMenu("Open Recent")) {
ImGui::MenuItem("project1.txt");
ImGui::MenuItem("project2.txt");
ImGui::MenuItem("project3.txt");
ImGui::EndMenu();
}
ImGui::Separator();
if (ImGui::MenuItem("Save", "Ctrl+S")) {
// Save file
}
if (ImGui::MenuItem("Save As..", "Ctrl+Shift+S")) {
// Save as
}
ImGui::Separator();
if (ImGui::MenuItem("Quit", "Alt+F4")) {
// Quit application
}
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Edit")) {
if (ImGui::MenuItem("Undo", "Ctrl+Z")) {
// Undo
}
if (ImGui::MenuItem("Redo", "Ctrl+Y", false, false)) {
// Redo (disabled)
}
ImGui::Separator();
if (ImGui::MenuItem("Cut", "Ctrl+X")) {
// Cut
}
if (ImGui::MenuItem("Copy", "Ctrl+C")) {
// Copy
}
if (ImGui::MenuItem("Paste", "Ctrl+V")) {
// Paste
}
ImGui::EndMenu();
}
if (ImGui::BeginMenu("View")) {
static bool show_grid = true;
static bool show_wireframe = false;
static bool show_normals = false;
ImGui::MenuItem("Grid", NULL, &show_grid);
ImGui::MenuItem("Wireframe", NULL, &show_wireframe);
ImGui::MenuItem("Normals", NULL, &show_normals);
ImGui::Separator();
if (ImGui::BeginMenu("Camera")) {
if (ImGui::MenuItem("Perspective")) {
// Set perspective camera
}
if (ImGui::MenuItem("Orthographic")) {
// Set orthographic camera
}
ImGui::EndMenu();
}
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Help")) {
if (ImGui::MenuItem("Documentation")) {
// Open docs
}
if (ImGui::MenuItem("About")) {
// Show about dialog
}
ImGui::EndMenu();
}
ImGui::EndMainMenuBar();
}
}
Notes
- MenuItem keyboard shortcuts are displayed but NOT processed by Dear ImGui. You need to handle keyboard input yourself.
- You can use
## in labels to hide text after it (useful for creating unique IDs).
- Menus can be nested to any depth.
- Use
ImGui::Separator() to add horizontal lines between menu items.
- MenuItem returns true on activation, which happens on mouse release (not press).