AutoClickerDlg.cpp

 // AutoClickerDlg.cpp : implementation file
//
// Copyright (C) 2007, 2007 Gordon W. Ahn (gordon.ahn@softahead.com)
// All rights reserved. May not be sold for profit.
//
//

#include "stdafx.h"
#include "AutoClicker.h"
#include "AutoClickerDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define WM_KEY_ESCAPE  (WM_USER+1)
#define WM_KEY_PAUSE   (WM_USER+2)
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
HHOOK hHook=NULL;
CString strProgName="Auto Clicker Ver 1.0";;
class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAutoClickerDlg dialog

CAutoClickerDlg::CAutoClickerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CAutoClickerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CAutoClickerDlg)
m_bAlwaysTop = FALSE;
m_nX = 0;
m_nY = 0;
m_nInterval = 0;
m_nClicks = 0;
m_nMinutes = 0;
m_nMouseBtn = -1;
m_nCount = 0;
m_nCurrentClicks = 0;
m_strCurrentSpan = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CAutoClickerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAutoClickerDlg)
DDX_Control(pDX, IDC_HOMEPAGE, m_Homepage);
DDX_Control(pDX, IDC_XY_LIST, m_XYList);
DDX_Check(pDX, IDC_WIN_ALWAYS_TOP_CHK, m_bAlwaysTop);
DDX_Text(pDX, IDC_X_EDT, m_nX);
DDX_Text(pDX, IDC_Y_EDT, m_nY);
DDX_Text(pDX, IDC_CLICK_INTERVAL_EDT, m_nInterval);
DDX_Text(pDX, IDC_STOP_AFTER_CLICKS_EDT, m_nClicks);
DDX_Text(pDX, IDC_STOP_AFTER_MINS_EDT, m_nMinutes);
DDX_Radio(pDX, IDC_MOUSE_BUTTON_RDO, m_nMouseBtn);
DDX_Text(pDX, IDC_COUNT_EDT, m_nCount);
DDX_Text(pDX, IDC_CURRENT_CLICKS_EDT, m_nCurrentClicks);
DDX_Text(pDX, IDC_CURRENT_MINS_EDT, m_strCurrentSpan);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAutoClickerDlg, CDialog)
//{{AFX_MSG_MAP(CAutoClickerDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CANCELMODE()
ON_BN_CLICKED(IDOK, OnStart)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_SAVE_BTN, OnFileSave)
ON_BN_CLICKED(IDC_LOAD_BTN, OnFileLoad)
ON_BN_CLICKED(IDC_WIN_ALWAYS_TOP_CHK, OnWinAlwaysTopChk)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_KEY_ESCAPE,OnKeyEscape)
ON_MESSAGE(WM_KEY_PAUSE,OnKeyPause)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAutoClickerDlg message handlers

LRESULT CALLBACK HookMouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
HWND hwnd;
LRESULT lResult = 0;
if(nCode == HC_ACTION)
    {
        if(((EVENTMSG*)lParam)->message == VK_ESCAPE)
{
hwnd= ::FindWindow(NULL, strProgName);
::SendMessage(hwnd,WM_KEY_ESCAPE,1,1);
return 1;
}
else if(((EVENTMSG*)lParam)->message == VK_PAUSE)
{
hwnd=::FindWindow(NULL, strProgName);
::SendMessage(hwnd,WM_KEY_PAUSE,1,1);
return 1;
}
    }

return CallNextHookEx( hHook, nCode, wParam, lParam);
}

BOOL CAutoClickerDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog.  The framework does this automatically
//  when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here

m_nMouseBtn = 0; //Left Button on Mouse.
m_nClicks = 0; //0 click
m_nInterval = 500; //500ms
m_bAlwaysTop = TRUE; //Always Top
m_nListIdx = 0;
m_nCount = 0;

SetWindowPos(&wndTopMost,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE); //Always Top...
SetWindowText(strProgName);
memset(&xyList,0,sizeof(&xyList));
m_bStart =FALSE;
m_bPause = FALSE;
DWORD dwStyle = CHyperLink::StyleUnderline |
CHyperLink::StyleUseHover  |
                    CHyperLink::StyleDownClick ;

m_Homepage.SetURL(_T("http://www.softahead.com/"));
m_Homepage.ModifyLinkStyle(0,dwStyle);

UpdateData(FALSE);

//Starting Hooking...
hHook = SetWindowsHookEx(13,(HOOKPROC)::HookMouseProc,GetModuleHandle(NULL), 0);

return TRUE;  // return TRUE  unless you set the focus to a control
}

void CAutoClickerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CAutoClickerDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CAutoClickerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

void CAutoClickerDlg::OnCancelMode()
{
CDialog::OnCancelMode();

// TODO: Add your message handler code here

}

BOOL CAutoClickerDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
CPoint point;
CString strList;
UpdateData(TRUE);
if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_SPACE)
{
GetCursorPos(&point);
m_nX = point.x;
m_nY = point.y;
strList.Format("X=%d, Y=%d",point.x,point.y);
m_XYList.AddString(strList);
xyList[m_nListIdx].x = point.x;
xyList[m_nListIdx].y = point.y;
m_nListIdx++;
m_nCount++;
UpdateData(FALSE);

return TRUE;
}
else if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_DELETE)
{
memset(&xyList,0,sizeof(&xyList));
m_nListIdx = 0;
m_nX = 0;
m_nY = 0;
m_nCount = 0;
m_XYList.ResetContent();
UpdateData(FALSE);

return TRUE;
}

return CDialog::PreTranslateMessage(pMsg);
}

void CAutoClickerDlg::OnStart()
{
// TODO: Add your control notification handler code here
if(m_bStart) return;

if(xyList[0].x<1 || xyList[0].y<1)
{
MessageBox("No Point.","Check List",MB_OK|MB_ICONERROR);
return;
}

UpdateData(TRUE);
m_bStart = TRUE;

if (m_bPause)
{
m_bPause = FALSE;
}
else  
{
m_nCurrentClicks = 0;
m_tStart = CTime::GetCurrentTime();
m_nCurPos = m_nListIdx;
}

//Move once.
SetCursorPos(xyList[m_nListIdx-m_nCurPos].x,xyList[m_nListIdx-m_nCurPos].y); //Move point
if(m_nMouseBtn==0)
{
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, GetMessageExtraInfo()); // Left Key Down
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, GetMessageExtraInfo()); // Left Key UP
}
if(m_nMouseBtn==1)
{
mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, GetMessageExtraInfo()); // Right Key Down
mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, GetMessageExtraInfo()); // Right Key Up
}
SetTimer(1,m_nInterval,NULL);

m_nCurrentClicks++;
UpdateData(FALSE);

GetDlgItem(IDOK)->EnableWindow(FALSE);
GetDlgItem(IDC_CLICK_INTERVAL_EDT)->EnableWindow(FALSE);
GetDlgItem(IDC_STOP_AFTER_CLICKS_EDT)->EnableWindow(FALSE);
GetDlgItem(IDC_STOP_AFTER_MINS_EDT)->EnableWindow(FALSE);
}

void CAutoClickerDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default

//Starting
m_tEnd = CTime::GetCurrentTime();
m_tsSpan = m_tEnd - m_tStart;

if(nIDEvent==1)
{
UpdateData(TRUE);
m_nCurPos--;
if((m_nListIdx-m_nCurPos)==m_nListIdx) m_nCurPos = m_nListIdx;

SetCursorPos(xyList[m_nListIdx-m_nCurPos].x,xyList[m_nListIdx-m_nCurPos].y); //Move Position
if(m_nMouseBtn==0)
{
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, GetMessageExtraInfo()); // Left Key Down
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, GetMessageExtraInfo()); // Left Key Up
}
if(m_nMouseBtn==1)
{
mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, GetMessageExtraInfo()); // Right Key Down
mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, GetMessageExtraInfo()); // Right Key Up
}
m_nCurrentClicks++;
m_strCurrentSpan = m_tsSpan.Format("%H:%M:%S");

if ( m_nClicks !=0 && m_nCurrentClicks > m_nClicks )
OnKeyEscape(0,0);
if ( m_nMinutes != 0 && m_tsSpan.GetMinutes() > (int) m_nMinutes )
OnKeyEscape(0,0);

UpdateData(FALSE);
}
else if(nIDEvent==2)
{
//Keeping clicking automatically
UpdateData(TRUE);
if(m_nMouseBtn==0)
{
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, GetMessageExtraInfo()); // Left Key Down
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, GetMessageExtraInfo()); // Left Key Up
}
if(m_nMouseBtn==1)
{
mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, GetMessageExtraInfo()); // Right Key Down
mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, GetMessageExtraInfo()); // Right Key Up
}
m_nCurrentClicks++;
m_strCurrentSpan = m_tsSpan.Format("%H:%M:%S");

if ( m_nClicks !=0 && m_nCurrentClicks > m_nClicks )
OnKeyEscape(0,0);
if ( m_nMinutes != 0 && m_tsSpan.GetMinutes() > (int) m_nMinutes )
OnKeyEscape(0,0);

UpdateData(FALSE);
}


CDialog::OnTimer(nIDEvent);
}

long CAutoClickerDlg::OnKeyEscape(WPARAM wParam, LPARAM lParam)
{
UpdateData(TRUE);
if(m_bStart)
{
m_bStart = FALSE;
KillTimer(1);
KillTimer(2);
GetDlgItem(IDOK)->EnableWindow(TRUE);
GetDlgItem(IDC_CLICK_INTERVAL_EDT)->EnableWindow(TRUE);
GetDlgItem(IDC_STOP_AFTER_CLICKS_EDT)->EnableWindow(TRUE);
GetDlgItem(IDC_STOP_AFTER_MINS_EDT)->EnableWindow(TRUE);
}
return 1;
}

long CAutoClickerDlg::OnKeyPause(WPARAM wParam, LPARAM lParam)
{
UpdateData(TRUE);
if(m_bStart)
{
m_bStart = FALSE;
KillTimer(1);
KillTimer(2);
MessageBox("Program has been paused. Click [Start] to restart");
GetDlgItem(IDOK)->EnableWindow(TRUE);
GetDlgItem(IDC_CLICK_INTERVAL_EDT)->EnableWindow(TRUE);
GetDlgItem(IDC_STOP_AFTER_CLICKS_EDT)->EnableWindow(TRUE);
GetDlgItem(IDC_STOP_AFTER_MINS_EDT)->EnableWindow(TRUE);
}

m_bPause = TRUE;
return 1;
}

void CAutoClickerDlg::OnFileSave()
{
// TODO: Add your control notification handler code here
CFile save;
CFileDialog pDlg(FALSE,"COL",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"XY File (*.XYF)|*.XYF|");
pDlg.m_ofn.lpstrTitle = "Save to File";

if (m_XYList.GetCount() == 0)
{
MessageBox("No List.","Failure",MB_OK|MB_ICONERROR);
return;
}

if(pDlg.DoModal()==IDOK)
{
if(save.Open(pDlg.GetPathName(),CFile::modeCreate|CFile::modeWrite|CFile::typeBinary))
{
save.Write(xyList,sizeof(xyList));
save.Close();
}
}
}

void CAutoClickerDlg::OnFileLoad()
{
// TODO: Add your control notification handler code here
CFile saved;
CString strList;
CFileDialog pDlg(TRUE,"COL",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"XY File (*.XYF)|*.XYF|");
pDlg.m_ofn.lpstrTitle = "Open file";

UpdateData(TRUE);

if(pDlg.DoModal()==IDOK)
{
if(saved.Open(pDlg.GetPathName(),CFile::modeRead|CFile::modeNoTruncate|CFile::typeBinary))
{
//Initialize
memset(xyList,0,sizeof(xyList));
saved.Read(xyList,sizeof(xyList));
saved.Close();

m_XYList.ResetContent();
m_nX = 0;
m_nY = 0;
m_nCount = 0;
m_nListIdx = 0;

for(int i=0;i<200;i++)
{
if(xyList[i].x>0 && xyList[i].y>0)
{
m_nCount++;
m_nListIdx++;
strList.Format("X=%d Y=%d",xyList[i].x,xyList[i].y);
m_XYList.AddString(strList);
}
}

}
else MessageBox("Not found.","Failure",MB_OK|MB_ICONERROR);
}

UpdateData(FALSE);
}

void CAutoClickerDlg::OnWinAlwaysTopChk()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
if(m_bAlwaysTop==TRUE)
SetWindowPos(&wndTopMost,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
else
SetWindowPos(&wndNoTopMost,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
}

Project Homepage: