/*------------------------------------------------------------------------------------
   Name: screenshot-post.mq4
   Copyright ©2010, Xaphod, http://www.forexfactory.com/xaphod
   
   Description: Take a screen shot on the first tick after a new bar.
                Post a tick to avoid waiting for a new tick.
                Requires ddl's enabled.
                Taking a screenshot on the new bar produces a bad screenshot.
	          
   Change log: 
       2011-08-08. Xaphod, v1.00 
-------------------------------------------------------------------------------------*/
// Indicator properties
#property copyright "Copyright © 2010, Xaphod"
#property link      "http://www.forexfactory.com/xaphod"

#property indicator_chart_window
//#include <xDebug.mqh>

#import "user32.dll"
   // Messaging
   int      PostMessageA(int hWnd,int Msg,int wParam,int lParam);
   int      RegisterWindowMessageA(string MessageName);
#import

// Constant definitions
#define INDICATOR_NAME "screenshot-post"
#define INDICATOR_VERSION "1.00"



//-----------------------------------------------------------------------------
// function: init()
// Description: Custom indicator initialization function.
//-----------------------------------------------------------------------------
int init() {
  return(0);
}

//-----------------------------------------------------------------------------
// function: deinit()
// Description: Custom indicator deinitialization function.
//-----------------------------------------------------------------------------
int deinit() {
   return (0);
}


///-----------------------------------------------------------------------------
// function: start()
// Description: Custom indicator iteration function.
//-----------------------------------------------------------------------------
int start() {
  static datetime tCutTime=0;
  static bool bScnShot=False;
  
  // Take screen shot if screen shot flag is set
  if (bScnShot) {
    WindowScreenShot(Symbol()+"_"+TF2Str(Period())+"_"+TimeCurrent()+".gif",800,600);
    bScnShot=False;
  }
  
  // On new bar set screen shot flag and post a tick
  if (tCutTime<Time[0]) {
    tCutTime=Time[0];
    bScnShot=True;
    PostTick();
  }

  return(0);
}
//+------------------------------------------------------------------+




//-----------------------------------------------------------------------------
// function: TF2Str()
// Description: Convert time-frame to a string
//-----------------------------------------------------------------------------
string TF2Str(int iPeriod) {
  switch(iPeriod) {
    case PERIOD_M1: return("M1");
    case PERIOD_M5: return("M5");
    case PERIOD_M15: return("M15");
    case PERIOD_M30: return("M30");
    case PERIOD_H1: return("H1");
    case PERIOD_H4: return("H4");
    case PERIOD_D1: return("D1");
    case PERIOD_W1: return("W1");
    case PERIOD_MN1: return("MN1");
    default: return("M"+iPeriod);
  }
}

//-----------------------------------------------------------------------------
// function: PostTick()
// Description: Post a tick to metetrader
//-----------------------------------------------------------------------------
void PostTick() {
  int hwnd = WindowHandle(Symbol(), Period());
  int msg = RegisterWindowMessageA("MetaTrader4_Internal_Message");
  PostMessageA(hwnd, msg, 2, 1);
  return(0);
}


