ホーム » Linux » OPENFILENAME

2021年6月
 12345
6789101112
13141516171819
20212223242526
27282930  

カテゴリー

アーカイブ

ブログ統計情報

  • 79,957 アクセス



OPENFILENAME

コンソール AP では,標準入力を使用していた.

#include	"quotm.hxx"
#include	"existff.hxx"

inline	bool	Test	(void)
{
	while(true)	{
		tstring	str ;
		{
			tstring	buf ;
			buf.resize(1000) ;
			std::terr << _T("file ...=") ;
			std::tin.getline(&buf[0],std::streamsize(buf.size())) ;
			str = buf.c_str() ;
			if   	(str == _T("q"))	{	break ;		}
			else if (str == _T("Q"))	{	break ;		}
			str = ::QuotM_Del_All(str) ;
			if (str.empty())   		{	continue ;	}
			}
		{
			if (::File_IsNothing(str)) 	{	continue ;	}
			std::tout << str << std::endl ;
			}
		}
	return	true ;
	}

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	{
		Test() ;
		}
	return	0 ;
	}

以前作成した CFileDialog を使用したもの.

#include	<afxwin.h>
#include	"F_Dialog.hxx"
#include	"quotm.hxx"

inline	bool	Test	(void)
{
	while(true)	{
		tstring	str ;
		{
			str = ::FD_GetOpenFile(_T("./")) ;
			if (str.empty())   		{	break ;		}
			}
		{
			if (::File_IsNothing(str))	{	continue ;	}
			std::tout << str << std::endl ;
			}
		}
	return	true ;
	}

CWinApp theApp;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) {
		std::terr << _T("error : AfxWinInit") << std::endl ;
		nRetCode = 1;
		}
	else  {
		Test() ;
		}
	return nRetCode;
	}

////////////////////////////////////////////////////////////////////////////////////////

#include	<AfxExt.h>
#include	"BrowseFF.cxx"

「開く」ダイアログを表示するだけのコード.

{
	OPENFILENAME	ofn =	{	0	} ;
	ofn.lStructSize = sizeof(OPENFILENAME) ;
	if (!::GetOpenFileName(&ofn))	{	return	tstring() ;	}
//	...
	}

OPENFILENAME の指定が複雑なので,使いそうな内容でまとめてみた.

OPENFILENAME
lStructSize sizeof(OPENFILENAME)
hwndOwner NULL の時,モードレスになる.
CFileDialog では DoModal で求めている.
lpstrFile 選択されたファイル名が入る.
OFN_ALLOWMULTISELECT の場合は,下のメモリイメージ.
nMaxFile lpstrFile で確保している領域の文字数

OFN_ALLOWMULTISELECT の場合の結果の lpstrFile .

0x02C00078  44 3a 5c 44 6f 63 75 6d 65 6e 74 5c 56 53 5c 56  D:\Document\VS\V
0x02C00088  53 5c 32 30 30 35 5c 54 5f 4f 70 65 6e 46 5c 54  S\2005\T_OpenF\T
0x02C00098  5f 4f 46 5f 4d 46 43    54 5f 4f 46 5f 4d 46 43  _OF_MFC.T_OF_MFC
0x02C000A8  2e 42 41 4b    54 5f 4f 46 5f 4d 46 43 2e 63 70  .BAK.T_OF_MFC.cp
0x02C000B8  70    54 5f 4f 46 5f 4d 46 43 2e 76 63 70 72 6f  p.T_OF_MFC.vcpro
0x02C000C8  6a    54 5f 4f 46 5f 4d 46 43 2e 76 63 70 72 6f  j.T_OF_MFC.vcpro
0x02C000D8  6a 2e 5a 31 37 30 53 30 2e 49 77 61 6f 2e 75 73  j.Z170S0.Iwao.us
0x02C000E8  65 72       00 00 00 00 00 00 00 00 00 00 00 00  er..............

0x027E6B50  44 00 3a 00 5c 00 44 00 6f 00 63 00 75 00 6d 00 65 00 6e 00 74 00 5c 00 56 00 53 00 5c 00 56 00  D:\Document\VS\V
0x027E6B70  53 00 5c 00 32 00 30 00 30 00 35 00 5c 00 54 00 5f 00 4f 00 70 00 65 00 6e 00 46 00 5c 00 54 00  S\2005\T_OpenF\T
0x027E6B90  5f 00 4f 00 46 00 5f 00 4d 00 46 00 43 00 00 00 54 00 5f 00 4f 00 46 00 5f 00 4d 00 46 00 43 00  _OF_MFC.T_OF_MFC
0x027E6BB0  2e 00 42 00 41 00 4b 00 00 00 54 00 5f 00 4f 00 46 00 5f 00 4d 00 46 00 43 00 2e 00 63 00 70 00  .BAK.T_OF_MFC.cp
0x027E6BD0  70 00 00 00 54 00 5f 00 4f 00 46 00 5f 00 4d 00 46 00 43 00 2e 00 76 00 63 00 70 00 72 00 6f 00  p.T_OF_MFC.vcpro
0x027E6BF0  6a 00 00 00 54 00 5f 00 4f 00 46 00 5f 00 4d 00 46 00 43 00 2e 00 76 00 63 00 70 00 72 00 6f 00  j.T_OF_MFC.vcpro
0x027E6C10  6a 00 2e 00 5a 00 31 00 37 00 30 00 53 00 30 00 2e 00 49 00 77 00 61 00 6f 00 2e 00 75 00 73 00  j.Z170S0.Iwao.us
0x027E6C30  65 00 72 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  er..............

lpstrFile	0x02c10078 "D:\Document\VS\VS\2005\T_OpenF\T_OF_MFC\ReadMe.txt"	char *

デバッガで OPENFILENAME_SIZE_VERSION_400 となる様にしたもの.
OPENFILENAME_SIZE_VERSION_400


MFC 9 で bVistaStyle を FALSE とすると
CFileDialog bVistaStyle == FALSE
VC 8 exe と同様の,左側がフォルダツリーではない表示のもの.
デフォルトの TRUE だと
CFileDialog bVistaStyle == TRUE
COM を利用したもの?
Common Item Dialog


今度は MFC を使用しない方法.
::GetOpenFileName を使用する方法で OFN_ALLOWMULTISELECT を指定すると実行されなかった.
見た目のエラーにはならない?が,API を呼んで FALSE で抜けてくる.
::CommDlgExtendedError で調べると FNERR_INVALIDFILENAME だった.
存在しないファイル名などを与えるとうまくないみたい.
まだ何か足りないみたいで Win3.x 頃の表示になってしまった.
GetOpenFileName  Win3.x
選択したファイル名などもスペースで区切られている.
OFN_ALLOWMULTISELECT を指定する時は OFN_EXPLORER も指定する必要があるみたい.
次の所に書かれていた.
OPENFILENAMEW structure (commdlg.h)
OFN_ALLOWMULTISELECT | OFN_EXPLORER の場合 lpstrFile に存在しないファイル名でも問題なさそう.


複数選択可能な「開く」ダイアログのコードは次の様な感じ.

tstring	GetOpenFile	(LPCTSTR default_name=_T("./"))
{
	HWND	hwnd = ::GetConsoleHwnd() ;
	tstring	str_file = default_name ;
	    	str_file.resize(260*100) ;
	OPENFILENAME	ofn =	{	0	} ;
	{
		ofn.lStructSize	= sizeof(OPENFILENAME) ;
		ofn.hwndOwner	= hwnd ;
		ofn.nMaxFile	= DWORD(str_file.size()) ;
		ofn.lpstrFile	= &str_file[0] ;
		ofn.Flags   	= OFN_ALLOWMULTISELECT | OFN_EXPLORER | OFN_HIDEREADONLY ;
		}
	if (!::GetOpenFileName(&ofn)) {
		DWORD	err = ::CommDlgExtendedError() ;
		std::terr << ::To_tstring(u_32(err),16) << std::endl ;
		return	tstring() ;
		}
	tstring	sel_file ;
	{
		sel_file = ofn.lpstrFile ;
	//	...
		}
	return	sel_file ;
	}

拡張子のフィルタなどは指定していない.また,選択されたファイルを取得するコードもこれから.


選択されたファイルを取得するコード.

tstring	String_Change	(c_tstring& str_,const TCHAR src,const TCHAR dst)
{
	tstring	str = str_ ;
	for (size_t index=0 ; index<str.size() ; index++) {
		TCHAR	ch = str[index] ;
		if (ch == src) {
			str[index] = dst ;
			}
		}
	return	str ;
	}

	tstring	sel_file ;
	{
		sel_file = ofn.lpstrFile ;
		sel_file = str_file ;
		sel_file = ::String_Change(ofn.lpstrFile,_T('\x0'),_T('\n')) ;
		sel_file = ::String_Change(str_file,     _T('\x0'),_T('\n')) ;
		sel_file = ::String_TrimBoth(sel_file) ;
		}

ofn.lpstrFile は ASCIIZ となってしまうので,str_file をそのまま使用する必要がある.
Win3.x 形式の lpstrFile のダンプ.

0x02311F00  0043 003a 005c 0055 0053 0045 0052 0053 005c 0050 0055 0042 004c 0049 0043 005c  C.:.\.U.S.E.R.S.\.P.U.B.L.I.C.\.
0x02311F20  0044 004f 0043 0055 004d 0045 004e 0054 0053 005c 0020 0042 0061 0063 006b 0043  D.O.C.U.M.E.N.T.S.\. .B.a.c.k.C.
0x02311F40  0050 002e 0062 0061 0074 002e 006c 006e 006b 0020 0057 0049 004e 0031 0030 002d  P...b.a.t...l.n.k. .W.I.N.1.0.-.
0x02311F60  007e 0031 002e 004c 004e 004b 0020 005a 0031 0037 0030 0044 004f 007e 0031 002e  ~.1...L.N.K. .Z.1.7.0.D.O.~.1...
0x02311F80  004c 004e 004b 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000  L.N.K...........................

区切りがスペースになるため,ファイル名などにスペースが含まれている場合は 8.3 形式 になる.


lpstrFilter を指定.

	tstring	filter ;
		filter	+= _T("Executable Files (*.exe)|*.exe|") ;
		filter	+= _T("All Files (*.*)|*.*|") ;
		filter	+= _T("|") ;
		filter   = ::String_Change(filter,_T('|'),_T('\x0')) ;
	OPENFILENAME	ofn =	{	0	} ;
	{
		ofn.lStructSize	= sizeof(OPENFILENAME) ;
	//	...
		ofn.lpstrFilter	= &filter[0] ;
		}

ここまでのものを WinXP 環境で表示すると
GetOpenFileName WinXP


WFDialog.hxx

Is this 投稿 useful? Useful Useless 0 of 0 people say this 投稿 is useful.

2件のコメント

コメントする

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

%d人のブロガーが「いいね」をつけました。