当前位置:主页 > 查看内容

从Win32过渡到MFC第四天

发布时间:2021-07-10 00:00| 位朋友查看

简介:从Win32过渡到MFC 简单介绍 Win32: Win32应用程序的简称SDK编程可以开发32位应用程序也可以开发64位应用程序。 VC6 有2000多个API VS2013新增许许多多的API MFC是什么微软基础类库英语 Microsoft Foundation Classes简称MFC是 微软公司 提供的一个 类库 clas……

从Win32过渡到MFC

简单介绍

Win32: Win32应用程序的简称,SDK编程,可以开发32位应用程序,也可以开发64位应用程序。
VC6 有2000多个API , VS2013新增许许多多的API

MFC是什么:微软基础类库(英语:Microsoft Foundation Classes,简称MFC)是微软公司提供的一个类库(class libraries),以C++类的形式封装了[Windows API](https://baike.baidu.com/item/Windows API/6088382),并且包含一个应用程序框架,以减少应用程序开发人员的工作量。其中包含大量Windows句柄封装类和很多Windows的内建控件和组件的封装类。

好处:代码的可重用性,代码和数据更加紧密结合。
缺点:界面太丑,美化太复杂,什么东西的都要重绘,界面与逻辑没分开

界面库:Qt(庞大的帮助文档), duilib(帮助文档不够详细),easyX(实际项目开发当中,没有)

我们为什么还要学习MFC?
1、巩固Win32
2、开阔思路、开阔眼界

创建MFC

建立MFC应用程序三部曲
1、建立CWinApp派生类
2、重写InitInstance虚函数
3、定义CWinApp派生类的全局变量theApp (名字随意建议用theApp)

//1、建立CWinApp派生类
class CHelloApp : public CWinApp
{
public:
	//相当于程序的入口,相当于第一个执行的
	//MFC初始化过程 启动管理
	BOOL InitInstance();

	//退出
	int ExitInstance();
};

//2、重写InitInstance虚函数
    BOOL CHelloApp::InitInstance()
{
	CHelloDlg dlg;
	dlg.DoModal();
	return TRUE;
}
//退出
int CHelloApp::ExitInstance()
{
	//清理操作
	return CWinApp::ExitInstance();
}

//3、定义CWinApp派生类的全局变量theApp (名字随意建议用theApp)
    CHelloApp theApp;  //MFC应用程序的核心对象,一个应用程序有且只有一个应用程序对象。

创建Dialog

class CHelloDlg : public CDialog
{
private:
	HICON m_hIcon;
public:
	CHelloDlg();

	//对话框初始化函数
	BOOL OnInitDialog();
};

CHelloDlg::CHelloDlg() :CDialog(IDD_MAIN_DLG)
{
	//加载图标
	m_hIcon = ::LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_LOGO));
}
//对话框初始化函数   WM_INITDIALOG消息的关联函数 , 对话框已经创建好了,还没有显示出来而已
BOOL CHelloDlg::OnInitDialog()
{
	//设置窗口标题
	SetWindowText(L"这是我的MFC应用程序");

	//设置图标
	SetIcon(m_hIcon, TRUE);
	SetIcon(m_hIcon, FALSE);

	//LPSTR, LPCTSTR , char*, const char*,
	//CString
	//string

	//CString str;//空字符串
	//CString str(L"Hello World");
	//CString str('a', 10);

	//CString str = L"我爱中国";

	//求字符串的长度
	//int n = str.GetLength(); //获取字符的个数

	//判断一个字符串是否为空
	/*if (str.IsEmpty())
	{
		MessageBox(L"为空");
	}
	else
	{
		MessageBox(L"不为空");
	}*/

	//清空字符串
	//str.Empty();

	//if (str.IsEmpty())
	//{
	//MessageBox(L"为空");
	//}
	//else
	//{
	//MessageBox(L"不为空");
	//}

	//获取字符串的某一个元素
	//wchar_t ch = str.GetAt(0);
	//wchar_t ch = str[0];

	//指定设置某一个元素
	//str.SetAt(0, L'他');

	//+, +=, =
	//CString str1 = L"123456";
	//CString str2;
	//str2 += str;

	//字符串截取
	//Left 、Right 、Mid

	//CString str1 = L"HelloWorld";
	//str = str1.Left(5);
	//str = str1.Right(3);
	//str = str1.Mid(4);
	//str = str1.Mid(4, 3);

	//字符串转化函数
	//CString str = "Hello World";
	//CString str2 = str.MakeUpper();
	//CString str2 = str.MakeLower();
	//CString str2 = str.MakeReverse();

	//字符串替换函数
	//CString str = "Hello World";
	//str.Replace(L"Hello", L"11111");
	//str.Replace(L'o', L'5');

	//移除字符
	//str.Remove(L'o');

	//插入字符
	//str.Insert(2, L'5');

	//删除字符
	//str.Delete(2, 3);

	//格式化字符串
	//CString str;
	//str.Format(L"热心网友小赵有%d个老婆, 每个老婆都是从淘宝上面买来的,每一个单价%f,总共买了%d个,型号为:%c", 5, 99.99f, 10, 'L');

	//去除空格
	//CString str = L"  67Hello  ";
	//str.TrimLeft();
	//str.TrimRight();
	//str.Trim();

	//str = str + L"123";

	//字符串查找函数
	//int pos = str.Find(L'F', 1);
	//int pos = str.Find(L"lo", 1);

	//反向查找
	//CString str = L"Hello World";
	//int n = str.ReverseFind(L'e');

	//CString str = L"C:\\Progame File\\QQ\\Tencent.exe";

	//int n = str.ReverseFind(L'\\');
	//str = str.Mid(n + 1);

	//SetWindowText(str);

	//获取指针
	//CString str = L"HelloWorld";
	//str.GetBuffer();

	//CPoint类 - 点
	//POINT
	//CPoint pt;
	//GetCursorPos(&pt);

	//CString str;
	//str.Format(L"当前鼠标坐标:(%d, %d)", pt.x, pt.y);
	//MessageBox(str);

	//pt.Offset(-1, -1);
	//str.Format(L"当前鼠标坐标:(%d, %d)", pt.x, pt.y);
	//MessageBox(str);

	//==, != , +, +=, -

	//CSize类 - 大小
	//CSize size(800,600);
	//== ,!= , + ,+=, -, -=
	
	//CRect类 ,矩形类
	//CRect
	/*CRect rect;
	GetWindowRect(&rect);

	CString str;
	str.Format(L"(%d, %d, %d, %d)", rect.left, rect.top,rect.right,rect.bottom);
	SetWindowText(str);

	CPoint pt = rect.BottomRight();
	int nWidth = rect.Width();
	int nHeight = rect.Height();
	
	CRect rt;
	rt.SetRect(0, 0, 0, 0);
	if (rt.IsRectNull())
	{
		MessageBox(L"为空");
	}
	else
	{
		MessageBox(L"不为空");
	}*/

	//CTime类
	//CTime
	//获取当前时间
	CTime time = CTime::GetCurrentTime();
	//int nYear = time.GetYear();
	//int nMonth = time.GetMonth();
	//int nDay = time.GetDay();
	//int nHour = time.GetHour();
	//int nMinute = time.GetMinute();
	//int nSecond = time.GetSecond();
	//int nDayOfWeek = time.GetDayOfWeek();

	CString str = time.Format(L"当前时间:%Y-%m-%d %H:%M:%S");

	//CString str;
	//str.Format(L"当前时间: %d-%d-%d %d:%d:%d 星期:%d", nYear, nMonth, nDay, nHour, nMinute, nSecond, nDayOfWeek-1);
	SetWindowText(str);
	
	return TRUE;
}
;原文链接:https://blog.csdn.net/m0_52894937/article/details/115712200
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文


随机推荐