`

MFC 学习笔记

阅读更多
这个笔记是在观看《孙鑫MFC教程》时写的,他用的是VC6.0,但是Win7和VC6.0 有不兼容的情况,我本人用VS2008来做的一些实例和练习。

窗口创建过程1. 设计窗口类;2. 注册窗口类;3. 创建窗口;4. 显示及更新窗口   
以一个名称为Test的MFC程序系统默认会产生:CAboutDlg(继承)、CMainFrame(继承CFrameWnd)、CTestApp(继承CWinApp)、CTestDoc(继承CDocument)、CTestView(继承CView)五个类
添加消息响应函数时,同时在三个地方加入了代码:1. 自己添加的消息响应函数代码2. 在相应的头文件,在 DECLARE_MESSAGE_MAP() 函数之前,会自动添加进去宏的声明3. 在相应的源文件,在BEGIN_MESSAGE_MAP(CDrawView, CView) END_MESSAGE_MAP()之间,也有相应的宏的声明
m_hWnd这是从MFC底层类库中派生来的数据成员,在类中可以直接使用。如: HDC hdc; hdc = ::GetDC(m_hWnd); ::ReleaseDC(m_hWnd, hdc);




对于DC(DeviceContext对象,用于画图,下面是一些代码)
// 	CDC *pDC = GetDC();
// 	pDC->MoveTo(m_ptOrigin);
// 	pDC->LineTo(m_ptEnd);
// 	ReleaseDC(pDC);

// 	CClientDC dc(this);
// 	dc.MoveTo(m_ptOrigin);
// 	dc.LineTo(m_ptEnd);

 dc(GetDesktopWindow());
// 	dc.MoveTo(m_ptOrigin);
// 	dc.LineTo(m_ptEnd);




在一个自己建立的modal()类型的对话框上有3个便签和一个加按钮。且有3个整型数据关联。实现相加功能。
//int num1, num2, num3;
	//CString str1, str2, str3;
	
	//GetDlgItem(IDC_EDIT1)->GetWindowText(str1);
	//GetDlgItem(IDC_EDIT2)->GetWindowText(str2);
	//num1=_ttoi((LPCTSTR)str1);
	//num2=_ttoi((LPCTSTR)str2);
	//num3=num1+num2;
	//_itot(num3,(TCHAR*)str3.GetBuffer(10),10);
	//str3.ReleaseBuffer();
	//GetDlgItem(IDC_EDIT3)->SetWindowText(str3);//当计算好num1+num2 显示在第三个编辑框控件中


	//GetDlgItemText(IDC_EDIT1, str1);
	//GetDlgItemText(IDC_EDIT2, str2);
	//num1=_ttoi((LPCTSTR)str1);
	//num2=_ttoi((LPCTSTR)str2);
	//num3=num1+num2;
	//_itot(num3,(TCHAR*)str3.GetBuffer(10),10);
	//str3.ReleaseBuffer();
	//SetDlgItemText(IDC_EDIT3, str3);

	/*num1 = GetDlgItemInt(IDC_EDIT1);
	num2 = GetDlgItemInt(IDC_EDIT2);
	num3 = num1 + num2;
	SetDlgItemInt(IDC_EDIT3, num3);*/

	/*UpdateData();
	m_num3 = m_num1 + m_num2;
	UpdateData(false);*/

	//m_edit1.GetWindowText(str1);
	//m_edit2.GetWindowText(str2);
	//num1=_ttoi((LPCTSTR)str1);
	//num2=_ttoi((LPCTSTR)str2);
	//num3=num1+num2;
	//_itot(num3,(TCHAR*)str3.GetBuffer(10),10);
	//str3.ReleaseBuffer();
	//m_edit3.SetWindowText(str3);//当计算好num1+num2 显示在第三个编辑框控件中





人为手动改变对话框的大小
// TODO: 在此添加控件通知处理程序代码
	CString str;
	GetDlgItemText(ID_SHOUSUO, str);
	if (str == "收缩<<")
	{
		SetDlgItemText(ID_SHOUSUO, L"扩展>>");
	} 
	else
	{
		SetDlgItemText(ID_SHOUSUO, L"收缩<<");
	}


	static CRect rectLarge;
	static CRect rectSmall;

	if (rectLarge.IsRectNull())
	{
		CRect rectSeparator;
		GetWindowRect(&rectLarge);
		GetDlgItem(IDC_SEPARATOR)->GetWindowRect(&rectSeparator);
	
		rectSmall.top = rectLarge.top;
		rectSmall.left = rectLarge.left;
		rectSmall.bottom = rectLarge.bottom;
		rectSmall.right = rectSeparator.right;

	}

	if (str == "收缩<<")
	{
		SetWindowPos(NULL, 0, 0, rectSmall.Width(), rectSmall.Height(), SWP_NOZORDER | SWP_NOMOVE);
	}
	else
	{
		SetWindowPos(NULL, 0, 0, rectLarge.Width(), rectLarge.Height(), SWP_NOZORDER | SWP_NOMOVE);
	}




互斥量的使用
#include <windows.h>
#include <iostream.h>

DWORD WINAPI Fun1Proc(
  LPVOID lpParameter   // thread data
);

DWORD WINAPI Fun2Proc(
  LPVOID lpParameter   // thread data
);
int index=0;
int tickets=100;
HANDLE hMutex;
void main()
{
	HANDLE hThread1;
	HANDLE hThread2;
	hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
	hThread2=CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);
	CloseHandle(hThread1);
	CloseHandle(hThread2);
	/*while(index++<1000)
		cout<<"main thread is running"<<endl;*/
	//hMutex=CreateMutex(NULL,TRUE,NULL);
	hMutex=CreateMutex(NULL,TRUE,"tickets");
	if(hMutex)
	{
		if(ERROR_ALREADY_EXISTS==GetLastError())
		{
			cout<<"only instance can run!"<<endl;
			return;
		}
	}
	WaitForSingleObject(hMutex,INFINITE);
	ReleaseMutex(hMutex);
	ReleaseMutex(hMutex);
	Sleep(4000);
//	Sleep(10);
}

DWORD WINAPI Fun1Proc(
  LPVOID lpParameter   // thread data
)
{
	/*while(index++<1000)
		cout<<"thread1 is running"<<endl;*/
	
	/*while(TRUE)
	{
		//ReleaseMutex(hMutex);
		WaitForSingleObject(hMutex,INFINITE);
		if(tickets>0)
		{
			Sleep(1);
			cout<<"thread1 sell ticket : "<<tickets--<<endl;
		}
		else
			break;
		ReleaseMutex(hMutex);
	}*/

	WaitForSingleObject(hMutex,INFINITE);
	cout<<"thread1 is running"<<endl;
	return 0;
}

DWORD WINAPI Fun2Proc(
  LPVOID lpParameter   // thread data
)
{
	
	/*while(TRUE)
	{
		//ReleaseMutex(hMutex);
		WaitForSingleObject(hMutex,INFINITE);
		if(tickets>0)
		{
			Sleep(1);
			cout<<"thread2 sell ticket : "<<tickets--<<endl;
		}
		else
			break;
		ReleaseMutex(hMutex);
	}*/
	WaitForSingleObject(hMutex,INFINITE);
	cout<<"thread2 is running"<<endl;
	return 0;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics