C++网页请求、窗口截取、日志运用、系统时间、C++转BYTE

1、请求网页信息

1.1、C++(MFC)

Cstring Get_Http_Date()
{
    CWinApp app((LPCTSTR)"123");
    app.InitApplication();
    AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0);
    CString m_URL = IP + "type=35&action=" + action + "&sn=" + sn + "&station=" + station +             "&uid=23428&pwd=123&nopwd=1&ToolName=AutoPacking&IP=172.20.1.53";
    CInternetSession session;
    CHttpFile *httpFile = (CHttpFile*)session.OpenURL(m_URL);
    DWORD dwStatusCode;
    httpFile->QueryInfoStatusCode(dwStatusCode);
    CString getdata = _T("");
    if (dwStatusCode == HTTP_STATUS_OK)
    {
	    CString line_data = _T("");
	    while (httpFile->ReadString(line_data))
	    {
	    	getdata += line_data;
	    }
	    getdata.TrimRight();
    }
    httpFile->Close();
    session.Close();
    return getdata;
}

1.2、 Qt获取网页信息

QString TestUi::Get_Mes_Data(string action,string code)
{	
	char url_c[512];
	memset(url_c, 0, 512);
	sprintf(url_c, "http://172.20.1.21/MD4API/api.ashx?type=35&action=%s&sn=%s&station=Cam_Tese&uid=23428&pwd=123&nopwd=1&ToolName=AutoPacking&IP=172.20.1.53", action.c_str(), SN.c_str());//设置IP地址


	QUrl url(url_c);
	//储存网页代码的文件    
	const QString FILE_NAME = "code.html";
	QEventLoop loop;
	m_Reply = m_NetManger->get(QNetworkRequest(url));
	//请求结束并下载完成后,退出子事件循环    
	QObject::connect(m_Reply, SIGNAL(finished()), &loop, SLOT(quit()));
	//开启子事件循环    
	loop.exec();

	//获取源码,打开文件
	QFile file(FILE_NAME);
	if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
	{
		qDebug() << "Cannot open the file: " << FILE_NAME;
		return "";
	}
	QTextStream out(&file);
	QString codeContent = m_Reply->readAll();//读取网页消息
	return codeContent;

}

2、截取窗口某个软件界面,并且获取软件界面坐标

#include 
#include 
#include 
#include
using namespace std;

void ShootScreen(const char* filename, HWND hWnd)
{
	HDC hdc = CreateDC("DISPLAY", NULL, NULL, NULL);
	int32_t ScrWidth = 0, ScrHeight = 0;
	RECT rect = { 0 };
	if (hWnd == NULL)
	{
		ScrWidth = GetDeviceCaps(hdc, HORZRES);
		ScrHeight = GetDeviceCaps(hdc, VERTRES);
	}
	else
	{
		GetWindowRect(hWnd, &rect);
		ScrWidth = rect.right - rect.left;
		ScrHeight = rect.bottom - rect.top;
	}
	HDC hmdc = CreateCompatibleDC(hdc);

	HBITMAP hBmpScreen = CreateCompatibleBitmap(hdc, ScrWidth, ScrHeight);
	HBITMAP holdbmp = (HBITMAP)SelectObject(hmdc, hBmpScreen);

	BITMAP bm;
	GetObject(hBmpScreen, sizeof(bm), &bm);

	BITMAPINFOHEADER bi = { 0 };
	bi.biSize = sizeof(BITMAPINFOHEADER);
	bi.biWidth = bm.bmWidth;
	bi.biHeight = bm.bmHeight;
	bi.biPlanes = bm.bmPlanes;
	bi.biBitCount = bm.bmBitsPixel;
	bi.biCompression = BI_RGB;
	bi.biSizeImage = bm.bmHeight * bm.bmWidthBytes;
	// 图片的像素数据
	char *buf = new char[bi.biSizeImage];
	BitBlt(hmdc, 0, 0, ScrWidth, ScrHeight, hdc, rect.left, rect.top, SRCCOPY);
	GetDIBits(hmdc, hBmpScreen, 0L, (DWORD)ScrHeight, buf, (LPBITMAPINFO)&bi, (DWORD)DIB_RGB_COLORS);

	BITMAPFILEHEADER bfh = { 0 };
	bfh.bfType = ((WORD)('M' << 8) | 'B');
	bfh.bfSize = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+bi.biSizeImage;
	bfh.bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
	HANDLE hFile = CreateFile(filename, GENERIC_WRITE, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
	DWORD dwWrite;
	WriteFile(hFile, &bfh, sizeof(BITMAPFILEHEADER), &dwWrite, NULL);
	WriteFile(hFile, &bi, sizeof(BITMAPINFOHEADER), &dwWrite, NULL);
	WriteFile(hFile, buf, bi.biSizeImage, &dwWrite, NULL);
	CloseHandle(hFile);
	hBmpScreen = (HBITMAP)SelectObject(hmdc, holdbmp);
}


int32_t main()
{
	HWND hwnd = FindWindow(0, "Kazam");
	RECT rect;
	GetWindowRect(hwnd, &rect);
	cout << "左上角坐标:" << rect.left << " " << rect.top << endl
		<< "右下角坐标" << rect.right << " " << rect.bottom << endl;
	//HWND hWnd = AfxGetMainWnd()->m_hWnd;
	char name[256] = { 0 };
	//for (int32_t i = 0; i < 20; ++i)
	//{
		sprintf_s(name, 256, "%d.bmp", 1);
		printf("shooting %s\n", name);
		ShootScreen(name, NULL);
		//Sleep(1000);  472  148    1482  787
	//}
	return 0;
}

3、设置鼠标窗口某个位置完成双击

POINT p;
GetCursorPos(&p);
SetCursorPos(x, y);//移动到指定位置,点击界面按钮
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);

4、日志模块的运用

ofstream m_LogStream;
//创建log文件
bool CPlcCommunication::CreateLogFile()
{
	
	char szDir[MAX_PATH];
	memset(szDir, 0, sizeof(szDir));
	//HMODULE hModule = AfxGetApp()->m_hInstance;  
	GetModuleFileName(NULL, szDir, MAX_PATH);//获取当前路径
	PathRemoveFileSpec(szDir);
	CString strPath = szDir;


	SYSTEMTIME SystemTime;

	GetLocalTime(&SystemTime);
	char cTime[256];
	memset(cTime, 0, sizeof(cTime));
	sprintf(cTime, "%04u_%02u_%02u_%02u_%02u_%02u_%03u",
		SystemTime.wYear, SystemTime.wMonth, SystemTime.wDay,
		SystemTime.wHour, SystemTime.wMinute, SystemTime.wSecond, SystemTime.wMilliseconds);

	CString strTimeData;
	strTimeData.Format("%04u_%02u_%02u", SystemTime.wYear, SystemTime.wMonth, SystemTime.wDay);
	CString strLog;


	strPath += "\\Log";
	if (!PathFileExists(strPath))
	{
		CreateDirectory(strPath, NULL);
	}
	strPath += "\\" + strTimeData;
	if (!PathFileExists(strPath))
	{
		CreateDirectory(strPath, NULL);
	}

	strLog.Format(_T("%s\\%s-PC%d.txt"), strPath, cTime);


	if (strLog.IsEmpty())
	{
		return false;

		m_LogStream.open(strLog.GetBuffer(0), std::ios::out);

		if (m_LogStream.is_open())
		{
			return true;
		}

		return false;
	}
}

//写日志
void CPlcCommunication::WriteLog (string cOneLineLog)
{
	EnterCriticalSection(&m_Log_Section);
	SYSTEMTIME SystemTime;

	GetLocalTime(&SystemTime);
	char cLog[256];
	memset(cLog, 0, sizeof(cLog));
	sprintf(cLog, "%02u:%02u:%02u:%03u	%s",
		SystemTime.wHour, SystemTime.wMinute, SystemTime.wSecond, SystemTime.wMilliseconds, cOneLineLog.c_str());

	if (m_LogStream.is_open())
	{
		m_LogStream << cLog << std::endl;
	}
	LeaveCriticalSection(&m_Log_Section);
}

void CPlcCommunication::CloseLogFile()
{
	m_LogStream.close();
}

5、系统时间

//将系统时间按格式显示
CTime currTime;
CString strTime;
currTime = CTime::GetCurrentTime();
strTime.Format(_T("%.4d-%.2d-%.2d-%.2d-%.2d-%.2d-%d.bmp"),
currTime.GetYear(), currTime.GetMonth(), currTime.GetDay(),
currTime.GetHour(), currTime.GetMinute(), currTime.GetSecond())

//获取耗时时间
DWORD start_time = GetTickCount();
DWORD end_time = GetTickCount();
//获取的时间值为毫秒  
int time=end_time-start_time

6、 将int类型数据拆分后放入到byte类型中

BYTE write_content[4];
int num = -1;
write_content[0] = (byte)(num & 0xff);
write_content[1] = (byte)(num >> 8 & 0xff);
write_content[2] = (byte)(num >> 16 & 0xff);
write_content[3] = (byte)(num >> 24 & 0xff);


//byte转int
 int bytesToInt(byte* bytes,int size = 4) 
{
    int addr = bytes[0] & 0xFF;
    addr |= ((bytes[1] << 8) & 0xFF00);
    addr |= ((bytes[2] << 16) & 0xFF0000);
    addr |= ((bytes[3] << 24) & 0xFF000000);
    return addr;
 }

 

你可能感兴趣的:(网络编程,C++)