//--- descrizione #property description "Lo script crea l'oggetto grafico \"Etichetta\"." //--- mostra la finestra dei parametri di input durante il lancio dello script #property script_show_inputs //--- parametri di input dello script input string InpName="Label"; // Nome Etichetta input int InpX=150; // distanza asse X input int InpY=150; // distanza asse Y input string InpFont="Arial"; // Font input int InpFontSize=14; // Grandezza del Font input color InpColor=clrRed; // Colore input double InpAngle=0.0; // Angolo di inclinazione in gradi input ENUM_ANCHOR_POINT InpAnchor=ANCHOR_CENTER; // Tipo di ancoraggio input bool InpBack=false; // Oggetto di sottofondo input bool InpSelection=true; // evidenza spostamento input bool InpHidden=true; // Nascosta nella lista oggetti input long InpZOrder=0; // Priorità per il click del mouse //+--------------------------------------------------------------------------------+ //| Crea un'etichetta di testo | //+--------------------------------------------------------------------------------+ bool LabelCreate(const long chart_ID=0, // ID del chart const string name="Label", // nome dell'etichetta const int sub_window=0, // indice sottofinestra const int x=0, // coordinate X const int y=0, // coordinate Y const ENUM_BASE_CORNER corner=CORNER_LEFT_UPPER, // angolo del chart per l'ancoraggio const string text="Label", // testo const string font="Arial", // font const int font_size=10, // grandezza font const color clr=clrRed, // colore const double angle=0.0, // pendenza testo const ENUM_ANCHOR_POINT anchor=ANCHOR_LEFT_UPPER, // tipo di ancoraggio const bool back=false, // in sottofondo const bool selection=false, // evidenzia moviemento const bool hidden=true, // nascosto nella lista oggetti const long z_order=0) // priorità per il click del mouse { //--- resetta il valore dell' errore ResetLastError(); //--- crea un'etichetta di testo if(!ObjectCreate(chart_ID,name,OBJ_LABEL,sub_window,0,0)) { Print(__FUNCTION__, ": fallimento nel creare un'etichetta di testo! Error code = ",GetLastError()); return(false); } //--- imposta le coordinate dell'etichetta ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x); ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y); //--- imposta l'angolo del chart, relativo a quali punti coordinate vengono definiti ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner); //--- imposta il testo ObjectSetString(chart_ID,name,OBJPROP_TEXT,text); //--- imposta il font ObjectSetString(chart_ID,name,OBJPROP_FONT,font); //--- imposta grandezza font ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size); //--- imposta l'angolo di inclinazione del testo ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle); //--- imposta il tipo di ancora ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor); //--- imposta il colore ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr); //--- mostra in primo piano (false) o sottofondo (true) ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back); //--- abilita (true) o disabilita (false) il modo di spostamento dell'etichetta con il mouse ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection); ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection); //--- nascondi (true) o mostra (falso) il nome di oggetto grafico nella lista degli oggetti ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden); //--- imposta la priorità per ricevere l'evento di un clic del mouse nel grafico ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order); //--- esecuzione avvenuta return(true); } //+--------------------------------------------------------------------------------+ //| Sposta l'etichetta di testo | //+--------------------------------------------------------------------------------+ bool LabelMove(const long chart_ID=0, // ID del chart const string name="Label", // nome dell'etichetta const int x=0, // coordinate X const int y=0) // coordinate Y { //--- resetta il valore dell' errore ResetLastError(); //--- sposta l'etichetta di testo if(!ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x)) { Print(__FUNCTION__, ": fallimento nello spostamento delle coordinate X dell'etichetta! Error code = ",GetLastError()); return(false); } if(!ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y)) { Print(__FUNCTION__, ": fallimento nello spostamento della coordinata Y dell'etichetta! Error code = ",GetLastError()); return(false); } //--- esecuzione avvenuta return(true); } //+--------------------------------------------------------------------------------+ //| Cambia angolo del chart per collegare l'etichetta | //+--------------------------------------------------------------------------------+ bool LabelChangeCorner(const long chart_ID=0, // ID del chart const string name="Label", // nome dell'etichetta const ENUM_BASE_CORNER corner=CORNER_LEFT_UPPER) // angolo del chart per l'ancoraggio { //--- resetta il valore dell' errore ResetLastError(); //--- cambia l'angolo di ancoraggio if(!ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner)) { Print(__FUNCTION__, ": fallimento nel cambiare l'angolo di ancoraggio! Error code = ",GetLastError()); return(false); } //--- esecuzione avvenuta return(true); } //+--------------------------------------------------------------------------------+ //| Cambia l'etichetta testo | //+--------------------------------------------------------------------------------+ bool LabelTextChange(const long chart_ID=0, // ID del chart const string name="Chart", // nome dell'oggetto const string text="Text") // testo { //--- resetta il valore dell' errore ResetLastError(); //--- cambia il testo dell'oggetto if(!ObjectSetString(chart_ID,name,OBJPROP_TEXT,text)) { Print(__FUNCTION__, ": fallimento nel cambiare il testo! Error code = ",GetLastError()); return(false); } //--- esecuzione avvenuta return(true); } //+--------------------------------------------------------------------------------+ //| Elimina l'etichetta di testo | //+--------------------------------------------------------------------------------+ bool LabelDelete(const long chart_ID=0, // ID del chart const string name="Label", // nome dell'etichetta { //--- resetta il valore dell' errore ResetLastError(); //--- elimina l'etichetta if(!ObjectDelete(chart_ID,name)) { Print(__FUNCTION__, ": fallimento nell'eliminare l'etichetta di testo! Error code = ",GetLastError()); return(false); } //--- esecuzione avvenuta return(true); } //+--------------------------------------------------------------------------------+ //| Funzione di avvio del programma Script | //+--------------------------------------------------------------------------------+ voidOnStart() { //--- memorizza le coordinate dell'etichetta nelle variabili locali int x=InpX; int y=InpY; //--- grandezza della finestra chart long x_distance; long y_distance; //--- imposta la grandezza della finestra if(!ChartGetInteger(0,CHART_WIDTH_IN_PIXELS,0,x_distance)) { Print("Fallimento nell'ottenere la grandezza del chart! Error code = ",GetLastError()); return; } if(!ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS,0,y_distance)) { Print("Fallimento nell'ottenere l'altezza del chart! Error code = ",GetLastError()); return; } //--- imposta la correttezza dei parametri di input if(InpX<0 || InpX>x_distance-1 || InpY<0 || InpY>y_distance-1) { Print("Error! Valori non corretti dei parametri di input!"); return; } //--- prepara il testo iniziale per l'etichetta string text; StringConcatenate(text,"Upper left corner: ",x,",",y); //--- crea un'etichetta di testo sul chart if(!LabelCreate(0,InpName,0,InpX,InpY,CORNER_LEFT_UPPER,text,InpFont,InpFontSize, InpColor,InpAngle,InpAnchor,InpBack,InpSelection,InpHidden,InpZOrder)) { return; } //--- redisegna il chart ed attende per mezzo secondo ChartRedraw(); Sleep(500); //--- sposta l'etichetta e modifica il testo, simultaneamente //--- numero di iterazioni per assi int h_steps=(int)(x_distance/2-InpX); int v_steps=(int)(y_distance/2-InpY); //--- sposta l'etichetta verso il basso for(int i=0;i<v_steps;i++) { //--- cambia le coordinate y+=2; //--- sposta l'etichetta e cambia il suo testo MoveAndTextChange(x,y,"Angolo sinistro superiore: "); } //--- ritardo di mezzo secondo Sleep(500); //--- sposta l'etichetta sulla destra for(int i=0;i<h_steps;i++) { //--- cambia le coordinate x+=2; //--- sposta l'etichetta e cambia il suo testo MoveAndTextChange(x,y,"Angolo sinistro superiore: "); } //--- ritardo di mezzo secondo Sleep(500); //--- sposta l'etichetta verso l'alto for(int i=0;i<v_steps;i++) { //--- cambia le coordinate y-=2; //--- sposta l'etichetta e cambia il suo testo MoveAndTextChange(x,y,"Angolo sinistro superiore: "); } //--- ritardo di mezzo secondo Sleep(500); //--- sposta l'etichetta a sinistra for(int i=0;i<h_steps;i++) { //--- cambia le coordinate x-=2; //--- sposta l'etichetta e cambia il suo testo MoveAndTextChange(x,y,"Angolo sinistro superiore: "); } //--- ritardo di mezzo secondo Sleep(500); //--- ora, sposta il punto cambiando l'angolo di ancoraggio //--- spostandolo verso l'angolo inferiore sinistro if(!LabelChangeCorner(0,InpName,CORNER_LEFT_LOWER)) return; //--- cambia il testo dell'etichetta StringConcatenate(text,"Lower left corner: ",x,",",y); if(!LabelTextChange(0,InpName,text)) return; //--- redraw the chart and wait for two seconds ChartRedraw(); Sleep(2000); //--- spostato all'angolo inferiore destro if(!LabelChangeCorner(0,InpName,CORNER_RIGHT_LOWER)) return; //--- cambia il testo dell'etichetta StringConcatenate(text,"Lower right corner: ",x,",",y); if(!LabelTextChange(0,InpName,text)) return; //--- redraw the chart and wait for two seconds ChartRedraw(); Sleep(2000); //--- spostato all'angolo superiore destro if(!LabelChangeCorner(0,InpName,CORNER_RIGHT_UPPER)) return; //--- cambia il testo dell'etichetta StringConcatenate(text,"Upper right corner: ",x,",",y); if(!LabelTextChange(0,InpName,text)) return; //--- redraw the chart and wait for two seconds ChartRedraw(); Sleep(2000); //--- spostandolo all'angolo superiore sinistro if(!LabelChangeCorner(0,InpName,CORNER_LEFT_UPPER)) return; //--- cambia il testo dell'etichetta StringConcatenate(text,"Upper left corner: ",x,",",y); if(!LabelTextChange(0,InpName,text)) return; //--- redraw the chart and wait for two seconds ChartRedraw(); Sleep(2000); //--- elimina l'etichetta LabelDelete(0,InpName); //--- redisegna il chart ed attende per mezzo secondo ChartRedraw(); Sleep(500); //--- } //+--------------------------------------------------------------------------------+ //| La funzione sposta l'oggetto e cambia il suo testo | //+--------------------------------------------------------------------------------+ bool MoveAndTextChange(const int x,const int y,string text) { //--- sposta l'etichetta if(!LabelMove(0,InpName,x,y)) return(false); //--- cambia il testo dell'etichetta StringConcatenate(text,text,x,",",y); if(!LabelTextChange(0,InpName,text)) return(false); //--- verifica se il funzionamento dello script è stato forzatamente disattivato if(IsStopped()) return(false); //--- redisegna il chart ChartRedraw(); // 0.01 secondi di ritardo Sleep(10); //--- esce dalla funzione return(true); } |