Artifact Content

Not logged in

Artifact 9208018681295ced95701497f254f395454f22af


     1  //--- K.I.LIB ---
     2  // kl_wcmn.h : windows-common-interface operatin
     3  
     4  #include "stdafx.h"
     5  #include "kilib.h"
     6  
     7  void kiSUtil::switchCurDirToExeDir()
     8  {
     9  	char exepath[MAX_PATH+50];
    10  	GetModuleFileName( NULL, exepath, MAX_PATH );
    11  	char* lastslash = 0;
    12  	for( char* p=exepath; *p; p=CharNext(p) )
    13  		if( *p=='\\' || *p=='/' )
    14  			lastslash = p;
    15  	if(lastslash)
    16  		*lastslash = '\0';
    17  	SetCurrentDirectory(exepath);
    18  }
    19  
    20  static int CALLBACK __ki__ofp( HWND w, UINT m, LPARAM l, LPARAM d )
    21  {
    22  	if( m==BFFM_INITIALIZED && d )
    23  		::SendMessage( w, BFFM_SETSELECTION, TRUE, d );
    24  	return 0;
    25  }
    26  
    27  bool kiSUtil::getFolderDlg( char* buf, HWND par, const char* title, const char* def )
    28  {
    29  	// 情報セット
    30  	BROWSEINFO bi;
    31  	ki_memzero( &bi, sizeof(bi) );
    32  	bi.hwndOwner      = par;
    33  	bi.pszDisplayName = buf;
    34  	bi.lpszTitle      = title;
    35  	bi.ulFlags        = BIF_RETURNONLYFSDIRS | BIF_DONTGOBELOWDOMAIN;
    36  	bi.lpfn           = __ki__ofp;
    37  	bi.lParam         = (long)def;
    38  
    39  	// ダイアログ表示
    40  	ITEMIDLIST* id = ::SHBrowseForFolder( &bi );
    41  	if( id==NULL )
    42  		return false;
    43  	::SHGetPathFromIDList( id, buf );
    44  	app()->shellFree( id );
    45  
    46  	// 終了
    47  	return true;
    48  }
    49  
    50  void kiSUtil::getFolderDlgOfEditBox( HWND wnd, HWND par, const char* title )
    51  {
    52  	char str[MAX_PATH];
    53  	::SendMessage( wnd, WM_GETTEXT, MAX_PATH, (LPARAM)str );
    54  	for( char* x=str,*l=str; *x; x=kiStr::next(x) )
    55  		l=x;
    56  	if( *l=='\\' || *l=='/' )
    57  		*l='\0';
    58  	if( getFolderDlg( str, par, title, str ) )
    59  		::SendMessage( wnd, WM_SETTEXT, 0, (LPARAM)str );
    60  }
    61  
    62  int kiSUtil::getSysIcon( const char* ext )
    63  {
    64  	kiPath tmp( kiPath::Tmp );
    65  	tmp += "noahicontest.";
    66  	tmp += ext;
    67  
    68  	SHFILEINFO fi;
    69  	::SHGetFileInfo( tmp, 0, &fi, sizeof(fi), 
    70  		SHGFI_USEFILEATTRIBUTES | SHGFI_SYSICONINDEX );
    71  	return fi.iIcon;
    72  }
    73  
    74  void kiSUtil::msgLastError( const char* msg )
    75  {
    76  	char* pMsg;
    77  	::FormatMessage( 
    78  		FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
    79  		NULL,::GetLastError(),MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR)&pMsg,0,NULL );
    80  	if( msg )
    81  		app()->msgBox( kiStr(msg) + "\r\n\r\n" + pMsg );
    82  	else
    83  		app()->msgBox( pMsg );
    84  	::LocalFree( pMsg );
    85  }
    86  
    87  void kiSUtil::createShortCut( const kiPath& at, const char* name )
    88  {
    89  	::CoInitialize(NULL);
    90  
    91  	IShellLink* psl;
    92  	if( SUCCEEDED(::CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,IID_IShellLink,(void**)&psl)) )
    93  	{
    94  		psl->SetPath( kiPath(kiPath::Exe_name) );
    95  		psl->SetWorkingDirectory( kiPath(kiPath::Exe,false) );
    96  
    97  		IPersistFile* ppf;
    98  		if( SUCCEEDED(psl->QueryInterface(IID_IPersistFile,(void**)&ppf)) )
    99  		{
   100  			wchar_t wsz[MAX_PATH]; 
   101  			kiPath lnkfile( at );
   102  			lnkfile += name, lnkfile += ".lnk";
   103  			::MultiByteToWideChar(CP_ACP,0,lnkfile,-1,wsz,MAX_PATH);
   104  			ppf->Save(wsz,TRUE);
   105  			ppf->Release();
   106  		}
   107  		psl->Release();
   108  	}
   109  	::CoUninitialize();
   110  }
   111  
   112  bool kiSUtil::exist( const char* fname )
   113  {
   114  	return 0xffffffff != ::GetFileAttributes( fname );
   115  }
   116  
   117  bool kiSUtil::isdir( const char* fname )
   118  {
   119  	DWORD attr = ::GetFileAttributes( fname );
   120  	return attr!=0xffffffff && (attr&FILE_ATTRIBUTE_DIRECTORY);
   121  }