Created
December 4, 2025 12:06
-
-
Save bg1bgst333/9e58730b54da8b9091936f15a3eae04f to your computer and use it in GitHub Desktop.
CFileDialog::GetFileExt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ヘッダのインクルード | |
| // 既定のヘッダ | |
| #include <string.h> // C文字列処理 | |
| #include <shlwapi.h> // シェルAPI | |
| // 独自のヘッダ | |
| extern "C" { // C言語として解釈する. | |
| #include "string_utility_cstring.h" // 文字列ユーティリティ(C文字列処理) | |
| } | |
| #include "FileDialog.h" // CFileDialog | |
| // コンストラクタCFileDialog() | |
| CFileDialog::CFileDialog(BOOL bOpenFileDalog, LPCTSTR lpszDefExt, LPCTSTR lpszFileName, DWORD dwFlags, LPCTSTR lpszFilter, CWindow* pParentWnd, DWORD dwSize) { | |
| // メンバの初期化. | |
| m_bOpenFileDalog = bOpenFileDalog; | |
| memset(&m_ofn, 0, sizeof(OPENFILENAME)); | |
| if (lpszDefExt != NULL) { | |
| _tcscpy(m_tszDefExt, lpszDefExt); | |
| m_ofn.lpstrDefExt = m_tszDefExt; | |
| } | |
| if (lpszFileName != NULL) { | |
| _tcscpy(m_tszFileName, lpszFileName); | |
| } | |
| else { | |
| memset(m_tszFileName, 0, sizeof(TCHAR) * 1024); | |
| } | |
| m_ofn.nMaxFile = 1024; | |
| m_ofn.lpstrFile = m_tszFileName; | |
| m_ofn.Flags = dwFlags; | |
| if (lpszFilter != NULL) { | |
| _tcscpy(m_tszFilter, lpszFilter); | |
| int len = _tcslen(m_tszFilter); | |
| replace_wbyte_all(m_tszFilter, len, _T('|'), _T('\0')); | |
| m_ofn.lpstrFilter = m_tszFilter; | |
| } | |
| if (pParentWnd != NULL) { | |
| m_ofn.hwndOwner = pParentWnd->m_hWnd; | |
| } | |
| if (dwSize == 0) { | |
| m_ofn.lStructSize = sizeof(OPENFILENAME); | |
| } | |
| else { | |
| m_ofn.lStructSize = dwSize; | |
| } | |
| } | |
| // モーダル表示. | |
| INT_PTR CFileDialog::DoModal() { | |
| // どちらのファイル選択ダイアログを開くか判定. | |
| if (m_bOpenFileDalog) { // "開く" | |
| BOOL bRet = GetOpenFileName(&m_ofn); | |
| if (bRet) { | |
| TCHAR* ptszExt = PathFindExtension(m_ofn.lpstrFile); | |
| m_tstrFileExt = ptszExt + 1; | |
| return IDOK; | |
| } | |
| else { | |
| return IDCANCEL; | |
| } | |
| } | |
| else { // "名前を付けて保存" | |
| BOOL bRet = GetSaveFileName(&m_ofn); | |
| if (bRet) { | |
| TCHAR* ptszExt = PathFindExtension(m_ofn.lpstrFile); | |
| m_tstrFileExt = ptszExt + 1; | |
| return IDOK; | |
| } | |
| else { | |
| return IDCANCEL; | |
| } | |
| } | |
| } | |
| // OPENFILENAMEの取得. | |
| OPENFILENAME& CFileDialog::GetOFN() { | |
| // OPENFILENAMEを返す. | |
| return m_ofn; | |
| } | |
| // 拡張子の取得. | |
| tstring CFileDialog::GetFileExt() { | |
| // m_tstrFileExtを返す. | |
| return m_tstrFileExt; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ヘッダのインクルード | |
| // 既定のヘッダ | |
| #include <windows.h> // 標準WindowsAPI | |
| #include <stdio.h> // C標準入出力 | |
| #include <commctrl.h> // コモンコントロール | |
| // 独自のヘッダ | |
| #include "MainWindow.h" // CMainWindow | |
| #include "FileDialog.h" // CFileDialog | |
| #include "CustomEdit.h" // CCustomEdit | |
| #include "ComboBox.h" // CComboBox | |
| #include "resource.h" | |
| // ウィンドウクラス登録関数RegisterClass. | |
| BOOL CMainWindow::RegisterClass(HINSTANCE hInstance) { | |
| // ウィンドウクラス名は"CMainWindow". | |
| return CWindow::RegisterClass(hInstance, _T("CMainWindow")); // CWindow::RegisterClassでウィンドウクラス名"CMainWindow"を登録. | |
| } | |
| // ウィンドウクラス登録関数RegisterClass.(メニュー名指定バージョン) | |
| BOOL CMainWindow::RegisterClass(HINSTANCE hInstance, LPCTSTR lpctszMenuName) { | |
| // メニュー名はlpctszMenuName. | |
| return CWindow::RegisterClass(hInstance, _T("CMainWindow"), lpctszMenuName); // CWindow::RegisterClassで, ウィンドウクラス名"CMainWindow", メニュー名lpctszMenuNameを登録. | |
| } | |
| // コンストラクタCMainWindow() | |
| CMainWindow::CMainWindow() { | |
| // メンバの初期化. | |
| m_hInstance = NULL; | |
| m_pMainMenu = NULL; | |
| m_pMultiView = NULL; | |
| } | |
| // デストラクタ~CMainWindow() | |
| CMainWindow::~CMainWindow() { | |
| // メンバの終了処理. | |
| Destroy(); // Destroyで子ウィンドウの破棄. | |
| } | |
| // ウィンドウ作成関数Create.(ウィンドウクラス名省略バージョン.) | |
| BOOL CMainWindow::Create(LPCTSTR lpctszWindowName, DWORD dwStyle, int x, int y, int iWidth, int iHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance) { | |
| // ウィンドウクラス名は"CMainWindow". | |
| return CWindow::Create(_T("CMainWindow"), lpctszWindowName, dwStyle, x, y, iWidth, iHeight, hWndParent, hMenu, hInstance); // CWindow::Createにウィンドウクラス名"CMainWindow"を指定. | |
| } | |
| // ウィンドウ作成関数CreateEx. | |
| BOOL CMainWindow::CreateEx(DWORD dwExStyle, LPCTSTR lpctszWindowName, DWORD dwStyle, int x, int y, int iWidth, int iHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance) { | |
| // ウィンドウクラス名は"CMainWindow". | |
| return CWindow::CreateEx(dwExStyle, _T("CMainWindow"), lpctszWindowName, dwStyle, x, y, iWidth, iHeight, hWndParent, hMenu, hInstance); // CWindow::CreateExにウィンドウクラス名"CMainWindow"を指定. | |
| } | |
| // ウィンドウ破棄関数Destroy | |
| BOOL CMainWindow::Destroy() { | |
| // 変数の初期化. | |
| BOOL bRet = FALSE; // bRetをFALSEで初期化. | |
| // DestroyChildrenを分けたので, 自身のウィンドウ破棄は問題ない. | |
| // まず子ウィンドウの破棄. | |
| DestroyChildren(); | |
| // 自身のウィンドウ破棄. | |
| bRet = CWindow::Destroy(); // 戻り値をbRetに格納. | |
| // bRetを返す. | |
| return bRet; | |
| } | |
| // 子ウィンドウ破棄関数DestroyChildren | |
| BOOL CMainWindow::DestroyChildren() { | |
| // 変数の初期化. | |
| BOOL bRet = FALSE; // bRetをFALSEで初期化. | |
| // メンバの終了処理. | |
| if (m_pMultiView != NULL) { | |
| bRet = m_pMultiView->Destroy(); | |
| delete m_pMultiView; | |
| m_pMultiView = NULL; | |
| } | |
| // 破棄したらTRUEを返す. | |
| if (bRet) { // TRUEなら. | |
| return TRUE; // TRUEを返す. | |
| } | |
| // 破棄しなければ, CWindowのDestroyChildrenを返す. | |
| return CWindow::DestroyChildren(); // CWindow::DestroyChildrenを返す. | |
| } | |
| // ウィンドウの作成が開始された時. | |
| int CMainWindow::OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct) { | |
| // 親クラスのOnCreateを呼ぶ. | |
| m_hInstance = lpCreateStruct->hInstance; | |
| int iRet = CWindow::OnCreate(hwnd, lpCreateStruct); // CWindow::OnCreateを呼び, 戻り値をiRetに格納. | |
| m_pMainMenu = GetMenu(); // CWindow::GetMenuでm_pMainMenu取得. | |
| if (m_pMainMenu == NULL) { // メニューハンドルが無い場合は, m_pMainMenuがNULLになる. | |
| m_pMainMenu = new CMenu(); | |
| BOOL bRet = m_pMainMenu->LoadMenu(lpCreateStruct->hInstance, IDM_MAINMENU); | |
| if (bRet) { | |
| SetMenu(m_pMainMenu); | |
| AddCommandHandler(ID_ITEM_FILE_OPEN, 0, (int(CWindow::*)(WPARAM, LPARAM)) & CMainWindow::OnFileOpen); | |
| // CMultiViewの生成. | |
| RECT rc = { 0 }; | |
| GetClientRect(hwnd, &rc); | |
| m_pMultiView = new CMultiView(); | |
| m_pMultiView->Create(_T(""), 0, 0, 0, rc.right - rc.left, rc.bottom - rc.top, hwnd, (HMENU)IDC_MULTIVIEW, m_hInstance); | |
| // アイテムの追加. | |
| m_pMultiView->Add(_T("Item0"), 0, 0, rc.right - rc.left, 25, m_hInstance); | |
| // マルチビューアイテムの取得. | |
| CMultiViewItem* pItem0 = m_pMultiView->Get(0); | |
| // マルチビューアイテム内にコンボボックス1を配置. | |
| CComboBox* pComboBox1 = new CComboBox(); | |
| pComboBox1->Create(_T("Item0-ComboBox1"), WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST, 0, 0, rc.right - rc.left, 100, pItem0->m_hWnd, (HMENU)WM_APP + 200, m_hInstance); | |
| pItem0->m_mapChildMap.insert(std::make_pair(_T("Item0-ComboBox1"), pComboBox1)); | |
| // コンボボックスに文字列アイテムを追加 | |
| pComboBox1->AddString(_T("あいうえお")); | |
| pComboBox1->AddString(_T("かきくけこ")); | |
| pComboBox1->AddString(_T("さしすせそ")); | |
| // 2番目の"さしすせそ"にセット. | |
| pComboBox1->SetCurSel(2); | |
| } | |
| } | |
| return iRet; // iRetを返す. | |
| } | |
| // ウィンドウが破棄された時. | |
| void CMainWindow::OnDestroy() { | |
| // メニューハンドラの削除. | |
| DeleteCommandHandler(ID_ITEM_FILE_OPEN, 0); | |
| // メニューの終了処理. | |
| CMenu::DeleteMenuHandleMap(); | |
| m_pMainMenu = NULL; | |
| // CWindowのOnDestroyを呼ぶ. | |
| CWindow::OnDestroy(); // CWindow::OnDestroyを呼ぶ. | |
| } | |
| // ウィンドウのサイズが変更された時. | |
| void CMainWindow::OnSize(UINT nType, int cx, int cy) { | |
| // マルチビューをメインウィンドウのクライアント領域サイズにリサイズ. | |
| if (m_pMultiView != NULL) { | |
| m_pMultiView->MoveWindow(0, 0, cx, cy); | |
| } | |
| } | |
| // ウィンドウが閉じられる時. | |
| int CMainWindow::OnClose() { | |
| // メッセージボックスで"Close CMainWindow OK?"と表示. | |
| int iRet = MessageBox(m_hWnd, _T("Close CMainWindow OK?"), _T("CFileDialog"), MB_OKCANCEL); // MessageBoxで"Close CMainWindow OK?"と表示し, 戻り値をiRetに格納. | |
| if (iRet != IDOK) { // OK以外.(Cancelなど.) | |
| return -1; // -1を返す. | |
| } | |
| // このウィンドウの破棄.(OnCloseの後, ウィンドウの破棄処理が勝手に行われるので, Destroyは不要なのでコメントアウト.) | |
| //Destroy(); // Destroyでこのウィンドウの破棄処理. | |
| // OKなので閉じる. | |
| return CWindow::OnClose(); // 親クラスのOnCloseを呼ぶ.(親クラスのOnCloseは常に閉じる処理になっている.) | |
| } | |
| // 開くが選択された時. | |
| int CMainWindow::OnFileOpen(WPARAM wParam, LPARAM lParam) { | |
| // "開く"ダイアログ | |
| CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY, _T("Text Files(*.txt)|*.txt|All Files(*.*)|*.*||")); | |
| INT_PTR ret = dlg.DoModal(); | |
| if (ret == IDOK) { | |
| MessageBox(m_hWnd, dlg.GetFileExt().c_str(), _T("CFileDialog"), MB_OK); | |
| } | |
| /* | |
| // マルチビューアイテムの取得. | |
| CMultiViewItem *pItem0 = m_pMultiView->Get(0); | |
| // コンボボックスの取得. | |
| CComboBox *pComboBox1 = (CComboBox *)pItem0->m_mapChildMap[_T("Item0-ComboBox1")]; | |
| // 選択されたインデックスの取得と表示. | |
| int idx = pComboBox1->GetCurSel(); | |
| TCHAR tszIdx[16] = {0}; | |
| _stprintf(tszIdx, _T("idx = %d"), idx); | |
| MessageBox(m_hWnd, tszIdx, _T("CComboBox"), MB_OK); | |
| */ | |
| // 0を返す. | |
| return 0; // 処理したので0. | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment