??xml version="1.0" encoding="utf-8" standalone="yes"?>
典型的代码是Q?br>GetDlgItem(ID)->SetWindowText(Caption)
于是Q今天想C改RadioButton的代码就出来了?br>
CButton *p_MyBut = new CButton(); |
BOOL Create( LPCTSTR lpszCaption, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID ); |
CButton* CTextEditorView::NewMyButton(int nID,CRect rect,int nStyle) { CString m_Caption; m_Caption.LoadString( nID ); //取按钮标?br>CButton *p_Button = new CButton(); ASSERT_VALID(p_Button); p_Button->Create( m_Caption, WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | nStyle, rect, this, nID ); //创徏按钮 return p_Button; } |
CButton *p_MyBut[3]; p_MyBut[0] = NewMyButton( ID_MYBUT1, CRect(10,20,50,35), BS_DEFPUSHBUTTON ); p_MyBut[1] = NewMyButton( ID_MYBUT2, CRect(55,20,95,35), 0 ); p_MyBut[2] = NewMyButton( ID_MYBUT3, CRect(100,20,140,35), 0 ); |
BEGIN_MESSAGE_MAP(CTextEditorView, CFormView) //{{AFX_MSG_MAP(CTextEditorView) ON_BN_CLICKED(IDC_ICONBUT0, OnIconbut0) //}}AFX_MSG_MAP END_MESSAGE_MAP() |
BEGIN_MESSAGE_MAP(CTextEditorView, CFormView) //{{AFX_MSG_MAP(CTextEditorView) ON_BN_CLICKED(IDC_ICONBUT0, OnIconbut0) //}}AFX_MSG_MAP ON_BN_CLICKED(ID_MYBUT1, OnMybut1) ON_BN_CLICKED(ID_MYBUT2, OnMybut2) ON_BN_CLICKED(ID_MYBUT3, OnMybut3) END_MESSAGE_MAP() |
protected: //{{AFX_MSG(CTextEditorView) afx_msg void OnIconbut0(); //}}AFX_MSG DECLARE_MESSAGE_MAP() |
protected: //{{AFX_MSG(CTextEditorView) afx_msg void OnIconbut0(); //}}AFX_MSG afx_msg void OnMybut1(); afx_msg void OnMybut2(); afx_msg void OnMybut3(); DECLARE_MESSAGE_MAP() |
void CTextEditorView::OnMybut1() { MessageBox( "哈!你单M动态按钮? ); } void CTextEditorView::OnMybut2() { …… } void CTextEditorView::OnMybut3() { …… } |
if( p_MyBut[0] ) delete p_MyBut[0]; |
CButton* CTextEditorView::NewMyRadio(int nID,CRect rect,int nStyle) { CString m_Caption; m_Caption.LoadString( nID ); //取按钮标?br> CButton *p_Radio = new CButton(); ASSERT_VALID(p_Radio); p_Radio->Create( m_Caption, WS_CHILD | WS_VISIBLE | nStyle | WS_TABSTOP | BS_AUTORADIOBUTTON, rect, this, nID ); //创徏按钮 return p_Radio; } |
CButton *p_MyRadio[3]; p_MyRadio[0] = NewMyRadio( IDC_MYRADIO1, CRect(15,90,60,105), WS_GROUP ); p_MyRadio[1] = NewMyRadio( IDC_MYRADIO2, CRect(15,108,60,123), 0 ); p_MyRadio[2] = NewMyRadio( IDC_MYRADIO3, CRect(15,126,60,141), 0 ); |
int m_SelRadio; |
CButton *p_MyRadio[3]; p_MyRadio[0] = NewMyRadio( IDC_MYRADIO1, CRect(15,90,60,105), WS_GROUP ); p_MyRadio[1] = NewMyRadio( IDC_MYRADIO2, CRect(15,108,60,123), 0 ); p_MyRadio[2] = NewMyRadio( IDC_MYRADIO3, CRect(15,126,60,141), 0 ); p_MyRadio[m_SelRadio]->SetCheck(1); //讄W一个单选ؓ选中状? |
BEGIN_MESSAGE_MAP(CTextEditorView, CFormView) //{{AFX_MSG_MAP(CTextEditorView) ON_BN_CLICKED(IDC_ICONBUT0, OnIconbut0) //ClassWizard在此处添?br>//}}AFX_MSG_MAP ON_BN_CLICKED(IDC_MYRADIO1, OnMyRadio1) //单选按? ON_BN_CLICKED(IDC_MYRADIO2, OnMyRadio2) //单选按? ON_BN_CLICKED(IDC_MYRADIO3, OnMyRadio3) //单选按? END_MESSAGE_MAP() |
protected: //{{AFX_MSG(CTextEditorView) afx_msg void OnIconbut0(); //ClassWizard在此处添?br>//}}AFX_MSG afx_msg void OnMyRadio1(); //单选按? afx_msg void OnMyRadio2(); //单选按? afx_msg void OnMyRadio3(); //单选按? DECLARE_MESSAGE_MAP() |
//单击单选按? void CTextEditorView::OnMyRadio1() { m_SelRadio=0; } //单击单选按? void CTextEditorView::OnMyRadio2() { m_SelRadio=1; } //单击单选按? void CTextEditorView::OnMyRadio3() { m_SelRadio=2; } |
CTextEditorView::~CTextEditorView() { int i; for( i=0; i<3; i++) { if(p_MyRadio[i]) delete p_MyRadio[i]; } } |
Ҏ一Q?/span>API部分Q?/span>
// OPENFILENAME ofn;
// char szFile[MAX_PATH];
// ZeroMemory(&ofn,sizeof(ofn));
// ofn.lStructSize = sizeof(ofn);
// ofn.lpstrFile = szFile;
// ofn.lpstrFile[0] = TEXT('\0');
// ofn.nMaxFile = sizeof(szFile);
// ofn.lpstrFilter = TEXT("all\0*.*\0jpg\0*.jpg\0bmp\0*.bmp\0"); //定义三个选项Q?/span>allQ?/span>text?/span>exe
// ofn.nFilterIndex = 1; //定义首选项是哪一?/span>
// ofn.lpstrFileTitle = NULL;
// ofn.nMaxFileTitle = 0;
// ofn.lpstrInitialDir = NULL;
// ofn.Flags = OFN_EXPLORER |OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
// if(GetOpenFileName(&ofn))
// {
// ::SetDlgItemText(this->m_hWnd,IDC_EDIT1,szFile);
// }
Ҏ二?/span>MFC实现
// CFileDialog dlg(TRUE, NULL, NULL,
// OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
// NULL);
//
// if(dlg.DoModal() == IDOK)
// {
// szFilename = dlg.GetPathName();
// ::MessageBox(NULL,szFilename,NULL,MB_OK);
// }
注意Q打开文g的方式和打开文g夹的方式是不同的。打开文g的方式是不断的打开直到出现末端为文Ӟ否则遇到文g夹还要l打开。而打开文g夹则是只要选择C个\径的文g夹就打开?/span>
下面的这U方式是打开文g?/span>Q?/span>MFC实现?/span>
static TCHAR strDirName[MAX_PATH];
BROWSEINFO bi;
CString szString = TEXT("选择一个源文g子文件夹");
bi.hwndOwner = ::GetFocus();
bi.pidlRoot = NULL;
bi.pszDisplayName = strDirName;
bi.lpszTitle = szString;
bi.ulFlags = BIF_BROWSEFORCOMPUTER | BIF_DONTGOBELOWDOMAIN | BIF_RETURNONLYFSDIRS;
bi.lpfn = NULL;
bi.lParam = 0;
bi.iImage = 0;
LPITEMIDLIST pItemIDList = ::SHBrowseForFolder(&bi);
if(pItemIDList == NULL)
{
return ;
}
::SHGetPathFromIDList(pItemIDList, strDirName);
CString str = strDirName;
if(str != "" && str.Right(1) != '\\')
str += '\\';
::SetDlgItemText(this->m_hWnd,IDC_EDIT1,str);
BROWSEINFO bi;
bi.hwndOwner = ::GetFocus();
bi.pidlRoot = NULL;
bi.pszDisplayName = strDirName;
bi.lpszTitle = lpszTitle;
bi.ulFlags = BIF_BROWSEFORCOMPUTER | BIF_DONTGOBELOWDOMAIN | BIF_RETURNONLYFSDIRS;
bi.lpfn = NULL;
bi.lParam = 0;
bi.iImage = 0;
LPITEMIDLIST pItemIDList = ::SHBrowseForFolder(&bi);
if(pItemIDList == NULL)
{
return "";
}
::SHGetPathFromIDList(pItemIDList, strDirName);
CString str = strDirName;
if(str != "" && str.Right(1) != '\\')
str += '\\';
return str;
}
最l得到的str是那个路径的名字。上面的函数如果和下面这个配套用的话,需要注意加?\",因ؓ需要多一个。这个函数的目的是Z打开一个文件夹的对话框让你选择?br>
3.VOID CFileIO::GetFileNamesInDir(CStringArray &StringArray, LPCTSTR lpszExpression)
{
// The File Name should apply with "d:\\fp\\temp.bmp" Style
CFileFind FileFind;
if(!FileFind.FindFile(lpszExpression))
return;
BOOL bFound;
do{
bFound = FileFind.FindNextFile();
if(!FileFind.IsDirectory())
StringArray.Add(FileFind.GetFileName());
}while(bFound);
}
传入一个\径,得到该\径下面的所有的文g名,q是存入数组Q不q没有那些\径名Q而只是文件名。还要不会搜索下面的所有的文g夹,只是当前文g夹下面是?br>但是Q你可以用上面这三个函数做很多事情了。递归调用Q完全可以实C…………
4.上面说到了递归调用。下面的q个是实际的实Cd一个目录下面所有的文g名,q且保存在一个CString数组中,而且q是详细的文件\径?br>static void GetAllFilePathInDir( CStringArray &strArray, CString strDir, CString strExt )
{
CFileFind finder;
CString strFileName;
if( strDir.IsEmpty() )
return;
if( strDir.Right(2) != "\\" )
strDir = strDir + "\\";
BOOL bWorking = finder.FindFile( strDir + "*.*" );
while( bWorking )
{
bWorking = finder.FindNextFile();
if ( finder.IsDots() )
continue;
if ( finder.IsDirectory() )
{
GetAllFilePathInDir( strArray, finder.GetFilePath(), strExt );
continue;
}
strFileName = finder.GetFilePath();
strFileName.MakeLower();
CString needExt = GetFileExt( strExt );
CString srcExt = GetFileExt( strFileName );
if( strExt == "*.*" || needExt == srcExt )
{
strArray.Add( strFileName );
}
}
finder.Close();
}
一个典型的调用参数是下面的这个:
GetAllFilePathInDir(FilenameALL,szSourceFilename,"*.jpg");
for (int i = 0; i < FilenameALL.GetSize(); i++)
{
::MessageBox(NULL,FilenameALL.GetAt(i),"ok",MB_OK);
}
注意Q前面第一个参数是表示要存攄数组Q第二个是你要查扄那个路径Q第三个是查扄文g名。可以用通配W来实现查找。这样就完成了功能?br>强大的MFC。。。?br>