Build notes

Eight Things the MQL5 Controls Library Does Not Tell You About CAppDialog

Batdorj Byambadorj · · 5 min read

MQL5 ships a Controls library, and CAppDialog gives an Expert Advisor a real window on the chart: title bar, client area, buttons, lists, edit fields. It works. Its header files are readable, which turns out to matter, because several of these behaviours are visible only there.

Each of the eight below cost me a build while putting a panel on an alert EA.

1. The close button unloads your EA

CAppDialog draws an [X]. Pressing it calls Destroy() with REASON_PROGRAM, which calls ExpertRemove().

For most panels that is reasonable. For anything meant to keep working while the window is out of the way it is a trap: the trader closes a window and silently stops the thing they rely on. CreateButtonClose is virtual, so override it and create nothing. Minimise and maximise stay.

2. Nothing is remembered between runs

CAppDialog::Create never calls IniFileLoad. Whether the window was minimised, where it sat — none of it survives a restart; the window comes up in whatever state your code asks for. Worth knowing before you spend an afternoon asking why your saved state will not come back. It will not, because it was never read.

3. "Create minimized" leaves the restore rectangle empty

Pass the minimised flag to CDialog::Create and m_norm_rect is never filled in, so the first maximise restores the window to a rectangle nobody wrote.

So minimise after the layout is built, not as part of creating it, and latch the min/max button to match. Related: compute the minimised rectangle when it is pressed, not when the panel is built. Changing timeframe reinitialises the EA, and in that instant the chart reports its own height as zero.

4. Visibility belongs to the library, not to you

CWndClient::Show passes through to CWndContainer::Show, which turns every child back on. Track visibility with a flag of your own as well and one piece of state has two owners.

Mine went wrong in a way that took a while to read: the panel was minimised, a periodic refresh kept running, the refresh called Show(), and a Pause button disappeared. The library was right and my flag was wrong. Visibility is the library's business; the panel's business is what is enabled and what colour it is. While the window is minimised, the refresh does not run at all.

5. ObjectCreate on a name that already exists succeeds

The one I would most want to know in advance, and it is not really about the Controls library — it is about what the library is built on.

Call ObjectCreate with the name of an object already on the chart and it returns success. It creates nothing. Your new widget is bound to the old object, with the old object's properties.

What that looked like: a previous build had hidden a button by setting OBJPROP_TIMEFRAMES to no periods. The object stayed on the chart. The new build "created" it, inherited the hidden flag, and then reported through IsVisible() — which asks the library's own state, not the chart — that the button was visible. The library said yes. The chart drew nothing.

Two conclusions. Delete every object under your own prefix before building the panel. And ask the chart about an object's state, never the library.

6. Prefix plus tag plus index is not a unique name

A group of controls named as prefix + tag + index can collide with a fixed name elsewhere. Mine did exactly this: fxa_ui_set + Op + 2 produced fxa_ui_setOp2, which was already an operator button's name.

By rule 5 the create succeeded. The group member took over the existing object, moved it to another row, and a button vanished from the window. A separator between tag and index — tag + "_" + i — makes the whole class of bug unwritable.

7. Select(-1) does not clear a list, it breaks it

CListView::Select(CONTROLS_INVALID_INDEX) reads like "select nothing". It is not.

Select calls CheckView, which scrolls to bring the given index into view. Index -1 pushes the list down a row, draws a row that does not exist at the top, and — m_current now being -1 — paints that nonexistent row as selected. Clearing a selection is not an act of selecting; ItemsClear already invalidates m_current.

8. Text is cut at 63 characters

Chart object text stops at 63 characters: rows, titles, labels, buttons, all of it.

Measured, not guessed: an 80-character title came out cut at exactly 63 while ObjectSetString reported success. The label drew. The last two columns were simply absent.

So every line is designed to fit 63, and anything that does not is truncated deliberately with an ellipsis. Columns that cannot fit moved into a detail block underneath — also 63 per line, so four lines rather than one. One more here: a title must be the same kind of widget as its rows. OBJ_EDIT draws text inside its own box, OBJ_LABEL does not, and mixing them means the heading never lines up.

Three more, just outside the dialog

The Object List is not evidence. The library creates its widgets with OBJPROP_HIDDEN set, and that window does not show hidden objects. I had a panel visible on the chart while the terminal cheerfully reported "Objects on … (0)". Count your objects in code.

Un-pressing a button needs a redraw. Setting OBJPROP_STATE to false does not lift it. MT5 keeps drawing it pressed until the chart is redrawn, so every layer that reads button state has to call ChartRedraw after it acts, or the panel looks frozen.

Check the shape before you blame the colour. A fired-alert arrow was tried in three colours and none stood out. The reason was not colour: OBJ_ARROW_DOWN is a hollow glyph with the chart showing through it. A filled arrow is OBJ_ARROW with OBJPROP_ARROWCODE, a property only accepted on OBJ_ARROW, so the type has to change — which means deleting and recreating every existing arrow, by rule 5. And OBJPROP_WIDTH is a 1-to-5 scale, not pixels: at 2 the filled glyph was nearly three candles wide, with nothing in between.

The short version

Read Dialog.mqh before trusting a method name. Delete your own objects before creating them. Ask the chart, not the library. Keep every line under 63 characters. And when something does not show up, check its shape before changing its colour.

The EA these notes come from is free to download, and the panel is described in How to Use.

Batdorj Byambadorj

Batdorj Byambadorj has been trading forex for his own account since 2007, mostly gold. He is the sole developer of fx-alert and writes every article on this site himself.

About the author

← All articles