guiInit
int guiInit(int hwnd, string short_name, int autostate_enabled = 1);
| Parameter | Description |
|---|---|
hwnd |
Chart window handle (e.g., WindowHandle(_Symbol, _Period)). |
short_name |
Program name for cache partitioning (e.g., MQL_PROGRAM_NAME). Empty shares one bucket per chart (legacy/tests). |
autostate_enabled |
Non-zero (default): save/restore on OnDeinit → OnInit. 0: disable both. |
Wrapper (recommended for most indicators):
int guiInit(int autostate_enabled = 1);
// Uses WindowHandle(_Symbol, _Period), MQL_PROGRAM_NAME, autostate on.
guiDeinit
void guiDeinit(int gui_id);
Destroys that session's chart objects and menu rows. No-op when gui_id <= 0 or unknown. When autostate is enabled, snapshots restorable objects before teardown.
guiHasRestoredState
bool guiHasRestoredState(int gui_id, int handle);
Returns true if a cached snapshot was applied when this object was created (re-attach after OnDeinit). Used internally by guiAddCheckbox / guiAddRadio to skip initial_checked / initial_selected presets when the user's last state was restored.
Recommended Skeleton
#include <guipro.mqh>
int gui_id;
//
int OnInit()
{
gui_id = guiInit();
if(gui_id <= 0)
return(INIT_FAILED);
// guiAdd*, guiAddMenu, ...
return(INIT_SUCCEEDED);
} // OnInit
//
void OnDeinit(const int reason)
{
guiDeinit(gui_id);
} // OnDeinit
Always call guiDeinit from OnDeinit. Do not rely on DLL unload for cleanup.
Minimal Indicator Example
One button, lifecycle, and click polling.
#property strict
#property indicator_chart_window
#include <guipro.mqh>
int gui_id;
int btn_handle;
//
int OnInit()
{
gui_id = guiInit();
btn_handle = guiAddButton(gui_id, 20, 20, 120, 28, "Click me");
return(INIT_SUCCEEDED);
} // OnInit
//
void OnDeinit(const int reason)
{
guiDeinit(gui_id);
} // OnDeinit
//
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
if(guiIsClicked(gui_id, btn_handle))
Print("Button clicked");
return(rates_total);
} // OnCalculate
Threading
Drive each gui_id from a single thread of control in normal indicator use (typically OnCalculate). Do not call the same session from multiple MQL threads or timers without your own ordering.