문자열 집합

유니코드 : 문자 하나당 2바이트의 공간 확보

멀티바이트 : ANSI에서는 1바이트의 공간을 다국어에서 2바이트의 공간을 확보(영어1byte, 한글2byte)

 

char 1바이트의 공간을 확보  
wchar_t 유니코드를 지원하기위해 사용
-2바이트의 공간 확보

L"abcd"
TCHAR 변형이 가능한 타입
문자집합이 적용됨
유니코드일때 TCHAR -> wchar_t
멀티바이트일때 TCHAR ->char
새로운 타입이 아님 (매크로)
 
결론 : 문자열 집합에 영향을 받기 싫다면 TCHAR를 쓰길 권장 

_T("")매크로

이 매크로의 역할은 문자집합으로 유니코드가 사용되면 유니코드 형식으로 바꾸어 주는 역할!!

 

파일 입력 코드

외울 코드는 아니고 필요할때마다 그냥 복붙하는게 답

CString m_strPath, str;

	CStdioFile rFile;

	CFileException ex;

	CFileDialog dlg(TRUE, _T("*.txt"), NULL, OFN_FILEMUSTEXIST | OFN_OVERWRITEPROMPT, _T("TXT Files(*.txt)|*.txt|"), NULL);
    	CString getFileString; 

	if (dlg.DoModal() == IDOK)

	{

		m_strPath = dlg.GetPathName();

		rFile.Open(m_strPath, CFile::modeReadWrite | CFile::typeText, &ex);



		while (rFile.ReadString(str))

		{

			getFileString += (str + _T("\r\n"));

		}

		rFile.Close();

                // edit control에 txt파일 쓰기

		mFileView.SetWindowTextW(getFileString);

	}


 

 

Step1) Class VIew 

Step2) ~~.dlg 클릭

Step3) 해당 모양 클릭

Step4) PreTranslateMessage 함수생성

Step5) 함수 입력 

BOOL CDbfReaderDlg::PreTranslateMessage(MSG* pMsg)
{
	// TODO: Add your specialized code here and/or call the base class
	if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN)
	{
		OnBnClickedBtnRun();
		return TRUE; 
	}
	return CDialogEx::PreTranslateMessage(pMsg);
}

ShellExecute 함수

1)txt 파일 열기

2)경로 열기

//txt 파일 열기
void CDbfReaderDlg::OnBnClickedBtnOpendata()
{
	if (openoutputDataPath == "")
	{
		MessageBox(_T("RUN 해주세요."));
		return;
	}
	//openoutputDataPath.Replace(L"\\", L"\\\\");
	// TODO: Add your control notification handler code here
	ShellExecute(NULL, _T("open"), _T("notepad"), openoutputDataPath, NULL, SW_SHOW);
	 

}


//폴더 열기 
void CDbfReaderDlg::OnBnClickedBtnOutpathopen()
{
	if (openoutputPath == "")
	{
		MessageBox(_T("RUN 해주세요."));
		return;
	}
	// TODO: Add your control notification handler code here
	
	
	// TODO: Add your control notification handler code here
	//ShellExecute(NULL, _T("open"), _T("notepad"), openinputPath, NULL, SW_SHOW);
	ShellExecute(NULL, _T("open"),  openoutputPath,NULL, NULL, SW_SHOW);
}

 

 

CFileFind란 Class를 사용하면 된다.

//해당경로에 폴더있는지 확인하는 것
CFileFind  cFileFinder;
bool temp=  cFileFinder.FindFile(_T("C:\\Users\\SW\\Desktop"));

 

 

 

 

ini등 실행파일의 위치 기반으로 전처리 작업을 해야할 때 사용할 수 있는 현재 실행파일의 위치(경로) 반환 소스이다.

	TCHAR path[260];
	GetModuleFileName(NULL, path, sizeof path); //path에 실행파일의 위치+.exe 까지 담는다.
	CString strPath = path;
	//파싱작업
	int i = strPath.ReverseFind('\\'); //실행 파일 이름을 지우기 위해 
	strPath = strPath.Left(i); //뒤에있는 현재 실행 파일 이름을 지운다.
	AfxMessageBox(strPath);

 

 

다이얼로그는 크게 파일/폴더 탐색 두가지로 구분 되며 다음은 파일경로를 탐색하여 그 위치의 저장하는 코드이다. 

 

1) Edit Control 생성

2) 파일 경로 설정

 

3) 결과

 

4)코드

 

void CTEST_CREATEDlg::OnBnClickedButton1()
{
	//char szFilter[20]  ;
	CString szFilter = _T("txt file(*.txt)|*.txt; |ALL File(*.*(|*.*||");
	CFileDialog dlg(FALSE, _T(""), NULL, OFN_OVERWRITEPROMPT, szFilter);
	//CFileDialog dlg(FALSE, "bmp", "pcmon", OFN_OVERWRITEPROMPT, szFilter);
	// TODO: Add your control notification handler code here
	CString strFolder;
	CString strPath;
	CString strFileName;
	CString strFolderPath;
	if (IDOK == dlg.DoModal())
	{
		strFolder = dlg.GetFolderPath();
		strPath = dlg.GetPathName();
		strFileName = dlg.GetFileName();

		m_strFFolder.SetWindowTextW(strFolder);
		m_strFPath.SetWindowTextW(strPath);
		m_strFName.SetWindowTextW(strFileName); 
		
	} 
}

 

기존의 char 배열에 szFilter를 CString으로 구현 --> MFC에 더 효율적 (유니코드, 멀티바이트 등의 형식오류 최소화)

 

 

5) 추가 

1) 경로에서 파일이름을 제외하고 경로만 추출

2) 경로에서 파일이름만 추출하고 싶을때  또는 마지막 경로depth를 추출

CString re = strPath.Mid(0, strPath.ReverseFind('\\'));//파일경로 (마지막경로제외)
CString res = strPath.Mid(strPath.ReverseFind('\\')+1, strPath.GetLength());//마지막경로

 

폴더 경로 가져오는 코드 

m_DbfData.clear();
BROWSEINFO BrInfo;
TCHAR szBuffer[512];// 경로저장 버퍼 

::ZeroMemory(&BrInfo, sizeof(BROWSEINFO));
::ZeroMemory(szBuffer, 512);

BrInfo.hwndOwner = GetSafeHwnd();
BrInfo.lpszTitle = _T("파일이 저장될 폴더를 선택하세요");
BrInfo.ulFlags = BIF_NEWDIALOGSTYLE | BIF_EDITBOX | BIF_RETURNONLYFSDIRS | TBIF_TEXT;
LPITEMIDLIST pItemIdList = ::SHBrowseForFolder(&BrInfo);
::SHGetPathFromIDList(pItemIdList, szBuffer);   

//path 경로 저장
CString inputPath =_T("");  
inputPath.Format(_T("%s"), szBuffer);

 

경로 옵션

BrInfo.ulFlags = BIF_NEWDIALOGSTYLE | BIF_EDITBOX | BIF_RETURNONLYFSDIRS | TBIF_TEXT;

BIF_BROWSEFORCOMPUTER    : 네트워크의 컴퓨터만 선택가능

BIF_BROWSEFORPRINTER        : 프린터만 선택가능

BIF_BROWSEINCLUDEFILES      : 파일도 표시

BIF_DONTGOBELOWDOMAIN  : 네트워크의 컴퓨터를 표시하지 않는다

BIF_EDITBOX                      : 에디트 박스를 표시한다

BIF_RETURNFSANCESTORS     : 네트워크의 컴퓨터만 선택가능

BIF_RETURNONLYFSDIRS        : 폴더만 선택가능

BIF_STATUSTEXT                  : 스테이터스 텍스트를 표시한다

BIF_VALIDATE                     : 부정 입력시에, BFFM_VALIDATEFAILED 이벤트

 

 

+ Recent posts