{"id":1544,"date":"2008-02-18T14:16:36","date_gmt":"2008-02-18T14:16:36","guid":{"rendered":"http:\/\/www.amibroker.org\/userkb\/2008\/02\/18\/moving-low-level-graphics-gfx-objects-on-your-charts\/"},"modified":"2011-02-28T11:17:31","modified_gmt":"2011-02-28T11:17:31","slug":"moving-low-level-graphics-gfx-objects-on-your-charts","status":"publish","type":"post","link":"http:\/\/www.amibroker.org\/editable_userkb\/2008\/02\/18\/moving-low-level-graphics-gfx-objects-on-your-charts\/","title":{"rendered":"Moving Low Level Graphics (GFX) Objects on your Charts"},"content":{"rendered":"
When drawing gfx objects on your charts, you can move them around by applying an X and Y offset to the coordinates of the object. The following code shows a simple example of how you can move a square object around your chart.<\/p>\n
<\/span>function <\/span>DrawButton<\/span>( <\/span>Text<\/span>, <\/span>x1<\/span>, <\/span>y1<\/span>, <\/span>x2<\/span>, <\/span>y2<\/span>, <\/span>BackColor <\/span>)\r{\r <\/span>GfxSetOverlayMode<\/span>( <\/span>0 <\/span>);\r <\/span>GfxSelectFont<\/span>( <\/span>"Tahoma"<\/span>, <\/span>12<\/span>, <\/span>800 <\/span>);\r <\/span>GfxSelectPen<\/span>( <\/span>colorBlack <\/span>);\r <\/span>GfxSetBkMode<\/span>( <\/span>1 <\/span>);\r <\/span>GfxSelectSolidBrush<\/span>( <\/span>BackColor <\/span>);\r <\/span>GfxSetBkColor<\/span>( <\/span>BackColor <\/span>);\r <\/span>GfxSetTextColor<\/span>( <\/span>1 <\/span>);\r <\/span>GfxRectangle<\/span>( <\/span>x1<\/span>, <\/span>y1<\/span>, <\/span>x2<\/span>, <\/span>y2 <\/span>);\r <\/span>GfxDrawText<\/span>( <\/span>Text<\/span>, <\/span>x1<\/span>, <\/span>y1<\/span>, <\/span>x2<\/span>, <\/span>y2<\/span>, <\/span>32 <\/span>| <\/span>1 <\/span>| <\/span>4 <\/span>);\r}\r \r<\/span>Yoffset <\/span>= <\/span>Param<\/span>( <\/span>"Button Row Offset (px)"<\/span>, <\/span>50<\/span>, <\/span>0<\/span>, <\/span>2000<\/span>, <\/span>5 <\/span>);\r<\/span>Xoffset <\/span>= <\/span>Param<\/span>( <\/span>"Button Column Offset (px)"<\/span>, <\/span>50<\/span>, <\/span>0<\/span>, <\/span>2000<\/span>, <\/span>5 <\/span>);\r<\/span>CellHeight <\/span>= <\/span>Param<\/span>(<\/span>"Cell Height"<\/span>,<\/span>20<\/span>,<\/span>5<\/span>,<\/span>200<\/span>,<\/span>5<\/span>); \r<\/span>CellWidth <\/span>= <\/span>Param<\/span>(<\/span>"Cell Width"<\/span>,<\/span>120<\/span>,<\/span>5<\/span>,<\/span>200<\/span>,<\/span>5<\/span>); \r<\/span>DrawButton<\/span>( <\/span>"TEST"<\/span>, <\/span>Xoffset<\/span>, <\/span>yoffset<\/span>, <\/span>Xoffset <\/span>+ <\/span>CellWidth<\/span>, <\/span>yOffset <\/span>+ <\/span>CellHeight<\/span>, <\/span>colorBlue <\/span>);<\/span><\/pre>\nIf you only need to adjust the position of your object once in awhile, this simple method may serve best. However, when you want to move an object on the fly, without having to open the Param window each time, you can do this by registering the coordinates of your first click on the object and then move the object to the location of your next mouse click. The program below shows how to do this. It also shows you how to detect whether your click is within the object area (see the CursorInField variable). <\/p>\n
To facilitate readability, the code has not been optimized. You can see in the captures below the code only adds about 4 microseconds to your execution time. It is doubtful that code optimization will have much effect on the overall execution time of your code. To test this code you Apply it to an indicator. At First Apply, it will display the Yellow Square with the word TEST in it:<\/p>\n
<\/a><\/p>\nWhen you click within the yellow square with your Left mouse button, it will turn Red:<\/p>\n