Created
December 1, 2025 01:35
-
-
Save bg1bgst333/fc8b72075361dc32cda97456f1c3b11a to your computer and use it in GitHub Desktop.
CComboBox::GetCurSel
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 "ComboBox.h" // CComboBox | |
| // コンストラクタCComboBox | |
| CComboBox::CComboBox() : CCustomControl() { | |
| } | |
| // デストラクタ~CComboBox | |
| CComboBox::~CComboBox() { | |
| // メンバの終了処理 | |
| Destroy(); // Destroyでこのウィンドウの終了処理をする. | |
| } | |
| // ウィンドウ作成関数Create. | |
| BOOL CComboBox::Create(LPCTSTR lpctszWindowName, DWORD dwStyle, int x, int y, int iWidth, int iHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance) { | |
| // ウィンドウクラス名には"ComboBox"を指定. | |
| return CCustomControl::Create(_T("ComboBox"), lpctszWindowName, dwStyle | ES_MULTILINE | ES_WANTRETURN | ES_AUTOHSCROLL | ES_AUTOVSCROLL, x, y, iWidth, iHeight, hWndParent, hMenu, hInstance); // CCustomControl::Createにウィンドウクラス名"ComboBox"を指定. | |
| } | |
| // コンボボックスに文字列を追加する関数AddString. | |
| void CComboBox::AddString(LPCTSTR lpctszStr) { | |
| // 指定の文字列を追加. | |
| SendMessage(m_hWnd, CB_ADDSTRING, 0, (LPARAM)lpctszStr); // SendMessageでCB_ADDSTRINGを送ることで指定の文字列を追加. | |
| } | |
| // コンボボックスで選択されている要素のインデックスを返すGetCurSel. | |
| int CComboBox::GetCurSel() { | |
| // 選択されている要素のインデックスを返す. | |
| return (int)SendMessage(m_hWnd, CB_GETCURSEL, 0, 0); // SendMessageでCB_GETCURSELを送ることで選択された要素のインデックスを取得. | |
| } |
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("さしすせそ")); | |
| } | |
| } | |
| 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("CComboBox"), 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.GetOFN().lpstrFile, _T("CComboBox"), 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