Title
最終更新日:1999/01/26
目次


☆ファイルの関連付け

コマンドプロンプトから、ファイルの関連付けを変更する場合、assoc コマンドとftype コマンドを
使います。 例えば、
C:> assoc .html 
.html=htmlfile
これは、"*.html"というファイルが"htmlfile"というファイルタイプに関連付けされていることを示し、 
"htmlfile"というファイルタイプにどのアプリケーションが関連付けられているか、調べるには
C:> ftype htmlfile
htmlfile=C:\netscape\Program\netscape.exe "%1" 
ということで、"*.html" が Netscape Navigatorに関連付けられていることが分かります。
これを
C:> assoc .html=htmlfile 
C:> ftype html=C:\ie\iexplore.exe "%1" 
で、"*.html" が Internet Explorer に関連付けられます。
関連付けを消したいときには、 
assoc .html= 
でOKです。 
★ファイルの検索: strFolder以下のフォルダーのファイルの検索
void SearchFile(CString strFolder) { CFileFind FileFind; // すべてのファイルを検索します CString strSearchFile = strFolder + _T("\\*.*"); if(!FileFind.FindFile(strSearchFile)) return; BOOL bContinue = TRUE; while(bContinue){ bContinue = FileFind.FindNextFile(); // ドット("." , "..")の場合 無視します if(FileFind.IsDots()) continue; // ディレクトリの場合、そのディレクトリ内を検索する // ため再起呼び出しを行います if(FileFind.IsDirectory()) SearchFile(FileFind.GetFilePath()); else // ファイルの場合、そのフルパスをトレースします TRACE("%s\n",FileFind.GetFilePath()); }; }
★szFileNameのファイルを読み込んで小文字に変換する

void FileTextLower(const CString szFileName)
{
	char *pBuffer = new char[0x1000];
	try{
		CFile file (szFileName, CFile::modeReadWrite);
		DWORD dwBytesRemaining = file.GetLength();
		
		UINT nBytesRead;
		DWORD dwPosition;
		
		While( dwBytesRemaining ){
			dwPosition = file.GetPosition();
			nBytesRead = file.Read ( pBuffer, 0x1000);
			::CharLowerBuff ( pBuffer, nBytesRead );
			file.Seek ((LONG) dwPosition, CFile::begin );
			file.Write ( pBuffer, nBytesRead );
			dwBytesRemaining -= nBytesRead;
		}
	}
	catch ( CFileException* e){
		if (e->m_cause == CFileException::fileNotFound)
			MessageBox ("File not found");
		else if (e->m_cause == CFileException::tooManyOpenfile)
			MessageBox ("No more file handles available");
		else if (e->m_cause == CFileException::hardIO)
			MessageBox ("Hardware error");
		else
			MessageBox ("UnKnown file error");
		e->Delete();
	}
	delete[] pBuffer;
}
★オープニングロゴはどのように表示 

Visual C++の場合には、もっと簡単な方法は、コンポーネントギャラリーから「スプラッシュスクリーン」
を挿入する。その後、操作についてもすぐに分かると思います。  
★strDataに、クリップボードのテキストを取得

BOOL GetClipBordString(CString & strData)
{
    if ( !OpenClipboard()) return FALSE;        
    if ( ::IsClipboardFormatAvailable( CF_TEXT))
    {
        HGLOBAL hglb = ::GetClipboardData( CF_TEXT);
        LPTSTR lpsz = (LPTSTR)::GlobalLock( hglb);
        if ( lpsz == NULL)
        {
            ::CloseClipboard();
			return FALSE;
        }
        else
        {
			strData = lpsz;
			::GlobalUnlock(hglb);
            ::CloseClipboard();
            return TRUE;
		}
    }
    else return FALSE;
}
★strDataに、クリップボードのテキストを格納

void CTmEditView::OnEditCopy(const CString strTemp) 
{
	long nLen = strTemp.GetLength();
	LPTSTR p = strTemp.GetBuffer(nLen);

	// Global Memory
	HGLOBAL hGlobal;
    LPSTR lpStr;
    hGlobal = ::GlobalAlloc(GHND, nLen + 1);
    if (hGlobal == NULL)   return;

    lpStr = (LPSTR)::GlobalLock(hGlobal);
    ::memcpy(lpStr, p, nLen);
    ::GlobalUnlock(hGlobal);
	strTemp.ReleaseBuffer();

	// ClipBord Open
    if (::OpenClipboard(AfxGetMainWnd()->m_hWnd) == 0) {
        ::GlobalFree(hGlobal);
        return ;
    }
	// Empty Clip Bord
    ::EmptyClipboard();
	// Set ClipBord Text
    ::SetClipboardData(CF_TEXT, hGlobal);
    ::CloseClipboard();
}
★テンポラリーファイルパスの取得

WinAPIのGetTempFileName()およびGetTempPath()
★自分のファイルパスの取得

void GetMyAppPath(CString & strMyPath)
{
	strMyPath = _T("");
	CString strFile;
	DWORD dwRet = ::GetModuleFileName(NULL, strFile.GetBuffer(MAX_PATH), MAX_PATH);
	strFile.ReleaseBuffer();
	if( dwRet<=0 )
		return;
	char drive[_MAX_DRIVE], dir[_MAX_DIR];
	_splitpath( (LPCSTR)strFile, drive, dir, NULL, NULL );
	strFile.Format("%s%s", drive, dir);
	strMyPath = strFile;
}
★ファイルパスの分解

	char path_buffer[_MAX_PATH];
	char drive[_MAX_DRIVE];
	char dire[_MAX_DIR];
	char fname[_MAX_FNAME];
	char fexe[_MAX_EXT ];

	::strcpy(path_buffer, dlg.GetPathName());
	_splitpath( path_buffer, drive, dire, fname, fexe );
	
	CString str;
	str.Format("drive=%s, dir=%s, fname=%s, exe=%s", drive, dire, fname, fexe);

★ファイルパスから拡張子、ファイル名を取得

CString FindExtension(const CString& name)
{
	int len = name.GetLength();
	int i;
	for (i = len-1; i >= 0; i--){
		if (name[i] == '.'){
			return name.Mid(i+1);
		}
	}
	return CString("");
}
CString FindFileName(const CString& name, BOOL bExt/*=TRUE*/)
{
	int len = name.GetLength();
	CString ext=FindExtension(name);
	for (int i = len-1; i >= 0; i--){
		if (name[i] == '\\' || name[i]=='/'){
			if(bExt || ext.IsEmpty())
				return name.Mid(i+1);
			else{
				CString s=name.Mid(i+1);
				s=s.Left(s.GetLength()-ext.GetLength()-1);
				return s;
			}
		}
	}
	return CString("");
}
★ファイルをオープンせずにファイルの実体があることを確認

BOOL IsFileExist(const CString strFilePath)
{
	CFileStatus stats;
	return CFile::GetStatus(strFilePath, stats);
}
★複数のツールバーを任意の位置に置く

void CMainFrame::DockControlBarLeftOf(CToolBar* Bar,CToolBar* LeftOf)
{
	CRect RC;

	RecalcLayout();
	LeftOf->GetWindowRect(&RC);
	RC.OffsetRect(1,0);
	DWORD  dw=LeftOf->GetBarStyle();
	UINT n = 0;
	n = (dw&CBRS_ALIGN_TOP) ? AFX_IDW_DOCKBAR_TOP : n;
	n = (dw&CBRS_ALIGN_BOTTOM && n==0) ? AFX_IDW_DOCKBAR_BOTTOM : n;
	n = (dw&CBRS_ALIGN_LEFT && n==0) ? AFX_IDW_DOCKBAR_LEFT : n;
	n = (dw&CBRS_ALIGN_RIGHT && n==0) ? AFX_IDW_DOCKBAR_RIGHT : n;
	DockControlBar(Bar,n,&RC);
}
★ファイルのロングパスを取得する

BOOL GetLongFileNameA(CString &LongFileName)
{
	WIN32_FIND_DATA ffd;
	char szTmpShortFile[MAX_PATH];
	CString szLongFile = _T("");
	LPTSTR lpYen;
	int nPos;

	lstrcpy(szTmpShortFile, LongFileName);
	if(FindFirstFile( szTmpShortFile, &ffd) == INVALID_HANDLE_VALUE)
		return FALSE;
	do{
		lpYen=strrchr( szTmpShortFile, _T('\\'));
		if(lpYen != NULL){
			nPos = lpYen-szTmpShortFile;
			if(szLongFile.IsEmpty())
				szLongFile.Format("%s", ffd.cFileName);
			else
				szLongFile.Format("%s\\%s", ffd.cFileName, szLongFile);
			szTmpShortFile[nPos] = _T('\0');
			FindFirstFile(szTmpShortFile, &ffd);
		}
		else
			szLongFile.Format("%s\\%s", szTmpShortFile, szLongFile);
	}while(lpYen != NULL );

	LongFileName = szLongFile;
	return TRUE;
}
★日付の取得

void OnNowDate(CString & strDate) 
{
	CTime t=CTime::GetCurrentTime();
	strDate = t.Format( "%Y/%m/%d %H:%M" );
}
★拡張子に関連付けらたファイルの起動

void OnNowDate(const CString strFilePath) 
{
	::ShellExecute( m_hWnd, "open", strFilePath, NULL, NULL, SW_SHOWNORMAL );
}
★ディレクトリのみを指定するコモンダイアログ

Win95とWinNT Ver 4.0では、 
WINSHELLAPI LPITEMIDLIST WINAPI SHBrowseForFolder(LPBROWSEINFO lpbi);
という関数を使えば簡単にできます。使い方は、次回更新時に!
★ファイルのコピー・削除・ReName

WinAPI
CopyFile(),CreateDirectory(),CreateFile,DeleteFile(),MoveFile(),RemoveDirectory()
など
あと、

	SHFILEOPSTRUCT fop;
	ZeroMemory(&fop, sizeof(fop));
	fop.hwnd = AfxGetMainWnd()->m_hWnd; 
	fop.wFunc = FO_COPY;
	fop.pFrom = lpFrom;// 元(複数指定する場合は NULLで区切ります 最後はNULL2文字で)
	fop.pTo =   lpTo;// 先(複数指定する場合は NULLで区切ります 最後はNULL2文字で)
	fop.fFlags = FOF_MULTIDESTFILES;
	int ret=0;
	ret = SHFileOperation(&fop);
	
の使用(詳細は次回)
★システムイメージリストの取得

	HIMAGELIST hImageList;
	SHFILEINFO shfi;
	hImageList = (HIMAGELIST)::SHGetFileInfo("C:\\", NULL, &shfi, sizeof(SHFILEINFO),
		SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
★RGBをモノクロに変換

アルゴリズム


サンプルコード

/* COLORREF - xBGR - */
void SetRGBtoGray(unsigned long * color)
{
	unsigned char r,g,b;
	unsigned char y;
	r= (unsigned char) ((*color) & 0xff);
	g =(unsigned char)(((*color) & 0xff00)>>8);
	b =(unsigned char)(((*color) & 0xff0000)>>16);
	y = (unsigned char) (0.299*(double)r + 0.587*(double)g + 0.114*(double)b);
	*color = 0;
	*color = y | y<<8 | y<<16;
}


目次



ご連絡はメールにて


GeoCities Japan