天天看點

從Win32過渡到MFC第四天從Win32過渡到MFC

從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;
}
           

繼續閱讀