天天看点

C++ MFC常用API

######判断编辑框字符串是否为空

AfxMessageBox("查找的字符串为空!");
           

######SHOW & HIDE

GetDlgItem(IDC_EDIT1)->ShowWindow(SW_HIDE);
           

######设置程序图标

m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
           

移动控件/窗口

//窗口
MoveWindow(0,0,640,480);
CenterWindow();	
//移动控件
GetDlgItem(IDOK)->MoveWindow(640-60,0,55,18,TRUE);
           

######获取文件路径

char	appdir[256];
GetCurrentDirectory(256,appdir);
	dir=appdir;
	if(dir.Right(8)=="运行程序")
			dir="图片/";
	else	
		//上一个文件夹
		dir="../运行程序/图片/";
           

定时器

//申明
BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
	ON_WM_TIMER()
END_MESSAGE_MAP()
afx_msg void OnTimer(UINT nIDEvent);
//调用 会被无限调用
void CMyDlg::OnTimer(UINT nIDEvent)
{	CClientDC dc(this);
	if(getpic("人",p)==FALSE)
			{AfxMessageBox(cc+"没找到!"); 
	//选择随机图片
	SelectObject(MemDC,bit);
	//画出来
	BitBlt(dc.m_hDC,200,160,w,h,MemDC,0,0,SRCCOPY); 
	p++;
	if(p>m1) p=m0;
	//nIDEvent 是个唯一标示
	CDialog::OnTimer(nIDEvent);
}
//调用
SetTimer(1,150,NULL);//1是标志,150是毫秒
           

图片透明

CClientDC dc(this);
int x = 200, y = 200;
if (getpic("人", p) == FALSE)
{
	AfxMessageBox(cc + "没找到!");return;
}
SelectObject(MemDC, bit);
TransparentBlt2(dc.m_hDC, x, y, w, h, MemDC, 0, 0, w, h, RGB(255, 255, 255));


void TransparentBlt2( HDC hdc0,	
					int nX0,int nY0,
					int nW0,int nH0,
					HDC hdc1,
					int nX1,int nY1,
					int nW1,int nH1,
					UINT Tcol
					)
{	HBITMAP hBMP   =CreateCompatibleBitmap(hdc0,nW0, nH0);
	HBITMAP mBMP   =CreateBitmap(nW0,nH0,1,1,NULL);	
	HDC		hDC    =CreateCompatibleDC(hdc0);
	HDC		mDC    =CreateCompatibleDC(hdc0);
	HBITMAP oldBMP =(HBITMAP)SelectObject(hDC, hBMP);
	HBITMAP oldmBMP=(HBITMAP)SelectObject(mDC, mBMP);
	if (nW0==nW1&&nH0==nH1)
		BitBlt(hDC,0,0,nW0,nH0,hdc1,nX1,nY1,SRCCOPY);
	else
		StretchBlt(hDC,0,0,nW0,nH0,hdc1,nX1,nY1,nW1,nH1,SRCCOPY);

	//SetBkColor(hDC, Tcol);
	BitBlt(mDC,0,0,nW0,nH0,hDC,0,0,SRCCOPY);
	SetBkColor(hDC, RGB(0,0,0));

	SetTextColor(hDC, RGB(255,255,255));
	BitBlt(hDC,0,0,nW0,nH0,mDC,0,0,SRCAND);
	SetBkColor(hdc0,RGB(255,255,255));
	SetTextColor(hdc0,RGB(0,0,0));

	BitBlt(hdc0,nX0,nY0,nW0,nH0,mDC,0,0,SRCAND);
	BitBlt(hdc0,nX0,nY0,nW0,nH0,hDC,0,0,SRCPAINT);

	SelectObject(hDC, oldBMP);				
	DeleteDC(hDC);
	SelectObject(mDC, oldmBMP);
	DeleteDC(mDC);
	DeleteObject(hBMP);
	DeleteObject(mBMP);
}
           

######Http(post/get)请求(https://blog.csdn.net/dragoo1/article/details/7800249)

因为需要全局引用 所以最好在stdafx.h申明

#include <stdio.h>
#include <string>
#include "Wininet.h"
#pragma comment(lib,"Wininet.lib")
std::string HttpRequest(CString   lpHostName, short sPort, CString lpUrl, CString lpMethod, char * lpPostData, int nPostDataLen);
           

stdafx.cpp实现

//模拟浏览器发送HTTP请求函数
 std::string HttpRequest(CString   lpHostName, short sPort, CString lpUrl, CString lpMethod, char * lpPostData, int nPostDataLen)
{
	HINTERNET hInternet = nullptr, hConnect = nullptr, hRequest = nullptr;

	BOOL bRet;

	std::string strResponse;

	hInternet = (HINSTANCE)InternetOpen(CString("User-Agent"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
	if (!hInternet)
		goto Ret0;

	hConnect = (HINSTANCE)InternetConnect(hInternet, lpHostName, sPort, NULL, CString("HTTP/1.1"), INTERNET_SERVICE_HTTP, 0, 0);
	if (!hConnect)
		goto Ret0;

	hRequest = (HINSTANCE)HttpOpenRequest(hConnect, lpMethod, lpUrl, CString("HTTP/1.1"), NULL, NULL, INTERNET_FLAG_RELOAD, 0);
	if (!hRequest)
		goto Ret0;

	//bRet = HttpAddRequestHeaders(hRequest,"Content-Type: application/x-www-form-urlencoded",Len(FORMHEADERS),HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD);
	//if(!bRet)
		//goto Ret0;

	bRet = HttpSendRequest(hRequest, NULL, 0, lpPostData, nPostDataLen);
	while (TRUE)
	{
		char cReadBuffer[4096];
		unsigned long lNumberOfBytesRead;
		bRet = InternetReadFile(hRequest, cReadBuffer, sizeof(cReadBuffer) - 1, &lNumberOfBytesRead);
		if (!bRet || !lNumberOfBytesRead)
			break;
		cReadBuffer[lNumberOfBytesRead] = 0;
		strResponse = strResponse + cReadBuffer;
	}

Ret0:
	if (hRequest)
		InternetCloseHandle(hRequest);
	if (hConnect)
		InternetCloseHandle(hConnect);
	if (hInternet)
		InternetCloseHandle(hInternet);

	return strResponse;
}
           

调用

//添加一个按钮的点击事件 
void CmQMFCDlg::OnClickedButton1()
{
	//发送左键点击事件给 OnLButtonDown 这个程序自己重写了 
	//SendMessage(WM_LBUTTONDOWN, WM_LBUTTONDOWN, 0x00010002);
	string result;
	//gank.io/api/today 
	//std::string strResponse = HttpRequest(_T("www.wanandroid.com"), 80, _T("article/list/1/json"), _T("GET"), NULL, 0);
	std::string strResponse = HttpRequest(_T("gank.io"), 80, _T("api/xiandu/categories"), _T("GET"), NULL, 0);
#pragma warning(disable:4996)//fopen_s  版本过旧警告
	FILE * fp = fopen("C:\\Users\\Administrator\\Desktop\\result.json", "wb");
	fwrite(strResponse.c_str(), strResponse.length(), 1, fp);
	fclose(fp);
	//将获取到的json写入到文本框上
	GetDlgItem(IDC_EDIT1)->SetWindowText(CString(strResponse.c_str()));
}
           

数据双向绑定

//会调用DoDataExchange方法
	UpdateData(true);//获取编辑框数据
	UpdateData(false);//更新总费用编辑框
           

不断跟新中。。。

继续阅读