So, i've been redesigning the program I've been making to make reusable functions, rather than copy/pasting almost identical code to several different functions (which is what I would have ended up doing).
These upcoming sections of code are functions designed to, on a button press, navigate to a URL, scrape the HTML to find a value. The value being the last page number of an element which stores messages, 10 per page, so 25 pages is equal to between 241-250 messages. Once the program knows how many pages there are, it will then proceed to 'tick' all the checkboxes on one page, delete them, and then repeat, for as many pages as there were when it began. To facilitate this I will be using a 'for' loop, although maybe in this case a 'while' loop may be better, input on this would be appreciated.
Here is the code, preceded by the name of the file each part is in. Please take note of the commented sections, I have included pertinent information along with the code. And note that some of this code is borrowed from an online tutorial that I have heavily modified, and don't fully understand, so please be nice.
SnipRobotDlg.h
// CSnipRobotDlg dialog
class CSnipRobotDlg : public CDialogEx
{
// Construction
public:
CSnipRobotDlg(CWnd* pParent = NULL); // standard constructor
void* GetElemFromID_Name(LPWSTR szID, IID nTypeIID);
int GetLastPageNumber(LPWSTR sUrl/*, something?*/);
// Rest of file.
SnipRobotDlg.cpp
// Returns an integer holding a value of the number of the last page.
int CSnipRobotDlg::GetLastPageNumber(LPWSTR sUrl/*, something?*/)
{
//----- Navigate to URL supplied in argument, check readystate of ActiveX control.
m_ctlBrowser.Navigate(sUrl, 0,0,0,0);
while (m_ctlBrowser.get_ReadyState() != READYSTATE_COMPLETE) {
DelayMs( 100 );
}
//----- Page is now ready to process, capture the HTML.
IHTMLDocument2* pDoc= (IHTMLDocument2*)m_ctlBrowser.get_Document();
IHTMLElement* pElemBody= NULL; // Why define as NULL @ decleration? In the next line it's re-defined...
pDoc->get_body(&pElemBody); // PElemBody is now holding the HTML.
//----- I'm really not sure why this is here, apart from making sBody = pElemBody, via scenic route.
CComBSTR bstrHTML;
pElemBody->get_innerHTML(&bstrHTML);
CString sBody= bstrHTML; // Easier to work with as a CString. Why is it easier to work with CString?
//MessageBox(sBody); // Debug, shows the string as a message.
//----- Zero-in on the data of interest.
int nOffset= sBody.Find(L"Snip");
CString sTmp= sBody.Mid(nOffset); // Remove the preceding HTML.
//MessageBox(sTmp); // Debug, shows the string as a message.
//----- Isolate last page number, save it to CString variable.
int nStart= sTmp.Find(L"Snip")+Snip;
int nEnd= sTmp.Find(L"Snip")-Snip;
CString sLastPageNumber= sTmp.Mid(nStart, nEnd-nStart);
//MessageBox(sLastPageNumber); // Debug, shows the string as a message.
//----- Convert string holding the value we need into an integer, for future mathematical/logical use.
int iConvertedLastPageNumber = _wtoi(sLastPageNumber.GetBuffer());
// I would really like a way of showing this int in/as a message, to prove it converted successfully.
//return(iConvertedLastPageNumber???);
}
void CSnipRobotDlg::OnBnClickedDeleteMessages()
{
int pWhatIsTheNamingConventionHere= GetLastPageNumber(L"http://Snip"/*, something?*/);
// Some more code to get the last page number here and to be useful.
int iFoo; //Ignore this line, imagine iFoo == iTheIntegerImGettingFrom_GetLastPageNumber_
for (int iTheIntegerImGettingFrom_GetLastPageNumber_ = 6; iFoo >= 0; iFoo--) // Should execute 6 times?
{
while (m_ctlBrowser.get_ReadyState() != READYSTATE_COMPLETE)
{
DelayMs(100);
}
// Mark the boxes, cannot mark 1-6 due to re-used ID's (I'll get you yet *shakefist*)
void* pElement= GetElemFromID_Name(L"n7", IID_IHTMLInputElement);
IHTMLInputElement* pCheckBoxSeven= (IHTMLInputElement*)pElement;
pCheckBoxSeven->put_checked(true);
pElement= GetElemFromID_Name(L"n8", IID_IHTMLInputElement);
IHTMLInputElement* pCheckBoxEight= (IHTMLInputElement*)pElement;
pCheckBoxEight->put_checked(true);
pElement= GetElemFromID_Name(L"n9", IID_IHTMLInputElement);
IHTMLInputElement* pCheckBoxNine= (IHTMLInputElement*)pElement;
pCheckBoxNine->put_checked(true);
pElement= GetElemFromID_Name(L"n10", IID_IHTMLInputElement);
IHTMLInputElement* pCheckBoxTen= (IHTMLInputElement*)pElement;
pCheckBoxTen->put_checked(true);
//------------------------------ now click the "Delete" button
pElement= GetElemFromID_Name(L"del", IID_IHTMLElement);
IHTMLElement* pDelete= (IHTMLElement*)pElement;
pDelete->click(); // GO!
} // It worked, once. It ticks all the boxes I've told it to (I'll get to ticking all 10 later).
}
The loop section of 'OnBnClickedDeleteMessages' will also probably end up as an autonomous and reusable fuction, leaving the only unique part of 'OnBnClickedDeleteMessages' the arguments it supplies to those functions. This seems like a logical design to me (a beginner).
pElement= GetElemFromID_Name(L"n10", IID_IHTMLInputElement);
IHTMLInputElement* pCheckBoxTen= (IHTMLInputElement*)pElement;
This is an example of getting a variable from another function, so I would have liked to be able to 'get it' myself but I can't understand whether or not I'm making a mistake with the arguments.