读书人

MFC 学习札记

发布时间: 2012-12-24 10:43:13 作者: rapoo

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

// 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;}elsebreak;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;}elsebreak;ReleaseMutex(hMutex);}*/WaitForSingleObject(hMutex,INFINITE);cout<<"thread2 is running"<<endl;return 0;}

读书人网 >VC/MFC

热点推荐