{"id":2616,"date":"2011-03-07T07:17:58","date_gmt":"2011-03-07T07:17:58","guid":{"rendered":"http:\/\/www.amibroker.org\/userkb\/?p=2616"},"modified":"2011-03-07T07:17:58","modified_gmt":"2011-03-07T07:17:58","slug":"equalize-x-range-for-all-windows","status":"publish","type":"post","link":"http:\/\/www.amibroker.org\/editable_userkb\/2011\/03\/07\/equalize-x-range-for-all-windows\/","title":{"rendered":"Equalize X-Range for all windows"},"content":{"rendered":"
This function was requested on the main list, and was solved with the help of several expert programmers from the list. Thanks guys!<\/p>\n
This function can be copied to an include file that is included in each program from which you might want to synchronize the datetime range for all visible windows. The function places a small [R] button at the right top of your chart. Clicking this button will set the datetime ranges of all windows equal to the one you click the button in. Note that this window has to be active for the button to work, i.e., if the window was not active (selected) it will require two clicks for the button to respond.<\/p>\n
<\/span>function <\/span>RangeAllWindows<\/span>()\r{\r <\/span>MX <\/span>= <\/span>GetCursorXPosition<\/span>( <\/span>1 <\/span>);\r <\/span>MY <\/span>= <\/span>GetCursorYPosition<\/span>( <\/span>1 <\/span>);\r <\/span>LeftClick <\/span>= <\/span>GetCursorMouseButtons<\/span>() == <\/span>9<\/span>;\r\r <\/span>\/\/ Place Ranging Button\r <\/span>ButtonSize<\/span>=<\/span>20<\/span>;\r <\/span>X2 <\/span>= <\/span>Status<\/span>( <\/span>"pxchartright" <\/span>)+<\/span>1<\/span>;\r <\/span>X1 <\/span>= <\/span>X2 <\/span>- <\/span>ButtonSize<\/span>;\r <\/span>Y1 <\/span>= <\/span>0<\/span>;\r <\/span>Y2 <\/span>= <\/span>Y1 <\/span>+ <\/span>ButtonSize<\/span>;\r <\/span>GfxSelectFont<\/span>( <\/span>"Tahoma"<\/span>, <\/span>ButtonSize <\/span>\/ <\/span>1.5<\/span>, <\/span>800 <\/span>);\r <\/span>GfxSelectPen<\/span>( <\/span>colorBlack <\/span>, <\/span>1 <\/span>);\r <\/span>GfxSelectSolidBrush<\/span>( <\/span>colorYellow <\/span>);\r <\/span>GfxRectangle<\/span>( <\/span>X1<\/span>, <\/span>Y1<\/span>, <\/span>X2<\/span>, <\/span>Y2 <\/span>);\r <\/span>GfxSetTextColor<\/span>( <\/span>colorBlack <\/span>);\r <\/span>GfxSetBkMode<\/span>( <\/span>1 <\/span>);\r <\/span>GfxDrawText<\/span>( <\/span>"R"<\/span>, <\/span>X1<\/span>, <\/span>Y1<\/span>, <\/span>X2<\/span>, <\/span>Y2<\/span>, <\/span>41 <\/span>);\r <\/span>OnButton <\/span>= <\/span>MX <\/span>>= <\/span>X1 <\/span>AND <\/span>MY <\/span>>= <\/span>Y1 <\/span>AND <\/span>MX <\/span><= <\/span>X2 <\/span>AND <\/span>MY <\/span><= <\/span>Y2<\/span>;\r\r if ( <\/span>OnButton <\/span>AND <\/span>LeftClick <\/span>)\r {\r <\/span>DT <\/span>= <\/span>DateTime<\/span>();\r <\/span>BI <\/span>= <\/span>BarIndex<\/span>();\r <\/span>FirstBarIndex <\/span>= <\/span>Status<\/span>( <\/span>"firstvisiblebarindex" <\/span>);\r <\/span>LastBarIndex <\/span>= <\/span>Status<\/span>( <\/span>"lastvisiblebarindex" <\/span>);\r <\/span>FirstDateTime <\/span>= <\/span>LastValue<\/span>( <\/span>ValueWhen<\/span>( <\/span>FirstBarIndex <\/span>== <\/span>BI<\/span>, <\/span>DT <\/span>) );\r <\/span>LastDateTime <\/span>= <\/span>LastValue<\/span>( <\/span>ValueWhen<\/span>( <\/span>LastBarIndex <\/span>== <\/span>BI<\/span>, <\/span>DT <\/span>) );\r <\/span>FirstDateTimestr <\/span>= <\/span>DateTimeToStr<\/span>( <\/span>FirstDateTime <\/span>);\r <\/span>LastDateTimestr <\/span>= <\/span>DateTimeToStr<\/span>( <\/span>LastDateTime <\/span>);\r <\/span>AB <\/span>= <\/span>CreateObject<\/span>( <\/span>"Broker.Application" <\/span>);\r <\/span>docs <\/span>= <\/span>AB<\/span>.<\/span>Documents<\/span>;\r <\/span>Qty <\/span>= <\/span>docs<\/span>.<\/span>Count<\/span>;\r\r for ( <\/span>i <\/span>= <\/span>0<\/span>; <\/span>i <\/span>< <\/span>Qty<\/span>; <\/span>i<\/span>++ ) <\/span>\/\/ Range all windows\r <\/span>{\r <\/span>doc <\/span>= <\/span>docs<\/span>.<\/span>Item<\/span>( <\/span>i <\/span>);\r <\/span>AW <\/span>= <\/span>doc<\/span>.<\/span>ActiveWindow<\/span>;\r <\/span>AW<\/span>.<\/span>Activate<\/span>();\r <\/span>AW<\/span>.<\/span>ZoomToRange<\/span>( <\/span>FirstDateTimestr<\/span>, <\/span>LastDateTimestr <\/span>);\r <\/span>\/\/ correct shift due to blank bars\r <\/span>WSHShell <\/span>= <\/span>CreateObject<\/span>( <\/span>"WScript.Shell" <\/span>);\r <\/span>WSHShell<\/span>.<\/span>AppActivate<\/span>( <\/span>"AmiBroker" <\/span>);\r <\/span>WSHShell<\/span>.<\/span>Sendkeys<\/span>( <\/span>