MFC编程总结

下拉列表框操作:
1.添加项并设置默认值

for(int i=0;i

或者,

 CString pictype[]={_T(".jpg"),_T(".png"),_T(".bmp")};
	 for(int i=0;i<3;i++)
		{
			m_combo3.AddString(pictype[i]);
		}
		m_combo3.SetCurSel(0);

2.给文本框赋值,取值

GetDlgItem(IDC_EDIT1)->SetWindowText(L"D:\\temp");
GetDlgItem(IDC_EDIT1)->GetWindowText(CString);

3.设置控件是否可用

m_btn1.EnableWindow(0);//不可用

4.设置控件是否隐藏

m_picture2.ShowWindow(false);//隐藏

5.设置checkbox为选中状态

 ((CButton*)GetDlgItem(IDC_CHECK2))->SetCheck(0);//不选中

6.选择文件夹

CFileFind finder;  
	CString path;  
	BOOL fileExist;  
  
	// 获取特定文件夹的LPITEMIDLIST,可以将之理解为HANDLE  
	// 所谓的特定文件夹,你可以用CSIDL_XXX来检索之。  
	LPITEMIDLIST rootLoation;  
	SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOP, &rootLoation);  
	if (rootLoation == NULL) 
	{ 
		return;  
	}  
	// 配置对话框  
	BROWSEINFO bi;  
	ZeroMemory(&bi, sizeof(bi));  
	bi.pidlRoot = rootLoation; // 文件夹对话框之根目录,不指定的话则为我的电脑  
	bi.lpszTitle = _T("对话框抬头"); // 可以不指定  
	// bi.ulFlags = BIF_EDITBOX | BIF_RETURNONLYFSDIRS;  
  
	// 打开对话框, 有点像DoModal  
	LPITEMIDLIST targetLocation = SHBrowseForFolder(&bi);  
	if (targetLocation != NULL) 
	{  
		TCHAR targetPath[MAX_PATH];  
		SHGetPathFromIDList(targetLocation, targetPath);  
		//MessageBox( targetPath );  
		GetDlgItem(IDC_EDIT1)->SetWindowText(targetPath);//将路径显示  
	} 

7.获取(combo)控件当前值

	long curIndex=m_combo2.GetCurSel();
	CString curResolution;
	m_combo2.GetLBText(curIndex,curResolution);

8.CString分离字符串,相当于split()

	CString cs1,cs2;
	AfxExtractSubString(cs1,curResolution,0,'x');//以x分割,分离后赋值给cs1
	AfxExtractSubString(cs2,curResolution,1,'x');

9.弹出子对话框
添加资源-dialog-基类:CDialog,添加头文件
#include “camDialog.h”

	camDialog dlg;
    dlg.DoModal();

声明全局变量:char * msg;
在用到的地方:extern char * msg; 比如在子对话框的cpp里
10.判断checkbox是否被选中

if(BST_CHECKED == IsDlgButtonChecked( IDC_CHECK1 ))
	{
		m_btn1.EnableWindow(1);
		extern bool isAvailabel;
		isAvailabel=1;
	}

11.添加初始化函数
在类向导里添加虚函数
BOOL CAboutDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();

// TODO:  在此添加额外的初始化

return TRUE;  // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE

}
12.判断radiobutton是否被选中

if (((CButton *)GetDlgItem(IDC_RADIO9))->GetCheck())
	{
		type=8;
	}

13.获取滑动条slider当前值

	extern float angle;
	int pos=m_slider2.GetPos();

14.控件值设置

//透明度
	m_slider1.SetRange(0,100);//设置滑动范围为1到100
    m_slider1.SetTicFreq(1);//每1个单位画一刻度
    m_slider1.SetPos(10);//设置滑块初始位置为1 
	//角度
	m_slider2.SetRange(0,360);//设置滑动范围为1到100
    m_slider2.SetTicFreq(1);//每1个单位画一刻度
    m_slider2.SetPos(0);//设置滑块初始位置为1 
	//默认内容
	//CString str1;
	GetDlgItem(IDC_EDIT1)->SetWindowText(L"水印");
	//位置
	((CButton *)GetDlgItem(IDC_RADIO5))->SetCheck(1);

15.获取系统字体

int CALLBACK NEnumFontNameProc(LOGFONT *plf, TEXTMETRIC* /*ptm*/, INT /*nFontType*/, LPARAM lParam)
{
		 CComboBox* comboBox = (CComboBox*) lParam;
		 comboBox->AddString(plf->lfFaceName);
		 return TRUE;
}

//列举本机所有字体
	::EnumFontFamilies(GetDC()->m_hDC, (LPTSTR) NULL, 			(FONTENUMPROC)NEnumFontNameProc, (LPARAM)&(m_font));
	int nCount = m_font.GetCount();
	m_font.SetCurSel(nCount-4);

类型转换:
1.long转CString

			long width=0;
			long height=0;
			camGetresolution(devindex,0,i,width,height);
			CString cswidth,csheight;
			cswidth.Format(_T("%d"),width);
			csheight.Format(_T("%d"),height);

2.CString转long

	long width= _tstoi(LPCTSTR(cs1));
	long height= _tstoi(LPCTSTR(cs2));

3.CString转char*

char* convertCStringToChars(CString string)
{
    int nLength=string.GetLength();
    char *c=new char[nLength+1];
    memset(c,0,nLength+1);
    WideCharToMultiByte(CP_ACP,0,string,nLength,c,nLength,NULL,NULL);
    return c;
}

4.CString转char【】

bool CStringToChar(CString str , char ch[])
{
	char* tmpch ;
	int wLen = WideCharToMultiByte(CP_ACP , 0 , str , -1 , NULL , 0 , NULL , NULL);
	tmpch = new char[wLen + 1 ];
	WideCharToMultiByte(CP_ACP , 0 , str ,-1 ,tmpch , wLen , NULL , NULL );
	for(int i = 0 ; i < wLen ; ++i)
		ch[i] = tmpch[i];
	return true;
}

你可能感兴趣的:(MFC,类型转换)