Class MT4Panel
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
//+------------------------------------------------------------------+ #property copyright "Copyright 2021, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property strict //+------------------------------------------------------------------+ bool CreateButton(string name) { ResetLastError(); if(!ObjectCreate(0, name, OBJ_BUTTON, 0, 0, 0)) { Print(__FUNCTION__, "! Error code = ", GetLastError()); return(false); } ObjectSetInteger(0, name, OBJPROP_XDISTANCE, 100); ObjectSetInteger(0, name, OBJPROP_YDISTANCE, 100); ObjectSetInteger(0, name, OBJPROP_XSIZE, 50); ObjectSetInteger(0, name, OBJPROP_YSIZE, 20); ObjectSetInteger(0, name, OBJPROP_CORNER, CORNER_LEFT_UPPER); ObjectSetString(0, name, OBJPROP_TEXT, "Button"); ObjectSetString(0, name, OBJPROP_FONT, "Arial"); ObjectSetInteger(0, name, OBJPROP_FONTSIZE, 10); ObjectSetInteger(0, name, OBJPROP_COLOR, clrBlack); ObjectSetInteger(0, name, OBJPROP_BGCOLOR, C'236,233,216'); ObjectSetInteger(0, name, OBJPROP_BORDER_COLOR, clrNONE); ObjectSetInteger(0, name, OBJPROP_BACK, false); ObjectSetInteger(0, name, OBJPROP_STATE, false); ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false); ObjectSetInteger(0, name, OBJPROP_SELECTED, false); ObjectSetInteger(0, name, OBJPROP_HIDDEN, true); ObjectSetInteger(0, name, OBJPROP_ZORDER, 0); return(true); } //+------------------------------------------------------------------+ bool CreateEdit(string name) { ResetLastError(); if(!ObjectCreate(0, name, OBJ_EDIT, 0, 0, 0)) { Print(__FUNCTION__, "! Error code = ", GetLastError()); return(false); } ObjectSetInteger(0, name, OBJPROP_XDISTANCE, 100); ObjectSetInteger(0, name, OBJPROP_YDISTANCE, 100); ObjectSetInteger(0, name, OBJPROP_XSIZE, 100); ObjectSetInteger(0, name, OBJPROP_YSIZE, 20); ObjectSetString(0, name, OBJPROP_TEXT, ""); ObjectSetString(0, name, OBJPROP_FONT, "Arial"); ObjectSetInteger(0, name, OBJPROP_FONTSIZE, 10); ObjectSetInteger(0, name, OBJPROP_ALIGN, ALIGN_CENTER); ObjectSetInteger(0, name, OBJPROP_READONLY, true); ObjectSetInteger(0, name, OBJPROP_CORNER, CORNER_LEFT_UPPER); ObjectSetInteger(0, name, OBJPROP_COLOR, clrBlack); ObjectSetInteger(0, name, OBJPROP_BGCOLOR, clrWhite); ObjectSetInteger(0, name, OBJPROP_BORDER_COLOR, clrDimGray); ObjectSetInteger(0, name, OBJPROP_BACK, false); ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false); ObjectSetInteger(0, name, OBJPROP_SELECTED, false); ObjectSetInteger(0, name, OBJPROP_HIDDEN, true); ObjectSetInteger(0, name, OBJPROP_ZORDER, 0); return(true); } //+------------------------------------------------------------------+ void ObjectSetXY(string name, int x, int y) { ObjectSet(name, OBJPROP_XDISTANCE, x); ObjectSet(name, OBJPROP_YDISTANCE, y); } //+------------------------------------------------------------------+ void ObjectSetSize(string name, int width, int height) { ObjectSet(name, OBJPROP_XSIZE, width); ObjectSet(name, OBJPROP_YSIZE, height); } //+------------------------------------------------------------------+ class MT4Panel { public: string itemsOld[]; string background, title, btnClose, btnMinus; struct Item { string name; int x0; int y0; }; Item items[]; int x, y, width, height, titHeight; bool MouseDown, MouseOnTitle; double x0Mouse, y0Mouse; MT4Panel() { this.x = 200; this.y = 100; this.width = 400; this.height = 100; this.titHeight = 20; this.MouseDown = false; this.MouseOnTitle = false; this.x0Mouse = 0; this.y0Mouse = 0; static int MyObject_ID = 0; this.background = "MT4Panel-background" + MyObject_ID; this.title = "MT4Panel-title" + MyObject_ID; this.btnClose = "MT4Panel-btnClose" + MyObject_ID; this.btnMinus = "MT4Panel-btnMinus" + MyObject_ID; MyObject_ID++; this.createModal(); ChartSetInteger(0, CHART_EVENT_MOUSE_MOVE, 1); } void createModal() { CreateEdit(this.background); ObjectSetInteger(0, this.background, OBJPROP_BGCOLOR, clrLavender); CreateEdit(this.title); ObjectSetInteger(0, this.title, OBJPROP_BGCOLOR, clrWhite); ObjectSetInteger(0, this.title, OBJPROP_FONTSIZE, 12); ObjectSetText(this.title, "Dashboard MT4-Panel"); CreateButton(this.btnClose); ObjectSetInteger(0, this.btnClose, OBJPROP_BGCOLOR, clrWhite); ObjectSetInteger(0, this.btnClose, OBJPROP_FONTSIZE, 12); ObjectSetText(this.btnClose, "x"); CreateButton(this.btnMinus); ObjectSetInteger(0, this.btnMinus, OBJPROP_BGCOLOR, clrWhite); ObjectSetInteger(0, this.btnMinus, OBJPROP_FONTSIZE, 12); ObjectSetText(this.btnMinus, "-"); this.addItem(this.background, 0, 0); this.addItem(this.title, 0, 0); this.addItem(this.btnClose, this.width - this.titHeight, 0); this.addItem(this.btnMinus, this.width - this.titHeight * 2, 0); this.drawSize(); } void drawSize() { ObjectSetSize(this.background, this.width, this.height); ObjectSetSize(this.title, this.width - this.titHeight * 2, this.titHeight); ObjectSetSize(this.btnClose, this.titHeight, this.titHeight); ObjectSetSize(this.btnMinus, this.titHeight, this.titHeight); } void drawPosition() { setItemPosition(this.btnClose, this.width - this.titHeight, 0); setItemPosition(this.btnMinus, this.width - this.titHeight * 2, 0); } void setSize(int width, int height) { this.width = width; this.height = height; drawSize(); drawPosition(); } void setTitleHeight(int titHeight) { this.titHeight = titHeight; drawSize(); drawPosition(); } void addItem(string name, int x0, int y0) { int s = ArraySize(this.items); ArrayResize(this.items, s + 1, 10); this.items[s].name = name; this.setItemPosition(s, x0, y0); } void setItemPosition(int i, int x0, int y0) { this.items[i].x0 = x0; this.items[i].y0 = y0; ObjectSetXY(this.items[i].name, this.x + x0, this.y + y0); } void setItemPosition(string name, int x0, int y0) { int s = ArraySize(this.items); int n = -1; for(int i = 0; i < s; i++) { if(this.items[i].name == name) { n = i; break; } } if(n > -1) setItemPosition(n, x0, y0); } void setPosition(int x, int y) { this.x = x; this.y = y; int s = ArraySize(this.items); for(int i = 0; i < s; i++) { string n = this.items[i].name; int xN = this.x + this.items[i].x0; int yN = this.y + this.items[i].y0; ObjectSetXY(n, xN, yN); } } void showOnTop() { bool state = ObjectGet(this.btnMinus, OBJPROP_STATE); int s = ArraySize(this.items); for(int i = 0; i < s; i++) { ObjectSetInteger(0, this.items[i].name, OBJPROP_TIMEFRAMES, EMPTY); if(!state) { ObjectSetInteger(0, this.items[i].name, OBJPROP_TIMEFRAMES, OBJ_ALL_PERIODS); } else if(state && 0 < i && i <= 3) { ObjectSetInteger(0, this.items[i].name, OBJPROP_TIMEFRAMES, OBJ_ALL_PERIODS); } } } void deletePanel() { int s = ArraySize(this.items); for(int i = 0; i < s; i++) { ObjectDelete(0, this.items[i].name); } } void onEventPanel(int id, long lparam, double dparam, string sparam) { if(id == CHARTEVENT_MOUSE_MOVE) { if(sparam == "1" && !this.MouseDown) { this.MouseDown = true; this.x0Mouse = lparam - this.x; this.y0Mouse = dparam - this.y; if(this.x0Mouse > 0 && this.x0Mouse < this.width && this.y0Mouse > 0 && this.y0Mouse < this.titHeight) { this.MouseOnTitle = true; this.showOnTop(); } } //action when hold down mouse and move if(sparam == "1" && this.MouseDown) { if(this.MouseOnTitle) { int xNew = lparam - this.x0Mouse; int yNew = dparam - this.y0Mouse; int xMin = 100 - this.width; int yMin = 0; int xMax = ChartGetInteger(0, CHART_WIDTH_IN_PIXELS, 0) - 100; int yMax = ChartGetInteger(0, CHART_HEIGHT_IN_PIXELS, 0) - this.titHeight; if(xNew < xMin) xNew = xMin; if(yNew < yMin) yNew = yMin; if(xNew > xMax) xNew = xMax; if(yNew > yMax) yNew = yMax; this.setPosition(xNew, yNew); ChartSetInteger(ChartID(), CHART_MOUSE_SCROLL, 0); } } //action when up mouse if(sparam == "0" && this.MouseDown) { this.MouseDown = false; this.MouseOnTitle = false; ChartSetInteger(ChartID(), CHART_MOUSE_SCROLL, 1); } } if(id == CHARTEVENT_OBJECT_CLICK) { if(sparam == this.btnMinus) { this.showOnTop(); } else if(sparam == this.btnClose) { this.deletePanel(); } } } }; //+------------------------------------------------------------------+ |