Buttons are fundamental interactive widgets that respond to user clicks. Dear ImGui provides several button types for different use cases.
Create a clickable button that returns true when pressed:
IMGUI_API bool Button ( const char * label, const ImVec2 & size = ImVec2 ( 0 , 0 ));
The text displayed on the button
size
ImVec2
default: "ImVec2(0, 0)"
Button size in pixels. Use (0,0) for auto-sizing to fit the label.
Basic Button
Sized Buttons
static int clicked = 0 ;
if ( ImGui :: Button ( "Click Me" )) {
clicked ++ ;
}
if (clicked & 1 ) {
ImGui :: SameLine ();
ImGui :: Text ( "Thanks for clicking me!" );
}
A button with reduced padding, useful for embedding in text:
IMGUI_API bool SmallButton ( const char * label);
ImGui :: Text ( "Inline button:" );
ImGui :: SameLine ();
if ( ImGui :: SmallButton ( "small" )) {
// Button clicked
}
A button without visual decoration, useful for custom rendering:
IMGUI_API bool InvisibleButton ( const char * str_id, const ImVec2 & size, ImGuiButtonFlags flags = 0 );
String identifier (not displayed)
Optional button flags:
ImGuiButtonFlags_MouseButtonLeft (default)
ImGuiButtonFlags_MouseButtonRight
ImGuiButtonFlags_MouseButtonMiddle
if ( ImGui :: InvisibleButton ( "##invisible" , ImVec2 ( 100 , 100 ))) {
// Clicked
}
// Right-click button
if ( ImGui :: InvisibleButton ( "##context" , ImVec2 ( 50 , 50 ), ImGuiButtonFlags_MouseButtonRight)) {
ImGui :: OpenPopup ( "context_menu" );
}
A button displaying a directional arrow:
IMGUI_API bool ArrowButton ( const char * str_id, ImGuiDir dir);
Arrow direction:
ImGuiDir_Left
ImGuiDir_Right
ImGuiDir_Up
ImGuiDir_Down
static int counter = 0 ;
float spacing = ImGui :: GetStyle (). ItemInnerSpacing . x ;
if ( ImGui :: ArrowButton ( "##left" , ImGuiDir_Left)) { counter -- ; }
ImGui :: SameLine ( 0.0 f , spacing);
if ( ImGui :: ArrowButton ( "##right" , ImGuiDir_Right)) { counter ++ ; }
ImGui :: SameLine ();
ImGui :: Text ( " %d " , counter);
Make buttons fire continuously while held down:
static int counter = 0 ;
float spacing = ImGui :: GetStyle (). ItemInnerSpacing . x ;
ImGui :: PushItemFlag (ImGuiItemFlags_ButtonRepeat, true );
if ( ImGui :: ArrowButton ( "##left" , ImGuiDir_Left)) { counter -- ; }
ImGui :: SameLine ( 0.0 f , spacing);
if ( ImGui :: ArrowButton ( "##right" , ImGuiDir_Right)) { counter ++ ; }
ImGui :: PopItemFlag ();
ImGui :: SameLine ();
ImGui :: Text ( " %d " , counter);
With ImGuiItemFlags_ButtonRepeat enabled, the button returns true multiple times while held, not just on initial press.
Customize button colors using style modifiers:
// Single colored button
ImGui :: PushStyleColor (ImGuiCol_Button, ImVec4 ( 1.0 f , 0.0 f , 0.0 f , 1.0 f ));
ImGui :: PushStyleColor (ImGuiCol_ButtonHovered, ImVec4 ( 1.0 f , 0.2 f , 0.2 f , 1.0 f ));
ImGui :: PushStyleColor (ImGuiCol_ButtonActive, ImVec4 ( 0.8 f , 0.0 f , 0.0 f , 1.0 f ));
if ( ImGui :: Button ( "Red Button" )) {
// Clicked
}
ImGui :: PopStyleColor ( 3 );
// Multiple colored buttons with HSV
for ( int i = 0 ; i < 7 ; i ++ ) {
if (i > 0 ) ImGui :: SameLine ();
ImGui :: PushID (i);
ImGui :: PushStyleColor (ImGuiCol_Button, (ImVec4) ImColor :: HSV (i / 7.0 f , 0.6 f , 0.6 f ));
ImGui :: PushStyleColor (ImGuiCol_ButtonHovered, (ImVec4) ImColor :: HSV (i / 7.0 f , 0.7 f , 0.7 f ));
ImGui :: PushStyleColor (ImGuiCol_ButtonActive, (ImVec4) ImColor :: HSV (i / 7.0 f , 0.8 f , 0.8 f ));
ImGui :: Button ( "Click" );
ImGui :: PopStyleColor ( 3 );
ImGui :: PopID ();
}
Checkbox()
Toggle boolean values:
IMGUI_API bool Checkbox ( const char * label, bool * v);
IMGUI_API bool CheckboxFlags ( const char * label, int * flags, int flags_value);
IMGUI_API bool CheckboxFlags ( const char * label, unsigned int * flags, unsigned int flags_value);
Basic Checkbox
Checkbox Flags
static bool check = true ;
ImGui :: Checkbox ( "Enable Feature" , & check);
if (check) {
ImGui :: Text ( "Feature is enabled" );
}
Select one option from a group:
IMGUI_API bool RadioButton ( const char * label, bool active);
IMGUI_API bool RadioButton ( const char * label, int * v, int v_button);
Manual Mode
Automatic Mode
static int selected = 0 ;
if ( ImGui :: RadioButton ( "Option A" , selected == 0 )) selected = 0 ;
if ( ImGui :: RadioButton ( "Option B" , selected == 1 )) selected = 1 ;
if ( ImGui :: RadioButton ( "Option C" , selected == 2 )) selected = 2 ;
static int e = 0 ;
ImGui :: RadioButton ( "radio a" , & e, 0 ); ImGui :: SameLine ();
ImGui :: RadioButton ( "radio b" , & e, 1 ); ImGui :: SameLine ();
ImGui :: RadioButton ( "radio c" , & e, 2 );
Create a button with an image:
IMGUI_API bool ImageButton ( const char * str_id, ImTextureRef tex_ref, const ImVec2 & image_size,
const ImVec2 & uv0 = ImVec2 ( 0 , 0 ), const ImVec2 & uv1 = ImVec2 ( 1 , 1 ),
const ImVec4 & bg_col = ImVec4 ( 0 , 0 , 0 , 0 ),
const ImVec4 & tint_col = ImVec4 ( 1 , 1 , 1 , 1 ));
String identifier for the button
Texture reference (your texture ID cast to ImTextureRef)
Size of the image to display
uv0, uv1
ImVec2
default: "(0,0), (1,1)"
UV coordinates for the image texture
ImTextureRef my_texture = /* your texture */ ;
ImVec2 button_size = ImVec2 ( 32.0 f , 32.0 f );
for ( int i = 0 ; i < 8 ; i ++ ) {
ImGui :: PushID (i);
if (i > 0 ) ImGui :: SameLine ();
ImVec4 bg_col = ImVec4 ( 0.0 f , 0.0 f , 0.0 f , 1.0 f );
ImVec4 tint_col = ImVec4 ( 1.0 f , 1.0 f , 1.0 f , 1.0 f );
if ( ImGui :: ImageButton ( "" , my_texture, button_size,
ImVec2 ( 0 , 0 ), ImVec2 ( 1 , 1 ), bg_col, tint_col)) {
// Button clicked
}
ImGui :: PopID ();
}
Add tooltips to buttons for additional information:
ImGui :: Button ( "Hover Me" );
ImGui :: SetItemTooltip ( "This is a tooltip" );
// Or using BeginItemTooltip for complex tooltips
ImGui :: Button ( "Advanced" );
if ( ImGui :: BeginItemTooltip ()) {
ImGui :: Text ( "This is a more complex tooltip" );
ImGui :: BulletText ( "Feature 1" );
ImGui :: BulletText ( "Feature 2" );
ImGui :: EndTooltip ();
}
Complete Example
void ShowButtonDemo () {
ImGui :: Begin ( "Button Demo" );
// Standard button
static int click_count = 0 ;
if ( ImGui :: Button ( "Standard Button" )) {
click_count ++ ;
}
ImGui :: Text ( "Clicked %d times" , click_count);
ImGui :: Spacing ();
// Small button
ImGui :: Text ( "Inline:" ); ImGui :: SameLine ();
if ( ImGui :: SmallButton ( "small" )) {
// Clicked
}
ImGui :: Spacing ();
// Arrow buttons with repeat
static int counter = 0 ;
ImGui :: PushItemFlag (ImGuiItemFlags_ButtonRepeat, true );
if ( ImGui :: ArrowButton ( "##left" , ImGuiDir_Left)) counter -- ;
ImGui :: SameLine ();
if ( ImGui :: ArrowButton ( "##right" , ImGuiDir_Right)) counter ++ ;
ImGui :: PopItemFlag ();
ImGui :: SameLine ();
ImGui :: Text ( " %d " , counter);
ImGui :: Spacing ();
// Checkbox
static bool enabled = true ;
ImGui :: Checkbox ( "Enable Feature" , & enabled);
ImGui :: Spacing ();
// Radio buttons
static int selection = 0 ;
ImGui :: RadioButton ( "Option A" , & selection, 0 ); ImGui :: SameLine ();
ImGui :: RadioButton ( "Option B" , & selection, 1 ); ImGui :: SameLine ();
ImGui :: RadioButton ( "Option C" , & selection, 2 );
ImGui :: Spacing ();
// Colored button
ImGui :: PushStyleColor (ImGuiCol_Button, ImVec4 ( 0.8 f , 0.2 f , 0.2 f , 1.0 f ));
ImGui :: PushStyleColor (ImGuiCol_ButtonHovered, ImVec4 ( 0.9 f , 0.3 f , 0.3 f , 1.0 f ));
ImGui :: PushStyleColor (ImGuiCol_ButtonActive, ImVec4 ( 0.7 f , 0.1 f , 0.1 f , 1.0 f ));
if ( ImGui :: Button ( "Danger Button" )) {
// Perform dangerous action
}
ImGui :: PopStyleColor ( 3 );
ImGui :: End ();
}
Best Practices
Use PushID() / PopID() or the ## label syntax when you need multiple buttons with the same label.
Button size (0,0) means auto-size. Use (-FLT_MIN, 0) for full-width buttons.