Welcome to Blog No 3 in this miniseries of 10 daily blogs in which I will exploit MT4GUIs functionalities.
I hope you find it both interesting as well as valuable!
Sincerely
Thommy Tikka
Does your family & friends use your computer now and then?
Afraid that they will start your EA without knowing how to handle it?
Don’t be, with this piece of code you are the only one allowed to start it!
// mt4gui-SimpleEALogin.mq4 // Created by Thommy Tikka (thommy_tikka@hotmail.com) // Version 0.1, 20130403 // Include library file with common functions for mt4gui #include <mt4gui.mqh> // Include library and import dll to be able to remove EA from chart automatically (advanced) #include <WinUser32.mqh> #import "user32.dll" int GetAncestor(int, int); #import // Declare global variables int hwnd = 0; int loginBtn, exitBtn; int moveUpBtn, moveRightBtn, moveDownBtn, moveLeftBtn; int loginHeader, loginPanel; int usernameTextField, passwordTextField; int gUIXPosition, gUIYPosition; int authenticationFail=0; // User credentials (fill in the ones you want to use) string username = "Administrator", password = "Password"; // MT4 function called during the module initialization //+-------------------------------------------------------------------------------------------+ int init() { hwnd = WindowHandle(Symbol(),Period()); guiRemoveAll(hwnd); // Measures chart width and height and sets default GUI X/Y-positions to center of the chart gUIXPosition = guiGetChartWidth(hwnd)/2-90; gUIYPosition = guiGetChartHeight(hwnd)/2-85; // Add interface with panel and buttons to chart function call BuildInterface(); return(0); } // MT4 function called during deinitialization of the module //+-------------------------------------------------------------------------------------------+ int deinit() { // Very important to cleanup and remove all gui items from chart if (hwnd>0) guiRemoveAll(hwnd); guiCleanup(hwnd); return(0); } // MT4 function called on every MT4 tick //+-------------------------------------------------------------------------------------------+ int start() { // Call function ManageEvents on every MT4 tick ManageEvents(); } // MT4GUI functions to capture Menu & Button Events //+-------------------------------------------------------------------------------------------+ void ManageEvents() { // If exitBtn is clicked execute function ExitEA() if (guiIsClicked(hwnd,exitBtn)) ExitEA(); // If loginBtn is clicked execute function LoginEA() if (guiIsClicked(hwnd,loginBtn)) LoginEA(); // If moveBtns are clicked change the default X/Y coordinates accordingly (Remove all objects and rebuild interface) if (guiIsClicked(hwnd,moveUpBtn)) {gUIYPosition = gUIYPosition-10; guiRemoveAll(hwnd); BuildInterface();} if (guiIsClicked(hwnd,moveRightBtn)) {gUIXPosition = gUIXPosition+10; guiRemoveAll(hwnd); BuildInterface();} if (guiIsClicked(hwnd,moveDownBtn)) {gUIYPosition = gUIYPosition+10; guiRemoveAll(hwnd); BuildInterface();} if (guiIsClicked(hwnd,moveLeftBtn)) {gUIXPosition = gUIXPosition-10; guiRemoveAll(hwnd); BuildInterface();} } // MT4GUI functions to build Interface with labels, buttons & textfields //+-------------------------------------------------------------------------------------------+ void BuildInterface() { // Build Login panel (labels) and set look and feel for it loginHeader = guiAdd(hwnd,"label",gUIXPosition,gUIYPosition,180,20,"Please login:"); loginPanel = guiAdd(hwnd,"label",gUIXPosition,gUIYPosition+20,180,150,""); guiSetBgColor(hwnd,loginHeader,DarkBlue); guiSetTextColor(hwnd,loginHeader,White); guiSetBgColor(hwnd,loginPanel,DarkSlateGray); // Create buttons, Login & exit and set look and feel for them loginBtn = guiAdd(hwnd,"button",gUIXPosition+100,gUIYPosition+110,70,40,""); guiSetBorderColor(hwnd,loginBtn,RoyalBlue); guiSetBgColor(hwnd,loginBtn, Blue); guiSetTextColor(hwnd,loginBtn,White); guiSetText(hwnd,loginBtn,"Login",25,"Arial Bold"); exitBtn = guiAdd(hwnd,"button",gUIXPosition+10,gUIYPosition+110,70,40,""); guiSetBorderColor(hwnd,exitBtn,OrangeRed); guiSetBgColor(hwnd,exitBtn,Red); guiSetTextColor(hwnd,exitBtn,Black); guiSetText(hwnd,exitBtn,"Exit",25,"Arial Bold"); // Create four arrow buttons, for move panel moveUpBtn = guiAdd(hwnd,"button",gUIXPosition+80,gUIYPosition-20,20,20,""); guiSetTextColor(hwnd,moveUpBtn,White); guiSetBorderColor(hwnd,moveUpBtn,Black); guiSetBgColor(hwnd,moveUpBtn,Black); guiSetText(hwnd,moveUpBtn,CharToStr(217),20,"Wingdings"); moveRightBtn = guiAdd(hwnd,"button",gUIXPosition+180,gUIYPosition+80,20,20,""); guiSetTextColor(hwnd,moveRightBtn,White); guiSetBorderColor(hwnd,moveRightBtn,Black); guiSetBgColor(hwnd,moveRightBtn,Black); guiSetText(hwnd,moveRightBtn,CharToStr(216),20,"Wingdings"); moveDownBtn = guiAdd(hwnd,"button",gUIXPosition+80,gUIYPosition+170,20,20,""); guiSetTextColor(hwnd,moveDownBtn,White); guiSetBorderColor(hwnd,moveDownBtn,Black); guiSetBgColor(hwnd,moveDownBtn,Black); guiSetText(hwnd,moveDownBtn,CharToStr(218),20,"Wingdings"); moveLeftBtn = guiAdd(hwnd,"button",gUIXPosition-20,gUIYPosition+80,20,20,""); guiSetTextColor(hwnd,moveLeftBtn,White); guiSetBorderColor(hwnd,moveLeftBtn,Black); guiSetBgColor(hwnd,moveLeftBtn,Black); guiSetText(hwnd,moveLeftBtn,CharToStr(215),20,"Wingdings"); //Add a username and password text field for manual input using user adjustable X/Y-position (size fixed to 150x20) usernameTextField = guiAdd(hwnd,"text",gUIXPosition+10,gUIYPosition+40,160,20,"Username"); passwordTextField = guiAdd(hwnd,"text",gUIXPosition+10,gUIYPosition+70,160,20,"Password"); guiSetTextColor(hwnd,usernameTextField,Gray); guiSetTextColor(hwnd,passwordTextField,Gray); } // Windows/MT4 function to exit EA (advanced) //+-------------------------------------------------------------------------------------------+ void ExitEA() { Alert ("This exits your EA"); int hWnd = WindowHandle(Symbol(), Period()); #define MT4_WMCMD_REMOVE_EXPERT 33050 /* Remove expert advisor from chart */ PostMessageA(hWnd, WM_COMMAND, MT4_WMCMD_REMOVE_EXPERT, 0); } // MT4GUI function check textfields (username & password), MT4 pop-up alerts //+-------------------------------------------------------------------------------------------+ void LoginEA() { //Check that Username and password are correct if (guiGetText(hwnd,usernameTextField)==username && guiGetText(hwnd,passwordTextField)==password) {Alert ("Authorization successful (call your own function for continuation of EA execution)");guiRemoveAll(hwnd);authenticationFail=0;} else if (authenticationFail==0) {Alert ("Authorization failed, please try again (you have 2 of 2 retries left)"); authenticationFail++;} else if (authenticationFail==1) {Alert ("Authorization failed, please try again (you have 1 of 2 retries left)"); authenticationFail++;} else if (authenticationFail==2) {Alert ("Authentication failed 3 times, this blocks your login"); guiEnable (hwnd, loginBtn,0);} }
Thats all folks!
Tomorrow I will show you how you can use MT4-Terminal/chart functions with MT4GUI.
You must be logged in to post a comment.