7.1 ディレクトリ操作 (1) カレントディレクトリの操作 #include "myWin.h" static TCHAR strPath[]=TEXT("C:\\Users\\Shirai\\Documents\\cpp"); static TCHAR crDir[MAX_PATH+1]; static HWND list; void procCreate(HWND hw, WPARAM wp,LPARAM lp){ GetCurrentDirectory(MAX_PATH+1,crDir); MessageBox(hw,crDir,TEXT("Current Path"),MB_OK); SetCurrentDirectory(strPath); list = CreateWindow(TEXT("LISTBOX"),NULL, WS_CHILD|WS_VISIBLE|LBS_STANDARD, 0, 0, 200, 200, hw, (HMENU) 1, hInstance,NULL); SendMessage(list, LB_DIR, DDL_READWRITE,(LPARAM)TEXT("*.*")); } LRESULT CALLBACK WndProc(HWND hw, UINT msg, WPARAM wp,LPARAM lp){ switch(msg){ case WM_DESTROY : PostQuitMessage(0) ;return 0; case WM_CREATE : procCreate(hw,wp,lp);return 0; } return DefWindowProc(hw,msg,wp,lp); } (3) システムファイルの位置を知る #include "myWin.h" static TCHAR strSys[MAX_PATH+1]; static TCHAR strWin[MAX_PATH+1]; void procCreate(HWND hw, WPARAM wp,LPARAM lp){ GetWindowsDirectory(strWin,MAX_PATH+1); GetSystemDirectory (strSys,MAX_PATH+1); } void procPaint(HWND hw, WPARAM wp,LPARAM lp){ HDC hdc; PAINTSTRUCT ps;TEXTMETRIC tm; hdc=BeginPaint(hw,&ps); GetTextMetrics(hdc, &tm); TextOut(hdc, 0, 0 ,strWin,lstrlen(strWin)); TextOut(hdc, 0, tm.tmHeight,strSys,lstrlen(strSys)); EndPaint(hw,&ps); } LRESULT CALLBACK WndProc(HWND hw, UINT msg, WPARAM wp,LPARAM lp){ switch(msg){ case WM_DESTROY : PostQuitMessage(0) ;return 0; case WM_CREATE : procCreate(hw,wp,lp);return 0; case WM_PAINT : procPaint (hw,wp,lp);return 0; } return DefWindowProc(hw,msg,wp,lp); } (5) ディレクトリの作成・削除のプログラム例 #include "myWin.h" #define BT_1 1 #define BT_2 2 #define LS_1 3 static TCHAR strDir[]=TEXT("D:\\TestDir"), curDir[]=TEXT("D:\\"); static HWND list; void reDspList(VOID){ SendMessage(list,LB_RESETCONTENT,0,0); SendMessage(list,LB_DIR,DDL_DIRECTORY|DDL_READWRITE, (LPARAM)TEXT("*.*")); } void procCreate(HWND hw, WPARAM wp,LPARAM lp){ CreateWindow(TEXT("BUTTON"),TEXT("Dir作成"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, 0,0,100,30,hw,(HMENU)BT_1, hInstance,NULL); CreateWindow(TEXT("BUTTON"),TEXT("Dir消去"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, 0,30,100,30,hw, (HMENU)BT_2, hInstance,NULL); list=CreateWindow(TEXT("LISTBOX"),NULL, WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|WS_BORDER, 10,80,200,300,hw,(HMENU)LS_1, hInstance,NULL); SetCurrentDirectory(curDir); reDspList(); } void procCommand(HWND hw, WPARAM wp,LPARAM lp){ switch(LOWORD(wp)){ case BT_1: if(!CreateDirectory(strDir,NULL)) MessageBox(hw,TEXT("ディレクトリ作成失敗"),TEXT("Error"), MB_OK); reDspList(); return; case BT_2: if(!RemoveDirectory(strDir)) MessageBox(hw,TEXT("ディレクトリ削除失敗"),TEXT("Error"), MB_OK); reDspList(); return; } } LRESULT CALLBACK WndProc(HWND hw, UINT msg, WPARAM wp,LPARAM lp){ switch(msg){ case WM_DESTROY : PostQuitMessage(0) ;return 0; case WM_CREATE : procCreate(hw,wp,lp);return 0; case WM_COMMAND : procCommand(hw,wp,lp);return 0; } return DefWindowProc(hw,msg,wp,lp); } 7.2 ファイル操作 (1) ハンドルの取得 (2) ファイル情報の取得 #include "myWin.h" static OPENFILENAME CC; static TCHAR strFile[2048],strFileTitle[128]; static TCHAR sFilter[]=TEXT("Text Files\0*.txt\0All Files\0*.*\0"); void procCreate(HWND hw, WPARAM wp,LPARAM lp){ CC.lStructSize =sizeof(OPENFILENAME); CC.hwndOwner =hw ; CC.hInstance =hInstance; CC.lpstrFilter =sFilter ; CC.nFilterIndex =1; CC.lpstrCustomFilter=NULL ; CC.nMaxCustFilter=0; CC.lpstrFile =strFile ; CC.nMaxFile =2048; CC.lpstrFileTitle =strFileTitle; CC.nMaxFileTitle =128; CC.lpstrInitialDir =TEXT("D:\\"); CC.lpstrTitle =TEXT("テストだよ"); CC.lpstrDefExt =TEXT("txt") ; CC.Flags =0; } void procLButtonDown(HWND hw, WPARAM wp,LPARAM lp){ TCHAR strType[8];unsigned long lLeng; DWORD hiLeng,loLeng; if(GetOpenFileName(&CC)){ HANDLE hF=CreateFile(CC.lpstrFile,GENERIC_READ, 0,NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL); if(hF==INVALID_HANDLE_VALUE){ MessageBox(hw,TEXT("ファイルオープンエラー"),TEXT("エラー"),MB_OK); return; } DWORD dFType=GetFileType(hF); loLeng=GetFileSize(hF,&hiLeng); lLeng=(((unsigned long)hiLeng)<<16) |( (unsigned long)loLeng); wsprintf(strFileTitle,TEXT("%s=%d Bytes, File Type %s"),strFile,lLeng, dFType==FILE_TYPE_DISK ?TEXT("Disk"): dFType==FILE_TYPE_CHAR ?TEXT("Char"): dFType==FILE_TYPE_PIPE ?TEXT("Pipe"):TEXT("Unknwn")); MessageBox(hw,strFileTitle,TEXT("ファイル情報"),MB_OK); CloseHandle(hF);//これを忘れないこと } } LRESULT CALLBACK WndProc(HWND hw, UINT msg, WPARAM wp,LPARAM lp){ switch(msg){ case WM_DESTROY : PostQuitMessage(0) ;return 0; case WM_CREATE : procCreate (hw,wp,lp);return 0; case WM_LBUTTONUP: procLButtonDown(hw,wp,lp);return 0; } return DefWindowProc(hw,msg,wp,lp); } (3) ファイルの削除 ? #include "myWin.h" static OPENFILENAME CC; static TCHAR strFile[2048],strFileTitle[128], strMsg[2100]; static TCHAR sFilter[]=TEXT("Text Files\0*.txt\0All Files\0*.*\0"); void procCreate(HWND hw, WPARAM wp,LPARAM lp){ CC.lStructSize =sizeof(OPENFILENAME); CC.hwndOwner =hw ; CC.hInstance =hInstance; CC.lpstrFilter =sFilter ; CC.nFilterIndex =1; CC.lpstrCustomFilter=NULL ; CC.nMaxCustFilter=0; CC.lpstrFile =strFile ; CC.nMaxFile =2048; CC.lpstrFileTitle =strFileTitle; CC.nMaxFileTitle =128; CC.lpstrInitialDir =TEXT("D:\\"); CC.lpstrTitle =TEXT("ファイル削除"); CC.lpstrDefExt =TEXT("txt") ; CC.Flags =0; } void procLButtonDown(HWND hw, WPARAM wp,LPARAM lp){ if(GetOpenFileName(&CC)){ wsprintf(strMsg,TEXT("削除します。よろしいですか?\n%s"),strFile); if(MessageBox(hw,strMsg,TEXT("ファイル操作"),MB_OKCANCEL)==IDOK){ if(!DeleteFile(strFile)) MessageBox(hw,TEXT("削除できません"),TEXT("エラー"),MB_OK); } GetOpenFileName(&CC); } } LRESULT CALLBACK WndProc(HWND hw, UINT msg, WPARAM wp,LPARAM lp){ switch(msg){ case WM_DESTROY : PostQuitMessage(0) ;return 0; case WM_CREATE : procCreate (hw,wp,lp);return 0; case WM_LBUTTONUP: procLButtonDown(hw,wp,lp);return 0; } return DefWindowProc(hw,msg,wp,lp); } (4) ファイルの複写 ? #include "myWin.h" static OPENFILENAME CC; static TCHAR strFile[2048],strFileTitle[128], strOld[2048]; static TCHAR sFilter[]=TEXT("Text Files\0*.txt\0All Files\0*.*\0"); void procCreate(HWND hw, WPARAM wp,LPARAM lp){ CC.lStructSize =sizeof(OPENFILENAME); CC.hwndOwner =hw ; CC.hInstance =hInstance; CC.lpstrFilter =sFilter ; CC.nFilterIndex =1; CC.lpstrCustomFilter=NULL ; CC.nMaxCustFilter=0; CC.lpstrFile =strFile ; CC.nMaxFile =2048; CC.lpstrFileTitle =strFileTitle; CC.nMaxFileTitle =128; CC.lpstrInitialDir =TEXT("D:\\"); CC.lpstrTitle =TEXT("ファイル複写"); CC.lpstrDefExt =TEXT("txt") ; CC.Flags =0; } void procLButtonDown(HWND hw, WPARAM wp,LPARAM lp){ if(GetOpenFileName(&CC)){ lstrcpy(strOld,strFile); if(GetSaveFileName(&CC)){ if(!CopyFile(strOld,strFile,TRUE)) MessageBox(hw,TEXT("複写できません"),TEXT("エラー"),MB_OK); } GetOpenFileName(&CC); } } LRESULT CALLBACK WndProc(HWND hw, UINT msg, WPARAM wp,LPARAM lp){ switch(msg){ case WM_DESTROY : PostQuitMessage(0) ;return 0; case WM_CREATE : procCreate (hw,wp,lp);return 0; case WM_LBUTTONUP: procLButtonDown(hw,wp,lp);return 0; } return DefWindowProc(hw,msg,wp,lp); } (5) ファイルおよびディレクトリの移動 #include "myWin.h" static OPENFILENAME CC; static TCHAR strFile[2048],strFileTitle[128], strOld[2048]; static TCHAR sFilter[]=TEXT("Text Files\0*.txt\0All Files\0*.*\0"); void procCreate(HWND hw, WPARAM wp,LPARAM lp){ CC.lStructSize =sizeof(OPENFILENAME); CC.hwndOwner =hw ; CC.hInstance =hInstance; CC.lpstrFilter =sFilter ; CC.nFilterIndex =1; CC.lpstrCustomFilter=NULL ; CC.nMaxCustFilter=0; CC.lpstrFile =strFile ; CC.nMaxFile =2048; CC.lpstrFileTitle =strFileTitle; CC.nMaxFileTitle =128; CC.lpstrInitialDir =TEXT("D:\\"); CC.lpstrTitle =TEXT("ファイル移動"); CC.lpstrDefExt =TEXT("txt") ; CC.Flags =0; } void procLButtonDown(HWND hw, WPARAM wp,LPARAM lp){ if(GetOpenFileName(&CC)){ lstrcpy(strOld,strFile); if(GetSaveFileName(&CC)){ if(!MoveFile(strOld,strFile)) MessageBox(hw,TEXT("移動できません"),TEXT("エラー"),MB_OK); } GetOpenFileName(&CC); } } LRESULT CALLBACK WndProc(HWND hw, UINT msg, WPARAM wp,LPARAM lp){ switch(msg){ case WM_DESTROY : PostQuitMessage(0) ;return 0; case WM_CREATE : procCreate (hw,wp,lp);return 0; case WM_LBUTTONUP: procLButtonDown(hw,wp,lp);return 0; } return DefWindowProc(hw,msg,wp,lp); } 7.4 ファイルへの書込み (1) ファイルへの書込み方法 #include "myWin.h" static OPENFILENAME CC; static TCHAR strTxt[]=TEXT("\xFEFFくものある日\r\nくもは かなしい\r\n") TEXT("くものない日\r\nそらは さびしい\r\n") TEXT("\r\n 八木重吉『雲』より\r\n"); static TCHAR strFile[2048],strFileTitle[128]; static TCHAR sFilter[]=TEXT("Text Files\0*.txt\0All Files\0*.*\0"); static DWORD dwWriteSize; void procCreate(HWND hw, WPARAM wp,LPARAM lp){ CC.lStructSize =sizeof(OPENFILENAME); CC.hwndOwner =hw ; CC.hInstance =hInstance; CC.lpstrFilter =sFilter ; CC.nFilterIndex =1; CC.lpstrCustomFilter=NULL ; CC.nMaxCustFilter=0; CC.lpstrFile =strFile ; CC.nMaxFile =2048; CC.lpstrFileTitle =strFileTitle; CC.nMaxFileTitle =128; CC.lpstrInitialDir =TEXT("D:\\"); CC.lpstrTitle =TEXT("テストだよ"); CC.lpstrDefExt =TEXT("txt") ; CC.Flags =0; } void procLButtonDown(HWND hw, WPARAM wp,LPARAM lp){ if(GetSaveFileName(&CC)){ HANDLE hF=CreateFile(CC.lpstrFile,GENERIC_WRITE, 0,NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,NULL); if(hF==INVALID_HANDLE_VALUE){ MessageBox(hw,TEXT("ファイルオープンエラー"),TEXT("エラー"),MB_OK); return; } WriteFile(hF,strTxt,lstrlen(strTxt)*2+1,&dwWriteSize,NULL); FlushFileBuffers(hF); CloseHandle(hF); } } LRESULT CALLBACK WndProc(HWND hw, UINT msg, WPARAM wp,LPARAM lp){ switch(msg){ case WM_DESTROY : PostQuitMessage(0) ;return 0; case WM_CREATE : procCreate (hw,wp,lp);return 0; case WM_LBUTTONUP: procLButtonDown(hw,wp,lp);return 0; } return DefWindowProc(hw,msg,wp,lp); } (2) ファイルポインタによる読込みと書込み #include "myWin.h" #include // 関数_wtoiを使っているのでこれをインクルード static OPENFILENAME CC; static TCHAR strFile[2048],strFileTitle[512]; static TCHAR sFilter[]=TEXT("Data Files\0*.dt\0All Files\0*.*\0"); static HANDLE hF;static HWND ed1,ed2;static TCHAR str[128]; static DWORD dwWriteSize; enum label_ID{ LB_1= 1, LB_2}; enum edit_ID{ ED_1=11, ED_2}; enum button_ID{ BT_1=21, BT_2}; void procCreate(HWND hw, WPARAM wp,LPARAM lp){ //ダイアログボックスの形式 CC.lStructSize =sizeof(OPENFILENAME); CC.hwndOwner =hw ; CC.hInstance =hInstance; CC.lpstrFilter =sFilter ; CC.nFilterIndex =1; CC.lpstrCustomFilter=NULL ; CC.nMaxCustFilter=0; CC.lpstrFile =strFile ; CC.nMaxFile =2048; CC.lpstrFileTitle =strFileTitle; CC.nMaxFileTitle =512; CC.lpstrInitialDir =TEXT("D:\\"); CC.lpstrTitle =TEXT("テストだよ"); CC.lpstrDefExt =TEXT("dt") ; CC.Flags =0; //コントロールの配置 CreateWindow(TEXT("STATIC"),TEXT("番号"), WS_CHILD|WS_VISIBLE|SS_CENTER, 10,10,50,20,hw,(HMENU)LB_1,hInstance,NULL); CreateWindow(TEXT("STATIC"),TEXT("文字列"), WS_CHILD|WS_VISIBLE|SS_CENTER, 80,10,100,20,hw,(HMENU)LB_2,hInstance,NULL); ed1=CreateWindow(TEXT("EDIT"),TEXT("0"), WS_CHILD|WS_VISIBLE|ES_RIGHT|WS_BORDER, 10,40,50,20,hw,(HMENU)ED_1,hInstance,NULL); ed2=CreateWindow(TEXT("EDIT"),TEXT("保存データ"), WS_CHILD|WS_VISIBLE|ES_LEFT|WS_BORDER, 65,40,300,20,hw,(HMENU)ED_2,hInstance,NULL); CreateWindow(TEXT("BUTTON"),TEXT("書込み"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, 10,90,80,20,hw,(HMENU)BT_1,hInstance,NULL); CreateWindow(TEXT("BUTTON"),TEXT("読込み"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, 100,90,80,20,hw,(HMENU)BT_2,hInstance,NULL); //ファイルのオープン if(GetSaveFileName(&CC)){ hF=CreateFile(CC.lpstrFile,GENERIC_WRITE|GENERIC_READ, 0,NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL,NULL); if(hF==INVALID_HANDLE_VALUE) MessageBox(hw,TEXT("ファイルオープンエラー"),TEXT("エラー"),MB_OK); } } LONG setPointerToEdP(){//ed1で指定されたレコードにファイルポインタを移動 GetWindowText(ed1,str,128); LONG nn=(LONG)_wtoi(str)*256; SetFilePointer(hF,nn,NULL,FILE_BEGIN); return nn; } void dspData(HWND hw,TCHAR title[],LONG lpos, TCHAR str[]){ TCHAR dtStr[256]; wsprintf(dtStr,TEXT(" バイト位置=%d: %s"),lpos,str); MessageBox(hw,dtStr,title,MB_OK); } void procWrite(HWND hw){ //ed1で指定されたレコードに DWORD dwNum; //ed2のデータを書き込む LONG nn=setPointerToEdP() ; GetWindowText(ed2,str,128); WriteFile(hF, str,256,&dwNum,NULL); FlushFileBuffers(hF); dspData(hw,TEXT("Write"),nn,str); } void procRead(HWND hw){ //ed1で指定されたレコードから DWORD dwNum; //データを読み込みed2に設定する LONG nn=setPointerToEdP() ; ReadFile(hF, str,256,&dwNum,NULL); dspData(hw,TEXT("Read"),nn,str); SetWindowText(ed2,str); } void procCommand(HWND hw, WPARAM wp,LPARAM lp){ switch(LOWORD(wp)){ case BT_1:procWrite(hw);return;//書込みボタン case BT_2:procRead (hw);return;//読込みボタン } } LRESULT CALLBACK WndProc(HWND hw, UINT msg, WPARAM wp,LPARAM lp){ switch(msg){ case WM_DESTROY : FlushFileBuffers(hF);CloseHandle(hF); PostQuitMessage(0) ;return 0; case WM_CREATE : procCreate (hw,wp,lp);return 0; case WM_COMMAND : procCommand (hw,wp,lp);return 0; } return DefWindowProc(hw,msg,wp,lp); }